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