2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Short cut for conversion from double precision to decimal
2N/A * floating point
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include <sys/types.h>
2N/A#include <sys/isa_defs.h>
2N/A#include "base_conversion.h"
2N/A
2N/A/*
2N/A * Powers of ten rounded up. If i is the largest index such that
2N/A * tbl_decade[i] <= x, then:
2N/A *
2N/A * if i == 0 then x < 10^-49
2N/A * else if i == TBL_DECADE_MAX then x >= 10^67
2N/A * else 10^(i-TBL_DECADE_OFFSET) <= x < 10^(i-TBL_DECADE_OFFSET+1)
2N/A */
2N/A
2N/A#define TBL_DECADE_OFFSET 50
2N/A#define TBL_DECADE_MAX 117
2N/A
2N/Astatic const double tbl_decade[TBL_DECADE_MAX + 1] = {
2N/A 0.0,
2N/A 1.00000000000000012631e-49, 1.00000000000000012631e-48,
2N/A 1.00000000000000009593e-47, 1.00000000000000002300e-46,
2N/A 1.00000000000000013968e-45, 1.00000000000000007745e-44,
2N/A 1.00000000000000007745e-43, 1.00000000000000003762e-42,
2N/A 1.00000000000000000576e-41, 1.00000000000000013321e-40,
2N/A 1.00000000000000009243e-39, 1.00000000000000009243e-38,
2N/A 1.00000000000000006632e-37, 1.00000000000000010809e-36,
2N/A 1.00000000000000000786e-35, 1.00000000000000014150e-34,
2N/A 1.00000000000000005597e-33, 1.00000000000000005597e-32,
2N/A 1.00000000000000008334e-31, 1.00000000000000008334e-30,
2N/A 1.00000000000000008334e-29, 1.00000000000000008334e-28,
2N/A 1.00000000000000003849e-27, 1.00000000000000003849e-26,
2N/A 1.00000000000000003849e-25, 1.00000000000000010737e-24,
2N/A 1.00000000000000010737e-23, 1.00000000000000004860e-22,
2N/A 1.00000000000000009562e-21, 1.00000000000000009562e-20,
2N/A 1.00000000000000009562e-19, 1.00000000000000007154e-18,
2N/A 1.00000000000000007154e-17, 1.00000000000000010236e-16,
2N/A 1.00000000000000007771e-15, 1.00000000000000015659e-14,
2N/A 1.00000000000000003037e-13, 1.00000000000000018184e-12,
2N/A 1.00000000000000010106e-11, 1.00000000000000003643e-10,
2N/A 1.00000000000000006228e-09, 1.00000000000000002092e-08,
2N/A 1.00000000000000008710e-07, 1.00000000000000016651e-06,
2N/A 1.00000000000000008180e-05, 1.00000000000000004792e-04,
2N/A 1.00000000000000002082e-03, 1.00000000000000002082e-02,
2N/A 1.00000000000000005551e-01, 1.00000000000000000000e+00,
2N/A 1.00000000000000000000e+01, 1.00000000000000000000e+02,
2N/A 1.00000000000000000000e+03, 1.00000000000000000000e+04,
2N/A 1.00000000000000000000e+05, 1.00000000000000000000e+06,
2N/A 1.00000000000000000000e+07, 1.00000000000000000000e+08,
2N/A 1.00000000000000000000e+09, 1.00000000000000000000e+10,
2N/A 1.00000000000000000000e+11, 1.00000000000000000000e+12,
2N/A 1.00000000000000000000e+13, 1.00000000000000000000e+14,
2N/A 1.00000000000000000000e+15, 1.00000000000000000000e+16,
2N/A 1.00000000000000000000e+17, 1.00000000000000000000e+18,
2N/A 1.00000000000000000000e+19, 1.00000000000000000000e+20,
2N/A 1.00000000000000000000e+21, 1.00000000000000000000e+22,
2N/A 1.00000000000000008389e+23, 1.00000000000000011744e+24,
2N/A 1.00000000000000009060e+25, 1.00000000000000004765e+26,
2N/A 1.00000000000000001329e+27, 1.00000000000000017821e+28,
2N/A 1.00000000000000009025e+29, 1.00000000000000001988e+30,
2N/A 1.00000000000000007618e+31, 1.00000000000000005366e+32,
2N/A 1.00000000000000008969e+33, 1.00000000000000006087e+34,
2N/A 1.00000000000000015310e+35, 1.00000000000000004242e+36,
2N/A 1.00000000000000007194e+37, 1.00000000000000016638e+38,
2N/A 1.00000000000000009082e+39, 1.00000000000000003038e+40,
2N/A 1.00000000000000000620e+41, 1.00000000000000004489e+42,
2N/A 1.00000000000000001394e+43, 1.00000000000000008821e+44,
2N/A 1.00000000000000008821e+45, 1.00000000000000011990e+46,
2N/A 1.00000000000000004385e+47, 1.00000000000000004385e+48,
2N/A 1.00000000000000007630e+49, 1.00000000000000007630e+50,
2N/A 1.00000000000000015937e+51, 1.00000000000000012614e+52,
2N/A 1.00000000000000020590e+53, 1.00000000000000007829e+54,
2N/A 1.00000000000000001024e+55, 1.00000000000000009190e+56,
2N/A 1.00000000000000004835e+57, 1.00000000000000008319e+58,
2N/A 1.00000000000000008319e+59, 1.00000000000000012779e+60,
2N/A 1.00000000000000009211e+61, 1.00000000000000003502e+62,
2N/A 1.00000000000000005786e+63, 1.00000000000000002132e+64,
2N/A 1.00000000000000010901e+65, 1.00000000000000013239e+66,
2N/A 1.00000000000000013239e+67
2N/A};
2N/A
2N/A/*
2N/A * Convert a positive double precision integer x <= 2147483647999999744
2N/A * (the largest double less than 2^31 * 10^9; this implementation works
2N/A * up to the largest double less than 2^25 * 10^12) to a string of ASCII
2N/A * decimal digits, adding leading zeroes so that the result has at least
2N/A * n digits. The string is terminated by a null byte, and its length
2N/A * is returned.
2N/A *
2N/A * This routine assumes round-to-nearest mode is in effect and any
2N/A * exceptions raised will be ignored.
2N/A */
2N/A
2N/A#define tenm4 tbl_decade[TBL_DECADE_OFFSET - 4]
2N/A#define ten4 tbl_decade[TBL_DECADE_OFFSET + 4]
2N/A#define tenm12 tbl_decade[TBL_DECADE_OFFSET - 12]
2N/A#define ten12 tbl_decade[TBL_DECADE_OFFSET + 12]
2N/A#define one tbl_decade[TBL_DECADE_OFFSET]
2N/A
2N/Astatic int
2N/A__double_to_digits(double x, char *s, int n)
2N/A{
2N/A double y;
2N/A int d[5], i, j;
2N/A char *ss, tmp[4];
2N/A
2N/A /* decompose x into four-digit chunks */
2N/A y = (int)(x * tenm12);
2N/A x -= y * ten12;
2N/A if (x < 0.0) {
2N/A y -= one;
2N/A x += ten12;
2N/A }
2N/A d[0] = (int)(y * tenm4);
2N/A d[1] = (int)(y - d[0] * ten4);
2N/A y = (int)(x * tenm4);
2N/A d[4] = (int)(x - y * ten4);
2N/A d[2] = (int)(y * tenm4);
2N/A d[3] = (int)(y - d[2] * ten4);
2N/A
2N/A /*
2N/A * Find the first nonzero chunk or the point at which to start
2N/A * converting so we have n digits, whichever comes first.
2N/A */
2N/A ss = s;
2N/A if (n > 20) {
2N/A for (j = 0; j < n - 20; j++)
2N/A *ss++ = '0';
2N/A i = 0;
2N/A } else {
2N/A for (i = 0; d[i] == 0 && n <= ((4 - i) << 2); i++)
2N/A ;
2N/A __four_digits_quick(d[i], tmp);
2N/A for (j = 0; tmp[j] == '0' && n <= ((4 - i) << 2) + 3 - j; j++)
2N/A ;
2N/A while (j < 4)
2N/A *ss++ = tmp[j++];
2N/A i++;
2N/A }
2N/A
2N/A /* continue converting four-digit chunks */
2N/A while (i < 5) {
2N/A __four_digits_quick(d[i], ss);
2N/A ss += 4;
2N/A i++;
2N/A }
2N/A
2N/A *ss = '\0';
2N/A return (ss - s);
2N/A}
2N/A
2N/A/*
2N/A * Round a positive double precision number *x to the nearest integer,
2N/A * returning the result and passing back an indication of accuracy in
2N/A * *pe. On entry, nrx is the number of rounding errors already com-
2N/A * mitted in forming *x. On exit, *pe is 0 if *x was already integral
2N/A * and exact, 1 if the result is the correctly rounded integer value
2N/A * but not exact, and 2 if error in *x precludes determining the cor-
2N/A * rectly rounded integer value (i.e., the error might be larger than
2N/A * 1/2 - |*x - rx|, where rx is the nearest integer to *x).
2N/A */
2N/A
2N/Astatic union {
2N/A unsigned int i[2];
2N/A double d;
2N/A} C[] = {
2N/A#ifdef _LITTLE_ENDIAN
2N/A { 0x00000000, 0x43300000 },
2N/A { 0x00000000, 0x3ca00000 },
2N/A { 0x00000000, 0x3fe00000 },
2N/A { 0xffffffff, 0x3fdfffff },
2N/A#else
2N/A { 0x43300000, 0x00000000 },
2N/A { 0x3ca00000, 0x00000000 },
2N/A { 0x3fe00000, 0x00000000 },
2N/A { 0x3fdfffff, 0xffffffff }, /* nextafter(1/2, 0) */
2N/A#endif
2N/A};
2N/A
2N/A#define two52 C[0].d
2N/A#define twom53 C[1].d
2N/A#define half C[2].d
2N/A#define halfdec C[3].d
2N/A
2N/Astatic double
2N/A__arint_set_n(double *x, int nrx, int *pe)
2N/A{
2N/A int hx;
2N/A double rx, rmx;
2N/A
2N/A#ifdef _LITTLE_ENDIAN
2N/A hx = *(1+(int *)x);
2N/A#else
2N/A hx = *(int *)x;
2N/A#endif
2N/A if (hx >= 0x43300000) {
2N/A /* x >= 2^52, so it's already integral */
2N/A if (nrx == 0)
2N/A *pe = 0;
2N/A else if (nrx == 1 && hx < 0x43400000)
2N/A *pe = 1;
2N/A else
2N/A *pe = 2;
2N/A return (*x);
2N/A } else if (hx < 0x3fe00000) {
2N/A /* x < 1/2 */
2N/A if (nrx > 1 && hx == 0x3fdfffff)
2N/A *pe = (*x == halfdec)? 2 : 1;
2N/A else
2N/A *pe = 1;
2N/A return (0.0);
2N/A }
2N/A
2N/A rx = (*x + two52) - two52;
2N/A if (nrx == 0) {
2N/A *pe = (rx == *x)? 0 : 1;
2N/A } else {
2N/A rmx = rx - *x;
2N/A if (rmx < 0.0)
2N/A rmx = -rmx;
2N/A *pe = (nrx * twom53 * *x < half - rmx)? 1 : 2;
2N/A }
2N/A return (rx);
2N/A}
2N/A
2N/A/*
2N/A * Attempt to convert dd to a decimal record *pd according to the
2N/A * modes in *pm using double precision floating point. Return zero
2N/A * and sets *ps to reflect any exceptions incurred if successful.
2N/A * Return a nonzero value if unsuccessful.
2N/A */
2N/Aint
2N/A__fast_double_to_decimal(double *dd, decimal_mode *pm, decimal_record *pd,
2N/A fp_exception_field_type *ps)
2N/A{
2N/A int i, is, esum, eround, hd;
2N/A double dds;
2N/A __ieee_flags_type fb;
2N/A
2N/A if (pm->rd != fp_nearest)
2N/A return (1);
2N/A
2N/A if (pm->df == fixed_form) {
2N/A /* F format */
2N/A if (pm->ndigits < 0 || pm->ndigits > __TBL_TENS_MAX)
2N/A return (1);
2N/A __get_ieee_flags(&fb);
2N/A dds = __dabs(dd);
2N/A esum = 0;
2N/A if (pm->ndigits) {
2N/A /* scale by a positive power of ten */
2N/A if (pm->ndigits > __TBL_TENS_EXACT) {
2N/A dds *= __tbl_tens[pm->ndigits];
2N/A esum = 2;
2N/A } else {
2N/A dds = __mul_set(dds, __tbl_tens[pm->ndigits],
2N/A &eround);
2N/A esum = eround;
2N/A }
2N/A }
2N/A if (dds > 2147483647999999744.0) {
2N/A __set_ieee_flags(&fb);
2N/A return (1);
2N/A }
2N/A dds = __arint_set_n(&dds, esum, &eround);
2N/A if (eround == 2) {
2N/A /* error is too large to round reliably; punt */
2N/A __set_ieee_flags(&fb);
2N/A return (1);
2N/A }
2N/A if (dds == 0.0) {
2N/A is = (pm->ndigits > 0)? pm->ndigits : 1;
2N/A for (i = 0; i < is; i++)
2N/A pd->ds[i] = '0';
2N/A pd->ds[is] = '\0';
2N/A eround++;
2N/A } else {
2N/A is = __double_to_digits(dds, pd->ds, pm->ndigits);
2N/A }
2N/A pd->ndigits = is;
2N/A pd->exponent = -pm->ndigits;
2N/A } else {
2N/A /* E format */
2N/A if (pm->ndigits < 1 || pm->ndigits > 18)
2N/A return (1);
2N/A __get_ieee_flags(&fb);
2N/A dds = __dabs(dd);
2N/A /* find the decade containing dds */
2N/A#ifdef _LITTLE_ENDIAN
2N/A hd = *(1+(int *)dd);
2N/A#else
2N/A hd = *(int *)dd;
2N/A#endif
2N/A hd = (hd >> 20) & 0x7ff;
2N/A if (hd >= 0x400) {
2N/A if (hd > 0x4e0)
2N/A i = TBL_DECADE_MAX;
2N/A else
2N/A i = TBL_DECADE_MAX - ((0x4e0 - hd) >> 2);
2N/A } else {
2N/A if (hd < 0x358)
2N/A i = 0;
2N/A else
2N/A i = TBL_DECADE_OFFSET - ((0x3ff - hd) >> 2);
2N/A }
2N/A while (dds < tbl_decade[i])
2N/A i--;
2N/A /* determine the power of ten by which to scale */
2N/A i = pm->ndigits - 1 - (i - TBL_DECADE_OFFSET);
2N/A esum = 0;
2N/A if (i > 0) {
2N/A /* scale by a positive power of ten */
2N/A if (i > __TBL_TENS_EXACT) {
2N/A if (i > __TBL_TENS_MAX) {
2N/A __set_ieee_flags(&fb);
2N/A return (1);
2N/A }
2N/A dds *= __tbl_tens[i];
2N/A esum = 2;
2N/A } else {
2N/A dds = __mul_set(dds, __tbl_tens[i], &eround);
2N/A esum = eround;
2N/A }
2N/A } else if (i < 0) {
2N/A /* scale by a negative power of ten */
2N/A if (-i > __TBL_TENS_EXACT) {
2N/A if (-i > __TBL_TENS_MAX) {
2N/A __set_ieee_flags(&fb);
2N/A return (1);
2N/A }
2N/A dds /= __tbl_tens[-i];
2N/A esum = 2;
2N/A } else {
2N/A dds = __div_set(dds, __tbl_tens[-i], &eround);
2N/A esum = eround;
2N/A }
2N/A }
2N/A dds = __arint_set_n(&dds, esum, &eround);
2N/A if (eround == 2) {
2N/A /* error is too large to round reliably; punt */
2N/A __set_ieee_flags(&fb);
2N/A return (1);
2N/A }
2N/A is = __double_to_digits(dds, pd->ds, 1);
2N/A if (is > pm->ndigits) {
2N/A /*
2N/A * The result rounded up to the next larger power
2N/A * of ten; just discard the last zero and adjust
2N/A * the exponent.
2N/A */
2N/A pd->ds[--is] = '\0';
2N/A i--;
2N/A }
2N/A pd->ndigits = is;
2N/A pd->exponent = -i;
2N/A }
2N/A *ps = (eround == 0)? 0 : (1 << fp_inexact);
2N/A __set_ieee_flags(&fb);
2N/A return (0);
2N/A}