1 /*
2 * CDDL HEADER START
3 *
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.
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 #if defined(ELFOBJ)
31 #pragma weak nearbyint = __nearbyint
32 #endif
33
34 /*
35 * nearbyint(x) returns the nearest fp integer to x in the direction
36 * corresponding to the current rounding direction without raising
37 * the inexact exception.
38 *
39 * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN,
40 * nearbyint(x) is also NaN.
41 */
42
43 #include "libm.h"
44 #include "fenv_synonyms.h"
45 #include <fenv.h>
46
47 double
48 __nearbyint(double x) {
49 union {
50 unsigned i[2];
51 double d;
52 } xx;
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 /* assumes sparc-like QNaN */
66 #else
67 return (x + x);
68 #endif
69 return (x);
70 } else if ((hx | xx.i[LOWORD]) == 0) /* x is zero */
71 return (x);
72
73 /* get the rounding mode */
74 rm = fegetround();
75
76 /* flip the sense of directed roundings if x is negative */
77 if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
78 rm = (FE_UPWARD + FE_DOWNWARD) - rm;
79
80 /* handle |x| < 1 */
81 if (hx < 0x3ff00000) {
82 if (rm == FE_UPWARD || (rm == FE_TONEAREST &&
83 (hx >= 0x3fe00000 && ((hx & 0xfffff) | xx.i[LOWORD]))))
84 xx.i[HIWORD] = sx | 0x3ff00000;
85 else
86 xx.i[HIWORD] = sx;
87 xx.i[LOWORD] = 0;
88 return (xx.d);
89 }
90
91 /* round x at the integer bit */
92 j = 0x433 - (hx >> 20);
93 if (j >= 32) {
94 i = 1 << (j - 32);
95 frac = ((xx.i[HIWORD] << 1) << (63 - j)) |
96 (xx.i[LOWORD] >> (j - 32));
97 if (xx.i[LOWORD] & (i - 1))
98 frac |= 1;
99 if (!frac)
100 return (x);
101 xx.i[LOWORD] = 0;
102 xx.i[HIWORD] &= ~(i - 1);
103 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
104 ((frac > 0x80000000u) || ((frac == 0x80000000) &&
105 (xx.i[HIWORD] & i)))))
106 xx.i[HIWORD] += i;
107 } else {
108 i = 1 << j;
109 frac = (xx.i[LOWORD] << 1) << (31 - j);
110 if (!frac)
111 return (x);
112 xx.i[LOWORD] &= ~(i - 1);
113 if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
114 (frac > 0x80000000u || ((frac == 0x80000000) &&
115 (xx.i[LOWORD] & i))))) {
116 xx.i[LOWORD] += i;
117 if (xx.i[LOWORD] == 0)
118 xx.i[HIWORD]++;
119 }
120 }
121 return (xx.d);
122 }
123
124 #if 0
125
126 /*
127 * Alternate implementations for SPARC, x86, using fp ops. These may
128 * be faster depending on how expensive saving and restoring the fp
129 * modes and status flags is.
130 */
131
132 #include "libm.h"
133 #include "fma.h"
134
135 #if defined(__sparc)
136
137 double
138 __nearbyint(double x) {
139 union {
140 unsigned i[2];
141 double d;
142 } xx, yy;
143 double z;
144 unsigned hx, sx, fsr, oldfsr;
145 int rm;
146
147 xx.d = x;
148 sx = xx.i[0] & 0x80000000;
149 hx = xx.i[0] & ~0x80000000;
150
151 /* handle trivial cases */
152 if (hx >= 0x43300000) /* x is nan, inf, or already integral */
153 return (x + 0.0);
154 else if ((hx | xx.i[1]) == 0) /* x is zero */
155 return (x);
156
157 /* save the fsr */
158 __fenv_getfsr(&oldfsr);
159
160 /* handle |x| < 1 */
161 if (hx < 0x3ff00000) {
162 /* flip the sense of directed roundings if x is negative */
163 rm = oldfsr >> 30;
164 if (sx)
165 rm ^= rm >> 1;
166 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3fe00000 &&
167 ((hx & 0xfffff) | xx.i[1]))))
168 xx.i[0] = sx | 0x3ff00000;
169 else
170 xx.i[0] = sx;
171 xx.i[1] = 0;
172 return (xx.d);
173 }
174
175 /* clear the inexact trap */
176 fsr = oldfsr & ~FSR_NXM;
177 __fenv_setfsr(&fsr);
178
179 /* round x at the integer bit */
180 yy.i[0] = sx | 0x43300000;
181 yy.i[1] = 0;
182 z = (x + yy.d) - yy.d;
183
184 /* restore the old fsr */
185 __fenv_setfsr(&oldfsr);
186
187 return (z);
188 }
189
190 #elif defined(__x86)
191
192 /* inline template */
193 extern long double frndint(long double);
194
195 double
196 __nearbyint(double x) {
197 long double z;
198 unsigned oldcwsw, cwsw;
199
200 /* save the control and status words, mask the inexact exception */
201 __fenv_getcwsw(&oldcwsw);
202 cwsw = oldcwsw | 0x00200000;
203 __fenv_setcwsw(&cwsw);
204
205 z = frndint((long double) x);
206
207 /*
208 * restore the control and status words, preserving all but the
209 * inexact flag
210 */
211 __fenv_getcwsw(&cwsw);
212 oldcwsw |= (cwsw & 0x1f);
213 __fenv_setcwsw(&oldcwsw);
214
215 /* note: the value of z is representable in double precision */
216 return (z);
217 }
218
219 #else
220 #error Unknown architecture
221 #endif
222
223 #endif