17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*7257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
297c478bd9Sstevel@tonic-gate #include <floatingpoint.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include "libc.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * Ensure that this "portable" code is only used on big-endian ISAs
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate #if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
377c478bd9Sstevel@tonic-gate #error "big-endian only!"
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * Convert a double precision floating point # into a 64-bit unsigned int.
427c478bd9Sstevel@tonic-gate *
437c478bd9Sstevel@tonic-gate * For compatibility with Sun's other conversion routines, pretend result
447c478bd9Sstevel@tonic-gate * is signed if input is negative.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate unsigned long long
__dtoull(double dval)477c478bd9Sstevel@tonic-gate __dtoull(double dval)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate int i0; /* bitslam */
507c478bd9Sstevel@tonic-gate unsigned i1; /* bitslam */
517c478bd9Sstevel@tonic-gate int exp; /* exponent */
527c478bd9Sstevel@tonic-gate unsigned int m0; /* most significant word of mantissa */
537c478bd9Sstevel@tonic-gate unsigned int m1; /* least sig. word of mantissa */
547c478bd9Sstevel@tonic-gate unsigned int _fp_current_exceptions = 0;
557c478bd9Sstevel@tonic-gate union {
567c478bd9Sstevel@tonic-gate int i[2];
577c478bd9Sstevel@tonic-gate double d;
587c478bd9Sstevel@tonic-gate } u;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * Extract the exponent and check boundary conditions.
627c478bd9Sstevel@tonic-gate * Notice that the exponent is equal to the bit number where
637c478bd9Sstevel@tonic-gate * we want the most significant bit to live.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate u.d = dval;
667c478bd9Sstevel@tonic-gate i0 = u.i[0];
677c478bd9Sstevel@tonic-gate i1 = u.i[1];
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate exp = ((i0 >> 20) & 0x7ff) - 0x3ff;
707c478bd9Sstevel@tonic-gate if (exp < 0) {
717c478bd9Sstevel@tonic-gate /* abs(x) < 1.0, so round to 0 */
727c478bd9Sstevel@tonic-gate return ((unsigned long long)0);
737c478bd9Sstevel@tonic-gate } else if (exp > 63) {
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
767c478bd9Sstevel@tonic-gate * overflow, Inf, NaN set fp_invalid exception
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate _fp_current_exceptions |= (1 << (int)fp_invalid);
797c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
807c478bd9Sstevel@tonic-gate if (i0 < 0)
817c478bd9Sstevel@tonic-gate return ((unsigned long long)LLONG_MIN);
827c478bd9Sstevel@tonic-gate else
837c478bd9Sstevel@tonic-gate return (ULLONG_MAX); /* MAXLONG */
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /* Extract the mantissa. */
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate m0 = 0x80000000 | ((i0 << 11) & 0x7ffff800) | ((i1 >> 21) & 0x7ff);
897c478bd9Sstevel@tonic-gate m1 = i1 << 11;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * The most significant bit of the mantissa is now in bit 63 of m0:m1.
937c478bd9Sstevel@tonic-gate * Shift right by (63 - exp) bits.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate switch (exp) {
967c478bd9Sstevel@tonic-gate case 63:
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate case 31:
997c478bd9Sstevel@tonic-gate m1 = m0;
1007c478bd9Sstevel@tonic-gate m0 = 0;
1017c478bd9Sstevel@tonic-gate break;
1027c478bd9Sstevel@tonic-gate default:
1037c478bd9Sstevel@tonic-gate if (exp > 31) {
1047c478bd9Sstevel@tonic-gate m1 = (m0 << (exp - 31)) | (m1 >> (63 - exp));
1057c478bd9Sstevel@tonic-gate m0 = (m0 >> (63 - exp));
1067c478bd9Sstevel@tonic-gate } else {
1077c478bd9Sstevel@tonic-gate m1 = (m0 >> (31 - exp));
1087c478bd9Sstevel@tonic-gate m0 = 0;
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate break;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (i0 < 0) {
1147c478bd9Sstevel@tonic-gate if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
1157c478bd9Sstevel@tonic-gate m0 = 0x80000000;
1167c478bd9Sstevel@tonic-gate m1 = 0;
1177c478bd9Sstevel@tonic-gate } else {
1187c478bd9Sstevel@tonic-gate m0 = ~m0;
1197c478bd9Sstevel@tonic-gate m1 = ~m1;
1207c478bd9Sstevel@tonic-gate if (++m1 == 0)
1217c478bd9Sstevel@tonic-gate m0++;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
1267c478bd9Sstevel@tonic-gate return (((unsigned long long)m0 << 32) | m1);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * Convert a floating point number into a 64-bit unsigned int.
1317c478bd9Sstevel@tonic-gate *
1327c478bd9Sstevel@tonic-gate * For compatibility with Sun's other conversion routines, pretend result
1337c478bd9Sstevel@tonic-gate * is signed if input is negative.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate unsigned long long
__ftoull(float fval)1367c478bd9Sstevel@tonic-gate __ftoull(float fval)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate int i0; /* bitslam */
1397c478bd9Sstevel@tonic-gate int exp; /* exponent */
1407c478bd9Sstevel@tonic-gate unsigned int m0; /* most significant word of mantissa */
1417c478bd9Sstevel@tonic-gate unsigned int m1; /* least sig. word of mantissa */
1427c478bd9Sstevel@tonic-gate unsigned int _fp_current_exceptions = 0;
1437c478bd9Sstevel@tonic-gate union {
1447c478bd9Sstevel@tonic-gate int i;
1457c478bd9Sstevel@tonic-gate float f;
1467c478bd9Sstevel@tonic-gate } u;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Extract the exponent and check boundary conditions.
1507c478bd9Sstevel@tonic-gate * Notice that the exponent is equal to the bit number where
1517c478bd9Sstevel@tonic-gate * we want the most significant bit to live.
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate u.f = fval;
1547c478bd9Sstevel@tonic-gate i0 = u.i;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate exp = ((i0 >> 23) & 0xff) - 0x7f;
1577c478bd9Sstevel@tonic-gate if (exp < 0) {
1587c478bd9Sstevel@tonic-gate /* abs(x) < 1.0, so round to 0 */
1597c478bd9Sstevel@tonic-gate return ((unsigned long long)0);
1607c478bd9Sstevel@tonic-gate } else if (exp > 63) {
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
1637c478bd9Sstevel@tonic-gate * overflow, Inf, NaN set fp_invalid exception
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate _fp_current_exceptions |= (1 << (int)fp_invalid);
1667c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
1677c478bd9Sstevel@tonic-gate if (i0 < 0)
1687c478bd9Sstevel@tonic-gate return ((unsigned long long)LLONG_MIN);
1697c478bd9Sstevel@tonic-gate else
1707c478bd9Sstevel@tonic-gate return (ULLONG_MAX); /* MAXLONG */
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /* Extract the mantissa. */
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate m0 = 0x80000000 | (i0 << 8) & 0x7fffff00;
1767c478bd9Sstevel@tonic-gate m1 = 0;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * The most significant bit of the mantissa is now in bit 63 of m0:m1.
1807c478bd9Sstevel@tonic-gate * Shift right by (63 - exp) bits.
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate switch (exp) {
1837c478bd9Sstevel@tonic-gate case 63:
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case 31:
1867c478bd9Sstevel@tonic-gate m1 = m0;
1877c478bd9Sstevel@tonic-gate m0 = 0;
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate default:
1907c478bd9Sstevel@tonic-gate if (exp > 31) {
1917c478bd9Sstevel@tonic-gate m1 = m0 << (exp - 31);
1927c478bd9Sstevel@tonic-gate m0 = (m0 >> (63 - exp));
1937c478bd9Sstevel@tonic-gate } else {
1947c478bd9Sstevel@tonic-gate m1 = (m0 >> (31 - exp));
1957c478bd9Sstevel@tonic-gate m0 = 0;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate break;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate if (i0 < 0) {
2017c478bd9Sstevel@tonic-gate if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
2027c478bd9Sstevel@tonic-gate m0 = 0x80000000;
2037c478bd9Sstevel@tonic-gate m1 = 0;
2047c478bd9Sstevel@tonic-gate } else {
2057c478bd9Sstevel@tonic-gate m0 = ~m0;
2067c478bd9Sstevel@tonic-gate m1 = ~m1;
2077c478bd9Sstevel@tonic-gate if (++m1 == 0)
2087c478bd9Sstevel@tonic-gate m0++;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
2137c478bd9Sstevel@tonic-gate return (((unsigned long long)m0 << 32) | m1);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Convert an extended precision floating point # into a 64-bit unsigned int.
2187c478bd9Sstevel@tonic-gate *
2197c478bd9Sstevel@tonic-gate * For compatibility with Sun's other conversion routines, pretend result
2207c478bd9Sstevel@tonic-gate * is signed if input is negative.
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate unsigned long long
_Q_qtoull(long double ld)2237c478bd9Sstevel@tonic-gate _Q_qtoull(long double ld)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate int i0;
2267c478bd9Sstevel@tonic-gate unsigned int i1, i2; /* a long double is 128-bit in length */
2277c478bd9Sstevel@tonic-gate int exp; /* exponent */
2287c478bd9Sstevel@tonic-gate unsigned int m0; /* most significant word of mantissa */
2297c478bd9Sstevel@tonic-gate unsigned int m1; /* least sig. word of mantissa */
2307c478bd9Sstevel@tonic-gate unsigned int _fp_current_exceptions = 0;
2317c478bd9Sstevel@tonic-gate int *plngdbl = (int *)&ld;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /* Only 96-bits of precision used */
2347c478bd9Sstevel@tonic-gate i0 = plngdbl[0];
2357c478bd9Sstevel@tonic-gate i1 = plngdbl[1];
2367c478bd9Sstevel@tonic-gate i2 = plngdbl[2];
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Extract the exponent and check boundary conditions.
2407c478bd9Sstevel@tonic-gate * Notice that the exponent is equal to the bit number where
2417c478bd9Sstevel@tonic-gate * we want the most significant bit to live.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate exp = ((i0 >> 16) & 0x7fff) - 0x3fff;
2447c478bd9Sstevel@tonic-gate if (exp < 0) {
2457c478bd9Sstevel@tonic-gate return ((long long)0); /* abs(x) < 1.0, so round to 0 */
2467c478bd9Sstevel@tonic-gate } else if (exp > 63) {
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate * abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
2497c478bd9Sstevel@tonic-gate * overflow, Inf, NaN set fp_invalid exception
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate _fp_current_exceptions |= (1 << (int)fp_invalid);
2527c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
2537c478bd9Sstevel@tonic-gate if (i0 < 0)
2547c478bd9Sstevel@tonic-gate return ((unsigned long long)LLONG_MIN);
2557c478bd9Sstevel@tonic-gate else
2567c478bd9Sstevel@tonic-gate return (ULLONG_MAX); /* MAXLONG */
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* Extract the mantissa. */
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate m0 = 0x80000000 | ((i0<<15) & 0x7fff8000) | ((i1>>17) & 0x7fff);
2627c478bd9Sstevel@tonic-gate m1 = (i1 << 15) | ((i2 >> 17) & 0x7fff);
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * The most significant bit of the mantissa is now in bit 63 of m0:m1.
2667c478bd9Sstevel@tonic-gate * Shift right by (63 - exp) bits.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate switch (exp) {
2697c478bd9Sstevel@tonic-gate case 63:
2707c478bd9Sstevel@tonic-gate break;
2717c478bd9Sstevel@tonic-gate case 31:
2727c478bd9Sstevel@tonic-gate m1 = m0;
2737c478bd9Sstevel@tonic-gate m0 = 0;
2747c478bd9Sstevel@tonic-gate break;
2757c478bd9Sstevel@tonic-gate default:
2767c478bd9Sstevel@tonic-gate if (exp > 31) {
2777c478bd9Sstevel@tonic-gate m1 = (m0 << (exp - 31)) | (m1 >> (63 - exp));
2787c478bd9Sstevel@tonic-gate m0 = (m0 >> (63 - exp));
2797c478bd9Sstevel@tonic-gate } else {
2807c478bd9Sstevel@tonic-gate m1 = (m0 >> (31 - exp));
2817c478bd9Sstevel@tonic-gate m0 = 0;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate break;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate if (i0 < 0) {
2877c478bd9Sstevel@tonic-gate if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
2887c478bd9Sstevel@tonic-gate m0 = 0x80000000;
2897c478bd9Sstevel@tonic-gate m1 = 0;
2907c478bd9Sstevel@tonic-gate } else {
2917c478bd9Sstevel@tonic-gate m0 = ~m0;
2927c478bd9Sstevel@tonic-gate m1 = ~m1;
2937c478bd9Sstevel@tonic-gate if (++m1 == 0)
2947c478bd9Sstevel@tonic-gate m0++;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate (void) _Q_set_exception(_fp_current_exceptions);
2997c478bd9Sstevel@tonic-gate return (((unsigned long long)m0 << 32) | m1);
3007c478bd9Sstevel@tonic-gate }
301