Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/llrintl.c
+++ new/usr/src/lib/libm/common/m9x/llrintl.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 llrintl = __llrintl
32 32 #if defined(__sparcv9) || defined(__amd64)
33 33 #pragma weak lrintl = __llrintl
34 34 #pragma weak __lrintl = __llrintl
35 35 #endif
36 36 #endif
37 37
38 38 #include "libm.h"
39 39
40 40 #if defined(__sparc)
41 41
42 42 #include "fma.h"
43 43 #include "fenv_inlines.h"
44 44
45 45 long long
46 46 llrintl(long double x) {
47 47 union {
48 48 unsigned i[4];
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
49 49 long double q;
50 50 } xx;
51 51 union {
52 52 unsigned i[2];
53 53 long long l;
54 54 } zz;
55 55 union {
56 56 unsigned i;
57 57 float f;
58 58 } tt;
59 - unsigned int hx, sx, frac;
60 - unsigned int fsr;
59 + unsigned int hx, sx, frac, fsr;
61 60 int rm, j;
62 61 volatile float dummy;
63 62
64 63 xx.q = x;
65 64 sx = xx.i[0] & 0x80000000;
66 65 hx = xx.i[0] & ~0x80000000;
67 66
68 67 /* handle trivial cases */
69 68 if (hx > 0x403e0000) { /* |x| > 2^63 + ... or x is nan */
70 69 /* convert an out-of-range float */
71 70 tt.i = sx | 0x7f000000;
72 71 return ((long long) tt.f);
73 72 } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) /* x is zero */
74 73 return (0LL);
75 74
76 75 /* get the rounding mode */
77 76 __fenv_getfsr32(&fsr);
78 77 rm = fsr >> 30;
79 78
80 79 /* flip the sense of directed roundings if x is negative */
81 80 if (sx)
82 81 rm ^= rm >> 1;
83 82
84 83 /* handle |x| < 1 */
85 84 if (hx < 0x3fff0000) {
86 85 dummy = 1.0e30f; /* x is nonzero, so raise inexact */
87 86 dummy += 1.0e-30f;
88 87 if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 &&
89 88 ((hx & 0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
90 89 return (sx ? -1LL : 1LL);
91 90 return (0LL);
92 91 }
93 92
94 93 /* extract the integer and fractional parts of x */
95 94 j = 0x406f - (hx >> 16);
96 95 xx.i[0] = 0x10000 | (xx.i[0] & 0xffff);
97 96 if (j >= 96) {
98 97 zz.i[0] = 0;
99 98 zz.i[1] = xx.i[0] >> (j - 96);
100 99 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
101 100 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3])
102 101 frac |= 1;
103 102 } else if (j >= 64) {
104 103 zz.i[0] = xx.i[0] >> (j - 64);
105 104 zz.i[1] = ((xx.i[0] << 1) << (95 - j)) | (xx.i[1] >> (j - 64));
106 105 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
107 106 if (((xx.i[2] << 1) << (95 - j)) | xx.i[3])
108 107 frac |= 1;
109 108 } else {
110 109 zz.i[0] = ((xx.i[0] << 1) << (63 - j)) | (xx.i[1] >> (j - 32));
111 110 zz.i[1] = ((xx.i[1] << 1) << (63 - j)) | (xx.i[2] >> (j - 32));
112 111 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
113 112 if ((xx.i[3] << 1) << (63 - j))
114 113 frac |= 1;
115 114 }
116 115
117 116 /* round */
118 117 if (frac && (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
119 118 (frac == 0x80000000 && (zz.i[1] & 1)))))) {
120 119 if (++zz.i[1] == 0)
121 120 zz.i[0]++;
122 121 }
123 122
124 123 /* check for result out of range (note that z is |x| at this point) */
125 124 if (zz.i[0] > 0x80000000u || (zz.i[0] == 0x80000000 && (zz.i[1] ||
126 125 !sx))) {
127 126 tt.i = sx | 0x7f000000;
128 127 return ((long long) tt.f);
129 128 }
130 129
131 130 /* raise inexact if need be */
132 131 if (frac) {
133 132 dummy = 1.0e30F;
134 133 dummy += 1.0e-30F;
135 134 }
136 135
137 136 /* negate result if need be */
138 137 if (sx) {
139 138 zz.i[0] = ~zz.i[0];
140 139 zz.i[1] = -zz.i[1];
141 140 if (zz.i[1] == 0)
142 141 zz.i[0]++;
143 142 }
144 143 return (zz.l);
145 144 }
146 145 #elif defined(__x86)
147 146 long long
148 147 llrintl(long double x) {
149 148 /*
150 149 * Note: The following code works on x86 (in the default rounding
151 150 * precision mode), but one ought to just use the fistpll instruction
152 151 * instead.
153 152 */
154 153 union {
155 154 unsigned i[3];
156 155 long double e;
157 156 } xx, yy;
158 157 int ex;
159 158
160 159 xx.e = x;
161 160 ex = xx.i[2] & 0x7fff;
162 161
163 162 if (ex < 0x403e) { /* |x| < 2^63 */
164 163 /* add and subtract a power of two to round x to an integer */
165 164 yy.i[2] = (xx.i[2] & 0x8000) | 0x403e;
166 165 yy.i[1] = 0x80000000;
167 166 yy.i[0] = 0;
168 167 x = (x + yy.e) - yy.e;
169 168 }
170 169
171 170 /* now x is nan, inf, or integral */
172 171 return ((long long) x);
173 172 }
174 173 #else
175 174 #error Unknown architecture
176 175 #endif
↓ open down ↓ |
106 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX