Print this page
5262 libm needs to be carefully unifdef'd
5268 libm doesn't need to hide symbols which are already local
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/nearbyintf.c
+++ new/usr/src/lib/libm/common/m9x/nearbyintf.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
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
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 -#if defined(ELFOBJ)
31 30 #pragma weak nearbyintf = __nearbyintf
32 -#endif
33 31
34 32 #include "libm.h"
35 33 #include "fenv_synonyms.h"
36 34 #include <fenv.h>
37 35
38 36 float
39 37 __nearbyintf(float x) {
40 38 union {
41 39 unsigned i;
42 40 float f;
43 41 } xx;
44 42 unsigned hx, sx, i, frac;
45 43 int rm;
46 44
47 45 xx.f = x;
48 46 sx = xx.i & 0x80000000;
49 47 hx = xx.i & ~0x80000000;
50 48
51 49 /* handle trivial cases */
52 50 if (hx >= 0x4b000000) { /* x is nan, inf, or already integral */
53 51 if (hx > 0x7f800000) /* x is nan */
54 52 return (x * x); /* + -> * for Cheetah */
55 53 return (x);
56 54 } else if (hx == 0) /* x is zero */
57 55 return (x);
58 56
59 57 /* get the rounding mode */
60 58 rm = fegetround();
61 59
62 60 /* flip the sense of directed roundings if x is negative */
63 61 if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
64 62 rm = (FE_UPWARD + FE_DOWNWARD) - rm;
65 63
66 64 /* handle |x| < 1 */
67 65 if (hx < 0x3f800000) {
68 66 if (rm == FE_UPWARD || (rm == FE_TONEAREST && hx > 0x3f000000))
69 67 xx.i = sx | 0x3f800000;
70 68 else
71 69 xx.i = sx;
72 70 return (xx.f);
73 71 }
74 72
75 73 /* round x at the integer bit */
76 74 i = 1 << (0x96 - (hx >> 23));
77 75 frac = hx & (i - 1);
78 76 if (!frac)
79 77 return (x);
80 78
81 79 hx &= ~(i - 1);
82 80 if (rm == FE_UPWARD || (rm == FE_TONEAREST && (frac > (i >> 1) ||
83 81 ((frac == (i >> 1)) && (hx & i)))))
84 82 xx.i = sx | (hx + i);
85 83 else
86 84 xx.i = sx | hx;
87 85 return (xx.f);
88 86 }
89 87
90 88 #if 0
91 89
92 90 /*
93 91 * Alternate implementations for SPARC, x86, using fp ops. These may
94 92 * be faster depending on how expensive saving and restoring the fp
95 93 * modes and status flags is.
96 94 */
97 95
98 96 #include "libm.h"
99 97 #include "fma.h"
100 98
101 99 #if defined(__sparc)
102 100
103 101 float
104 102 __nearbyintf(float x) {
105 103 union {
106 104 unsigned i;
107 105 float f;
108 106 } xx, yy;
109 107 float z;
110 108 unsigned hx, sx, fsr, oldfsr;
111 109 int rm;
112 110
113 111 xx.f = x;
114 112 sx = xx.i & 0x80000000;
115 113 hx = xx.i & ~0x80000000;
116 114
117 115 /* handle trivial cases */
118 116 if (hx >= 0x4b000000) /* x is nan, inf, or already integral */
119 117 return (x + 0.0f);
120 118 else if (hx == 0) /* x is zero */
121 119 return (x);
122 120
123 121 /* save the fsr */
124 122 __fenv_getfsr(&oldfsr);
125 123
126 124 /* handle |x| < 1 */
127 125 if (hx < 0x3f800000) {
128 126 /* flip the sense of directed roundings if x is negative */
129 127 rm = oldfsr >> 30;
130 128 if (sx)
131 129 rm ^= rm >> 1;
132 130 if (rm == FSR_RP || (rm == FSR_RN && hx > 0x3f000000))
133 131 xx.i = sx | 0x3f800000;
134 132 else
135 133 xx.i = sx;
136 134 return (xx.f);
137 135 }
138 136
139 137 /* clear the inexact trap */
140 138 fsr = oldfsr & ~FSR_NXM;
141 139 __fenv_setfsr(&fsr);
142 140
143 141 /* round x at the integer bit */
144 142 yy.i = sx | 0x4b000000;
145 143 z = (x + yy.f) - yy.f;
146 144
147 145 /* restore the old fsr */
148 146 __fenv_setfsr(&oldfsr);
149 147
150 148 return (z);
151 149 }
152 150
153 151 #elif defined(__x86)
154 152
155 153 /* inline template */
156 154 extern long double frndint(long double);
157 155
158 156 float
159 157 __nearbyintf(float x) {
160 158 long double z;
161 159 unsigned oldcwsw, cwsw;
162 160
163 161 /* save the control and status words, mask the inexact exception */
164 162 __fenv_getcwsw(&oldcwsw);
165 163 cwsw = oldcwsw | 0x00200000;
166 164 __fenv_setcwsw(&cwsw);
167 165
168 166 z = frndint((long double) x);
169 167
170 168 /*
171 169 * restore the control and status words, preserving all but the
172 170 * inexact flag
173 171 */
174 172 __fenv_getcwsw(&cwsw);
175 173 oldcwsw |= (cwsw & 0x1f);
176 174 __fenv_setcwsw(&oldcwsw);
177 175
178 176 /* note: the value of z is representable in single precision */
179 177 return (z);
180 178 }
181 179
182 180 #else
183 181 #error Unknown architecture
184 182 #endif
185 183
186 184 #endif
↓ open down ↓ |
144 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX