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 nearbyint = __nearbyint
33 * nearbyint(x) returns the nearest fp integer to x in the direction
34 * corresponding to the current rounding direction without raising
35 * the inexact exception.
37 * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN,
38 * nearbyint(x) is also NaN.
45 __nearbyint(double x
) {
50 unsigned hx
, sx
, i
, frac
;
54 sx
= xx
.i
[HIWORD
] & 0x80000000;
55 hx
= xx
.i
[HIWORD
] & ~0x80000000;
57 /* handle trivial cases */
58 if (hx
>= 0x43300000) { /* x is nan, inf, or already integral */
59 if (hx
>= 0x7ff00000) /* x is inf or nan */
60 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
61 return (hx
>= 0x7ff80000 ? x
: x
+ x
);
62 /* assumes sparc-like QNaN */
67 } else if ((hx
| xx
.i
[LOWORD
]) == 0) /* x is zero */
70 /* get the rounding mode */
73 /* flip the sense of directed roundings if x is negative */
74 if (sx
&& (rm
== FE_UPWARD
|| rm
== FE_DOWNWARD
))
75 rm
= (FE_UPWARD
+ FE_DOWNWARD
) - rm
;
78 if (hx
< 0x3ff00000) {
79 if (rm
== FE_UPWARD
|| (rm
== FE_TONEAREST
&&
80 (hx
>= 0x3fe00000 && ((hx
& 0xfffff) | xx
.i
[LOWORD
]))))
81 xx
.i
[HIWORD
] = sx
| 0x3ff00000;
88 /* round x at the integer bit */
89 j
= 0x433 - (hx
>> 20);
92 frac
= ((xx
.i
[HIWORD
] << 1) << (63 - j
)) |
93 (xx
.i
[LOWORD
] >> (j
- 32));
94 if (xx
.i
[LOWORD
] & (i
- 1))
99 xx
.i
[HIWORD
] &= ~(i
- 1);
100 if ((rm
== FE_UPWARD
) || ((rm
== FE_TONEAREST
) &&
101 ((frac
> 0x80000000u
) || ((frac
== 0x80000000) &&
102 (xx
.i
[HIWORD
] & i
)))))
106 frac
= (xx
.i
[LOWORD
] << 1) << (31 - j
);
109 xx
.i
[LOWORD
] &= ~(i
- 1);
110 if ((rm
== FE_UPWARD
) || ((rm
== FE_TONEAREST
) &&
111 (frac
> 0x80000000u
|| ((frac
== 0x80000000) &&
112 (xx
.i
[LOWORD
] & i
))))) {
114 if (xx
.i
[LOWORD
] == 0)