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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/isa_defs.h>
31 #include <floatingpoint.h>
36 * Ensure that this "portable" code is only used on big-endian ISAs
38 #if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
39 #error "big-endian only!"
43 * Convert a double precision floating point # into a 64-bit unsigned int.
45 * For compatibility with Sun's other conversion routines, pretend result
46 * is signed if input is negative.
52 unsigned i1
; /* bitslam */
53 int exp
; /* exponent */
54 unsigned int m0
; /* most significant word of mantissa */
55 unsigned int m1
; /* least sig. word of mantissa */
56 unsigned int _fp_current_exceptions
= 0;
63 * Extract the exponent and check boundary conditions.
64 * Notice that the exponent is equal to the bit number where
65 * we want the most significant bit to live.
71 exp
= ((i0
>> 20) & 0x7ff) - 0x3ff;
73 /* abs(x) < 1.0, so round to 0 */
74 return ((unsigned long long)0);
75 } else if (exp
> 63) {
77 * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
78 * overflow, Inf, NaN set fp_invalid exception
80 _fp_current_exceptions
|= (1 << (int)fp_invalid
);
81 (void) _Q_set_exception(_fp_current_exceptions
);
83 return ((unsigned long long)LLONG_MIN
);
85 return (ULLONG_MAX
); /* MAXLONG */
88 /* Extract the mantissa. */
90 m0
= 0x80000000 | ((i0
<< 11) & 0x7ffff800) | ((i1
>> 21) & 0x7ff);
94 * The most significant bit of the mantissa is now in bit 63 of m0:m1.
95 * Shift right by (63 - exp) bits.
106 m1
= (m0
<< (exp
- 31)) | (m1
>> (63 - exp
));
107 m0
= (m0
>> (63 - exp
));
109 m1
= (m0
>> (31 - exp
));
116 if ((int)m0
< 0) { /* x < MINLLONG; return MINLLONG */
127 (void) _Q_set_exception(_fp_current_exceptions
);
128 return (((unsigned long long)m0
<< 32) | m1
);
132 * Convert a floating point number into a 64-bit unsigned int.
134 * For compatibility with Sun's other conversion routines, pretend result
135 * is signed if input is negative.
140 int i0
; /* bitslam */
141 int exp
; /* exponent */
142 unsigned int m0
; /* most significant word of mantissa */
143 unsigned int m1
; /* least sig. word of mantissa */
144 unsigned int _fp_current_exceptions
= 0;
151 * Extract the exponent and check boundary conditions.
152 * Notice that the exponent is equal to the bit number where
153 * we want the most significant bit to live.
158 exp
= ((i0
>> 23) & 0xff) - 0x7f;
160 /* abs(x) < 1.0, so round to 0 */
161 return ((unsigned long long)0);
162 } else if (exp
> 63) {
164 * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
165 * overflow, Inf, NaN set fp_invalid exception
167 _fp_current_exceptions
|= (1 << (int)fp_invalid
);
168 (void) _Q_set_exception(_fp_current_exceptions
);
170 return ((unsigned long long)LLONG_MIN
);
172 return (ULLONG_MAX
); /* MAXLONG */
175 /* Extract the mantissa. */
177 m0
= 0x80000000 | (i0
<< 8) & 0x7fffff00;
181 * The most significant bit of the mantissa is now in bit 63 of m0:m1.
182 * Shift right by (63 - exp) bits.
193 m1
= m0
<< (exp
- 31);
194 m0
= (m0
>> (63 - exp
));
196 m1
= (m0
>> (31 - exp
));
203 if ((int)m0
< 0) { /* x < MINLLONG; return MINLLONG */
214 (void) _Q_set_exception(_fp_current_exceptions
);
215 return (((unsigned long long)m0
<< 32) | m1
);
219 * Convert an extended precision floating point # into a 64-bit unsigned int.
221 * For compatibility with Sun's other conversion routines, pretend result
222 * is signed if input is negative.
225 _Q_qtoull(long double ld
)
228 unsigned int i1
, i2
; /* a long double is 128-bit in length */
229 int exp
; /* exponent */
230 unsigned int m0
; /* most significant word of mantissa */
231 unsigned int m1
; /* least sig. word of mantissa */
232 unsigned int _fp_current_exceptions
= 0;
233 int *plngdbl
= (int *)&ld
;
235 /* Only 96-bits of precision used */
241 * Extract the exponent and check boundary conditions.
242 * Notice that the exponent is equal to the bit number where
243 * we want the most significant bit to live.
245 exp
= ((i0
>> 16) & 0x7fff) - 0x3fff;
247 return ((long long)0); /* abs(x) < 1.0, so round to 0 */
248 } else if (exp
> 63) {
250 * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
251 * overflow, Inf, NaN set fp_invalid exception
253 _fp_current_exceptions
|= (1 << (int)fp_invalid
);
254 (void) _Q_set_exception(_fp_current_exceptions
);
256 return ((unsigned long long)LLONG_MIN
);
258 return (ULLONG_MAX
); /* MAXLONG */
261 /* Extract the mantissa. */
263 m0
= 0x80000000 | ((i0
<<15) & 0x7fff8000) | ((i1
>>17) & 0x7fff);
264 m1
= (i1
<< 15) | ((i2
>> 17) & 0x7fff);
267 * The most significant bit of the mantissa is now in bit 63 of m0:m1.
268 * Shift right by (63 - exp) bits.
279 m1
= (m0
<< (exp
- 31)) | (m1
>> (63 - exp
));
280 m0
= (m0
>> (63 - exp
));
282 m1
= (m0
>> (31 - exp
));
289 if ((int)m0
< 0) { /* x < MINLLONG; return MINLLONG */
300 (void) _Q_set_exception(_fp_current_exceptions
);
301 return (((unsigned long long)m0
<< 32) | m1
);