Copyright (C) 1998-2001 by Lucent Technologies
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
x = b->x;
xe = x + b->wds;
#ifdef Pack_32
do {
if (*x) {
--*x;
break;
}
*x++ = 0xffffffffUL;
}
while(x < xe);
#else
do {
y = *x - borrow;
borrow = (y & 0x10000) >> 16;
*x++ = y & 0xffff;
} while(borrow && x < xe);
#endif
}
static int
#ifdef KR_headers
all_on(b, n) CONST Bigint *b; int n;
#else
all_on(CONST Bigint *b, int n)
#endif
{
CONST ULong *x, *xe;
x = b->x;
xe = x + ((unsigned int)n >> kshift);
while(x < xe)
if ((*x++ & ALL_ON) != ALL_ON)
return 0;
if (n &= kmask)
return ((*x | (ALL_ON << n)) & ALL_ON) == ALL_ON;
return 1;
}
Bigint *
#ifdef KR_headers
set_ones(b, n) Bigint *b; int n;
#else
set_ones(Bigint *b, int n)
#endif
{
int k;
ULong *x, *xe;
k = (unsigned int)(n + ((1 << kshift) - 1)) >> kshift;
if (b->k < k) {
Bfree(b);
b = Balloc(k);
if (b == NULL)
return NULL;
}
k = (unsigned int)n >> kshift;
if (n &= kmask)
k++;
b->wds = k;
x = b->x;
xe = x + k;
while(x < xe)
*x++ = ALL_ON;
if (n)
x[-1] >>= ULbits - n;
return b;
}
static int
rvOK
#ifdef KR_headers
(d, fpi, expt, bits, exact, rd, irv)
U *d; CONST FPI *fpi; Long *expt; ULong *bits; int exact, rd, *irv;
#else
(U *d, CONST FPI *fpi, Long *expt, ULong *bits, int exact, int rd, int *irv)
#endif
{
Bigint *b;
ULong carry, inex, lostbits;
int bdif, e, j, k, k1, nb, rv;
carry = rv = 0;
b = d2b(dval(d), &e, &bdif);
bdif -= nb = fpi->nbits;
e += bdif;
if (bdif <= 0) {
if (exact)
goto trunc;
goto ret;
}
if (P == nb) {
if (
#ifndef IMPRECISE_INEXACT
exact &&
#endif
fpi->rounding ==
#ifdef RND_PRODQUOT
FPI_Round_near
#else
Flt_Rounds
#endif
) goto trunc;
goto ret;
}
switch(rd) {
case 1: /* round down (toward -Infinity) */
goto trunc;
case 2: /* round up (toward +Infinity) */
break;
default: /* round near */
k = bdif - 1;
if (!k) {
if (!exact)
goto ret;
if (b->x[0] & 2)
break;
goto trunc;
}
if (b->x[(unsigned int)k>>kshift] & ((ULong)1 << (k & kmask)))
break;
goto trunc;
}
/* "break" cases: round up 1 bit, then truncate; bdif > 0 */
carry = 1;
trunc:
inex = lostbits = 0;
if (bdif > 0) {
if ( (lostbits = any_on(b, bdif)) !=0)
inex = STRTOG_Inexlo;
rshift(b, bdif);
if (carry) {
inex = STRTOG_Inexhi;
b = increment(b);
if ( (j = nb & kmask) !=0)
j = ULbits - j;
if (hi0bits(b->x[b->wds - 1]) != j) {
if (!lostbits)
lostbits = b->x[0] & 1;
rshift(b, 1);
e++;
}
}
}
else if (bdif < 0) {
b = lshift(b, -bdif);
if (b == NULL)
return STRTOG_NoMemory;
}
if (e < fpi->emin) {
k = fpi->emin - e;
e = fpi->emin;
if (k > nb || fpi->sudden_underflow) {
b->wds = inex = 0;
*irv = STRTOG_Underflow | STRTOG_Inexlo;
}
else {
k1 = k - 1;
if (k1 > 0 && !lostbits)
lostbits = any_on(b, k1);
if (!lostbits && !exact)
goto ret;
lostbits |=
carry = b->x[(unsigned int)k1>>kshift] &
(1 << (k1 & kmask));
rshift(b, k);
*irv = STRTOG_Denormal;
if (carry) {
b = increment(b);
inex = STRTOG_Inexhi | STRTOG_Underflow;
}
else if (lostbits)
inex = STRTOG_Inexlo | STRTOG_Underflow;
}
}
else if (e > fpi->emax) {
e = fpi->emax + 1;
*irv = STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
#ifndef NO_ERRNO
errno = ERANGE;
#endif
b->wds = inex = 0;
}
*expt = e;
copybits(bits, nb, b);
*irv |= inex;
rv = 1;
ret:
Bfree(b);
return rv;
}
#ifndef VAX
static int
#ifdef KR_headers
mantbits(d) U *d;
#else
mantbits(U *d)
#endif
{
ULong L;
#ifdef VAX
L = word1(d) << 16 | word1(d) >> 16;
if (L)
#else
if ( (L = word1(d)) !=0)
#endif
return P - lo0bits(&L);
#ifdef VAX
L = word0(d) << 16 | word0(d) >> 16 | Exp_msk11;
#else
L = word0(d) | Exp_msk1;
#endif
return P - 32 - lo0bits(&L);
}
#endif /* !VAX */
int
strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *expt, ULong *bits,
locale_t loc)
{
int abe, abits, asub;
#ifdef INFNAN_CHECK
int decpt;
#endif
int bb0, bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, denorm;
int dsign, e, e1, e2, emin, esign, finished, i, inex, irv;
int j, k, nbits, nd, nd0, nf, nz, nz0, rd, rvbits, rve, rve1, sign;
int sudden_underflow = 0; /* pacify gcc */
CONST char *s, *s0, *s1;
double adj0, tol;
Long L;
U adj, rv;
ULong *b, *be, y, z;
Bigint *ab, *bb, *bb1, *bd, *bd0, *bs, *delta, *rvb, *rvb0;
#ifdef USE_LOCALE /*{{*/
char *decimalpoint = localeconv_l(loc)->decimal_point;
size_t dplen = strlen(decimalpoint);
#endif /*USE_LOCALE}}*/
e2 = 0; /* XXX gcc */
irv = STRTOG_Zero;
denorm = sign = nz0 = nz = 0;
dval(&rv) = 0.;
rvb = 0;
nbits = fpi->nbits;
for(s = s00;;s++) switch(*s) {
case '-':
sign = 1;
/* FALLTHROUGH */
case '+':
if (*++s)
goto break2;
/* FALLTHROUGH */
case 0:
sign = 0;
irv = STRTOG_NoNumber;
s = s00;
goto ret;
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
case ' ':
continue;
default:
goto break2;
}
break2:
if (*s == '0') {
#ifndef NO_HEX_FP
switch(s[1]) {
case 'x':
case 'X':
irv = gethex(&s, fpi, expt, &rvb, sign, loc);
if (irv == STRTOG_NoNumber) {
s = s00;
sign = 0;
}
goto ret;
}
#endif
nz0 = 1;
while(*++s == '0') ;
if (!*s)
goto ret;
}
sudden_underflow = fpi->sudden_underflow;
s0 = s;
y = z = 0;
#ifdef INFNAN_CHECK
decpt = 0;
#endif
for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
if (nd < 9)
y = 10*y + c - '0';
else if (nd < 16)
z = 10*z + c - '0';
nd0 = nd;
#ifdef USE_LOCALE
if (c == *decimalpoint) {
for(i = 1; decimalpoint[i]; ++i)
if (s[i] != decimalpoint[i])
goto dig_done;
s += i;
c = *s;
#else
if (c == '.') {
c = *++s;
#endif
#ifdef INFNAN_CHECK
decpt = 1;
#endif
if (!nd) {
for(; c == '0'; c = *++s)
nz++;
if (c > '0' && c <= '9') {
s0 = s;
nf += nz;
nz = 0;
goto have_dig;
}
goto dig_done;
}
for(; c >= '0' && c <= '9'; c = *++s) {
have_dig:
nz++;
if (c -= '0') {
nf += nz;
for(i = 1; i < nz; i++)
if (nd++ < 9)
y *= 10;
else if (nd <= DBL_DIG + 1)
z *= 10;
if (nd++ < 9)
y = 10*y + c;
else if (nd <= DBL_DIG + 1)
z = 10*z + c;
nz = 0;
}
}
}/*}*/
dig_done:
e = 0;
if (c == 'e' || c == 'E') {
if (!nd && !nz && !nz0) {
irv = STRTOG_NoNumber;
s = s00;
goto ret;
}
s00 = s;
esign = 0;
switch(c = *++s) {
case '-':
esign = 1;
/* FALLTHROUGH */
case '+':
c = *++s;
}
if (c >= '0' && c <= '9') {
while(c == '0')
c = *++s;
if (c > '0' && c <= '9') {
L = c - '0';
s1 = s;
while((c = *++s) >= '0' && c <= '9')
L = 10*L + c - '0';
if (s - s1 > 8 || L > 19999)
/* Avoid confusion from exponents
* so large that e might overflow.
*/
e = 19999; /* safe for 16 bit ints */
else
e = (int)L;
if (esign)
e = -e;
}
else
e = 0;
}
else
s = s00;
}
if (!nd) {
if (!nz && !nz0) {
#ifdef INFNAN_CHECK
/* Check for Nan and Infinity */
if (!decpt)
switch(c) {
case 'i':
case 'I':
if (match(&s,"nf")) {
--s;
if (!match(&s,"inity"))
++s;
irv = STRTOG_Infinite;
goto infnanexp;
}
break;
case 'n':
case 'N':
if (match(&s, "an")) {
irv = STRTOG_NaN;
*expt = fpi->emax + 1;
#ifndef No_Hex_NaN
if (*s == '(') /*)*/
irv = hexnan(&s, fpi, bits);
#endif
goto infnanexp;
}
}
#endif /* INFNAN_CHECK */
irv = STRTOG_NoNumber;
s = s00;
}
goto ret;
}
irv = STRTOG_Normal;
e1 = e -= nf;
rd = 0;
switch(fpi->rounding & 3) {
case FPI_Round_up:
rd = 2 - sign;
break;
case FPI_Round_zero:
rd = 1;
break;
case FPI_Round_down:
rd = 1 + sign;
}
/* Now we have nd0 digits, starting at s0, followed by a
* decimal point, followed by nd-nd0 digits. The number we're
* after is the integer represented by those digits times
* 10**e */
if (!nd0)
nd0 = nd;
k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
dval(&rv) = y;
if (k > 9)
dval(&rv) = tens[k - 9] * dval(&rv) + z;
bd0 = 0;
if (nbits <= P && nd <= DBL_DIG) {
if (!e) {
if (rvOK(&rv, fpi, expt, bits, 1, rd, &irv))
goto ret;
}
else if (e > 0) {
if (e <= Ten_pmax) {
#ifdef VAX
goto vax_ovfl_check;
#else
i = fivesbits[e] + mantbits(&rv) <= P;
/* rv = */ rounded_product(dval(&rv), tens[e]);
if (rvOK(&rv, fpi, expt, bits, i, rd, &irv))
goto ret;
e1 -= e;
goto rv_notOK;
#endif
}
i = DBL_DIG - nd;
if (e <= Ten_pmax + i) {
/* A fancier test would sometimes let us do
* this for larger i values.
*/
e2 = e - i;
e1 -= i;
dval(&rv) *= tens[i];
#ifdef VAX
/* VAX exponent range is so narrow we must
* worry about overflow here...
*/
vax_ovfl_check:
dval(&adj) = dval(&rv);
word0(&adj) -= P*Exp_msk1;
/* adj = */ rounded_product(dval(&adj), tens[e2]);
if ((word0(&adj) & Exp_mask)
> Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
goto rv_notOK;
word0(&adj) += P*Exp_msk1;
dval(&rv) = dval(&adj);
#else
/* rv = */ rounded_product(dval(&rv), tens[e2]);
#endif
if (rvOK(&rv, fpi, expt, bits, 0, rd, &irv))
goto ret;
e1 -= e2;
}
}
#ifndef Inaccurate_Divide
else if (e >= -Ten_pmax) {
/* rv = */ rounded_quotient(dval(&rv), tens[-e]);
if (rvOK(&rv, fpi, expt, bits, 0, rd, &irv))
goto ret;
e1 -= e;
}
#endif
}
rv_notOK:
e1 += nd - k;