xref: /illumos-gate/usr/src/lib/libc/port/fp/decimal_bin.c (revision 1da57d55)
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 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Conversion from decimal to binary floating point
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31*7257d1b4Sraf #include "lint.h"
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include "base_conversion.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Convert the integer part of a nonzero base-10^4 _big_float *pd
377c478bd9Sstevel@tonic-gate  * to base 2^16 in **ppb.  The converted value is accurate to nsig
387c478bd9Sstevel@tonic-gate  * significant bits.  On exit, *sticky is nonzero if *pd had a
397c478bd9Sstevel@tonic-gate  * nonzero fractional part.  If pd->exponent > 0 and **ppb is not
407c478bd9Sstevel@tonic-gate  * large enough to hold the final converted value (i.e., the con-
417c478bd9Sstevel@tonic-gate  * verted significand scaled by 10^pd->exponent), then on exit,
427c478bd9Sstevel@tonic-gate  * *ppb will point to a newly allocated _big_float, which must be
437c478bd9Sstevel@tonic-gate  * freed by the caller.  (The number of significant bits we need
447c478bd9Sstevel@tonic-gate  * should fit in pb, but __big_float_times_power may allocate new
457c478bd9Sstevel@tonic-gate  * storage anyway because the exact product could require more than
467c478bd9Sstevel@tonic-gate  * 16000 bits.)
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * This routine does not check that **ppb is large enough to hold
497c478bd9Sstevel@tonic-gate  * the result of converting the significand of *pd.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static void
__big_decimal_to_big_binary(_big_float * pd,int nsig,_big_float ** ppb,int * sticky)527c478bd9Sstevel@tonic-gate __big_decimal_to_big_binary(_big_float *pd, int nsig, _big_float **ppb,
537c478bd9Sstevel@tonic-gate     int *sticky)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	_big_float	*pb;
567c478bd9Sstevel@tonic-gate 	int		i, j, len, s;
577c478bd9Sstevel@tonic-gate 	unsigned int	carry;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	pb = *ppb;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* convert pd a digit at a time, most significant first */
627c478bd9Sstevel@tonic-gate 	if (pd->bexponent + ((pd->blength - 1) << 2) >= 0) {
637c478bd9Sstevel@tonic-gate 		pb->bsignificand[0] = pd->bsignificand[pd->blength - 1];
647c478bd9Sstevel@tonic-gate 		len = 1;
657c478bd9Sstevel@tonic-gate 		for (i = pd->blength - 2; i >= 0 &&
667c478bd9Sstevel@tonic-gate 		    pd->bexponent + (i << 2) >= 0; i--) {
677c478bd9Sstevel@tonic-gate 			/* multiply pb by 10^4 and add next digit */
687c478bd9Sstevel@tonic-gate 			carry = pd->bsignificand[i];
697c478bd9Sstevel@tonic-gate 			for (j = 0; j < len; j++) {
707c478bd9Sstevel@tonic-gate 				carry += (unsigned int)pb->bsignificand[j]
717c478bd9Sstevel@tonic-gate 				    * 10000;
727c478bd9Sstevel@tonic-gate 				pb->bsignificand[j] = carry & 0xffff;
737c478bd9Sstevel@tonic-gate 				carry >>= 16;
747c478bd9Sstevel@tonic-gate 			}
757c478bd9Sstevel@tonic-gate 			if (carry)
767c478bd9Sstevel@tonic-gate 				pb->bsignificand[j++] = carry;
777c478bd9Sstevel@tonic-gate 			len = j;
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	} else {
807c478bd9Sstevel@tonic-gate 		i = pd->blength - 1;
817c478bd9Sstevel@tonic-gate 		len = 0;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* convert any partial digit */
857c478bd9Sstevel@tonic-gate 	if (i >= 0 && pd->bexponent + (i << 2) > -4) {
867c478bd9Sstevel@tonic-gate 		s = pd->bexponent + (i << 2) + 4;
877c478bd9Sstevel@tonic-gate 		/* multiply pb by 10^s and add partial digit */
887c478bd9Sstevel@tonic-gate 		carry = pd->bsignificand[i];
897c478bd9Sstevel@tonic-gate 		if (s == 1) {
907c478bd9Sstevel@tonic-gate 			s = carry % 1000;
917c478bd9Sstevel@tonic-gate 			carry = carry / 1000;
927c478bd9Sstevel@tonic-gate 			for (j = 0; j < len; j++) {
937c478bd9Sstevel@tonic-gate 				carry += (unsigned int)pb->bsignificand[j]
947c478bd9Sstevel@tonic-gate 				    * 10;
957c478bd9Sstevel@tonic-gate 				pb->bsignificand[j] = carry & 0xffff;
967c478bd9Sstevel@tonic-gate 				carry >>= 16;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		} else if (s == 2) {
997c478bd9Sstevel@tonic-gate 			s = carry % 100;
1007c478bd9Sstevel@tonic-gate 			carry = carry / 100;
1017c478bd9Sstevel@tonic-gate 			for (j = 0; j < len; j++) {
1027c478bd9Sstevel@tonic-gate 				carry += (unsigned int)pb->bsignificand[j]
1037c478bd9Sstevel@tonic-gate 				    * 100;
1047c478bd9Sstevel@tonic-gate 				pb->bsignificand[j] = carry & 0xffff;
1057c478bd9Sstevel@tonic-gate 				carry >>= 16;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 		} else {
1087c478bd9Sstevel@tonic-gate 			s = carry % 10;
1097c478bd9Sstevel@tonic-gate 			carry = carry / 10;
1107c478bd9Sstevel@tonic-gate 			for (j = 0; j < len; j++) {
1117c478bd9Sstevel@tonic-gate 				carry += (unsigned int)pb->bsignificand[j]
1127c478bd9Sstevel@tonic-gate 				    * 1000;
1137c478bd9Sstevel@tonic-gate 				pb->bsignificand[j] = carry & 0xffff;
1147c478bd9Sstevel@tonic-gate 				carry >>= 16;
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		if (carry)
1187c478bd9Sstevel@tonic-gate 			pb->bsignificand[j++] = carry;
1197c478bd9Sstevel@tonic-gate 		len = j;
1207c478bd9Sstevel@tonic-gate 		i--;
1217c478bd9Sstevel@tonic-gate 	} else {
1227c478bd9Sstevel@tonic-gate 		s = 0;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	pb->blength = len;
1267c478bd9Sstevel@tonic-gate 	pb->bexponent = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* continue accumulating sticky flag */
1297c478bd9Sstevel@tonic-gate 	while (i >= 0)
1307c478bd9Sstevel@tonic-gate 		s |= pd->bsignificand[i--];
1317c478bd9Sstevel@tonic-gate 	*sticky = s;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (pd->bexponent > 0) {
1347c478bd9Sstevel@tonic-gate 		/* scale pb by 10^pd->exponent */
1357c478bd9Sstevel@tonic-gate 		__big_float_times_power(pb, 10, pd->bexponent, nsig, ppb);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Convert the decimal_record *pd to an unpacked datum *px accurately
1417c478bd9Sstevel@tonic-gate  * enough that *px can be rounded correctly to sigbits significant bits.
1427c478bd9Sstevel@tonic-gate  * (We may assume sigbits <= 113.)
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static void
__decimal_to_unpacked(unpacked * px,decimal_record * pd,int sigbits)1457c478bd9Sstevel@tonic-gate __decimal_to_unpacked(unpacked *px, decimal_record *pd, int sigbits)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	_big_float	d, b, *pbd, *pbb;
1487c478bd9Sstevel@tonic-gate 	char		*ds;
1497c478bd9Sstevel@tonic-gate 	int		ids, i, ix, exp, ndigs;
1507c478bd9Sstevel@tonic-gate 	int		sticky, powtwo, sigdigits;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	px->sign = pd->sign;
1537c478bd9Sstevel@tonic-gate 	px->fpclass = pd->fpclass;
1547c478bd9Sstevel@tonic-gate 	ds = pd->ds;
1557c478bd9Sstevel@tonic-gate 	ndigs = pd->ndigits;
1567c478bd9Sstevel@tonic-gate 	exp = pd->exponent;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* remove trailing zeroes */
1597c478bd9Sstevel@tonic-gate 	while (ndigs > 0 && ds[ndigs - 1] == '0') {
1607c478bd9Sstevel@tonic-gate 		exp++;
1617c478bd9Sstevel@tonic-gate 		ndigs--;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	if (ndigs < 1) {
1647c478bd9Sstevel@tonic-gate 		/* nothing left */
1657c478bd9Sstevel@tonic-gate 		px->fpclass = fp_zero;
1667c478bd9Sstevel@tonic-gate 		return;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* convert remaining digits to a base-10^4 _big_float */
1707c478bd9Sstevel@tonic-gate 	d.bsize = _BIG_FLOAT_SIZE;
1717c478bd9Sstevel@tonic-gate 	d.bexponent = exp;
1727c478bd9Sstevel@tonic-gate 	d.blength = (ndigs + 3) >> 2;
1737c478bd9Sstevel@tonic-gate 	i = d.blength - 1;
1747c478bd9Sstevel@tonic-gate 	ids = ndigs - (d.blength << 2);
1757c478bd9Sstevel@tonic-gate 	switch (ids) {
1767c478bd9Sstevel@tonic-gate 	case -1:
1777c478bd9Sstevel@tonic-gate 		d.bsignificand[i] = 100 * ds[ids + 1] +
1787c478bd9Sstevel@tonic-gate 		    10 * ds[ids + 2] + ds[ids + 3] - 111 * '0';
1797c478bd9Sstevel@tonic-gate 		i--;
1807c478bd9Sstevel@tonic-gate 		ids += 4;
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	case -2:
1847c478bd9Sstevel@tonic-gate 		d.bsignificand[i] = 10 * ds[ids + 2] + ds[ids + 3] - 11 * '0';
1857c478bd9Sstevel@tonic-gate 		i--;
1867c478bd9Sstevel@tonic-gate 		ids += 4;
1877c478bd9Sstevel@tonic-gate 		break;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	case -3:
1907c478bd9Sstevel@tonic-gate 		d.bsignificand[i] = ds[ids + 3] - '0';
1917c478bd9Sstevel@tonic-gate 		i--;
1927c478bd9Sstevel@tonic-gate 		ids += 4;
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	while (i >= 0) {
1967c478bd9Sstevel@tonic-gate 		d.bsignificand[i] = 1000 * ds[ids] + 100 * ds[ids + 1] +
1977c478bd9Sstevel@tonic-gate 		    10 * ds[ids + 2] + ds[ids + 3] - 1111 * '0';
1987c478bd9Sstevel@tonic-gate 		i--;
1997c478bd9Sstevel@tonic-gate 		ids += 4;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	pbd = &d;
2037c478bd9Sstevel@tonic-gate 	powtwo = 0;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* pre-scale to get the bits we want into the integer part */
2067c478bd9Sstevel@tonic-gate 	if (exp < 0) {
2077c478bd9Sstevel@tonic-gate 		/* i is a lower bound on log10(x) */
2087c478bd9Sstevel@tonic-gate 		i = exp + ndigs - 1;
2097c478bd9Sstevel@tonic-gate 		if (i <= 0 || ((i * 217705) >> 16) < sigbits + 2) {
2107c478bd9Sstevel@tonic-gate 			/*
2117c478bd9Sstevel@tonic-gate 			 * Scale by 2^(sigbits + 2 + u) where
2127c478bd9Sstevel@tonic-gate 			 * u is an upper bound on -log2(x).
2137c478bd9Sstevel@tonic-gate 			 */
2147c478bd9Sstevel@tonic-gate 			powtwo = sigbits + 2;
2157c478bd9Sstevel@tonic-gate 			if (i < 0)
2167c478bd9Sstevel@tonic-gate 				powtwo += ((-i * 217706) + 65535) >> 16;
2177c478bd9Sstevel@tonic-gate 			else if (i > 0)
2187c478bd9Sstevel@tonic-gate 				powtwo -= (i * 217705) >> 16;
2197c478bd9Sstevel@tonic-gate 			/*
2207c478bd9Sstevel@tonic-gate 			 * Take sigdigits large enough to get
2217c478bd9Sstevel@tonic-gate 			 * all integral digits correct.
2227c478bd9Sstevel@tonic-gate 			 */
2237c478bd9Sstevel@tonic-gate 			sigdigits = i + 1 + (((powtwo * 19729) + 65535) >> 16);
2247c478bd9Sstevel@tonic-gate 			__big_float_times_power(&d, 2, powtwo, sigdigits, &pbd);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* convert to base 2^16 */
2297c478bd9Sstevel@tonic-gate 	b.bsize = _BIG_FLOAT_SIZE;
2307c478bd9Sstevel@tonic-gate 	pbb = &b;
2317c478bd9Sstevel@tonic-gate 	__big_decimal_to_big_binary(pbd, sigbits + 2, &pbb, &sticky);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* adjust pbb->bexponent based on the scale factor above */
2347c478bd9Sstevel@tonic-gate 	pbb->bexponent -= powtwo;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/* convert to unpacked */
2377c478bd9Sstevel@tonic-gate 	ix = 0;
2387c478bd9Sstevel@tonic-gate 	for (i = pbb->blength - 1; i > 0 && ix < 5; i -= 2) {
2397c478bd9Sstevel@tonic-gate 		px->significand[ix++] = (pbb->bsignificand[i] << 16) |
2407c478bd9Sstevel@tonic-gate 		    pbb->bsignificand[i - 1];
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if (ix < 5) {
2437c478bd9Sstevel@tonic-gate 		/* pad with zeroes */
2447c478bd9Sstevel@tonic-gate 		if (i == 0)
2457c478bd9Sstevel@tonic-gate 			px->significand[ix++] = pbb->bsignificand[i] << 16;
2467c478bd9Sstevel@tonic-gate 		while (ix < 5)
2477c478bd9Sstevel@tonic-gate 			px->significand[ix++] = 0;
2487c478bd9Sstevel@tonic-gate 	} else {
2497c478bd9Sstevel@tonic-gate 		/* truncate and set a sticky bit if necessary */
2507c478bd9Sstevel@tonic-gate 		while (i >= 0 && pbb->bsignificand[i] == 0)
2517c478bd9Sstevel@tonic-gate 			i--;
2527c478bd9Sstevel@tonic-gate 		if (i >= 0)
2537c478bd9Sstevel@tonic-gate 			px->significand[4] |= 1;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	if (sticky | pd->more)
2567c478bd9Sstevel@tonic-gate 		px->significand[4] |= 1;
2577c478bd9Sstevel@tonic-gate 	px->exponent = pbb->bexponent + (pbb->blength << 4) - 1;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* normalize so the most significant bit is set */
2607c478bd9Sstevel@tonic-gate 	while (px->significand[0] < 0x80000000u) {
2617c478bd9Sstevel@tonic-gate 		px->significand[0] = (px->significand[0] << 1) |
2627c478bd9Sstevel@tonic-gate 		    (px->significand[1] >> 31);
2637c478bd9Sstevel@tonic-gate 		px->significand[1] = (px->significand[1] << 1) |
2647c478bd9Sstevel@tonic-gate 		    (px->significand[2] >> 31);
2657c478bd9Sstevel@tonic-gate 		px->significand[2] = (px->significand[2] << 1) |
2667c478bd9Sstevel@tonic-gate 		    (px->significand[3] >> 31);
2677c478bd9Sstevel@tonic-gate 		px->significand[3] = (px->significand[3] << 1) |
2687c478bd9Sstevel@tonic-gate 		    (px->significand[4] >> 31);
2697c478bd9Sstevel@tonic-gate 		px->significand[4] <<= 1;
2707c478bd9Sstevel@tonic-gate 		px->exponent--;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (pbd != &d)
2747c478bd9Sstevel@tonic-gate 		(void) free((void *)pbd);
2757c478bd9Sstevel@tonic-gate 	if (pbb != &b)
2767c478bd9Sstevel@tonic-gate 		(void) free((void *)pbb);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Convert a string s consisting of n <= 18 ASCII decimal digits
2817c478bd9Sstevel@tonic-gate  * to an integer value in double precision format, and set *pe
2827c478bd9Sstevel@tonic-gate  * to the number of rounding errors incurred (0 or 1).
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static double
__digits_to_double(char * s,int n,int * pe)2857c478bd9Sstevel@tonic-gate __digits_to_double(char *s, int n, int *pe)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int	i, acc;
2887c478bd9Sstevel@tonic-gate 	double	t, th, tl;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (n <= 9) {
2917c478bd9Sstevel@tonic-gate 		acc = s[0] - '0';
2927c478bd9Sstevel@tonic-gate 		for (i = 1; i < n; i++) {
2937c478bd9Sstevel@tonic-gate 			/* acc <- 10 * acc + next digit */
2947c478bd9Sstevel@tonic-gate 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		t = (double)acc;
2977c478bd9Sstevel@tonic-gate 		*pe = 0;
2987c478bd9Sstevel@tonic-gate 	} else {
2997c478bd9Sstevel@tonic-gate 		acc = s[0] - '0';
3007c478bd9Sstevel@tonic-gate 		for (i = 1; i < (n - 9); i++) {
3017c478bd9Sstevel@tonic-gate 			/* acc <- 10 * acc + next digit */
3027c478bd9Sstevel@tonic-gate 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		th = 1.0e9 * (double)acc;	/* this will be exact */
3057c478bd9Sstevel@tonic-gate 		acc = s[n - 9] - '0';
3067c478bd9Sstevel@tonic-gate 		for (i = n - 8; i < n; i++) {
3077c478bd9Sstevel@tonic-gate 			/* acc <- 10 * acc + next digit */
3087c478bd9Sstevel@tonic-gate 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 		tl = (double)acc;
3117c478bd9Sstevel@tonic-gate 		/* add and indicate whether or not the sum is exact */
3127c478bd9Sstevel@tonic-gate 		t = th + tl;
3137c478bd9Sstevel@tonic-gate 		*pe = ((t - th) == tl)? 0 : 1;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	return (t);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static union {
3197c478bd9Sstevel@tonic-gate 	int	i[2];
3207c478bd9Sstevel@tonic-gate 	double	d;
3217c478bd9Sstevel@tonic-gate } C[] = {
3227c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
3237c478bd9Sstevel@tonic-gate 	{ 0x00000000, 0x3cc40000 },
3247c478bd9Sstevel@tonic-gate #else
3257c478bd9Sstevel@tonic-gate 	{ 0x3cc40000, 0x00000000 },	/* 5 * 2^-53 */
3267c478bd9Sstevel@tonic-gate #endif
3277c478bd9Sstevel@tonic-gate };
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate #define	five2m53	C[0].d
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static int
__fast_decimal_to_single(single * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)3327c478bd9Sstevel@tonic-gate __fast_decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
3337c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	double			dds, delta, ddsplus, ddsminus, df1;
3367c478bd9Sstevel@tonic-gate 	int			n, exp, rounded, e;
3377c478bd9Sstevel@tonic-gate 	float			f1, f2;
3387c478bd9Sstevel@tonic-gate 	__ieee_flags_type	fb;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (pm->rd != fp_nearest)
3417c478bd9Sstevel@tonic-gate 		return (0);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	exp = pd->exponent;
3447c478bd9Sstevel@tonic-gate 	if (pd->ndigits <= 18) {
3457c478bd9Sstevel@tonic-gate 		rounded = 0;
3467c478bd9Sstevel@tonic-gate 		n = pd->ndigits;
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		rounded = 1;
3497c478bd9Sstevel@tonic-gate 		n = 18;
3507c478bd9Sstevel@tonic-gate 		exp += pd->ndigits - 18;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * exp must be in the range of the table, and the result
3547c478bd9Sstevel@tonic-gate 	 * must not underflow or overflow.
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	if (exp < -__TBL_TENS_MAX || exp + n < -36 || exp + n > 38)
3577c478bd9Sstevel@tonic-gate 		return (0);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	__get_ieee_flags(&fb);
3607c478bd9Sstevel@tonic-gate 	dds = __digits_to_double(pd->ds, n, &e);
3617c478bd9Sstevel@tonic-gate 	if (e != 0)
3627c478bd9Sstevel@tonic-gate 		rounded = 1;
3637c478bd9Sstevel@tonic-gate 	if (exp > 0) {
3647c478bd9Sstevel@tonic-gate 		/* small positive exponent */
3657c478bd9Sstevel@tonic-gate 		if (exp > __TBL_TENS_EXACT)
3667c478bd9Sstevel@tonic-gate 			rounded = 1;
3677c478bd9Sstevel@tonic-gate 		if (rounded) {
3687c478bd9Sstevel@tonic-gate 			dds *= __tbl_tens[exp];
3697c478bd9Sstevel@tonic-gate 		} else {
3707c478bd9Sstevel@tonic-gate 			dds = __mul_set(dds, __tbl_tens[exp], &e);
3717c478bd9Sstevel@tonic-gate 			if (e)
3727c478bd9Sstevel@tonic-gate 				rounded = 1;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	} else if (exp < 0) {
3757c478bd9Sstevel@tonic-gate 		/* small negative exponent */
3767c478bd9Sstevel@tonic-gate 		if (-exp > __TBL_TENS_EXACT)
3777c478bd9Sstevel@tonic-gate 			rounded = 1;
3787c478bd9Sstevel@tonic-gate 		if (rounded) {
3797c478bd9Sstevel@tonic-gate 			dds /= __tbl_tens[-exp];
3807c478bd9Sstevel@tonic-gate 		} else {
3817c478bd9Sstevel@tonic-gate 			dds = __div_set(dds, __tbl_tens[-exp], &e);
3827c478bd9Sstevel@tonic-gate 			if (e)
3837c478bd9Sstevel@tonic-gate 				rounded = 1;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * At this point dds may have four rounding errors due to
3897c478bd9Sstevel@tonic-gate 	 * (i) truncation of pd->ds to 18 digits, (ii) inexact con-
3907c478bd9Sstevel@tonic-gate 	 * version of pd->ds to binary, (iii) scaling by a power of
3917c478bd9Sstevel@tonic-gate 	 * ten that is not exactly representable, and (iv) roundoff
3927c478bd9Sstevel@tonic-gate 	 * error in the multiplication.  Below we will incur one more
3937c478bd9Sstevel@tonic-gate 	 * rounding error when we add or subtract delta and dds.  We
3947c478bd9Sstevel@tonic-gate 	 * construct delta so that even after this last rounding error,
3957c478bd9Sstevel@tonic-gate 	 * ddsplus is an upper bound on the exact value and ddsminus
3967c478bd9Sstevel@tonic-gate 	 * is a lower bound.  Then as long as both of these quantities
3977c478bd9Sstevel@tonic-gate 	 * round to the same single precision number, that number
3987c478bd9Sstevel@tonic-gate 	 * will be the correctly rounded single precision result.
3997c478bd9Sstevel@tonic-gate 	 * (If any rounding errors have been committed, then we must
4007c478bd9Sstevel@tonic-gate 	 * also be certain that the result can't be exact.)
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	delta = five2m53 * dds;
4037c478bd9Sstevel@tonic-gate 	ddsplus = dds + delta;
4047c478bd9Sstevel@tonic-gate 	ddsminus = dds - delta;
4057c478bd9Sstevel@tonic-gate 	f1 = (float)(ddsplus);
4067c478bd9Sstevel@tonic-gate 	f2 = (float)(ddsminus);
4077c478bd9Sstevel@tonic-gate 	df1 = f1;
4087c478bd9Sstevel@tonic-gate 	__set_ieee_flags(&fb);
4097c478bd9Sstevel@tonic-gate 	if (f1 != f2)
4107c478bd9Sstevel@tonic-gate 		return (0);
4117c478bd9Sstevel@tonic-gate 	if (rounded) {
4127c478bd9Sstevel@tonic-gate 		/*
4137c478bd9Sstevel@tonic-gate 		 * If ddsminus <= f1 <= ddsplus, the result might be
4147c478bd9Sstevel@tonic-gate 		 * exact; we have to convert the long way to be sure.
4157c478bd9Sstevel@tonic-gate 		 */
4167c478bd9Sstevel@tonic-gate 		if (ddsminus <= df1 && df1 <= ddsplus)
4177c478bd9Sstevel@tonic-gate 			return (0);
4187c478bd9Sstevel@tonic-gate 		*ps = (1 << fp_inexact);
4197c478bd9Sstevel@tonic-gate 	} else {
4207c478bd9Sstevel@tonic-gate 		*ps = (df1 == dds)? 0 : (1 << fp_inexact);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	*px = (pd->sign)? -f1 : f1;
4237c478bd9Sstevel@tonic-gate 	return (1);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Attempt conversion to double using floating-point arithmetic.
4287c478bd9Sstevel@tonic-gate  * Return 1 if it works (at most one rounding error), 0 if it doesn't.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate static int
__fast_decimal_to_double(double * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)4317c478bd9Sstevel@tonic-gate __fast_decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
4327c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	double			dds;
4357c478bd9Sstevel@tonic-gate 	int			e;
4367c478bd9Sstevel@tonic-gate 	__ieee_flags_type	fb;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (pm->rd != fp_nearest || pd->ndigits > 18 || pd->exponent
4397c478bd9Sstevel@tonic-gate 	    > __TBL_TENS_EXACT || pd->exponent < -__TBL_TENS_EXACT)
4407c478bd9Sstevel@tonic-gate 		return (0);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	__get_ieee_flags(&fb);
4437c478bd9Sstevel@tonic-gate 	dds = __digits_to_double(pd->ds, pd->ndigits, &e);
4447c478bd9Sstevel@tonic-gate 	if (e != 0) {
4457c478bd9Sstevel@tonic-gate 		__set_ieee_flags(&fb);
4467c478bd9Sstevel@tonic-gate 		return (0);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	if (pd->exponent > 0)
4497c478bd9Sstevel@tonic-gate 		dds = __mul_set(dds, __tbl_tens[pd->exponent], &e);
4507c478bd9Sstevel@tonic-gate 	else if (pd->exponent < 0)
4517c478bd9Sstevel@tonic-gate 		dds = __div_set(dds, __tbl_tens[-pd->exponent], &e);
4527c478bd9Sstevel@tonic-gate 	*px = (pd->sign)? -dds : dds;
4537c478bd9Sstevel@tonic-gate 	*ps = (e)? (1 << fp_inexact) : 0;
4547c478bd9Sstevel@tonic-gate 	__set_ieee_flags(&fb);
4557c478bd9Sstevel@tonic-gate 	return (1);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /* PUBLIC FUNCTIONS */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  * The following routines convert the decimal record *pd to a floating
4627c478bd9Sstevel@tonic-gate  * point value *px observing the rounding mode specified in pm->rd and
4637c478bd9Sstevel@tonic-gate  * passing back any floating point exceptions that occur in *ps.
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  * pd->sign and pd->fpclass are always taken into account.  pd->exponent
4667c478bd9Sstevel@tonic-gate  * and pd->ds are used when pd->fpclass is fp_normal or fp_subnormal.
4677c478bd9Sstevel@tonic-gate  * In these cases, pd->ds must contain a null-terminated string of one
4687c478bd9Sstevel@tonic-gate  * or more ASCII digits, the first of which is not zero, and pd->ndigits
4697c478bd9Sstevel@tonic-gate  * must equal the length of this string.  If m is the integer represented
4707c478bd9Sstevel@tonic-gate  * by the string pd->ds, then *px will be set to a correctly rounded
4717c478bd9Sstevel@tonic-gate  * approximation to
4727c478bd9Sstevel@tonic-gate  *
4737c478bd9Sstevel@tonic-gate  *   -1**(pd->sign) * m * 10**(pd->exponent)
4747c478bd9Sstevel@tonic-gate  *
4757c478bd9Sstevel@tonic-gate  * (If pd->more != 0 then additional nonzero digits are assumed to follow
4767c478bd9Sstevel@tonic-gate  * those in pd->ds, so m is effectively replaced by m + epsilon in the
4777c478bd9Sstevel@tonic-gate  * expression above.)
4787c478bd9Sstevel@tonic-gate  *
4797c478bd9Sstevel@tonic-gate  * For example, if pd->exponent == -2 and pd->ds holds "1234", then *px
4807c478bd9Sstevel@tonic-gate  * will be a correctly rounded approximation to 12.34.
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * Note that the only mode that matters is the rounding direction pm->rd;
4837c478bd9Sstevel@tonic-gate  * pm->df and pm->ndigits are never used.
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /* maximum decimal exponent we need to consider */
4877c478bd9Sstevel@tonic-gate #define	SINGLE_MAXE	  47
4887c478bd9Sstevel@tonic-gate #define	DOUBLE_MAXE	 326
4897c478bd9Sstevel@tonic-gate #define	EXTENDED_MAXE	4968
4907c478bd9Sstevel@tonic-gate #define	QUAD_MAXE	4968
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate void
decimal_to_single(single * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)4937c478bd9Sstevel@tonic-gate decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
4947c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	single_equivalence	*kluge;
4977c478bd9Sstevel@tonic-gate 	unpacked		u;
4987c478bd9Sstevel@tonic-gate 	fp_exception_field_type	ef;
4997c478bd9Sstevel@tonic-gate 	int			i;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/* special values */
5027c478bd9Sstevel@tonic-gate 	kluge = (single_equivalence *)px;
5037c478bd9Sstevel@tonic-gate 	switch (pd->fpclass) {
5047c478bd9Sstevel@tonic-gate 	case fp_zero:
5057c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5067c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0;
5077c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
5087c478bd9Sstevel@tonic-gate 		*ps = 0;
5097c478bd9Sstevel@tonic-gate 		return;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	case fp_infinity:
5127c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5137c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0xff;
5147c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
5157c478bd9Sstevel@tonic-gate 		*ps = 0;
5167c478bd9Sstevel@tonic-gate 		return;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	case fp_quiet:
5197c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5207c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0xff;
5217c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0x7fffff;
5227c478bd9Sstevel@tonic-gate 		*ps = 0;
5237c478bd9Sstevel@tonic-gate 		return;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	case fp_signaling:
5267c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5277c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0xff;
5287c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0x3fffff;
5297c478bd9Sstevel@tonic-gate 		*ps = 0;
5307c478bd9Sstevel@tonic-gate 		return;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* numeric values */
5347c478bd9Sstevel@tonic-gate 	ef = 0;
5357c478bd9Sstevel@tonic-gate 	if (pd->exponent + pd->ndigits > SINGLE_MAXE) {
5367c478bd9Sstevel@tonic-gate 		/* result must overflow */
5377c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
5387c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
5397c478bd9Sstevel@tonic-gate 		u.exponent = 0x000fffff;
5407c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
5417c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
5427c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
5437c478bd9Sstevel@tonic-gate 	} else if (pd->exponent + pd->ndigits < -SINGLE_MAXE) {
5447c478bd9Sstevel@tonic-gate 		/* result must underflow completely */
5457c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
5467c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
5477c478bd9Sstevel@tonic-gate 		u.exponent = -0x000fffff;
5487c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
5497c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
5507c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
5517c478bd9Sstevel@tonic-gate 	} else {
5527c478bd9Sstevel@tonic-gate 		/* result may be in range */
5537c478bd9Sstevel@tonic-gate 		if (__fast_decimal_to_single(px, pm, pd, &ef) == 1) {
5547c478bd9Sstevel@tonic-gate 			*ps = ef;
5557c478bd9Sstevel@tonic-gate 			if (ef != 0)
5567c478bd9Sstevel@tonic-gate 				__base_conversion_set_exception(ef);
5577c478bd9Sstevel@tonic-gate 			return;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		__decimal_to_unpacked(&u, pd, 24);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	__pack_single(&u, px, pm->rd, &ef);
5627c478bd9Sstevel@tonic-gate 	*ps = ef;
5637c478bd9Sstevel@tonic-gate 	if (ef != 0)
5647c478bd9Sstevel@tonic-gate 		__base_conversion_set_exception(ef);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate void
decimal_to_double(double * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)5687c478bd9Sstevel@tonic-gate decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
5697c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	double_equivalence	*kluge;
5727c478bd9Sstevel@tonic-gate 	unpacked		u;
5737c478bd9Sstevel@tonic-gate 	fp_exception_field_type	ef;
5747c478bd9Sstevel@tonic-gate 	int			i;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/* special values */
5777c478bd9Sstevel@tonic-gate 	kluge = (double_equivalence *)px;
5787c478bd9Sstevel@tonic-gate 	switch (pd->fpclass) {
5797c478bd9Sstevel@tonic-gate 	case fp_zero:
5807c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5817c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0;
5827c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
5837c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
5847c478bd9Sstevel@tonic-gate 		*ps = 0;
5857c478bd9Sstevel@tonic-gate 		return;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	case fp_infinity:
5887c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5897c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7ff;
5907c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
5917c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
5927c478bd9Sstevel@tonic-gate 		*ps = 0;
5937c478bd9Sstevel@tonic-gate 		return;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	case fp_quiet:
5967c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
5977c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7ff;
5987c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0xfffff;
5997c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
6007c478bd9Sstevel@tonic-gate 		*ps = 0;
6017c478bd9Sstevel@tonic-gate 		return;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	case fp_signaling:
6047c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
6057c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7ff;
6067c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0x7ffff;
6077c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
6087c478bd9Sstevel@tonic-gate 		*ps = 0;
6097c478bd9Sstevel@tonic-gate 		return;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/* numeric values */
6137c478bd9Sstevel@tonic-gate 	ef = 0;
6147c478bd9Sstevel@tonic-gate 	if (pd->exponent + pd->ndigits > DOUBLE_MAXE) {
6157c478bd9Sstevel@tonic-gate 		/* result must overflow */
6167c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
6177c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
6187c478bd9Sstevel@tonic-gate 		u.exponent = 0x000fffff;
6197c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
6207c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
6217c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
6227c478bd9Sstevel@tonic-gate 	} else if (pd->exponent + pd->ndigits < -DOUBLE_MAXE) {
6237c478bd9Sstevel@tonic-gate 		/* result must underflow completely */
6247c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
6257c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
6267c478bd9Sstevel@tonic-gate 		u.exponent = -0x000fffff;
6277c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
6287c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
6297c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
6307c478bd9Sstevel@tonic-gate 	} else {
6317c478bd9Sstevel@tonic-gate 		/* result may be in range */
6327c478bd9Sstevel@tonic-gate 		if (__fast_decimal_to_double(px, pm, pd, &ef) == 1) {
6337c478bd9Sstevel@tonic-gate 			*ps = ef;
6347c478bd9Sstevel@tonic-gate 			if (ef != 0)
6357c478bd9Sstevel@tonic-gate 				__base_conversion_set_exception(ef);
6367c478bd9Sstevel@tonic-gate 			return;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		__decimal_to_unpacked(&u, pd, 53);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	__pack_double(&u, px, pm->rd, &ef);
6417c478bd9Sstevel@tonic-gate 	*ps = ef;
6427c478bd9Sstevel@tonic-gate 	if (ef != 0)
6437c478bd9Sstevel@tonic-gate 		__base_conversion_set_exception(ef);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate void
decimal_to_extended(extended * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)6477c478bd9Sstevel@tonic-gate decimal_to_extended(extended *px, decimal_mode *pm, decimal_record *pd,
6487c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	extended_equivalence	*kluge;
6517c478bd9Sstevel@tonic-gate 	unpacked		u;
6527c478bd9Sstevel@tonic-gate 	double_equivalence	dd;
6537c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
6547c478bd9Sstevel@tonic-gate 	int			i;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/* special values */
6577c478bd9Sstevel@tonic-gate 	kluge = (extended_equivalence *)px;
6587c478bd9Sstevel@tonic-gate 	switch (pd->fpclass) {
6597c478bd9Sstevel@tonic-gate 	case fp_zero:
6607c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
6617c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0;
6627c478bd9Sstevel@tonic-gate 		kluge->f.significand = 0;
6637c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
6647c478bd9Sstevel@tonic-gate 		*ps = 0;
6657c478bd9Sstevel@tonic-gate 		return;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	case fp_infinity:
6687c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
6697c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
6707c478bd9Sstevel@tonic-gate 		kluge->f.significand = 0x80000000;
6717c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
6727c478bd9Sstevel@tonic-gate 		*ps = 0;
6737c478bd9Sstevel@tonic-gate 		return;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	case fp_quiet:
6767c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
6777c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
6787c478bd9Sstevel@tonic-gate 		kluge->f.significand = 0xffffffff;
6797c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
6807c478bd9Sstevel@tonic-gate 		*ps = 0;
6817c478bd9Sstevel@tonic-gate 		return;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	case fp_signaling:
6847c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
6857c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
6867c478bd9Sstevel@tonic-gate 		kluge->f.significand = 0xbfffffff;
6877c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
6887c478bd9Sstevel@tonic-gate 		*ps = 0;
6897c478bd9Sstevel@tonic-gate 		return;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* numeric values */
6937c478bd9Sstevel@tonic-gate 	ef = 0;
6947c478bd9Sstevel@tonic-gate 	if (pd->exponent + pd->ndigits > EXTENDED_MAXE) {
6957c478bd9Sstevel@tonic-gate 		/* result must overflow */
6967c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
6977c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
6987c478bd9Sstevel@tonic-gate 		u.exponent = 0x000fffff;
6997c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
7007c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
7017c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
7027c478bd9Sstevel@tonic-gate 	} else if (pd->exponent + pd->ndigits < -EXTENDED_MAXE) {
7037c478bd9Sstevel@tonic-gate 		/* result must underflow completely */
7047c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
7057c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
7067c478bd9Sstevel@tonic-gate 		u.exponent = -0x000fffff;
7077c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
7087c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
7097c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
7107c478bd9Sstevel@tonic-gate 	} else {
7117c478bd9Sstevel@tonic-gate 		/* result may be in range */
7127c478bd9Sstevel@tonic-gate 		if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
7137c478bd9Sstevel@tonic-gate 		    ef == 0) {
7147c478bd9Sstevel@tonic-gate 			u.sign = dd.f.msw.sign;
7157c478bd9Sstevel@tonic-gate 			u.fpclass = fp_normal;
7167c478bd9Sstevel@tonic-gate 			u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
7177c478bd9Sstevel@tonic-gate 			u.significand[0] = ((0x100000 |
7187c478bd9Sstevel@tonic-gate 			    dd.f.msw.significand) << 11) |
7197c478bd9Sstevel@tonic-gate 			    (dd.f.significand2 >> 21);
7207c478bd9Sstevel@tonic-gate 			u.significand[1] = dd.f.significand2 << 11;
7217c478bd9Sstevel@tonic-gate 			for (i = 2; i < UNPACKED_SIZE; i++)
7227c478bd9Sstevel@tonic-gate 				u.significand[i] = 0;
7237c478bd9Sstevel@tonic-gate 		} else {
7247c478bd9Sstevel@tonic-gate 			__decimal_to_unpacked(&u, pd, 64);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	__pack_extended(&u, px, pm->rd, &ef);
7287c478bd9Sstevel@tonic-gate 	*ps = ef;
7297c478bd9Sstevel@tonic-gate 	if (ef != 0)
7307c478bd9Sstevel@tonic-gate 		__base_conversion_set_exception(ef);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate void
decimal_to_quadruple(quadruple * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)7347c478bd9Sstevel@tonic-gate decimal_to_quadruple(quadruple *px, decimal_mode *pm, decimal_record *pd,
7357c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	quadruple_equivalence	*kluge;
7387c478bd9Sstevel@tonic-gate 	unpacked		u;
7397c478bd9Sstevel@tonic-gate 	double_equivalence	dd;
7407c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
7417c478bd9Sstevel@tonic-gate 	int			i;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/* special values */
7447c478bd9Sstevel@tonic-gate 	kluge = (quadruple_equivalence *)px;
7457c478bd9Sstevel@tonic-gate 	switch (pd->fpclass) {
7467c478bd9Sstevel@tonic-gate 	case fp_zero:
7477c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
7487c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0;
7497c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
7507c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
7517c478bd9Sstevel@tonic-gate 		kluge->f.significand3 = 0;
7527c478bd9Sstevel@tonic-gate 		kluge->f.significand4 = 0;
7537c478bd9Sstevel@tonic-gate 		*ps = 0;
7547c478bd9Sstevel@tonic-gate 		return;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	case fp_infinity:
7577c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
7587c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
7597c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0;
7607c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0;
7617c478bd9Sstevel@tonic-gate 		kluge->f.significand3 = 0;
7627c478bd9Sstevel@tonic-gate 		kluge->f.significand4 = 0;
7637c478bd9Sstevel@tonic-gate 		*ps = 0;
7647c478bd9Sstevel@tonic-gate 		return;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	case fp_quiet:
7677c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
7687c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
7697c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0xffff;
7707c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
7717c478bd9Sstevel@tonic-gate 		kluge->f.significand3 = 0xffffffff;
7727c478bd9Sstevel@tonic-gate 		kluge->f.significand4 = 0xffffffff;
7737c478bd9Sstevel@tonic-gate 		*ps = 0;
7747c478bd9Sstevel@tonic-gate 		return;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	case fp_signaling:
7777c478bd9Sstevel@tonic-gate 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
7787c478bd9Sstevel@tonic-gate 		kluge->f.msw.exponent = 0x7fff;
7797c478bd9Sstevel@tonic-gate 		kluge->f.msw.significand = 0x7fff;
7807c478bd9Sstevel@tonic-gate 		kluge->f.significand2 = 0xffffffff;
7817c478bd9Sstevel@tonic-gate 		kluge->f.significand3 = 0xffffffff;
7827c478bd9Sstevel@tonic-gate 		kluge->f.significand4 = 0xffffffff;
7837c478bd9Sstevel@tonic-gate 		*ps = 0;
7847c478bd9Sstevel@tonic-gate 		return;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/* numeric values */
7887c478bd9Sstevel@tonic-gate 	ef = 0;
7897c478bd9Sstevel@tonic-gate 	if (pd->exponent + pd->ndigits > QUAD_MAXE) {
7907c478bd9Sstevel@tonic-gate 		/* result must overflow */
7917c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
7927c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
7937c478bd9Sstevel@tonic-gate 		u.exponent = 0x000fffff;
7947c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
7957c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
7967c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
7977c478bd9Sstevel@tonic-gate 	} else if (pd->exponent + pd->ndigits < -QUAD_MAXE) {
7987c478bd9Sstevel@tonic-gate 		/* result must underflow completely */
7997c478bd9Sstevel@tonic-gate 		u.sign = (pd->sign != 0);
8007c478bd9Sstevel@tonic-gate 		u.fpclass = fp_normal;
8017c478bd9Sstevel@tonic-gate 		u.exponent = -0x000fffff;
8027c478bd9Sstevel@tonic-gate 		u.significand[0] = 0x80000000;
8037c478bd9Sstevel@tonic-gate 		for (i = 1; i < UNPACKED_SIZE; i++)
8047c478bd9Sstevel@tonic-gate 			u.significand[i] = 0;
8057c478bd9Sstevel@tonic-gate 	} else {
8067c478bd9Sstevel@tonic-gate 		/* result may be in range */
8077c478bd9Sstevel@tonic-gate 		if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
8087c478bd9Sstevel@tonic-gate 		    ef == 0) {
8097c478bd9Sstevel@tonic-gate 			u.sign = dd.f.msw.sign;
8107c478bd9Sstevel@tonic-gate 			u.fpclass = fp_normal;
8117c478bd9Sstevel@tonic-gate 			u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
8127c478bd9Sstevel@tonic-gate 			u.significand[0] = ((0x100000 |
8137c478bd9Sstevel@tonic-gate 			    dd.f.msw.significand) << 11) |
8147c478bd9Sstevel@tonic-gate 			    (dd.f.significand2 >> 21);
8157c478bd9Sstevel@tonic-gate 			u.significand[1] = dd.f.significand2 << 11;
8167c478bd9Sstevel@tonic-gate 			for (i = 2; i < UNPACKED_SIZE; i++)
8177c478bd9Sstevel@tonic-gate 				u.significand[i] = 0;
8187c478bd9Sstevel@tonic-gate 		} else {
8197c478bd9Sstevel@tonic-gate 			__decimal_to_unpacked(&u, pd, 113);
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	__pack_quadruple(&u, px, pm->rd, &ef);
8237c478bd9Sstevel@tonic-gate 	*ps = ef;
8247c478bd9Sstevel@tonic-gate 	if (ef != 0)
8257c478bd9Sstevel@tonic-gate 		__base_conversion_set_exception(ef);
8267c478bd9Sstevel@tonic-gate }
827