Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/nearbyint.c
+++ new/usr/src/lib/libm/common/m9x/nearbyint.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #if defined(ELFOBJ)
31 31 #pragma weak nearbyint = __nearbyint
32 32 #endif
33 33
34 34 /*
35 35 * nearbyint(x) returns the nearest fp integer to x in the direction
36 36 * corresponding to the current rounding direction without raising
37 37 * the inexact exception.
38 38 *
39 39 * nearbyint(x) is x unchanged if x is +/-0 or +/-inf. If x is NaN,
40 40 * nearbyint(x) is also NaN.
41 41 */
42 42
43 43 #include "libm.h"
44 44 #include "fenv_synonyms.h"
45 45 #include <fenv.h>
46 46
47 47 double
48 48 __nearbyint(double x) {
49 49 union {
50 50 unsigned i[2];
51 51 double d;
52 52 } xx;
53 53 unsigned hx, sx, i, frac;
54 54 int rm, j;
55 55
56 56 xx.d = x;
57 57 sx = xx.i[HIWORD] & 0x80000000;
58 58 hx = xx.i[HIWORD] & ~0x80000000;
59 59
60 60 /* handle trivial cases */
61 61 if (hx >= 0x43300000) { /* x is nan, inf, or already integral */
62 62 if (hx >= 0x7ff00000) /* x is inf or nan */
63 63 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
64 64 return (hx >= 0x7ff80000 ? x : x + x);
65 65 /* assumes sparc-like QNaN */
66 66 #else
67 67 return (x + x);
68 68 #endif
69 69 return (x);
70 70 } else if ((hx | xx.i[LOWORD]) == 0) /* x is zero */
71 71 return (x);
72 72
73 73 /* get the rounding mode */
74 74 rm = fegetround();
75 75
76 76 /* flip the sense of directed roundings if x is negative */
77 77 if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
78 78 rm = (FE_UPWARD + FE_DOWNWARD) - rm;
79 79
80 80 /* handle |x| < 1 */
81 81 if (hx < 0x3ff00000) {
82 82 if (rm == FE_UPWARD || (rm == FE_TONEAREST &&
83 83 (hx >= 0x3fe00000 && ((hx & 0xfffff) | xx.i[LOWORD]))))
84 84 xx.i[HIWORD] = sx | 0x3ff00000;
85 85 else
86 86 xx.i[HIWORD] = sx;
87 87 xx.i[LOWORD] = 0;
88 88 return (xx.d);
89 89 }
90 90
91 91 /* round x at the integer bit */
92 92 j = 0x433 - (hx >> 20);
↓ open down ↓ |
92 lines elided |
↑ open up ↑ |
93 93 if (j >= 32) {
94 94 i = 1 << (j - 32);
95 95 frac = ((xx.i[HIWORD] << 1) << (63 - j)) |
96 96 (xx.i[LOWORD] >> (j - 32));
97 97 if (xx.i[LOWORD] & (i - 1))
98 98 frac |= 1;
99 99 if (!frac)
100 100 return (x);
101 101 xx.i[LOWORD] = 0;
102 102 xx.i[HIWORD] &= ~(i - 1);
103 - if (rm == FE_UPWARD || (rm == FE_TONEAREST &&
104 - (frac > 0x80000000u || (frac == 0x80000000) &&
105 - (xx.i[HIWORD] & i))))
103 + if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
104 + ((frac > 0x80000000u) || ((frac == 0x80000000) &&
105 + (xx.i[HIWORD] & i)))))
106 106 xx.i[HIWORD] += i;
107 107 } else {
108 108 i = 1 << j;
109 109 frac = (xx.i[LOWORD] << 1) << (31 - j);
110 110 if (!frac)
111 111 return (x);
112 112 xx.i[LOWORD] &= ~(i - 1);
113 - if (rm == FE_UPWARD || (rm == FE_TONEAREST &&
114 - (frac > 0x80000000u || (frac == 0x80000000) &&
115 - (xx.i[LOWORD] & i)))) {
113 + if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
114 + (frac > 0x80000000u || ((frac == 0x80000000) &&
115 + (xx.i[LOWORD] & i))))) {
116 116 xx.i[LOWORD] += i;
117 117 if (xx.i[LOWORD] == 0)
118 118 xx.i[HIWORD]++;
119 119 }
120 120 }
121 121 return (xx.d);
122 122 }
123 123
124 124 #if 0
125 125
126 126 /*
127 127 * Alternate implementations for SPARC, x86, using fp ops. These may
128 128 * be faster depending on how expensive saving and restoring the fp
129 129 * modes and status flags is.
130 130 */
131 131
132 132 #include "libm.h"
133 133 #include "fma.h"
134 134
135 135 #if defined(__sparc)
136 136
137 137 double
138 138 __nearbyint(double x) {
139 139 union {
140 140 unsigned i[2];
141 141 double d;
142 142 } xx, yy;
143 143 double z;
144 144 unsigned hx, sx, fsr, oldfsr;
145 145 int rm;
146 146
147 147 xx.d = x;
148 148 sx = xx.i[0] & 0x80000000;
149 149 hx = xx.i[0] & ~0x80000000;
150 150
151 151 /* handle trivial cases */
152 152 if (hx >= 0x43300000) /* x is nan, inf, or already integral */
153 153 return (x + 0.0);
154 154 else if ((hx | xx.i[1]) == 0) /* x is zero */
155 155 return (x);
156 156
157 157 /* save the fsr */
158 158 __fenv_getfsr(&oldfsr);
159 159
160 160 /* handle |x| < 1 */
161 161 if (hx < 0x3ff00000) {
162 162 /* flip the sense of directed roundings if x is negative */
163 163 rm = oldfsr >> 30;
164 164 if (sx)
165 165 rm ^= rm >> 1;
166 166 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3fe00000 &&
167 167 ((hx & 0xfffff) | xx.i[1]))))
168 168 xx.i[0] = sx | 0x3ff00000;
169 169 else
170 170 xx.i[0] = sx;
171 171 xx.i[1] = 0;
172 172 return (xx.d);
173 173 }
174 174
175 175 /* clear the inexact trap */
176 176 fsr = oldfsr & ~FSR_NXM;
177 177 __fenv_setfsr(&fsr);
178 178
179 179 /* round x at the integer bit */
180 180 yy.i[0] = sx | 0x43300000;
181 181 yy.i[1] = 0;
182 182 z = (x + yy.d) - yy.d;
183 183
184 184 /* restore the old fsr */
185 185 __fenv_setfsr(&oldfsr);
186 186
187 187 return (z);
188 188 }
189 189
190 190 #elif defined(__x86)
191 191
192 192 /* inline template */
193 193 extern long double frndint(long double);
194 194
195 195 double
196 196 __nearbyint(double x) {
197 197 long double z;
198 198 unsigned oldcwsw, cwsw;
199 199
200 200 /* save the control and status words, mask the inexact exception */
201 201 __fenv_getcwsw(&oldcwsw);
202 202 cwsw = oldcwsw | 0x00200000;
203 203 __fenv_setcwsw(&cwsw);
204 204
205 205 z = frndint((long double) x);
206 206
207 207 /*
208 208 * restore the control and status words, preserving all but the
209 209 * inexact flag
210 210 */
211 211 __fenv_getcwsw(&cwsw);
212 212 oldcwsw |= (cwsw & 0x1f);
213 213 __fenv_setcwsw(&oldcwsw);
214 214
215 215 /* note: the value of z is representable in double precision */
216 216 return (z);
217 217 }
218 218
219 219 #else
220 220 #error Unknown architecture
221 221 #endif
222 222
223 223 #endif
↓ open down ↓ |
98 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX