xref: /illumos-gate/usr/src/lib/libc/port/gen/_ftoll.c (revision 7aaede48)
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
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * 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  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf #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 number into a 64-bit int.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate long long
__dtoll(double dval)447c478bd9Sstevel@tonic-gate __dtoll(double dval)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	int i0, i1;		/* bitslam */
477c478bd9Sstevel@tonic-gate 	int exp;		/* exponent */
487c478bd9Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
497c478bd9Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
507c478bd9Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
517c478bd9Sstevel@tonic-gate 	union {
527c478bd9Sstevel@tonic-gate 		int i[2];
537c478bd9Sstevel@tonic-gate 		double d;
547c478bd9Sstevel@tonic-gate 	} u;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	/*
577c478bd9Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
587c478bd9Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
597c478bd9Sstevel@tonic-gate 	 * we want the most significant bit to live.
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 	u.d = dval;
627c478bd9Sstevel@tonic-gate 	i0 = u.i[0];
637c478bd9Sstevel@tonic-gate 	i1 = u.i[1];
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	exp = ((i0 >> 20) & 0x7ff) - 0x3ff;
667c478bd9Sstevel@tonic-gate 	if (exp < 0) {
677c478bd9Sstevel@tonic-gate 		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
687c478bd9Sstevel@tonic-gate 	} else if (exp > 62) {
697c478bd9Sstevel@tonic-gate 		/*
707c478bd9Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0,i1> == LLONG_MIN
717c478bd9Sstevel@tonic-gate 		 */
727c478bd9Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0xfffff) != 0 || i1 != 0) {
737c478bd9Sstevel@tonic-gate 			/*
747c478bd9Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
757c478bd9Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
767c478bd9Sstevel@tonic-gate 			 */
777c478bd9Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
787c478bd9Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		if (i0 < 0)
817c478bd9Sstevel@tonic-gate 			return (LLONG_MIN);
827c478bd9Sstevel@tonic-gate 		else
837c478bd9Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/* Extract the mantissa. */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	m0 = 0x40000000 | ((i0 << 10) & 0x3ffffc00) | ((i1 >> 22) & 0x3ff);
897c478bd9Sstevel@tonic-gate 	m1 = i1 << 10;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
937c478bd9Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	switch (exp) {
967c478bd9Sstevel@tonic-gate 	case 62:
977c478bd9Sstevel@tonic-gate 		break;
987c478bd9Sstevel@tonic-gate 	case 30:
997c478bd9Sstevel@tonic-gate 		m1 = m0;
1007c478bd9Sstevel@tonic-gate 		m0 = 0;
1017c478bd9Sstevel@tonic-gate 		break;
1027c478bd9Sstevel@tonic-gate 	default:
1037c478bd9Sstevel@tonic-gate 		if (exp > 30) {
1047c478bd9Sstevel@tonic-gate 			m1 = (m0 << (exp - 30)) |
105*7aaede48SToomas Soome 			    (m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
1067c478bd9Sstevel@tonic-gate 			m0 >>= 62 - exp;
1077c478bd9Sstevel@tonic-gate 		} else {
1087c478bd9Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
1097c478bd9Sstevel@tonic-gate 			m0 = 0;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 		break;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (i0 < 0) {
1157c478bd9Sstevel@tonic-gate 		m0 = ~m0;
1167c478bd9Sstevel@tonic-gate 		m1 = ~m1;
1177c478bd9Sstevel@tonic-gate 		if (++m1 == 0)
1187c478bd9Sstevel@tonic-gate 			m0++;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
1227c478bd9Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Convert a floating point number into a 64-bit int.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate long long
__ftoll(float fval)1297c478bd9Sstevel@tonic-gate __ftoll(float fval)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int i0;
1327c478bd9Sstevel@tonic-gate 	int exp;		/* exponent */
1337c478bd9Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
1347c478bd9Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
1357c478bd9Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
1367c478bd9Sstevel@tonic-gate 	union {
1377c478bd9Sstevel@tonic-gate 		int i;
1387c478bd9Sstevel@tonic-gate 		float f;
1397c478bd9Sstevel@tonic-gate 	} u;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
1437c478bd9Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
1447c478bd9Sstevel@tonic-gate 	 * we want the most significant bit to live.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	u.f = fval;
1477c478bd9Sstevel@tonic-gate 	i0 = u.i;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	exp = ((i0 >> 23) & 0xff) - 0x7f;
1507c478bd9Sstevel@tonic-gate 	if (exp < 0) {
1517c478bd9Sstevel@tonic-gate 		return ((long long) 0); /* abs(x) < 1.0, so round to 0 */
1527c478bd9Sstevel@tonic-gate 	} else if (exp > 62)  {
1537c478bd9Sstevel@tonic-gate 		/*
1547c478bd9Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0> == LLONG_MIN
1557c478bd9Sstevel@tonic-gate 		 */
1567c478bd9Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0x7fffff) != 0) {
1577c478bd9Sstevel@tonic-gate 			/*
1587c478bd9Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
1597c478bd9Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
1607c478bd9Sstevel@tonic-gate 			 */
1617c478bd9Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
1627c478bd9Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		if (i0 < 0)
1657c478bd9Sstevel@tonic-gate 			return (LLONG_MIN);
1667c478bd9Sstevel@tonic-gate 		else
1677c478bd9Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Extract the mantissa. */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	m0 = 0x40000000 | (i0 << 7) & 0x3fffff80;
1737c478bd9Sstevel@tonic-gate 	m1 = 0;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
1777c478bd9Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	switch (exp) {
1807c478bd9Sstevel@tonic-gate 	case 62:
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 	case 30:
1837c478bd9Sstevel@tonic-gate 		m1 = m0;
1847c478bd9Sstevel@tonic-gate 		m0 = 0;
1857c478bd9Sstevel@tonic-gate 		break;
1867c478bd9Sstevel@tonic-gate 	default:
1877c478bd9Sstevel@tonic-gate 		if (exp > 30) {
1887c478bd9Sstevel@tonic-gate 			m1 = m0 << (exp - 30);
1897c478bd9Sstevel@tonic-gate 			m0 >>= 62 - exp;
1907c478bd9Sstevel@tonic-gate 		} else {
1917c478bd9Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
1927c478bd9Sstevel@tonic-gate 			m0 = 0;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		break;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (i0 < 0) {
1987c478bd9Sstevel@tonic-gate 		m0 = ~m0;
1997c478bd9Sstevel@tonic-gate 		m1 = ~m1;
2007c478bd9Sstevel@tonic-gate 		if (++m1 == 0)
2017c478bd9Sstevel@tonic-gate 			m0++;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
2057c478bd9Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Convert an extended precision floating point number into a 64-bit int.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate long long
_Q_qtoll(long double longdbl)2127c478bd9Sstevel@tonic-gate _Q_qtoll(long double longdbl)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	int i0;
2157c478bd9Sstevel@tonic-gate 	unsigned int i1, i2;	/* a long double is 128-bit in length */
2167c478bd9Sstevel@tonic-gate 	int *plngdbl = (int *)&longdbl;
2177c478bd9Sstevel@tonic-gate 	int exp;		/* exponent */
2187c478bd9Sstevel@tonic-gate 	int m0;			/* most significant word of mantissa */
2197c478bd9Sstevel@tonic-gate 	unsigned int m1;	/* least sig. word of mantissa */
2207c478bd9Sstevel@tonic-gate 	unsigned int _fp_current_exceptions = 0;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Only 96-bits of precision used
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	i0 = plngdbl[0];
2267c478bd9Sstevel@tonic-gate 	i1 = plngdbl[1];
2277c478bd9Sstevel@tonic-gate 	i2 = plngdbl[2];
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Extract the exponent and check boundary conditions.
2317c478bd9Sstevel@tonic-gate 	 * Notice that the exponent is equal to the bit number where
2327c478bd9Sstevel@tonic-gate 	 * we want the most significant bit to live.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	exp = ((i0 >> 16) & 0x7fff) - 0x3fff;
2357c478bd9Sstevel@tonic-gate 	if (exp < 0) {
2367c478bd9Sstevel@tonic-gate 		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
2377c478bd9Sstevel@tonic-gate 	} else if (exp > 62)	{
2387c478bd9Sstevel@tonic-gate 		/*
2397c478bd9Sstevel@tonic-gate 		 * fp_invalid NOT raised if <i0,i1,i2,i3> when chopped to
2407c478bd9Sstevel@tonic-gate 		 * 64 bits == LLONG_MIN
2417c478bd9Sstevel@tonic-gate 		 */
2427c478bd9Sstevel@tonic-gate 		if (i0 >= 0 || exp != 63 || (i0 & 0xffff) != 0 || i1 != 0 ||
2437c478bd9Sstevel@tonic-gate 		    (i2 & 0xfffe0000) != 0) {
2447c478bd9Sstevel@tonic-gate 			/*
2457c478bd9Sstevel@tonic-gate 			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
2467c478bd9Sstevel@tonic-gate 			 * overflow, Inf, NaN set fp_invalid exception
2477c478bd9Sstevel@tonic-gate 			 */
2487c478bd9Sstevel@tonic-gate 			_fp_current_exceptions |= (1 << (int)fp_invalid);
2497c478bd9Sstevel@tonic-gate 			(void) _Q_set_exception(_fp_current_exceptions);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		if (i0 < 0)
2527c478bd9Sstevel@tonic-gate 			return (LLONG_MIN);
2537c478bd9Sstevel@tonic-gate 		else
2547c478bd9Sstevel@tonic-gate 			return (LLONG_MAX); /* MAXLONG */
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* Extract the mantissa. */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	m0 = 0x40000000 | ((i0 << 14) & 0x3fffc000) | ((i1 >> 18) & 0x3fff);
2607c478bd9Sstevel@tonic-gate 	m1 = (i1 << 14) | ((i2 >> 18) & 0x3fff);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
2647c478bd9Sstevel@tonic-gate 	 * Shift right by (62 - exp) bits.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	switch (exp) {
2677c478bd9Sstevel@tonic-gate 	case 62:
2687c478bd9Sstevel@tonic-gate 		break;
2697c478bd9Sstevel@tonic-gate 	case 30:
2707c478bd9Sstevel@tonic-gate 		m1 = m0;
2717c478bd9Sstevel@tonic-gate 		m0 = 0;
2727c478bd9Sstevel@tonic-gate 		break;
2737c478bd9Sstevel@tonic-gate 	default:
2747c478bd9Sstevel@tonic-gate 		if (exp > 30) {
2757c478bd9Sstevel@tonic-gate 			m1 = (m0 << (exp - 30)) |
276*7aaede48SToomas Soome 			    (m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
2777c478bd9Sstevel@tonic-gate 			m0 >>= 62 - exp;
2787c478bd9Sstevel@tonic-gate 		} else {
2797c478bd9Sstevel@tonic-gate 			m1 = m0 >> (30 - exp);
2807c478bd9Sstevel@tonic-gate 			m0 = 0;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 		break;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (i0 < 0) {
2867c478bd9Sstevel@tonic-gate 		m0 = ~m0;
2877c478bd9Sstevel@tonic-gate 		m1 = ~m1;
2887c478bd9Sstevel@tonic-gate 		if (++m1 == 0)
2897c478bd9Sstevel@tonic-gate 			m0++;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	(void) _Q_set_exception(_fp_current_exceptions);
2937c478bd9Sstevel@tonic-gate 	return ((long long)(((unsigned long long)m0 << 32) | m1));
2947c478bd9Sstevel@tonic-gate }
295