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