5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25 /*
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 #pragma weak nearbyint = __nearbyint
31
32 /*
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.
36 *
37 * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN,
38 * nearbyint(x) is also NaN.
39 */
40
41 #include "libm.h"
42 #include <fenv.h>
43
44 double
45 __nearbyint(double x) {
46 union {
47 unsigned i[2];
48 double d;
49 } xx;
50 unsigned hx, sx, i, frac;
51 int rm, j;
52
53 xx.d = x;
54 sx = xx.i[HIWORD] & 0x80000000;
55 hx = xx.i[HIWORD] & ~0x80000000;
56
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 */
63 #else
64 return (x + x);
65 #endif
66 return (x);
67 } else if ((hx | xx.i[LOWORD]) == 0) /* x is zero */
68 return (x);
69
70 /* get the rounding mode */
71 rm = fegetround();
72
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;
76
77 /* handle |x| < 1 */
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;
82 else
83 xx.i[HIWORD] = sx;
84 xx.i[LOWORD] = 0;
85 return (xx.d);
86 }
87
88 /* round x at the integer bit */
89 j = 0x433 - (hx >> 20);
90 if (j >= 32) {
91 i = 1 << (j - 32);
92 frac = ((xx.i[HIWORD] << 1) << (63 - j)) |
93 (xx.i[LOWORD] >> (j - 32));
94 if (xx.i[LOWORD] & (i - 1))
95 frac |= 1;
96 if (!frac)
97 return (x);
98 xx.i[LOWORD] = 0;
99 xx.i[HIWORD] &= ~(i - 1);
100 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
101 ((frac > 0x80000000u) || ((frac == 0x80000000) &&
102 (xx.i[HIWORD] & i)))))
103 xx.i[HIWORD] += i;
104 } else {
105 i = 1 << j;
106 frac = (xx.i[LOWORD] << 1) << (31 - j);
107 if (!frac)
108 return (x);
109 xx.i[LOWORD] &= ~(i - 1);
110 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
111 (frac > 0x80000000u || ((frac == 0x80000000) &&
112 (xx.i[LOWORD] & i))))) {
113 xx.i[LOWORD] += i;
114 if (xx.i[LOWORD] == 0)
115 xx.i[HIWORD]++;
116 }
117 }
118 return (xx.d);
119 }
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25
26 /*
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma weak nearbyint = __nearbyint
32
33 /*
34 * nearbyint(x) returns the nearest fp integer to x in the direction
35 * corresponding to the current rounding direction without raising
36 * the inexact exception.
37 *
38 * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN,
39 * nearbyint(x) is also NaN.
40 */
41
42 #include "libm.h"
43 #include <fenv.h>
44
45 double
46 __nearbyint(double x)
47 {
48 union {
49 unsigned i[2];
50 double d;
51 } xx;
52
53 unsigned hx, sx, i, frac;
54 int rm, j;
55
56 xx.d = x;
57 sx = xx.i[HIWORD] & 0x80000000;
58 hx = xx.i[HIWORD] & ~0x80000000;
59
60 /* handle trivial cases */
61 if (hx >= 0x43300000) { /* x is nan, inf, or already integral */
62 if (hx >= 0x7ff00000) /* x is inf or nan */
63 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
64 return (hx >= 0x7ff80000 ? x : x + x);
65
66 /* assumes sparc-like QNaN */
67 #else
68 return (x + x);
69 #endif
70 return (x);
71 } else if ((hx | xx.i[LOWORD]) == 0) { /* x is zero */
72 return (x);
73 }
74
75 /* get the rounding mode */
76 rm = fegetround();
77
78 /* flip the sense of directed roundings if x is negative */
79 if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
80 rm = (FE_UPWARD + FE_DOWNWARD) - rm;
81
82 /* handle |x| < 1 */
83 if (hx < 0x3ff00000) {
84 if (rm == FE_UPWARD || (rm == FE_TONEAREST && (hx >=
85 0x3fe00000 && ((hx & 0xfffff) | xx.i[LOWORD]))))
86 xx.i[HIWORD] = sx | 0x3ff00000;
87 else
88 xx.i[HIWORD] = sx;
89
90 xx.i[LOWORD] = 0;
91 return (xx.d);
92 }
93
94 /* round x at the integer bit */
95 j = 0x433 - (hx >> 20);
96
97 if (j >= 32) {
98 i = 1 << (j - 32);
99 frac = ((xx.i[HIWORD] << 1) << (63 - j)) | (xx.i[LOWORD] >> (j -
100 32));
101
102 if (xx.i[LOWORD] & (i - 1))
103 frac |= 1;
104
105 if (!frac)
106 return (x);
107
108 xx.i[LOWORD] = 0;
109 xx.i[HIWORD] &= ~(i - 1);
110
111 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) && ((frac >
112 0x80000000u) || ((frac == 0x80000000) && (xx.i[HIWORD] &
113 i)))))
114 xx.i[HIWORD] += i;
115 } else {
116 i = 1 << j;
117 frac = (xx.i[LOWORD] << 1) << (31 - j);
118
119 if (!frac)
120 return (x);
121
122 xx.i[LOWORD] &= ~(i - 1);
123
124 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) && (frac >
125 0x80000000u || ((frac == 0x80000000) && (xx.i[LOWORD] &
126 i))))) {
127 xx.i[LOWORD] += i;
128
129 if (xx.i[LOWORD] == 0)
130 xx.i[HIWORD]++;
131 }
132 }
133
134 return (xx.d);
135 }
|