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