4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak nearbyintf = __nearbyintf
36 __nearbyintf(float x
) {
41 unsigned hx
, sx
, i
, frac
;
45 sx
= xx
.i
& 0x80000000;
46 hx
= xx
.i
& ~0x80000000;
48 /* handle trivial cases */
49 if (hx
>= 0x4b000000) { /* x is nan, inf, or already integral */
50 if (hx
> 0x7f800000) /* x is nan */
51 return (x
* x
); /* + -> * for Cheetah */
53 } else if (hx
== 0) /* x is zero */
56 /* get the rounding mode */
59 /* flip the sense of directed roundings if x is negative */
60 if (sx
&& (rm
== FE_UPWARD
|| rm
== FE_DOWNWARD
))
61 rm
= (FE_UPWARD
+ FE_DOWNWARD
) - rm
;
64 if (hx
< 0x3f800000) {
65 if (rm
== FE_UPWARD
|| (rm
== FE_TONEAREST
&& hx
> 0x3f000000))
66 xx
.i
= sx
| 0x3f800000;
72 /* round x at the integer bit */
73 i
= 1 << (0x96 - (hx
>> 23));
79 if (rm
== FE_UPWARD
|| (rm
== FE_TONEAREST
&& (frac
> (i
>> 1) ||
80 ((frac
== (i
>> 1)) && (hx
& i
)))))
90 * Alternate implementations for SPARC, x86, using fp ops. These may
91 * be faster depending on how expensive saving and restoring the fp
92 * modes and status flags is.
101 __nearbyintf(float x
) {
107 unsigned hx
, sx
, fsr
, oldfsr
;
111 sx
= xx
.i
& 0x80000000;
112 hx
= xx
.i
& ~0x80000000;
114 /* handle trivial cases */
115 if (hx
>= 0x4b000000) /* x is nan, inf, or already integral */
117 else if (hx
== 0) /* x is zero */
121 __fenv_getfsr(&oldfsr
);
124 if (hx
< 0x3f800000) {
125 /* flip the sense of directed roundings if x is negative */
129 if (rm
== FSR_RP
|| (rm
== FSR_RN
&& hx
> 0x3f000000))
130 xx
.i
= sx
| 0x3f800000;
136 /* clear the inexact trap */
137 fsr
= oldfsr
& ~FSR_NXM
;
140 /* round x at the integer bit */
141 yy
.i
= sx
| 0x4b000000;
142 z
= (x
+ yy
.f
) - yy
.f
;
144 /* restore the old fsr */
145 __fenv_setfsr(&oldfsr
);
152 /* inline template */
153 extern long double frndint(long double);
156 __nearbyintf(float x
) {
158 unsigned oldcwsw
, cwsw
;
160 /* save the control and status words, mask the inexact exception */
161 __fenv_getcwsw(&oldcwsw
);
162 cwsw
= oldcwsw
| 0x00200000;
163 __fenv_setcwsw(&cwsw
);
165 z
= frndint((long double) x
);
168 * restore the control and status words, preserving all but the
171 __fenv_getcwsw(&cwsw
);
172 oldcwsw
|= (cwsw
& 0x1f);
173 __fenv_setcwsw(&oldcwsw
);
175 /* note: the value of z is representable in single precision */
180 #error Unknown architecture