1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
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 __llroundl = llroundl
32 #if defined(__sparcv9) || defined(__amd64)
33 #pragma weak lroundl = llroundl
34 #pragma weak __lroundl = llroundl
35 #endif
36
37 #include "libm.h"
38
39 #if defined(__sparc)
40 long long
41 llroundl(long double x)
42 {
43 union {
44 unsigned i[4];
45 long double q;
46 } xx;
47 union {
48 unsigned i[2];
49 long long l;
50 } zz;
51 union {
52 unsigned i;
53 float f;
54 } tt;
55
56 unsigned hx, sx, frac;
57 int j;
58
59 xx.q = x;
60 sx = xx.i[0] & 0x80000000;
61 hx = xx.i[0] & ~0x80000000;
62
63 /* handle trivial cases */
64 if (hx > 0x403e0000) { /* |x| > 2^63 + ... or x is nan */
65 /* convert an out-of-range float */
66 tt.i = sx | 0x7f000000;
67 return ((long long)tt.f);
68 }
69
70 /* handle |x| < 1 */
71 if (hx < 0x3fff0000) {
72 if (hx >= 0x3ffe0000)
73 return (sx ? -1LL : 1LL);
74
75 return (0LL);
76 }
77
78 /* extract the integer and fractional parts of x */
79 j = 0x406f - (hx >> 16);
80 xx.i[0] = 0x10000 | (xx.i[0] & 0xffff);
81
82 if (j >= 96) {
83 zz.i[0] = 0;
84 zz.i[1] = xx.i[0] >> (j - 96);
85 frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
86
87 if (((xx.i[1] << 1) << (127 - j)) | xx.i[2] | xx.i[3])
88 frac |= 1;
89 } else if (j >= 64) {
90 zz.i[0] = xx.i[0] >> (j - 64);
91 zz.i[1] = ((xx.i[0] << 1) << (95 - j)) | (xx.i[1] >> (j - 64));
92 frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
93
94 if (((xx.i[2] << 1) << (95 - j)) | xx.i[3])
95 frac |= 1;
96 } else {
97 zz.i[0] = ((xx.i[0] << 1) << (63 - j)) | (xx.i[1] >> (j - 32));
98 zz.i[1] = ((xx.i[1] << 1) << (63 - j)) | (xx.i[2] >> (j - 32));
99 frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
100
101 if ((xx.i[3] << 1) << (63 - j))
102 frac |= 1;
103 }
104
105 /* round */
106 if (frac >= 0x80000000u) {
107 if (++zz.i[1] == 0)
108 zz.i[0]++;
109 }
110
111 /* check for result out of range (note that z is |x| at this point) */
112 if (zz.i[0] > 0x80000000u || (zz.i[0] == 0x80000000 && (zz.i[1] ||
113 !sx))) {
114 tt.i = sx | 0x7f000000;
115 return ((long long)tt.f);
116 }
117
118 /* negate result if need be */
119 if (sx) {
120 zz.i[0] = ~zz.i[0];
121 zz.i[1] = -zz.i[1];
122
123 if (zz.i[1] == 0)
124 zz.i[0]++;
125 }
126
127 return (zz.l);
128 }
129 #elif defined(__x86)
130 long long
131 llroundl(long double x)
132 {
133 union {
134 unsigned i[3];
135 long double e;
136 } xx;
137
138 int ex, sx, i;
139
140 xx.e = x;
141 ex = xx.i[2] & 0x7fff;
142 sx = xx.i[2] & 0x8000;
143
144 if (ex < 0x403e) { /* |x| < 2^63 */
145 /* handle |x| < 1 */
146 if (ex < 0x3fff) {
147 if (ex >= 0x3ffe)
148 return (sx ? -1LL : 1LL);
149
150 return (0LL);
151 }
152
153 /* round x at the integer bit */
154 if (ex < 0x401e) {
155 i = 1 << (0x401d - ex);
156 xx.i[1] = (xx.i[1] + i) & ~(i | (i - 1));
157 xx.i[0] = 0;
158 } else {
159 i = 1 << (0x403d - ex);
160 xx.i[0] += i;
161
162 if (xx.i[0] < i)
163 xx.i[1]++;
164
165 xx.i[0] &= ~(i | (i - 1));
166 }
167
168 if (xx.i[1] == 0) {
169 xx.i[2] = sx | ++ex;
170 xx.i[1] = 0x80000000U;
171 }
172 }
173
174 /* now x is nan, inf, or integral */
175 return ((long long)xx.e);
176 }
177 #else
178 #error Unknown architecture
179 #endif