xref: /onnv-gate/usr/src/lib/libc/port/gen/_ftoll.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include <sys/isa_defs.h>
310Sstevel@tonic-gate #include <floatingpoint.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include "libc.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Ensure that this "portable" code is only used on big-endian ISAs
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate #if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
390Sstevel@tonic-gate #error	"big-endian only!"
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Convert a double precision floating point number into a 64-bit int.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate long long
__dtoll(double dval)460Sstevel@tonic-gate __dtoll(double dval)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	int i0, i1;		/* bitslam */
490Sstevel@tonic-gate 	int exp;		/* exponent */
500Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
510Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
520Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
530Sstevel@tonic-gate 	union {
540Sstevel@tonic-gate 		int i[2];
550Sstevel@tonic-gate 		double d;
560Sstevel@tonic-gate 	} u;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	/*
590Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
600Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
610Sstevel@tonic-gate 	 * we want the most significant bit to live.
620Sstevel@tonic-gate 	 */
630Sstevel@tonic-gate 	u.d = dval;
640Sstevel@tonic-gate 	i0 = u.i[0];
650Sstevel@tonic-gate 	i1 = u.i[1];
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	exp = ((i0 >> 20) & 0x7ff) - 0x3ff;
680Sstevel@tonic-gate 	if (exp < 0) {
690Sstevel@tonic-gate 		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
700Sstevel@tonic-gate 	} else if (exp > 62) {
710Sstevel@tonic-gate 		/*
720Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0,i1> == LLONG_MIN
730Sstevel@tonic-gate 		 */
740Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0xfffff) != 0 || i1 != 0) {
750Sstevel@tonic-gate 			/*
760Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
770Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
780Sstevel@tonic-gate 			 */
790Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
800Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		if (i0 < 0)
830Sstevel@tonic-gate 			return (LLONG_MIN);
840Sstevel@tonic-gate 		else
850Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/* Extract the mantissa. */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	m0 = 0x40000000 | ((i0 << 10) & 0x3ffffc00) | ((i1 >> 22) & 0x3ff);
910Sstevel@tonic-gate 	m1 = i1 << 10;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/*
940Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
950Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	switch (exp) {
980Sstevel@tonic-gate 	case 62:
990Sstevel@tonic-gate 		break;
1000Sstevel@tonic-gate 	case 30:
1010Sstevel@tonic-gate 		m1 = m0;
1020Sstevel@tonic-gate 		m0 = 0;
1030Sstevel@tonic-gate 		break;
1040Sstevel@tonic-gate 	default:
1050Sstevel@tonic-gate 		if (exp > 30) {
1060Sstevel@tonic-gate 			m1 = (m0 << (exp - 30)) |
107*6812Sraf 			    (m1 >> (62 - exp)) & ~(-1 << (exp - 30));
1080Sstevel@tonic-gate 			m0 >>= 62 - exp;
1090Sstevel@tonic-gate 		} else {
1100Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
1110Sstevel@tonic-gate 			m0 = 0;
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 		break;
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (i0 < 0) {
1170Sstevel@tonic-gate 		m0 = ~m0;
1180Sstevel@tonic-gate 		m1 = ~m1;
1190Sstevel@tonic-gate 		if (++m1 == 0)
1200Sstevel@tonic-gate 			m0++;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
1240Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Convert a floating point number into a 64-bit int.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate long long
__ftoll(float fval)1310Sstevel@tonic-gate __ftoll(float fval)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int i0;
1340Sstevel@tonic-gate 	int exp;		/* exponent */
1350Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
1360Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
1370Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
1380Sstevel@tonic-gate 	union {
1390Sstevel@tonic-gate 		int i;
1400Sstevel@tonic-gate 		float f;
1410Sstevel@tonic-gate 	} u;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
1450Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
1460Sstevel@tonic-gate 	 * we want the most significant bit to live.
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	u.f = fval;
1490Sstevel@tonic-gate 	i0 = u.i;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	exp = ((i0 >> 23) & 0xff) - 0x7f;
1520Sstevel@tonic-gate 	if (exp < 0) {
1530Sstevel@tonic-gate 		return ((long long) 0); /* abs(x) < 1.0, so round to 0 */
1540Sstevel@tonic-gate 	} else if (exp > 62)  {
1550Sstevel@tonic-gate 		/*
1560Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0> == LLONG_MIN
1570Sstevel@tonic-gate 		 */
1580Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0x7fffff) != 0) {
1590Sstevel@tonic-gate 			/*
1600Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
1610Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
1620Sstevel@tonic-gate 			 */
1630Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
1640Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 		if (i0 < 0)
1670Sstevel@tonic-gate 			return (LLONG_MIN);
1680Sstevel@tonic-gate 		else
1690Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* Extract the mantissa. */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	m0 = 0x40000000 | (i0 << 7) & 0x3fffff80;
1750Sstevel@tonic-gate 	m1 = 0;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
1790Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	switch (exp) {
1820Sstevel@tonic-gate 	case 62:
1830Sstevel@tonic-gate 		break;
1840Sstevel@tonic-gate 	case 30:
1850Sstevel@tonic-gate 		m1 = m0;
1860Sstevel@tonic-gate 		m0 = 0;
1870Sstevel@tonic-gate 		break;
1880Sstevel@tonic-gate 	default:
1890Sstevel@tonic-gate 		if (exp > 30) {
1900Sstevel@tonic-gate 			m1 = m0 << (exp - 30);
1910Sstevel@tonic-gate 			m0 >>= 62 - exp;
1920Sstevel@tonic-gate 		} else {
1930Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
1940Sstevel@tonic-gate 			m0 = 0;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (i0 < 0) {
2000Sstevel@tonic-gate 		m0 = ~m0;
2010Sstevel@tonic-gate 		m1 = ~m1;
2020Sstevel@tonic-gate 		if (++m1 == 0)
2030Sstevel@tonic-gate 			m0++;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
2070Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Convert an extended precision floating point number into a 64-bit int.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate long long
_Q_qtoll(long double longdbl)2140Sstevel@tonic-gate _Q_qtoll(long double longdbl)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	int i0;
2170Sstevel@tonic-gate 	unsigned int i1, i2;	/* a long double is 128-bit in length */
2180Sstevel@tonic-gate 	int *plngdbl = (int *)&longdbl;
2190Sstevel@tonic-gate 	int exp;		/* exponent */
2200Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
2210Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
2220Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * Only 96-bits of precision used
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 	i0 = plngdbl[0];
2280Sstevel@tonic-gate 	i1 = plngdbl[1];
2290Sstevel@tonic-gate 	i2 = plngdbl[2];
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
2330Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
2340Sstevel@tonic-gate 	 * we want the most significant bit to live.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	exp = ((i0 >> 16) & 0x7fff) - 0x3fff;
2370Sstevel@tonic-gate 	if (exp < 0) {
2380Sstevel@tonic-gate 		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
2390Sstevel@tonic-gate 	} else if (exp > 62)	{
2400Sstevel@tonic-gate 		/*
2410Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0,i1,i2,i3> when chopped to
2420Sstevel@tonic-gate 		 * 64 bits == LLONG_MIN
2430Sstevel@tonic-gate 		 */
2440Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0xffff) != 0 || i1 != 0 ||
2450Sstevel@tonic-gate 		    (i2 & 0xfffe0000) != 0) {
2460Sstevel@tonic-gate 			/*
2470Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
2480Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
2490Sstevel@tonic-gate 			 */
2500Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
2510Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 		if (i0 < 0)
2540Sstevel@tonic-gate 			return (LLONG_MIN);
2550Sstevel@tonic-gate 		else
2560Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* Extract the mantissa. */
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	m0 = 0x40000000 | ((i0 << 14) & 0x3fffc000) | ((i1 >> 18) & 0x3fff);
2620Sstevel@tonic-gate 	m1 = (i1 << 14) | ((i2 >> 18) & 0x3fff);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
2660Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	switch (exp) {
2690Sstevel@tonic-gate 	case 62:
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 	case 30:
2720Sstevel@tonic-gate 		m1 = m0;
2730Sstevel@tonic-gate 		m0 = 0;
2740Sstevel@tonic-gate 		break;
2750Sstevel@tonic-gate 	default:
2760Sstevel@tonic-gate 		if (exp > 30) {
2770Sstevel@tonic-gate 			m1 = (m0 << (exp - 30)) |
278*6812Sraf 			    (m1 >> (62 - exp)) & ~(-1 << (exp - 30));
2790Sstevel@tonic-gate 			m0 >>= 62 - exp;
2800Sstevel@tonic-gate 		} else {
2810Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
2820Sstevel@tonic-gate 			m0 = 0;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 		break;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (i0 < 0) {
2880Sstevel@tonic-gate 		m0 = ~m0;
2890Sstevel@tonic-gate 		m1 = ~m1;
2900Sstevel@tonic-gate 		if (++m1 == 0)
2910Sstevel@tonic-gate 			m0++;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
2950Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
2960Sstevel@tonic-gate }
297