xref: /illumos-gate/usr/src/lib/libc/port/fp/__flt_decim.c (revision 7257d1b4)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Short cut for conversion from double precision to decimal
317c478bd9Sstevel@tonic-gate  * floating point
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
34*7257d1b4Sraf #include "lint.h"
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
377c478bd9Sstevel@tonic-gate #include "base_conversion.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Powers of ten rounded up.  If i is the largest index such that
417c478bd9Sstevel@tonic-gate  * tbl_decade[i] <= x, then:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *  if i == 0 then x < 10^-49
447c478bd9Sstevel@tonic-gate  *  else if i == TBL_DECADE_MAX then x >= 10^67
457c478bd9Sstevel@tonic-gate  *  else 10^(i-TBL_DECADE_OFFSET) <= x < 10^(i-TBL_DECADE_OFFSET+1)
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	TBL_DECADE_OFFSET	50
497c478bd9Sstevel@tonic-gate #define	TBL_DECADE_MAX		117
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static const double tbl_decade[TBL_DECADE_MAX + 1] = {
527c478bd9Sstevel@tonic-gate 	0.0,
537c478bd9Sstevel@tonic-gate 	1.00000000000000012631e-49, 1.00000000000000012631e-48,
547c478bd9Sstevel@tonic-gate 	1.00000000000000009593e-47, 1.00000000000000002300e-46,
557c478bd9Sstevel@tonic-gate 	1.00000000000000013968e-45, 1.00000000000000007745e-44,
567c478bd9Sstevel@tonic-gate 	1.00000000000000007745e-43, 1.00000000000000003762e-42,
577c478bd9Sstevel@tonic-gate 	1.00000000000000000576e-41, 1.00000000000000013321e-40,
587c478bd9Sstevel@tonic-gate 	1.00000000000000009243e-39, 1.00000000000000009243e-38,
597c478bd9Sstevel@tonic-gate 	1.00000000000000006632e-37, 1.00000000000000010809e-36,
607c478bd9Sstevel@tonic-gate 	1.00000000000000000786e-35, 1.00000000000000014150e-34,
617c478bd9Sstevel@tonic-gate 	1.00000000000000005597e-33, 1.00000000000000005597e-32,
627c478bd9Sstevel@tonic-gate 	1.00000000000000008334e-31, 1.00000000000000008334e-30,
637c478bd9Sstevel@tonic-gate 	1.00000000000000008334e-29, 1.00000000000000008334e-28,
647c478bd9Sstevel@tonic-gate 	1.00000000000000003849e-27, 1.00000000000000003849e-26,
657c478bd9Sstevel@tonic-gate 	1.00000000000000003849e-25, 1.00000000000000010737e-24,
667c478bd9Sstevel@tonic-gate 	1.00000000000000010737e-23, 1.00000000000000004860e-22,
677c478bd9Sstevel@tonic-gate 	1.00000000000000009562e-21, 1.00000000000000009562e-20,
687c478bd9Sstevel@tonic-gate 	1.00000000000000009562e-19, 1.00000000000000007154e-18,
697c478bd9Sstevel@tonic-gate 	1.00000000000000007154e-17, 1.00000000000000010236e-16,
707c478bd9Sstevel@tonic-gate 	1.00000000000000007771e-15, 1.00000000000000015659e-14,
717c478bd9Sstevel@tonic-gate 	1.00000000000000003037e-13, 1.00000000000000018184e-12,
727c478bd9Sstevel@tonic-gate 	1.00000000000000010106e-11, 1.00000000000000003643e-10,
737c478bd9Sstevel@tonic-gate 	1.00000000000000006228e-09, 1.00000000000000002092e-08,
747c478bd9Sstevel@tonic-gate 	1.00000000000000008710e-07, 1.00000000000000016651e-06,
757c478bd9Sstevel@tonic-gate 	1.00000000000000008180e-05, 1.00000000000000004792e-04,
767c478bd9Sstevel@tonic-gate 	1.00000000000000002082e-03, 1.00000000000000002082e-02,
777c478bd9Sstevel@tonic-gate 	1.00000000000000005551e-01, 1.00000000000000000000e+00,
787c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+01, 1.00000000000000000000e+02,
797c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+03, 1.00000000000000000000e+04,
807c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+05, 1.00000000000000000000e+06,
817c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+07, 1.00000000000000000000e+08,
827c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+09, 1.00000000000000000000e+10,
837c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+11, 1.00000000000000000000e+12,
847c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+13, 1.00000000000000000000e+14,
857c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+15, 1.00000000000000000000e+16,
867c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+17, 1.00000000000000000000e+18,
877c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+19, 1.00000000000000000000e+20,
887c478bd9Sstevel@tonic-gate 	1.00000000000000000000e+21, 1.00000000000000000000e+22,
897c478bd9Sstevel@tonic-gate 	1.00000000000000008389e+23, 1.00000000000000011744e+24,
907c478bd9Sstevel@tonic-gate 	1.00000000000000009060e+25, 1.00000000000000004765e+26,
917c478bd9Sstevel@tonic-gate 	1.00000000000000001329e+27, 1.00000000000000017821e+28,
927c478bd9Sstevel@tonic-gate 	1.00000000000000009025e+29, 1.00000000000000001988e+30,
937c478bd9Sstevel@tonic-gate 	1.00000000000000007618e+31, 1.00000000000000005366e+32,
947c478bd9Sstevel@tonic-gate 	1.00000000000000008969e+33, 1.00000000000000006087e+34,
957c478bd9Sstevel@tonic-gate 	1.00000000000000015310e+35, 1.00000000000000004242e+36,
967c478bd9Sstevel@tonic-gate 	1.00000000000000007194e+37, 1.00000000000000016638e+38,
977c478bd9Sstevel@tonic-gate 	1.00000000000000009082e+39, 1.00000000000000003038e+40,
987c478bd9Sstevel@tonic-gate 	1.00000000000000000620e+41, 1.00000000000000004489e+42,
997c478bd9Sstevel@tonic-gate 	1.00000000000000001394e+43, 1.00000000000000008821e+44,
1007c478bd9Sstevel@tonic-gate 	1.00000000000000008821e+45, 1.00000000000000011990e+46,
1017c478bd9Sstevel@tonic-gate 	1.00000000000000004385e+47, 1.00000000000000004385e+48,
1027c478bd9Sstevel@tonic-gate 	1.00000000000000007630e+49, 1.00000000000000007630e+50,
1037c478bd9Sstevel@tonic-gate 	1.00000000000000015937e+51, 1.00000000000000012614e+52,
1047c478bd9Sstevel@tonic-gate 	1.00000000000000020590e+53, 1.00000000000000007829e+54,
1057c478bd9Sstevel@tonic-gate 	1.00000000000000001024e+55, 1.00000000000000009190e+56,
1067c478bd9Sstevel@tonic-gate 	1.00000000000000004835e+57, 1.00000000000000008319e+58,
1077c478bd9Sstevel@tonic-gate 	1.00000000000000008319e+59, 1.00000000000000012779e+60,
1087c478bd9Sstevel@tonic-gate 	1.00000000000000009211e+61, 1.00000000000000003502e+62,
1097c478bd9Sstevel@tonic-gate 	1.00000000000000005786e+63, 1.00000000000000002132e+64,
1107c478bd9Sstevel@tonic-gate 	1.00000000000000010901e+65, 1.00000000000000013239e+66,
1117c478bd9Sstevel@tonic-gate 	1.00000000000000013239e+67
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Convert a positive double precision integer x <= 2147483647999999744
1167c478bd9Sstevel@tonic-gate  * (the largest double less than 2^31 * 10^9; this implementation works
1177c478bd9Sstevel@tonic-gate  * up to the largest double less than 2^25 * 10^12) to a string of ASCII
1187c478bd9Sstevel@tonic-gate  * decimal digits, adding leading zeroes so that the result has at least
1197c478bd9Sstevel@tonic-gate  * n digits.  The string is terminated by a null byte, and its length
1207c478bd9Sstevel@tonic-gate  * is returned.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * This routine assumes round-to-nearest mode is in effect and any
1237c478bd9Sstevel@tonic-gate  * exceptions raised will be ignored.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	tenm4	tbl_decade[TBL_DECADE_OFFSET - 4]
1277c478bd9Sstevel@tonic-gate #define	ten4	tbl_decade[TBL_DECADE_OFFSET + 4]
1287c478bd9Sstevel@tonic-gate #define	tenm12	tbl_decade[TBL_DECADE_OFFSET - 12]
1297c478bd9Sstevel@tonic-gate #define	ten12	tbl_decade[TBL_DECADE_OFFSET + 12]
1307c478bd9Sstevel@tonic-gate #define	one	tbl_decade[TBL_DECADE_OFFSET]
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static int
__double_to_digits(double x,char * s,int n)1337c478bd9Sstevel@tonic-gate __double_to_digits(double x, char *s, int n)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	double		y;
1367c478bd9Sstevel@tonic-gate 	int		d[5], i, j;
1377c478bd9Sstevel@tonic-gate 	char		*ss, tmp[4];
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* decompose x into four-digit chunks */
1407c478bd9Sstevel@tonic-gate 	y = (int)(x * tenm12);
1417c478bd9Sstevel@tonic-gate 	x -= y * ten12;
1427c478bd9Sstevel@tonic-gate 	if (x < 0.0) {
1437c478bd9Sstevel@tonic-gate 		y -= one;
1447c478bd9Sstevel@tonic-gate 		x += ten12;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	d[0] = (int)(y * tenm4);
1477c478bd9Sstevel@tonic-gate 	d[1] = (int)(y - d[0] * ten4);
1487c478bd9Sstevel@tonic-gate 	y = (int)(x * tenm4);
1497c478bd9Sstevel@tonic-gate 	d[4] = (int)(x - y * ten4);
1507c478bd9Sstevel@tonic-gate 	d[2] = (int)(y * tenm4);
1517c478bd9Sstevel@tonic-gate 	d[3] = (int)(y - d[2] * ten4);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Find the first nonzero chunk or the point at which to start
1557c478bd9Sstevel@tonic-gate 	 * converting so we have n digits, whichever comes first.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	ss = s;
1587c478bd9Sstevel@tonic-gate 	if (n > 20) {
1597c478bd9Sstevel@tonic-gate 		for (j = 0; j < n - 20; j++)
1607c478bd9Sstevel@tonic-gate 			*ss++ = '0';
1617c478bd9Sstevel@tonic-gate 		i = 0;
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		for (i = 0; d[i] == 0 && n <= ((4 - i) << 2); i++)
1647c478bd9Sstevel@tonic-gate 			;
1657c478bd9Sstevel@tonic-gate 		__four_digits_quick(d[i], tmp);
1667c478bd9Sstevel@tonic-gate 		for (j = 0; tmp[j] == '0' && n <= ((4 - i) << 2) + 3 - j; j++)
1677c478bd9Sstevel@tonic-gate 			;
1687c478bd9Sstevel@tonic-gate 		while (j < 4)
1697c478bd9Sstevel@tonic-gate 			*ss++ = tmp[j++];
1707c478bd9Sstevel@tonic-gate 		i++;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* continue converting four-digit chunks */
1747c478bd9Sstevel@tonic-gate 	while (i < 5) {
1757c478bd9Sstevel@tonic-gate 		__four_digits_quick(d[i], ss);
1767c478bd9Sstevel@tonic-gate 		ss += 4;
1777c478bd9Sstevel@tonic-gate 		i++;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	*ss = '\0';
1817c478bd9Sstevel@tonic-gate 	return (ss - s);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Round a positive double precision number *x to the nearest integer,
1867c478bd9Sstevel@tonic-gate  * returning the result and passing back an indication of accuracy in
1877c478bd9Sstevel@tonic-gate  * *pe.  On entry, nrx is the number of rounding errors already com-
1887c478bd9Sstevel@tonic-gate  * mitted in forming *x.  On exit, *pe is 0 if *x was already integral
1897c478bd9Sstevel@tonic-gate  * and exact, 1 if the result is the correctly rounded integer value
1907c478bd9Sstevel@tonic-gate  * but not exact, and 2 if error in *x precludes determining the cor-
1917c478bd9Sstevel@tonic-gate  * rectly rounded integer value (i.e., the error might be larger than
1927c478bd9Sstevel@tonic-gate  * 1/2 - |*x - rx|, where rx is the nearest integer to *x).
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static union {
1967c478bd9Sstevel@tonic-gate 	unsigned int	i[2];
1977c478bd9Sstevel@tonic-gate 	double		d;
1987c478bd9Sstevel@tonic-gate } C[] = {
1997c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
2007c478bd9Sstevel@tonic-gate 	{ 0x00000000, 0x43300000 },
2017c478bd9Sstevel@tonic-gate 	{ 0x00000000, 0x3ca00000 },
2027c478bd9Sstevel@tonic-gate 	{ 0x00000000, 0x3fe00000 },
2037c478bd9Sstevel@tonic-gate 	{ 0xffffffff, 0x3fdfffff },
2047c478bd9Sstevel@tonic-gate #else
2057c478bd9Sstevel@tonic-gate 	{ 0x43300000, 0x00000000 },
2067c478bd9Sstevel@tonic-gate 	{ 0x3ca00000, 0x00000000 },
2077c478bd9Sstevel@tonic-gate 	{ 0x3fe00000, 0x00000000 },
2087c478bd9Sstevel@tonic-gate 	{ 0x3fdfffff, 0xffffffff },	/* nextafter(1/2, 0) */
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate #define	two52	C[0].d
2137c478bd9Sstevel@tonic-gate #define	twom53	C[1].d
2147c478bd9Sstevel@tonic-gate #define	half	C[2].d
2157c478bd9Sstevel@tonic-gate #define	halfdec	C[3].d
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static double
__arint_set_n(double * x,int nrx,int * pe)2187c478bd9Sstevel@tonic-gate __arint_set_n(double *x, int nrx, int *pe)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int	hx;
2217c478bd9Sstevel@tonic-gate 	double	rx, rmx;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
2247c478bd9Sstevel@tonic-gate 	hx = *(1+(int *)x);
2257c478bd9Sstevel@tonic-gate #else
2267c478bd9Sstevel@tonic-gate 	hx = *(int *)x;
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 	if (hx >= 0x43300000) {
2297c478bd9Sstevel@tonic-gate 		/* x >= 2^52, so it's already integral */
2307c478bd9Sstevel@tonic-gate 		if (nrx == 0)
2317c478bd9Sstevel@tonic-gate 			*pe = 0;
2327c478bd9Sstevel@tonic-gate 		else if (nrx == 1 && hx < 0x43400000)
2337c478bd9Sstevel@tonic-gate 			*pe = 1;
2347c478bd9Sstevel@tonic-gate 		else
2357c478bd9Sstevel@tonic-gate 			*pe = 2;
2367c478bd9Sstevel@tonic-gate 		return (*x);
2377c478bd9Sstevel@tonic-gate 	} else if (hx < 0x3fe00000) {
2387c478bd9Sstevel@tonic-gate 		/* x < 1/2 */
2397c478bd9Sstevel@tonic-gate 		if (nrx > 1 && hx == 0x3fdfffff)
2407c478bd9Sstevel@tonic-gate 			*pe = (*x == halfdec)? 2 : 1;
2417c478bd9Sstevel@tonic-gate 		else
2427c478bd9Sstevel@tonic-gate 			*pe = 1;
2437c478bd9Sstevel@tonic-gate 		return (0.0);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	rx = (*x + two52) - two52;
2477c478bd9Sstevel@tonic-gate 	if (nrx == 0) {
2487c478bd9Sstevel@tonic-gate 		*pe = (rx == *x)? 0 : 1;
2497c478bd9Sstevel@tonic-gate 	} else {
2507c478bd9Sstevel@tonic-gate 		rmx = rx - *x;
2517c478bd9Sstevel@tonic-gate 		if (rmx < 0.0)
2527c478bd9Sstevel@tonic-gate 			rmx = -rmx;
2537c478bd9Sstevel@tonic-gate 		*pe = (nrx * twom53 * *x < half - rmx)? 1 : 2;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	return (rx);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Attempt to convert dd to a decimal record *pd according to the
2607c478bd9Sstevel@tonic-gate  * modes in *pm using double precision floating point.  Return zero
2617c478bd9Sstevel@tonic-gate  * and sets *ps to reflect any exceptions incurred if successful.
2627c478bd9Sstevel@tonic-gate  * Return a nonzero value if unsuccessful.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate int
__fast_double_to_decimal(double * dd,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)2657c478bd9Sstevel@tonic-gate __fast_double_to_decimal(double *dd, decimal_mode *pm, decimal_record *pd,
2667c478bd9Sstevel@tonic-gate     fp_exception_field_type *ps)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int			i, is, esum, eround, hd;
2697c478bd9Sstevel@tonic-gate 	double			dds;
2707c478bd9Sstevel@tonic-gate 	__ieee_flags_type	fb;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (pm->rd != fp_nearest)
2737c478bd9Sstevel@tonic-gate 		return (1);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (pm->df == fixed_form) {
2767c478bd9Sstevel@tonic-gate 		/* F format */
2777c478bd9Sstevel@tonic-gate 		if (pm->ndigits < 0 || pm->ndigits > __TBL_TENS_MAX)
2787c478bd9Sstevel@tonic-gate 			return (1);
2797c478bd9Sstevel@tonic-gate 		__get_ieee_flags(&fb);
2807c478bd9Sstevel@tonic-gate 		dds = __dabs(dd);
2817c478bd9Sstevel@tonic-gate 		esum = 0;
2827c478bd9Sstevel@tonic-gate 		if (pm->ndigits) {
2837c478bd9Sstevel@tonic-gate 			/* scale by a positive power of ten */
2847c478bd9Sstevel@tonic-gate 			if (pm->ndigits > __TBL_TENS_EXACT) {
2857c478bd9Sstevel@tonic-gate 				dds *= __tbl_tens[pm->ndigits];
2867c478bd9Sstevel@tonic-gate 				esum = 2;
2877c478bd9Sstevel@tonic-gate 			} else {
2887c478bd9Sstevel@tonic-gate 				dds = __mul_set(dds, __tbl_tens[pm->ndigits],
2897c478bd9Sstevel@tonic-gate 				    &eround);
2907c478bd9Sstevel@tonic-gate 				esum = eround;
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		if (dds > 2147483647999999744.0) {
2947c478bd9Sstevel@tonic-gate 			__set_ieee_flags(&fb);
2957c478bd9Sstevel@tonic-gate 			return (1);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		dds = __arint_set_n(&dds, esum, &eround);
2987c478bd9Sstevel@tonic-gate 		if (eround == 2) {
2997c478bd9Sstevel@tonic-gate 			/* error is too large to round reliably; punt */
3007c478bd9Sstevel@tonic-gate 			__set_ieee_flags(&fb);
3017c478bd9Sstevel@tonic-gate 			return (1);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		if (dds == 0.0) {
3047c478bd9Sstevel@tonic-gate 			is = (pm->ndigits > 0)? pm->ndigits : 1;
3057c478bd9Sstevel@tonic-gate 			for (i = 0; i < is; i++)
3067c478bd9Sstevel@tonic-gate 				pd->ds[i] = '0';
3077c478bd9Sstevel@tonic-gate 			pd->ds[is] = '\0';
3087c478bd9Sstevel@tonic-gate 			eround++;
3097c478bd9Sstevel@tonic-gate 		} else {
3107c478bd9Sstevel@tonic-gate 			is = __double_to_digits(dds, pd->ds, pm->ndigits);
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		pd->ndigits = is;
3137c478bd9Sstevel@tonic-gate 		pd->exponent = -pm->ndigits;
3147c478bd9Sstevel@tonic-gate 	} else {
3157c478bd9Sstevel@tonic-gate 		/* E format */
3167c478bd9Sstevel@tonic-gate 		if (pm->ndigits < 1 || pm->ndigits > 18)
3177c478bd9Sstevel@tonic-gate 			return (1);
3187c478bd9Sstevel@tonic-gate 		__get_ieee_flags(&fb);
3197c478bd9Sstevel@tonic-gate 		dds = __dabs(dd);
3207c478bd9Sstevel@tonic-gate 		/* find the decade containing dds */
3217c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
3227c478bd9Sstevel@tonic-gate 		hd = *(1+(int *)dd);
3237c478bd9Sstevel@tonic-gate #else
3247c478bd9Sstevel@tonic-gate 		hd = *(int *)dd;
3257c478bd9Sstevel@tonic-gate #endif
3267c478bd9Sstevel@tonic-gate 		hd = (hd >> 20) & 0x7ff;
3277c478bd9Sstevel@tonic-gate 		if (hd >= 0x400) {
3287c478bd9Sstevel@tonic-gate 			if (hd > 0x4e0)
3297c478bd9Sstevel@tonic-gate 				i = TBL_DECADE_MAX;
3307c478bd9Sstevel@tonic-gate 			else
3317c478bd9Sstevel@tonic-gate 				i = TBL_DECADE_MAX - ((0x4e0 - hd) >> 2);
3327c478bd9Sstevel@tonic-gate 		} else {
3337c478bd9Sstevel@tonic-gate 			if (hd < 0x358)
3347c478bd9Sstevel@tonic-gate 				i = 0;
3357c478bd9Sstevel@tonic-gate 			else
3367c478bd9Sstevel@tonic-gate 				i = TBL_DECADE_OFFSET - ((0x3ff - hd) >> 2);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		while (dds < tbl_decade[i])
3397c478bd9Sstevel@tonic-gate 			i--;
3407c478bd9Sstevel@tonic-gate 		/* determine the power of ten by which to scale */
3417c478bd9Sstevel@tonic-gate 		i = pm->ndigits - 1 - (i - TBL_DECADE_OFFSET);
3427c478bd9Sstevel@tonic-gate 		esum = 0;
3437c478bd9Sstevel@tonic-gate 		if (i > 0) {
3447c478bd9Sstevel@tonic-gate 			/* scale by a positive power of ten */
3457c478bd9Sstevel@tonic-gate 			if (i > __TBL_TENS_EXACT) {
3467c478bd9Sstevel@tonic-gate 				if (i > __TBL_TENS_MAX) {
3477c478bd9Sstevel@tonic-gate 					__set_ieee_flags(&fb);
3487c478bd9Sstevel@tonic-gate 					return (1);
3497c478bd9Sstevel@tonic-gate 				}
3507c478bd9Sstevel@tonic-gate 				dds *= __tbl_tens[i];
3517c478bd9Sstevel@tonic-gate 				esum = 2;
3527c478bd9Sstevel@tonic-gate 			} else {
3537c478bd9Sstevel@tonic-gate 				dds = __mul_set(dds, __tbl_tens[i], &eround);
3547c478bd9Sstevel@tonic-gate 				esum = eround;
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 		} else if (i < 0) {
3577c478bd9Sstevel@tonic-gate 			/* scale by a negative power of ten */
3587c478bd9Sstevel@tonic-gate 			if (-i > __TBL_TENS_EXACT) {
3597c478bd9Sstevel@tonic-gate 				if (-i > __TBL_TENS_MAX) {
3607c478bd9Sstevel@tonic-gate 					__set_ieee_flags(&fb);
3617c478bd9Sstevel@tonic-gate 					return (1);
3627c478bd9Sstevel@tonic-gate 				}
3637c478bd9Sstevel@tonic-gate 				dds /= __tbl_tens[-i];
3647c478bd9Sstevel@tonic-gate 				esum = 2;
3657c478bd9Sstevel@tonic-gate 			} else {
3667c478bd9Sstevel@tonic-gate 				dds = __div_set(dds, __tbl_tens[-i], &eround);
3677c478bd9Sstevel@tonic-gate 				esum = eround;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		dds = __arint_set_n(&dds, esum, &eround);
3717c478bd9Sstevel@tonic-gate 		if (eround == 2) {
3727c478bd9Sstevel@tonic-gate 			/* error is too large to round reliably; punt */
3737c478bd9Sstevel@tonic-gate 			__set_ieee_flags(&fb);
3747c478bd9Sstevel@tonic-gate 			return (1);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		is = __double_to_digits(dds, pd->ds, 1);
3777c478bd9Sstevel@tonic-gate 		if (is > pm->ndigits) {
3787c478bd9Sstevel@tonic-gate 			/*
3797c478bd9Sstevel@tonic-gate 			 * The result rounded up to the next larger power
3807c478bd9Sstevel@tonic-gate 			 * of ten; just discard the last zero and adjust
3817c478bd9Sstevel@tonic-gate 			 * the exponent.
3827c478bd9Sstevel@tonic-gate 			 */
3837c478bd9Sstevel@tonic-gate 			pd->ds[--is] = '\0';
3847c478bd9Sstevel@tonic-gate 			i--;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		pd->ndigits = is;
3877c478bd9Sstevel@tonic-gate 		pd->exponent = -i;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	*ps = (eround == 0)? 0 : (1 << fp_inexact);
3907c478bd9Sstevel@tonic-gate 	__set_ieee_flags(&fb);
3917c478bd9Sstevel@tonic-gate 	return (0);
3927c478bd9Sstevel@tonic-gate }
393