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
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Conversion from decimal to binary floating point
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
33*6812Sraf #include "lint.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include "base_conversion.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * Convert the integer part of a nonzero base-10^4 _big_float *pd
390Sstevel@tonic-gate * to base 2^16 in **ppb. The converted value is accurate to nsig
400Sstevel@tonic-gate * significant bits. On exit, *sticky is nonzero if *pd had a
410Sstevel@tonic-gate * nonzero fractional part. If pd->exponent > 0 and **ppb is not
420Sstevel@tonic-gate * large enough to hold the final converted value (i.e., the con-
430Sstevel@tonic-gate * verted significand scaled by 10^pd->exponent), then on exit,
440Sstevel@tonic-gate * *ppb will point to a newly allocated _big_float, which must be
450Sstevel@tonic-gate * freed by the caller. (The number of significant bits we need
460Sstevel@tonic-gate * should fit in pb, but __big_float_times_power may allocate new
470Sstevel@tonic-gate * storage anyway because the exact product could require more than
480Sstevel@tonic-gate * 16000 bits.)
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * This routine does not check that **ppb is large enough to hold
510Sstevel@tonic-gate * the result of converting the significand of *pd.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static void
__big_decimal_to_big_binary(_big_float * pd,int nsig,_big_float ** ppb,int * sticky)540Sstevel@tonic-gate __big_decimal_to_big_binary(_big_float *pd, int nsig, _big_float **ppb,
550Sstevel@tonic-gate int *sticky)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate _big_float *pb;
580Sstevel@tonic-gate int i, j, len, s;
590Sstevel@tonic-gate unsigned int carry;
600Sstevel@tonic-gate
610Sstevel@tonic-gate pb = *ppb;
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* convert pd a digit at a time, most significant first */
640Sstevel@tonic-gate if (pd->bexponent + ((pd->blength - 1) << 2) >= 0) {
650Sstevel@tonic-gate pb->bsignificand[0] = pd->bsignificand[pd->blength - 1];
660Sstevel@tonic-gate len = 1;
670Sstevel@tonic-gate for (i = pd->blength - 2; i >= 0 &&
680Sstevel@tonic-gate pd->bexponent + (i << 2) >= 0; i--) {
690Sstevel@tonic-gate /* multiply pb by 10^4 and add next digit */
700Sstevel@tonic-gate carry = pd->bsignificand[i];
710Sstevel@tonic-gate for (j = 0; j < len; j++) {
720Sstevel@tonic-gate carry += (unsigned int)pb->bsignificand[j]
730Sstevel@tonic-gate * 10000;
740Sstevel@tonic-gate pb->bsignificand[j] = carry & 0xffff;
750Sstevel@tonic-gate carry >>= 16;
760Sstevel@tonic-gate }
770Sstevel@tonic-gate if (carry)
780Sstevel@tonic-gate pb->bsignificand[j++] = carry;
790Sstevel@tonic-gate len = j;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate } else {
820Sstevel@tonic-gate i = pd->blength - 1;
830Sstevel@tonic-gate len = 0;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate /* convert any partial digit */
870Sstevel@tonic-gate if (i >= 0 && pd->bexponent + (i << 2) > -4) {
880Sstevel@tonic-gate s = pd->bexponent + (i << 2) + 4;
890Sstevel@tonic-gate /* multiply pb by 10^s and add partial digit */
900Sstevel@tonic-gate carry = pd->bsignificand[i];
910Sstevel@tonic-gate if (s == 1) {
920Sstevel@tonic-gate s = carry % 1000;
930Sstevel@tonic-gate carry = carry / 1000;
940Sstevel@tonic-gate for (j = 0; j < len; j++) {
950Sstevel@tonic-gate carry += (unsigned int)pb->bsignificand[j]
960Sstevel@tonic-gate * 10;
970Sstevel@tonic-gate pb->bsignificand[j] = carry & 0xffff;
980Sstevel@tonic-gate carry >>= 16;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate } else if (s == 2) {
1010Sstevel@tonic-gate s = carry % 100;
1020Sstevel@tonic-gate carry = carry / 100;
1030Sstevel@tonic-gate for (j = 0; j < len; j++) {
1040Sstevel@tonic-gate carry += (unsigned int)pb->bsignificand[j]
1050Sstevel@tonic-gate * 100;
1060Sstevel@tonic-gate pb->bsignificand[j] = carry & 0xffff;
1070Sstevel@tonic-gate carry >>= 16;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate } else {
1100Sstevel@tonic-gate s = carry % 10;
1110Sstevel@tonic-gate carry = carry / 10;
1120Sstevel@tonic-gate for (j = 0; j < len; j++) {
1130Sstevel@tonic-gate carry += (unsigned int)pb->bsignificand[j]
1140Sstevel@tonic-gate * 1000;
1150Sstevel@tonic-gate pb->bsignificand[j] = carry & 0xffff;
1160Sstevel@tonic-gate carry >>= 16;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate if (carry)
1200Sstevel@tonic-gate pb->bsignificand[j++] = carry;
1210Sstevel@tonic-gate len = j;
1220Sstevel@tonic-gate i--;
1230Sstevel@tonic-gate } else {
1240Sstevel@tonic-gate s = 0;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate pb->blength = len;
1280Sstevel@tonic-gate pb->bexponent = 0;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /* continue accumulating sticky flag */
1310Sstevel@tonic-gate while (i >= 0)
1320Sstevel@tonic-gate s |= pd->bsignificand[i--];
1330Sstevel@tonic-gate *sticky = s;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (pd->bexponent > 0) {
1360Sstevel@tonic-gate /* scale pb by 10^pd->exponent */
1370Sstevel@tonic-gate __big_float_times_power(pb, 10, pd->bexponent, nsig, ppb);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * Convert the decimal_record *pd to an unpacked datum *px accurately
1430Sstevel@tonic-gate * enough that *px can be rounded correctly to sigbits significant bits.
1440Sstevel@tonic-gate * (We may assume sigbits <= 113.)
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate static void
__decimal_to_unpacked(unpacked * px,decimal_record * pd,int sigbits)1470Sstevel@tonic-gate __decimal_to_unpacked(unpacked *px, decimal_record *pd, int sigbits)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate _big_float d, b, *pbd, *pbb;
1500Sstevel@tonic-gate char *ds;
1510Sstevel@tonic-gate int ids, i, ix, exp, ndigs;
1520Sstevel@tonic-gate int sticky, powtwo, sigdigits;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate px->sign = pd->sign;
1550Sstevel@tonic-gate px->fpclass = pd->fpclass;
1560Sstevel@tonic-gate ds = pd->ds;
1570Sstevel@tonic-gate ndigs = pd->ndigits;
1580Sstevel@tonic-gate exp = pd->exponent;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /* remove trailing zeroes */
1610Sstevel@tonic-gate while (ndigs > 0 && ds[ndigs - 1] == '0') {
1620Sstevel@tonic-gate exp++;
1630Sstevel@tonic-gate ndigs--;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate if (ndigs < 1) {
1660Sstevel@tonic-gate /* nothing left */
1670Sstevel@tonic-gate px->fpclass = fp_zero;
1680Sstevel@tonic-gate return;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /* convert remaining digits to a base-10^4 _big_float */
1720Sstevel@tonic-gate d.bsize = _BIG_FLOAT_SIZE;
1730Sstevel@tonic-gate d.bexponent = exp;
1740Sstevel@tonic-gate d.blength = (ndigs + 3) >> 2;
1750Sstevel@tonic-gate i = d.blength - 1;
1760Sstevel@tonic-gate ids = ndigs - (d.blength << 2);
1770Sstevel@tonic-gate switch (ids) {
1780Sstevel@tonic-gate case -1:
1790Sstevel@tonic-gate d.bsignificand[i] = 100 * ds[ids + 1] +
1800Sstevel@tonic-gate 10 * ds[ids + 2] + ds[ids + 3] - 111 * '0';
1810Sstevel@tonic-gate i--;
1820Sstevel@tonic-gate ids += 4;
1830Sstevel@tonic-gate break;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate case -2:
1860Sstevel@tonic-gate d.bsignificand[i] = 10 * ds[ids + 2] + ds[ids + 3] - 11 * '0';
1870Sstevel@tonic-gate i--;
1880Sstevel@tonic-gate ids += 4;
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate case -3:
1920Sstevel@tonic-gate d.bsignificand[i] = ds[ids + 3] - '0';
1930Sstevel@tonic-gate i--;
1940Sstevel@tonic-gate ids += 4;
1950Sstevel@tonic-gate break;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate while (i >= 0) {
1980Sstevel@tonic-gate d.bsignificand[i] = 1000 * ds[ids] + 100 * ds[ids + 1] +
1990Sstevel@tonic-gate 10 * ds[ids + 2] + ds[ids + 3] - 1111 * '0';
2000Sstevel@tonic-gate i--;
2010Sstevel@tonic-gate ids += 4;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate pbd = &d;
2050Sstevel@tonic-gate powtwo = 0;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /* pre-scale to get the bits we want into the integer part */
2080Sstevel@tonic-gate if (exp < 0) {
2090Sstevel@tonic-gate /* i is a lower bound on log10(x) */
2100Sstevel@tonic-gate i = exp + ndigs - 1;
2110Sstevel@tonic-gate if (i <= 0 || ((i * 217705) >> 16) < sigbits + 2) {
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * Scale by 2^(sigbits + 2 + u) where
2140Sstevel@tonic-gate * u is an upper bound on -log2(x).
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate powtwo = sigbits + 2;
2170Sstevel@tonic-gate if (i < 0)
2180Sstevel@tonic-gate powtwo += ((-i * 217706) + 65535) >> 16;
2190Sstevel@tonic-gate else if (i > 0)
2200Sstevel@tonic-gate powtwo -= (i * 217705) >> 16;
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * Take sigdigits large enough to get
2230Sstevel@tonic-gate * all integral digits correct.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate sigdigits = i + 1 + (((powtwo * 19729) + 65535) >> 16);
2260Sstevel@tonic-gate __big_float_times_power(&d, 2, powtwo, sigdigits, &pbd);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate /* convert to base 2^16 */
2310Sstevel@tonic-gate b.bsize = _BIG_FLOAT_SIZE;
2320Sstevel@tonic-gate pbb = &b;
2330Sstevel@tonic-gate __big_decimal_to_big_binary(pbd, sigbits + 2, &pbb, &sticky);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /* adjust pbb->bexponent based on the scale factor above */
2360Sstevel@tonic-gate pbb->bexponent -= powtwo;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /* convert to unpacked */
2390Sstevel@tonic-gate ix = 0;
2400Sstevel@tonic-gate for (i = pbb->blength - 1; i > 0 && ix < 5; i -= 2) {
2410Sstevel@tonic-gate px->significand[ix++] = (pbb->bsignificand[i] << 16) |
2420Sstevel@tonic-gate pbb->bsignificand[i - 1];
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate if (ix < 5) {
2450Sstevel@tonic-gate /* pad with zeroes */
2460Sstevel@tonic-gate if (i == 0)
2470Sstevel@tonic-gate px->significand[ix++] = pbb->bsignificand[i] << 16;
2480Sstevel@tonic-gate while (ix < 5)
2490Sstevel@tonic-gate px->significand[ix++] = 0;
2500Sstevel@tonic-gate } else {
2510Sstevel@tonic-gate /* truncate and set a sticky bit if necessary */
2520Sstevel@tonic-gate while (i >= 0 && pbb->bsignificand[i] == 0)
2530Sstevel@tonic-gate i--;
2540Sstevel@tonic-gate if (i >= 0)
2550Sstevel@tonic-gate px->significand[4] |= 1;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate if (sticky | pd->more)
2580Sstevel@tonic-gate px->significand[4] |= 1;
2590Sstevel@tonic-gate px->exponent = pbb->bexponent + (pbb->blength << 4) - 1;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* normalize so the most significant bit is set */
2620Sstevel@tonic-gate while (px->significand[0] < 0x80000000u) {
2630Sstevel@tonic-gate px->significand[0] = (px->significand[0] << 1) |
2640Sstevel@tonic-gate (px->significand[1] >> 31);
2650Sstevel@tonic-gate px->significand[1] = (px->significand[1] << 1) |
2660Sstevel@tonic-gate (px->significand[2] >> 31);
2670Sstevel@tonic-gate px->significand[2] = (px->significand[2] << 1) |
2680Sstevel@tonic-gate (px->significand[3] >> 31);
2690Sstevel@tonic-gate px->significand[3] = (px->significand[3] << 1) |
2700Sstevel@tonic-gate (px->significand[4] >> 31);
2710Sstevel@tonic-gate px->significand[4] <<= 1;
2720Sstevel@tonic-gate px->exponent--;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate if (pbd != &d)
2760Sstevel@tonic-gate (void) free((void *)pbd);
2770Sstevel@tonic-gate if (pbb != &b)
2780Sstevel@tonic-gate (void) free((void *)pbb);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Convert a string s consisting of n <= 18 ASCII decimal digits
2830Sstevel@tonic-gate * to an integer value in double precision format, and set *pe
2840Sstevel@tonic-gate * to the number of rounding errors incurred (0 or 1).
2850Sstevel@tonic-gate */
2860Sstevel@tonic-gate static double
__digits_to_double(char * s,int n,int * pe)2870Sstevel@tonic-gate __digits_to_double(char *s, int n, int *pe)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate int i, acc;
2900Sstevel@tonic-gate double t, th, tl;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate if (n <= 9) {
2930Sstevel@tonic-gate acc = s[0] - '0';
2940Sstevel@tonic-gate for (i = 1; i < n; i++) {
2950Sstevel@tonic-gate /* acc <- 10 * acc + next digit */
2960Sstevel@tonic-gate acc = (acc << 1) + (acc << 3) + s[i] - '0';
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate t = (double)acc;
2990Sstevel@tonic-gate *pe = 0;
3000Sstevel@tonic-gate } else {
3010Sstevel@tonic-gate acc = s[0] - '0';
3020Sstevel@tonic-gate for (i = 1; i < (n - 9); i++) {
3030Sstevel@tonic-gate /* acc <- 10 * acc + next digit */
3040Sstevel@tonic-gate acc = (acc << 1) + (acc << 3) + s[i] - '0';
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate th = 1.0e9 * (double)acc; /* this will be exact */
3070Sstevel@tonic-gate acc = s[n - 9] - '0';
3080Sstevel@tonic-gate for (i = n - 8; i < n; i++) {
3090Sstevel@tonic-gate /* acc <- 10 * acc + next digit */
3100Sstevel@tonic-gate acc = (acc << 1) + (acc << 3) + s[i] - '0';
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate tl = (double)acc;
3130Sstevel@tonic-gate /* add and indicate whether or not the sum is exact */
3140Sstevel@tonic-gate t = th + tl;
3150Sstevel@tonic-gate *pe = ((t - th) == tl)? 0 : 1;
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate return (t);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate static union {
3210Sstevel@tonic-gate int i[2];
3220Sstevel@tonic-gate double d;
3230Sstevel@tonic-gate } C[] = {
3240Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
3250Sstevel@tonic-gate { 0x00000000, 0x3cc40000 },
3260Sstevel@tonic-gate #else
3270Sstevel@tonic-gate { 0x3cc40000, 0x00000000 }, /* 5 * 2^-53 */
3280Sstevel@tonic-gate #endif
3290Sstevel@tonic-gate };
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate #define five2m53 C[0].d
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate static int
__fast_decimal_to_single(single * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)3340Sstevel@tonic-gate __fast_decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
3350Sstevel@tonic-gate fp_exception_field_type *ps)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate double dds, delta, ddsplus, ddsminus, df1;
3380Sstevel@tonic-gate int n, exp, rounded, e;
3390Sstevel@tonic-gate float f1, f2;
3400Sstevel@tonic-gate __ieee_flags_type fb;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (pm->rd != fp_nearest)
3430Sstevel@tonic-gate return (0);
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate exp = pd->exponent;
3460Sstevel@tonic-gate if (pd->ndigits <= 18) {
3470Sstevel@tonic-gate rounded = 0;
3480Sstevel@tonic-gate n = pd->ndigits;
3490Sstevel@tonic-gate } else {
3500Sstevel@tonic-gate rounded = 1;
3510Sstevel@tonic-gate n = 18;
3520Sstevel@tonic-gate exp += pd->ndigits - 18;
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate * exp must be in the range of the table, and the result
3560Sstevel@tonic-gate * must not underflow or overflow.
3570Sstevel@tonic-gate */
3580Sstevel@tonic-gate if (exp < -__TBL_TENS_MAX || exp + n < -36 || exp + n > 38)
3590Sstevel@tonic-gate return (0);
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate __get_ieee_flags(&fb);
3620Sstevel@tonic-gate dds = __digits_to_double(pd->ds, n, &e);
3630Sstevel@tonic-gate if (e != 0)
3640Sstevel@tonic-gate rounded = 1;
3650Sstevel@tonic-gate if (exp > 0) {
3660Sstevel@tonic-gate /* small positive exponent */
3670Sstevel@tonic-gate if (exp > __TBL_TENS_EXACT)
3680Sstevel@tonic-gate rounded = 1;
3690Sstevel@tonic-gate if (rounded) {
3700Sstevel@tonic-gate dds *= __tbl_tens[exp];
3710Sstevel@tonic-gate } else {
3720Sstevel@tonic-gate dds = __mul_set(dds, __tbl_tens[exp], &e);
3730Sstevel@tonic-gate if (e)
3740Sstevel@tonic-gate rounded = 1;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate } else if (exp < 0) {
3770Sstevel@tonic-gate /* small negative exponent */
3780Sstevel@tonic-gate if (-exp > __TBL_TENS_EXACT)
3790Sstevel@tonic-gate rounded = 1;
3800Sstevel@tonic-gate if (rounded) {
3810Sstevel@tonic-gate dds /= __tbl_tens[-exp];
3820Sstevel@tonic-gate } else {
3830Sstevel@tonic-gate dds = __div_set(dds, __tbl_tens[-exp], &e);
3840Sstevel@tonic-gate if (e)
3850Sstevel@tonic-gate rounded = 1;
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * At this point dds may have four rounding errors due to
3910Sstevel@tonic-gate * (i) truncation of pd->ds to 18 digits, (ii) inexact con-
3920Sstevel@tonic-gate * version of pd->ds to binary, (iii) scaling by a power of
3930Sstevel@tonic-gate * ten that is not exactly representable, and (iv) roundoff
3940Sstevel@tonic-gate * error in the multiplication. Below we will incur one more
3950Sstevel@tonic-gate * rounding error when we add or subtract delta and dds. We
3960Sstevel@tonic-gate * construct delta so that even after this last rounding error,
3970Sstevel@tonic-gate * ddsplus is an upper bound on the exact value and ddsminus
3980Sstevel@tonic-gate * is a lower bound. Then as long as both of these quantities
3990Sstevel@tonic-gate * round to the same single precision number, that number
4000Sstevel@tonic-gate * will be the correctly rounded single precision result.
4010Sstevel@tonic-gate * (If any rounding errors have been committed, then we must
4020Sstevel@tonic-gate * also be certain that the result can't be exact.)
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate delta = five2m53 * dds;
4050Sstevel@tonic-gate ddsplus = dds + delta;
4060Sstevel@tonic-gate ddsminus = dds - delta;
4070Sstevel@tonic-gate f1 = (float)(ddsplus);
4080Sstevel@tonic-gate f2 = (float)(ddsminus);
4090Sstevel@tonic-gate df1 = f1;
4100Sstevel@tonic-gate __set_ieee_flags(&fb);
4110Sstevel@tonic-gate if (f1 != f2)
4120Sstevel@tonic-gate return (0);
4130Sstevel@tonic-gate if (rounded) {
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate * If ddsminus <= f1 <= ddsplus, the result might be
4160Sstevel@tonic-gate * exact; we have to convert the long way to be sure.
4170Sstevel@tonic-gate */
4180Sstevel@tonic-gate if (ddsminus <= df1 && df1 <= ddsplus)
4190Sstevel@tonic-gate return (0);
4200Sstevel@tonic-gate *ps = (1 << fp_inexact);
4210Sstevel@tonic-gate } else {
4220Sstevel@tonic-gate *ps = (df1 == dds)? 0 : (1 << fp_inexact);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate *px = (pd->sign)? -f1 : f1;
4250Sstevel@tonic-gate return (1);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate * Attempt conversion to double using floating-point arithmetic.
4300Sstevel@tonic-gate * Return 1 if it works (at most one rounding error), 0 if it doesn't.
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate static int
__fast_decimal_to_double(double * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)4330Sstevel@tonic-gate __fast_decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
4340Sstevel@tonic-gate fp_exception_field_type *ps)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate double dds;
4370Sstevel@tonic-gate int e;
4380Sstevel@tonic-gate __ieee_flags_type fb;
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate if (pm->rd != fp_nearest || pd->ndigits > 18 || pd->exponent
4410Sstevel@tonic-gate > __TBL_TENS_EXACT || pd->exponent < -__TBL_TENS_EXACT)
4420Sstevel@tonic-gate return (0);
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate __get_ieee_flags(&fb);
4450Sstevel@tonic-gate dds = __digits_to_double(pd->ds, pd->ndigits, &e);
4460Sstevel@tonic-gate if (e != 0) {
4470Sstevel@tonic-gate __set_ieee_flags(&fb);
4480Sstevel@tonic-gate return (0);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate if (pd->exponent > 0)
4510Sstevel@tonic-gate dds = __mul_set(dds, __tbl_tens[pd->exponent], &e);
4520Sstevel@tonic-gate else if (pd->exponent < 0)
4530Sstevel@tonic-gate dds = __div_set(dds, __tbl_tens[-pd->exponent], &e);
4540Sstevel@tonic-gate *px = (pd->sign)? -dds : dds;
4550Sstevel@tonic-gate *ps = (e)? (1 << fp_inexact) : 0;
4560Sstevel@tonic-gate __set_ieee_flags(&fb);
4570Sstevel@tonic-gate return (1);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate /* PUBLIC FUNCTIONS */
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate * The following routines convert the decimal record *pd to a floating
4640Sstevel@tonic-gate * point value *px observing the rounding mode specified in pm->rd and
4650Sstevel@tonic-gate * passing back any floating point exceptions that occur in *ps.
4660Sstevel@tonic-gate *
4670Sstevel@tonic-gate * pd->sign and pd->fpclass are always taken into account. pd->exponent
4680Sstevel@tonic-gate * and pd->ds are used when pd->fpclass is fp_normal or fp_subnormal.
4690Sstevel@tonic-gate * In these cases, pd->ds must contain a null-terminated string of one
4700Sstevel@tonic-gate * or more ASCII digits, the first of which is not zero, and pd->ndigits
4710Sstevel@tonic-gate * must equal the length of this string. If m is the integer represented
4720Sstevel@tonic-gate * by the string pd->ds, then *px will be set to a correctly rounded
4730Sstevel@tonic-gate * approximation to
4740Sstevel@tonic-gate *
4750Sstevel@tonic-gate * -1**(pd->sign) * m * 10**(pd->exponent)
4760Sstevel@tonic-gate *
4770Sstevel@tonic-gate * (If pd->more != 0 then additional nonzero digits are assumed to follow
4780Sstevel@tonic-gate * those in pd->ds, so m is effectively replaced by m + epsilon in the
4790Sstevel@tonic-gate * expression above.)
4800Sstevel@tonic-gate *
4810Sstevel@tonic-gate * For example, if pd->exponent == -2 and pd->ds holds "1234", then *px
4820Sstevel@tonic-gate * will be a correctly rounded approximation to 12.34.
4830Sstevel@tonic-gate *
4840Sstevel@tonic-gate * Note that the only mode that matters is the rounding direction pm->rd;
4850Sstevel@tonic-gate * pm->df and pm->ndigits are never used.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate /* maximum decimal exponent we need to consider */
4890Sstevel@tonic-gate #define SINGLE_MAXE 47
4900Sstevel@tonic-gate #define DOUBLE_MAXE 326
4910Sstevel@tonic-gate #define EXTENDED_MAXE 4968
4920Sstevel@tonic-gate #define QUAD_MAXE 4968
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate void
decimal_to_single(single * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)4950Sstevel@tonic-gate decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
4960Sstevel@tonic-gate fp_exception_field_type *ps)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate single_equivalence *kluge;
4990Sstevel@tonic-gate unpacked u;
5000Sstevel@tonic-gate fp_exception_field_type ef;
5010Sstevel@tonic-gate int i;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /* special values */
5040Sstevel@tonic-gate kluge = (single_equivalence *)px;
5050Sstevel@tonic-gate switch (pd->fpclass) {
5060Sstevel@tonic-gate case fp_zero:
5070Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5080Sstevel@tonic-gate kluge->f.msw.exponent = 0;
5090Sstevel@tonic-gate kluge->f.msw.significand = 0;
5100Sstevel@tonic-gate *ps = 0;
5110Sstevel@tonic-gate return;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate case fp_infinity:
5140Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5150Sstevel@tonic-gate kluge->f.msw.exponent = 0xff;
5160Sstevel@tonic-gate kluge->f.msw.significand = 0;
5170Sstevel@tonic-gate *ps = 0;
5180Sstevel@tonic-gate return;
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate case fp_quiet:
5210Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5220Sstevel@tonic-gate kluge->f.msw.exponent = 0xff;
5230Sstevel@tonic-gate kluge->f.msw.significand = 0x7fffff;
5240Sstevel@tonic-gate *ps = 0;
5250Sstevel@tonic-gate return;
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate case fp_signaling:
5280Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5290Sstevel@tonic-gate kluge->f.msw.exponent = 0xff;
5300Sstevel@tonic-gate kluge->f.msw.significand = 0x3fffff;
5310Sstevel@tonic-gate *ps = 0;
5320Sstevel@tonic-gate return;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate /* numeric values */
5360Sstevel@tonic-gate ef = 0;
5370Sstevel@tonic-gate if (pd->exponent + pd->ndigits > SINGLE_MAXE) {
5380Sstevel@tonic-gate /* result must overflow */
5390Sstevel@tonic-gate u.sign = (pd->sign != 0);
5400Sstevel@tonic-gate u.fpclass = fp_normal;
5410Sstevel@tonic-gate u.exponent = 0x000fffff;
5420Sstevel@tonic-gate u.significand[0] = 0x80000000;
5430Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
5440Sstevel@tonic-gate u.significand[i] = 0;
5450Sstevel@tonic-gate } else if (pd->exponent + pd->ndigits < -SINGLE_MAXE) {
5460Sstevel@tonic-gate /* result must underflow completely */
5470Sstevel@tonic-gate u.sign = (pd->sign != 0);
5480Sstevel@tonic-gate u.fpclass = fp_normal;
5490Sstevel@tonic-gate u.exponent = -0x000fffff;
5500Sstevel@tonic-gate u.significand[0] = 0x80000000;
5510Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
5520Sstevel@tonic-gate u.significand[i] = 0;
5530Sstevel@tonic-gate } else {
5540Sstevel@tonic-gate /* result may be in range */
5550Sstevel@tonic-gate if (__fast_decimal_to_single(px, pm, pd, &ef) == 1) {
5560Sstevel@tonic-gate *ps = ef;
5570Sstevel@tonic-gate if (ef != 0)
5580Sstevel@tonic-gate __base_conversion_set_exception(ef);
5590Sstevel@tonic-gate return;
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate __decimal_to_unpacked(&u, pd, 24);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate __pack_single(&u, px, pm->rd, &ef);
5640Sstevel@tonic-gate *ps = ef;
5650Sstevel@tonic-gate if (ef != 0)
5660Sstevel@tonic-gate __base_conversion_set_exception(ef);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate void
decimal_to_double(double * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)5700Sstevel@tonic-gate decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
5710Sstevel@tonic-gate fp_exception_field_type *ps)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate double_equivalence *kluge;
5740Sstevel@tonic-gate unpacked u;
5750Sstevel@tonic-gate fp_exception_field_type ef;
5760Sstevel@tonic-gate int i;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /* special values */
5790Sstevel@tonic-gate kluge = (double_equivalence *)px;
5800Sstevel@tonic-gate switch (pd->fpclass) {
5810Sstevel@tonic-gate case fp_zero:
5820Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5830Sstevel@tonic-gate kluge->f.msw.exponent = 0;
5840Sstevel@tonic-gate kluge->f.msw.significand = 0;
5850Sstevel@tonic-gate kluge->f.significand2 = 0;
5860Sstevel@tonic-gate *ps = 0;
5870Sstevel@tonic-gate return;
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate case fp_infinity:
5900Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5910Sstevel@tonic-gate kluge->f.msw.exponent = 0x7ff;
5920Sstevel@tonic-gate kluge->f.msw.significand = 0;
5930Sstevel@tonic-gate kluge->f.significand2 = 0;
5940Sstevel@tonic-gate *ps = 0;
5950Sstevel@tonic-gate return;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate case fp_quiet:
5980Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
5990Sstevel@tonic-gate kluge->f.msw.exponent = 0x7ff;
6000Sstevel@tonic-gate kluge->f.msw.significand = 0xfffff;
6010Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
6020Sstevel@tonic-gate *ps = 0;
6030Sstevel@tonic-gate return;
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate case fp_signaling:
6060Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
6070Sstevel@tonic-gate kluge->f.msw.exponent = 0x7ff;
6080Sstevel@tonic-gate kluge->f.msw.significand = 0x7ffff;
6090Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
6100Sstevel@tonic-gate *ps = 0;
6110Sstevel@tonic-gate return;
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate /* numeric values */
6150Sstevel@tonic-gate ef = 0;
6160Sstevel@tonic-gate if (pd->exponent + pd->ndigits > DOUBLE_MAXE) {
6170Sstevel@tonic-gate /* result must overflow */
6180Sstevel@tonic-gate u.sign = (pd->sign != 0);
6190Sstevel@tonic-gate u.fpclass = fp_normal;
6200Sstevel@tonic-gate u.exponent = 0x000fffff;
6210Sstevel@tonic-gate u.significand[0] = 0x80000000;
6220Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
6230Sstevel@tonic-gate u.significand[i] = 0;
6240Sstevel@tonic-gate } else if (pd->exponent + pd->ndigits < -DOUBLE_MAXE) {
6250Sstevel@tonic-gate /* result must underflow completely */
6260Sstevel@tonic-gate u.sign = (pd->sign != 0);
6270Sstevel@tonic-gate u.fpclass = fp_normal;
6280Sstevel@tonic-gate u.exponent = -0x000fffff;
6290Sstevel@tonic-gate u.significand[0] = 0x80000000;
6300Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
6310Sstevel@tonic-gate u.significand[i] = 0;
6320Sstevel@tonic-gate } else {
6330Sstevel@tonic-gate /* result may be in range */
6340Sstevel@tonic-gate if (__fast_decimal_to_double(px, pm, pd, &ef) == 1) {
6350Sstevel@tonic-gate *ps = ef;
6360Sstevel@tonic-gate if (ef != 0)
6370Sstevel@tonic-gate __base_conversion_set_exception(ef);
6380Sstevel@tonic-gate return;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate __decimal_to_unpacked(&u, pd, 53);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate __pack_double(&u, px, pm->rd, &ef);
6430Sstevel@tonic-gate *ps = ef;
6440Sstevel@tonic-gate if (ef != 0)
6450Sstevel@tonic-gate __base_conversion_set_exception(ef);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate void
decimal_to_extended(extended * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)6490Sstevel@tonic-gate decimal_to_extended(extended *px, decimal_mode *pm, decimal_record *pd,
6500Sstevel@tonic-gate fp_exception_field_type *ps)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate extended_equivalence *kluge;
6530Sstevel@tonic-gate unpacked u;
6540Sstevel@tonic-gate double_equivalence dd;
6550Sstevel@tonic-gate fp_exception_field_type ef;
6560Sstevel@tonic-gate int i;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /* special values */
6590Sstevel@tonic-gate kluge = (extended_equivalence *)px;
6600Sstevel@tonic-gate switch (pd->fpclass) {
6610Sstevel@tonic-gate case fp_zero:
6620Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
6630Sstevel@tonic-gate kluge->f.msw.exponent = 0;
6640Sstevel@tonic-gate kluge->f.significand = 0;
6650Sstevel@tonic-gate kluge->f.significand2 = 0;
6660Sstevel@tonic-gate *ps = 0;
6670Sstevel@tonic-gate return;
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate case fp_infinity:
6700Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
6710Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
6720Sstevel@tonic-gate kluge->f.significand = 0x80000000;
6730Sstevel@tonic-gate kluge->f.significand2 = 0;
6740Sstevel@tonic-gate *ps = 0;
6750Sstevel@tonic-gate return;
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate case fp_quiet:
6780Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
6790Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
6800Sstevel@tonic-gate kluge->f.significand = 0xffffffff;
6810Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
6820Sstevel@tonic-gate *ps = 0;
6830Sstevel@tonic-gate return;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate case fp_signaling:
6860Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
6870Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
6880Sstevel@tonic-gate kluge->f.significand = 0xbfffffff;
6890Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
6900Sstevel@tonic-gate *ps = 0;
6910Sstevel@tonic-gate return;
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate /* numeric values */
6950Sstevel@tonic-gate ef = 0;
6960Sstevel@tonic-gate if (pd->exponent + pd->ndigits > EXTENDED_MAXE) {
6970Sstevel@tonic-gate /* result must overflow */
6980Sstevel@tonic-gate u.sign = (pd->sign != 0);
6990Sstevel@tonic-gate u.fpclass = fp_normal;
7000Sstevel@tonic-gate u.exponent = 0x000fffff;
7010Sstevel@tonic-gate u.significand[0] = 0x80000000;
7020Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
7030Sstevel@tonic-gate u.significand[i] = 0;
7040Sstevel@tonic-gate } else if (pd->exponent + pd->ndigits < -EXTENDED_MAXE) {
7050Sstevel@tonic-gate /* result must underflow completely */
7060Sstevel@tonic-gate u.sign = (pd->sign != 0);
7070Sstevel@tonic-gate u.fpclass = fp_normal;
7080Sstevel@tonic-gate u.exponent = -0x000fffff;
7090Sstevel@tonic-gate u.significand[0] = 0x80000000;
7100Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
7110Sstevel@tonic-gate u.significand[i] = 0;
7120Sstevel@tonic-gate } else {
7130Sstevel@tonic-gate /* result may be in range */
7140Sstevel@tonic-gate if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
7150Sstevel@tonic-gate ef == 0) {
7160Sstevel@tonic-gate u.sign = dd.f.msw.sign;
7170Sstevel@tonic-gate u.fpclass = fp_normal;
7180Sstevel@tonic-gate u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
7190Sstevel@tonic-gate u.significand[0] = ((0x100000 |
7200Sstevel@tonic-gate dd.f.msw.significand) << 11) |
7210Sstevel@tonic-gate (dd.f.significand2 >> 21);
7220Sstevel@tonic-gate u.significand[1] = dd.f.significand2 << 11;
7230Sstevel@tonic-gate for (i = 2; i < UNPACKED_SIZE; i++)
7240Sstevel@tonic-gate u.significand[i] = 0;
7250Sstevel@tonic-gate } else {
7260Sstevel@tonic-gate __decimal_to_unpacked(&u, pd, 64);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate __pack_extended(&u, px, pm->rd, &ef);
7300Sstevel@tonic-gate *ps = ef;
7310Sstevel@tonic-gate if (ef != 0)
7320Sstevel@tonic-gate __base_conversion_set_exception(ef);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate void
decimal_to_quadruple(quadruple * px,decimal_mode * pm,decimal_record * pd,fp_exception_field_type * ps)7360Sstevel@tonic-gate decimal_to_quadruple(quadruple *px, decimal_mode *pm, decimal_record *pd,
7370Sstevel@tonic-gate fp_exception_field_type *ps)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate quadruple_equivalence *kluge;
7400Sstevel@tonic-gate unpacked u;
7410Sstevel@tonic-gate double_equivalence dd;
7420Sstevel@tonic-gate fp_exception_field_type ef;
7430Sstevel@tonic-gate int i;
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate /* special values */
7460Sstevel@tonic-gate kluge = (quadruple_equivalence *)px;
7470Sstevel@tonic-gate switch (pd->fpclass) {
7480Sstevel@tonic-gate case fp_zero:
7490Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
7500Sstevel@tonic-gate kluge->f.msw.exponent = 0;
7510Sstevel@tonic-gate kluge->f.msw.significand = 0;
7520Sstevel@tonic-gate kluge->f.significand2 = 0;
7530Sstevel@tonic-gate kluge->f.significand3 = 0;
7540Sstevel@tonic-gate kluge->f.significand4 = 0;
7550Sstevel@tonic-gate *ps = 0;
7560Sstevel@tonic-gate return;
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate case fp_infinity:
7590Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
7600Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
7610Sstevel@tonic-gate kluge->f.msw.significand = 0;
7620Sstevel@tonic-gate kluge->f.significand2 = 0;
7630Sstevel@tonic-gate kluge->f.significand3 = 0;
7640Sstevel@tonic-gate kluge->f.significand4 = 0;
7650Sstevel@tonic-gate *ps = 0;
7660Sstevel@tonic-gate return;
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate case fp_quiet:
7690Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
7700Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
7710Sstevel@tonic-gate kluge->f.msw.significand = 0xffff;
7720Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
7730Sstevel@tonic-gate kluge->f.significand3 = 0xffffffff;
7740Sstevel@tonic-gate kluge->f.significand4 = 0xffffffff;
7750Sstevel@tonic-gate *ps = 0;
7760Sstevel@tonic-gate return;
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate case fp_signaling:
7790Sstevel@tonic-gate kluge->f.msw.sign = (pd->sign)? 1 : 0;
7800Sstevel@tonic-gate kluge->f.msw.exponent = 0x7fff;
7810Sstevel@tonic-gate kluge->f.msw.significand = 0x7fff;
7820Sstevel@tonic-gate kluge->f.significand2 = 0xffffffff;
7830Sstevel@tonic-gate kluge->f.significand3 = 0xffffffff;
7840Sstevel@tonic-gate kluge->f.significand4 = 0xffffffff;
7850Sstevel@tonic-gate *ps = 0;
7860Sstevel@tonic-gate return;
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /* numeric values */
7900Sstevel@tonic-gate ef = 0;
7910Sstevel@tonic-gate if (pd->exponent + pd->ndigits > QUAD_MAXE) {
7920Sstevel@tonic-gate /* result must overflow */
7930Sstevel@tonic-gate u.sign = (pd->sign != 0);
7940Sstevel@tonic-gate u.fpclass = fp_normal;
7950Sstevel@tonic-gate u.exponent = 0x000fffff;
7960Sstevel@tonic-gate u.significand[0] = 0x80000000;
7970Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
7980Sstevel@tonic-gate u.significand[i] = 0;
7990Sstevel@tonic-gate } else if (pd->exponent + pd->ndigits < -QUAD_MAXE) {
8000Sstevel@tonic-gate /* result must underflow completely */
8010Sstevel@tonic-gate u.sign = (pd->sign != 0);
8020Sstevel@tonic-gate u.fpclass = fp_normal;
8030Sstevel@tonic-gate u.exponent = -0x000fffff;
8040Sstevel@tonic-gate u.significand[0] = 0x80000000;
8050Sstevel@tonic-gate for (i = 1; i < UNPACKED_SIZE; i++)
8060Sstevel@tonic-gate u.significand[i] = 0;
8070Sstevel@tonic-gate } else {
8080Sstevel@tonic-gate /* result may be in range */
8090Sstevel@tonic-gate if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
8100Sstevel@tonic-gate ef == 0) {
8110Sstevel@tonic-gate u.sign = dd.f.msw.sign;
8120Sstevel@tonic-gate u.fpclass = fp_normal;
8130Sstevel@tonic-gate u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
8140Sstevel@tonic-gate u.significand[0] = ((0x100000 |
8150Sstevel@tonic-gate dd.f.msw.significand) << 11) |
8160Sstevel@tonic-gate (dd.f.significand2 >> 21);
8170Sstevel@tonic-gate u.significand[1] = dd.f.significand2 << 11;
8180Sstevel@tonic-gate for (i = 2; i < UNPACKED_SIZE; i++)
8190Sstevel@tonic-gate u.significand[i] = 0;
8200Sstevel@tonic-gate } else {
8210Sstevel@tonic-gate __decimal_to_unpacked(&u, pd, 113);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate __pack_quadruple(&u, px, pm->rd, &ef);
8250Sstevel@tonic-gate *ps = ef;
8260Sstevel@tonic-gate if (ef != 0)
8270Sstevel@tonic-gate __base_conversion_set_exception(ef);
8280Sstevel@tonic-gate }
829