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.
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.
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]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak __lroundl = lroundl
32 #include <sys/isa_defs.h> /* _ILP32 */
38 lroundl(long double x
) {
47 unsigned hx
, sx
, frac
, l
;
51 sx
= xx
.i
[0] & 0x80000000;
52 hx
= xx
.i
[0] & ~0x80000000;
54 /* handle trivial cases */
55 if (hx
> 0x401e0000) { /* |x| > 2^31 + ... or x is nan */
56 /* convert an out-of-range float */
57 tt
.i
= sx
| 0x7f000000;
62 if (hx
< 0x3fff0000) {
64 return (sx
? -1L : 1L);
68 /* extract the integer and fractional parts of x */
69 j
= 0x406f - (hx
>> 16); /* 91 <= j <= 112 */
70 xx
.i
[0] = 0x10000 | (xx
.i
[0] & 0xffff);
71 if (j
>= 96) { /* 96 <= j <= 112 */
72 l
= xx
.i
[0] >> (j
- 96);
73 frac
= ((xx
.i
[0] << 1) << (127 - j
)) | (xx
.i
[1] >> (j
- 96));
74 if (((xx
.i
[1] << 1) << (127 - j
)) | xx
.i
[2] | xx
.i
[3])
76 } else { /* 91 <= j <= 95 */
77 l
= (xx
.i
[0] << (96 - j
)) | (xx
.i
[1] >> (j
- 64));
78 frac
= (xx
.i
[1] << (96 - j
)) | (xx
.i
[2] >> (j
- 64));
79 if ((xx
.i
[2] << (96 - j
)) | xx
.i
[3])
84 if (frac
>= 0x80000000U
)
87 /* check for result out of range (note that z is |x| at this point) */
88 if (l
> 0x80000000U
|| (l
== 0x80000000U
&& !sx
)) {
89 tt
.i
= sx
| 0x7f000000;
93 /* negate result if need be */
100 lroundl(long double x
) {
108 ex
= xx
.i
[2] & 0x7fff;
109 sx
= xx
.i
[2] & 0x8000;
110 if (ex
< 0x403e) { /* |x| < 2^63 */
111 if (ex
< 0x3fff) { /* |x| < 1 */
113 return (sx
? -1L : 1L);
117 /* round x at the integer bit */
119 i
= 1 << (0x401d - ex
);
120 xx
.i
[1] = (xx
.i
[1] + i
) & ~(i
| (i
- 1));
123 i
= 1 << (0x403d - ex
);
127 xx
.i
[0] &= ~(i
| (i
- 1));
131 xx
.i
[1] = 0x80000000U
;
135 /* now x is nan, inf, or integral */
136 return ((long) xx
.e
);
139 #error Unknown architecture
140 #endif /* defined(__sparc) || defined(__x86) */
142 #error Unsupported architecture
143 #endif /* defined(_ILP32) */