/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Conversion from decimal to binary floating point
*/
#include "lint.h"
#include <stdlib.h>
#include "base_conversion.h"
/*
* Convert the integer part of a nonzero base-10^4 _big_float *pd
* to base 2^16 in **ppb. The converted value is accurate to nsig
* significant bits. On exit, *sticky is nonzero if *pd had a
* nonzero fractional part. If pd->exponent > 0 and **ppb is not
* large enough to hold the final converted value (i.e., the con-
* verted significand scaled by 10^pd->exponent), then on exit,
* *ppb will point to a newly allocated _big_float, which must be
* freed by the caller. (The number of significant bits we need
* should fit in pb, but __big_float_times_power may allocate new
* storage anyway because the exact product could require more than
* 16000 bits.)
*
* This routine does not check that **ppb is large enough to hold
* the result of converting the significand of *pd.
*/
static void
int *sticky)
{
int i, j, len, s;
unsigned int carry;
/* convert pd a digit at a time, most significant first */
len = 1;
/* multiply pb by 10^4 and add next digit */
for (j = 0; j < len; j++) {
* 10000;
carry >>= 16;
}
if (carry)
len = j;
}
} else {
len = 0;
}
/* convert any partial digit */
/* multiply pb by 10^s and add partial digit */
if (s == 1) {
s = carry % 1000;
for (j = 0; j < len; j++) {
* 10;
carry >>= 16;
}
} else if (s == 2) {
s = carry % 100;
for (j = 0; j < len; j++) {
* 100;
carry >>= 16;
}
} else {
s = carry % 10;
for (j = 0; j < len; j++) {
* 1000;
carry >>= 16;
}
}
if (carry)
len = j;
i--;
} else {
s = 0;
}
/* continue accumulating sticky flag */
while (i >= 0)
s |= pd->bsignificand[i--];
*sticky = s;
/* scale pb by 10^pd->exponent */
}
}
/*
* Convert the decimal_record *pd to an unpacked datum *px accurately
* enough that *px can be rounded correctly to sigbits significant bits.
* (We may assume sigbits <= 113.)
*/
static void
{
char *ds;
/* remove trailing zeroes */
exp++;
ndigs--;
}
if (ndigs < 1) {
/* nothing left */
return;
}
/* convert remaining digits to a base-10^4 _big_float */
d.bsize = _BIG_FLOAT_SIZE;
i = d.blength - 1;
switch (ids) {
case -1:
i--;
ids += 4;
break;
case -2:
i--;
ids += 4;
break;
case -3:
i--;
ids += 4;
break;
}
while (i >= 0) {
i--;
ids += 4;
}
pbd = &d;
powtwo = 0;
/* pre-scale to get the bits we want into the integer part */
if (exp < 0) {
/* i is a lower bound on log10(x) */
/*
* Scale by 2^(sigbits + 2 + u) where
* u is an upper bound on -log2(x).
*/
if (i < 0)
else if (i > 0)
/*
* Take sigdigits large enough to get
* all integral digits correct.
*/
}
}
/* convert to base 2^16 */
b.bsize = _BIG_FLOAT_SIZE;
pbb = &b;
/* adjust pbb->bexponent based on the scale factor above */
/* convert to unpacked */
ix = 0;
}
if (ix < 5) {
/* pad with zeroes */
if (i == 0)
while (ix < 5)
} else {
/* truncate and set a sticky bit if necessary */
while (i >= 0 && pbb->bsignificand[i] == 0)
i--;
if (i >= 0)
}
/* normalize so the most significant bit is set */
}
if (pbd != &d)
if (pbb != &b)
}
/*
* Convert a string s consisting of n <= 18 ASCII decimal digits
* to an integer value in double precision format, and set *pe
* to the number of rounding errors incurred (0 or 1).
*/
static double
{
int i, acc;
if (n <= 9) {
acc = s[0] - '0';
for (i = 1; i < n; i++) {
/* acc <- 10 * acc + next digit */
}
t = (double)acc;
*pe = 0;
} else {
acc = s[0] - '0';
for (i = 1; i < (n - 9); i++) {
/* acc <- 10 * acc + next digit */
}
for (i = n - 8; i < n; i++) {
/* acc <- 10 * acc + next digit */
}
/* add and indicate whether or not the sum is exact */
}
return (t);
}
static union {
int i[2];
double d;
} C[] = {
#ifdef _LITTLE_ENDIAN
{ 0x00000000, 0x3cc40000 },
#else
{ 0x3cc40000, 0x00000000 }, /* 5 * 2^-53 */
#endif
};
#define five2m53 C[0].d
static int
{
return (0);
rounded = 0;
} else {
rounded = 1;
n = 18;
}
/*
* exp must be in the range of the table, and the result
* must not underflow or overflow.
*/
return (0);
if (e != 0)
rounded = 1;
if (exp > 0) {
/* small positive exponent */
if (exp > __TBL_TENS_EXACT)
rounded = 1;
if (rounded) {
} else {
if (e)
rounded = 1;
}
} else if (exp < 0) {
/* small negative exponent */
if (-exp > __TBL_TENS_EXACT)
rounded = 1;
if (rounded) {
} else {
if (e)
rounded = 1;
}
}
/*
* At this point dds may have four rounding errors due to
* (i) truncation of pd->ds to 18 digits, (ii) inexact con-
* version of pd->ds to binary, (iii) scaling by a power of
* ten that is not exactly representable, and (iv) roundoff
* error in the multiplication. Below we will incur one more
* rounding error when we add or subtract delta and dds. We
* construct delta so that even after this last rounding error,
* ddsplus is an upper bound on the exact value and ddsminus
* is a lower bound. Then as long as both of these quantities
* round to the same single precision number, that number
* will be the correctly rounded single precision result.
* (If any rounding errors have been committed, then we must
* also be certain that the result can't be exact.)
*/
return (0);
if (rounded) {
/*
* If ddsminus <= f1 <= ddsplus, the result might be
* exact; we have to convert the long way to be sure.
*/
return (0);
} else {
}
return (1);
}
/*
* Attempt conversion to double using floating-point arithmetic.
* Return 1 if it works (at most one rounding error), 0 if it doesn't.
*/
static int
{
double dds;
int e;
return (0);
if (e != 0) {
return (0);
}
return (1);
}
/* PUBLIC FUNCTIONS */
/*
* The following routines convert the decimal record *pd to a floating
* point value *px observing the rounding mode specified in pm->rd and
* passing back any floating point exceptions that occur in *ps.
*
* pd->sign and pd->fpclass are always taken into account. pd->exponent
* and pd->ds are used when pd->fpclass is fp_normal or fp_subnormal.
* In these cases, pd->ds must contain a null-terminated string of one
* or more ASCII digits, the first of which is not zero, and pd->ndigits
* must equal the length of this string. If m is the integer represented
* by the string pd->ds, then *px will be set to a correctly rounded
* approximation to
*
* -1**(pd->sign) * m * 10**(pd->exponent)
*
* (If pd->more != 0 then additional nonzero digits are assumed to follow
* those in pd->ds, so m is effectively replaced by m + epsilon in the
* expression above.)
*
* For example, if pd->exponent == -2 and pd->ds holds "1234", then *px
* will be a correctly rounded approximation to 12.34.
*
* Note that the only mode that matters is the rounding direction pm->rd;
* pm->df and pm->ndigits are never used.
*/
/* maximum decimal exponent we need to consider */
void
{
unpacked u;
int i;
/* special values */
case fp_zero:
*ps = 0;
return;
case fp_infinity:
*ps = 0;
return;
case fp_quiet:
*ps = 0;
return;
case fp_signaling:
*ps = 0;
return;
}
/* numeric values */
ef = 0;
/* result must overflow */
u.exponent = 0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
/* result must underflow completely */
u.exponent = -0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
/* result may be in range */
if (ef != 0)
return;
}
}
if (ef != 0)
}
void
{
unpacked u;
int i;
/* special values */
case fp_zero:
kluge->f.significand2 = 0;
*ps = 0;
return;
case fp_infinity:
kluge->f.significand2 = 0;
*ps = 0;
return;
case fp_quiet:
*ps = 0;
return;
case fp_signaling:
*ps = 0;
return;
}
/* numeric values */
ef = 0;
/* result must overflow */
u.exponent = 0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
/* result must underflow completely */
u.exponent = -0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
/* result may be in range */
if (ef != 0)
return;
}
}
if (ef != 0)
}
void
{
unpacked u;
int i;
/* special values */
case fp_zero:
kluge->f.significand = 0;
kluge->f.significand2 = 0;
*ps = 0;
return;
case fp_infinity:
kluge->f.significand2 = 0;
*ps = 0;
return;
case fp_quiet:
*ps = 0;
return;
case fp_signaling:
*ps = 0;
return;
}
/* numeric values */
ef = 0;
/* result must overflow */
u.exponent = 0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
/* result must underflow completely */
u.exponent = -0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
/* result may be in range */
ef == 0) {
u.significand[0] = ((0x100000 |
for (i = 2; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
}
}
if (ef != 0)
}
void
{
unpacked u;
int i;
/* special values */
case fp_zero:
kluge->f.significand2 = 0;
kluge->f.significand3 = 0;
kluge->f.significand4 = 0;
*ps = 0;
return;
case fp_infinity:
kluge->f.significand2 = 0;
kluge->f.significand3 = 0;
kluge->f.significand4 = 0;
*ps = 0;
return;
case fp_quiet:
*ps = 0;
return;
case fp_signaling:
*ps = 0;
return;
}
/* numeric values */
ef = 0;
/* result must overflow */
u.exponent = 0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
/* result must underflow completely */
u.exponent = -0x000fffff;
u.significand[0] = 0x80000000;
for (i = 1; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
/* result may be in range */
ef == 0) {
u.significand[0] = ((0x100000 |
for (i = 2; i < UNPACKED_SIZE; i++)
u.significand[i] = 0;
} else {
}
}
if (ef != 0)
}