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 __roundl = roundl
31
32 #include "libm.h"
33
34 #if defined(__sparc)
35 long double
36 roundl(long double x) {
37 union {
38 unsigned i[4];
39 long double q;
40 } xx;
41 unsigned hx, sx, v;
42 int j;
43
44 xx.q = x;
45 sx = xx.i[0] & 0x80000000;
46 hx = xx.i[0] & ~0x80000000;
47
48 /* handle trivial cases */
49 if (hx >= 0x406f0000) /* |x| >= 2^112 + ... or x is nan */
50 return (hx >= 0x7fff0000 ? x + x : x);
51
52 /* handle |x| < 1 */
53 if (hx < 0x3fff0000) {
54 if (hx >= 0x3ffe0000)
55 return (sx ? -1.0L : 1.0L);
56 return (sx ? -0.0L : 0.0L);
57 }
58
59 xx.i[0] = hx;
60 j = 0x406f - (hx >> 16); /* 1 <= j <= 112 */
61 if (j >= 96) { /* 96 <= j <= 112 */
62 v = (1U << (j - 96)) >> 1;
63 if (v) {
64 if (xx.i[0] & v)
65 xx.i[0] += v;
66 xx.i[0] &= ~(v - 1);
67 } else if (xx.i[1] & 0x80000000)
68 ++xx.i[0];
69 xx.i[1] = xx.i[2] = xx.i[3] = 0;
70 } else if (j >= 64) { /* 64 <= j <= 95 */
71 v = (1U << (j - 64)) >> 1;
72 if (v) {
73 if (xx.i[1] & v) {
74 xx.i[1] += v;
75 if (xx.i[1] < v)
76 ++xx.i[0];
77 }
78 xx.i[1] &= ~(v - 1);
79 } else if (xx.i[2] & 0x80000000) {
80 if (++xx.i[1] == 0)
81 ++xx.i[0];
82 }
83 xx.i[2] = xx.i[3] = 0;
84 } else if (j >= 32) { /* 32 <= j <= 63 */
85 v = (1U << (j - 32)) >> 1;
86 if (v) {
87 if (xx.i[2] & v) {
88 xx.i[2] += v;
89 if (xx.i[2] < v) {
90 if (++xx.i[1] == 0)
91 ++xx.i[0];
92 }
93 }
94 xx.i[2] &= ~(v - 1);
95 } else if (xx.i[3] & 0x80000000) {
96 if (++xx.i[2] == 0) {
97 if (++xx.i[1] == 0)
98 ++xx.i[0];
99 }
100 }
101 xx.i[3] = 0;
102 } else { /* 1 <= j <= 31 */
103 v = 1U << (j - 1);
104 if (xx.i[3] & v) {
105 xx.i[3] += v;
106 if (xx.i[3] < v) {
107 if (++xx.i[2] == 0) {
108 if (++xx.i[1] == 0)
109 ++xx.i[0];
110 }
111 }
112 }
113 xx.i[3] &= ~(v - 1);
114 }
115
116 /* negate result if need be */
117 if (sx)
118 xx.i[0] |= 0x80000000;
119 return (xx.q);
120 }
121 #elif defined(__x86)
122 long double
123 roundl(long double x) {
124 union {
125 unsigned i[3];
126 long double e;
127 } xx;
128 int ex, sx, i;
129
130 xx.e = x;
131 ex = xx.i[2] & 0x7fff;
132 sx = xx.i[2] & 0x8000;
133 if (ex < 0x403e) { /* |x| < 2^63 */
134 if (ex < 0x3fff) { /* |x| < 1 */
135 if (ex >= 0x3ffe)
136 return (sx ? -1.0L : 1.0L);
137 return (sx ? -0.0L : 0.0L);
138 }
139
140 /* round x at the integer bit */
141 if (ex < 0x401e) {
142 i = 1 << (0x401d - ex);
143 xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1));
144 xx.i[0] = 0;
145 } else {
146 i = 1 << (0x403d - ex);
147 xx.i[0] += i;
148 if (xx.i[0] < i)
149 xx.i[1]++;
150 xx.i[0] &= ~(i | (i - 1));
151 }
152 if (xx.i[1] == 0) {
153 xx.i[2] = sx | ++ex;
154 xx.i[1] = 0x80000000U;
155 }
156 return (xx.e);
157 } else if (ex < 0x7fff) /* x is integral */
158 return (x);
159 else /* inf or nan */
160 return (x + x);
161 }
162 #else
163 #error Unknown architecture
164 #endif /* defined(__sparc) || defined(__x86) */
|
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 __roundl = roundl
32
33 #include "libm.h"
34
35 #if defined(__sparc)
36 long double
37 roundl(long double x)
38 {
39 union {
40 unsigned i[4];
41 long double q;
42 } xx;
43
44 unsigned hx, sx, v;
45 int j;
46
47 xx.q = x;
48 sx = xx.i[0] & 0x80000000;
49 hx = xx.i[0] & ~0x80000000;
50
51 /* handle trivial cases */
52 if (hx >= 0x406f0000) /* |x| >= 2^112 + ... or x is nan */
53 return (hx >= 0x7fff0000 ? x + x : x);
54
55 /* handle |x| < 1 */
56 if (hx < 0x3fff0000) {
57 if (hx >= 0x3ffe0000)
58 return (sx ? -1.0L : 1.0L);
59
60 return (sx ? -0.0L : 0.0L);
61 }
62
63 xx.i[0] = hx;
64 j = 0x406f - (hx >> 16); /* 1 <= j <= 112 */
65
66 if (j >= 96) { /* 96 <= j <= 112 */
67 v = (1U << (j - 96)) >> 1;
68
69 if (v) {
70 if (xx.i[0] & v)
71 xx.i[0] += v;
72
73 xx.i[0] &= ~(v - 1);
74 } else if (xx.i[1] & 0x80000000) {
75 ++xx.i[0];
76 }
77
78 xx.i[1] = xx.i[2] = xx.i[3] = 0;
79 } else if (j >= 64) { /* 64 <= j <= 95 */
80 v = (1U << (j - 64)) >> 1;
81
82 if (v) {
83 if (xx.i[1] & v) {
84 xx.i[1] += v;
85
86 if (xx.i[1] < v)
87 ++xx.i[0];
88 }
89
90 xx.i[1] &= ~(v - 1);
91 } else if (xx.i[2] & 0x80000000) {
92 if (++xx.i[1] == 0)
93 ++xx.i[0];
94 }
95
96 xx.i[2] = xx.i[3] = 0;
97 } else if (j >= 32) { /* 32 <= j <= 63 */
98 v = (1U << (j - 32)) >> 1;
99
100 if (v) {
101 if (xx.i[2] & v) {
102 xx.i[2] += v;
103
104 if (xx.i[2] < v) {
105 if (++xx.i[1] == 0)
106 ++xx.i[0];
107 }
108 }
109
110 xx.i[2] &= ~(v - 1);
111 } else if (xx.i[3] & 0x80000000) {
112 if (++xx.i[2] == 0) {
113 if (++xx.i[1] == 0)
114 ++xx.i[0];
115 }
116 }
117
118 xx.i[3] = 0;
119 } else { /* 1 <= j <= 31 */
120 v = 1U << (j - 1);
121
122 if (xx.i[3] & v) {
123 xx.i[3] += v;
124
125 if (xx.i[3] < v) {
126 if (++xx.i[2] == 0) {
127 if (++xx.i[1] == 0)
128 ++xx.i[0];
129 }
130 }
131 }
132
133 xx.i[3] &= ~(v - 1);
134 }
135
136 /* negate result if need be */
137 if (sx)
138 xx.i[0] |= 0x80000000;
139
140 return (xx.q);
141 }
142 #elif defined(__x86)
143 long double
144 roundl(long double x)
145 {
146 union {
147 unsigned i[3];
148 long double e;
149 } xx;
150
151 int ex, sx, i;
152
153 xx.e = x;
154 ex = xx.i[2] & 0x7fff;
155 sx = xx.i[2] & 0x8000;
156
157 if (ex < 0x403e) { /* |x| < 2^63 */
158 if (ex < 0x3fff) { /* |x| < 1 */
159 if (ex >= 0x3ffe)
160 return (sx ? -1.0L : 1.0L);
161
162 return (sx ? -0.0L : 0.0L);
163 }
164
165 /* round x at the integer bit */
166 if (ex < 0x401e) {
167 i = 1 << (0x401d - ex);
168 xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1));
169 xx.i[0] = 0;
170 } else {
171 i = 1 << (0x403d - ex);
172 xx.i[0] += i;
173
174 if (xx.i[0] < i)
175 xx.i[1]++;
176
177 xx.i[0] &= ~(i | (i - 1));
178 }
179
180 if (xx.i[1] == 0) {
181 xx.i[2] = sx | ++ex;
182 xx.i[1] = 0x80000000U;
183 }
184
185 return (xx.e);
186 } else if (ex < 0x7fff) { /* x is integral */
187 return (x);
188 } else { /* inf or nan */
189 return (x + x);
190 }
191 }
192 #else
193 #error Unknown architecture
194 #endif /* defined(__sparc) || defined(__x86) */
|