/* $NetBSD: strtodg.c,v 1.5.14.1 2008/04/08 21:10:55 jdc Exp $ */
/****************************************************************
The author of this software is David M. Gay.
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.
****************************************************************/
/* Please send bug reports to David M. Gay (dmg at acm dot org,
* with " at " changed at "@" and " dot " changed to "."). */
#include <LibConfig.h>
#include "gdtoaimp.h"
#ifdef USE_LOCALE
#include "locale.h"
#endif
#if defined(_MSC_VER)
// Disable warnings about assignment within conditional expressions.
#endif
static CONST int
24, 26, 28, 31, 33, 35, 38, 40, 42, 45,
47, 49, 52
#ifdef VAX
, 54, 56
#endif
};
Bigint *
#ifdef KR_headers
#else
#endif
{
#ifdef Pack_16
#endif
x = b->x;
#ifdef Pack_32
do {
if (*x < (ULong)0xffffffffL) {
++*x;
return b;
}
*x++ = 0;
} while(x < xe);
#else
do {
y = *x + carry;
carry = y >> 16;
*x++ = y & 0xffff;
if (!carry)
return b;
} while(x < xe);
if (carry)
#endif
{
return NULL;
Bfree(b);
b = b1;
}
b->x[b->wds++] = 1;
}
return b;
}
int
#ifdef KR_headers
#else
#endif
{
#ifdef Pack_16
#endif
x = b->x;
#ifdef Pack_32
do {
if (*x) {
--*x;
break;
}
*x++ = 0xffffffffUL;
}
while(x < xe);
#else
do {
y = *x - borrow;
*x++ = y & 0xffff;
#endif
return STRTOG_Inexlo;
}
static int
#ifdef KR_headers
#else
#endif
{
x = b->x;
while(x < xe)
return 0;
if (n &= kmask)
return 1;
}
Bigint *
#ifdef KR_headers
#else
#endif
{
int k;
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
#ifdef KR_headers
#else
#endif
{
Bigint *b;
e += bdif;
if (bdif <= 0) {
if (exact)
goto trunc;
goto ret;
}
if (P == nb) {
if (
#ifndef IMPRECISE_INEXACT
exact &&
#endif
#ifdef RND_PRODQUOT
#else
#endif
) goto trunc;
goto ret;
}
switch(rd) {
case 1:
goto trunc;
case 2:
break;
default: /* round near */
k = bdif - 1;
if (!k) {
if (!exact)
goto ret;
if (b->x[0] & 2)
break;
goto trunc;
}
break;
goto trunc;
}
/* "break" cases: round up 1 bit, then truncate; bdif > 0 */
carry = 1;
if (bdif > 0) {
if (carry) {
b = increment(b);
j = ULbits - j;
if (!lostbits)
lostbits = b->x[0] & 1;
rshift(b, 1);
e++;
}
}
}
else if (bdif < 0)
}
else {
k1 = k - 1;
goto ret;
lostbits |=
rshift(b, k);
*irv = STRTOG_Denormal;
if (carry) {
b = increment(b);
}
else if (lostbits)
}
}
#ifndef NO_ERRNO
#endif
}
*expt = e;
rv = 1;
ret:
Bfree(b);
return rv;
}
#ifndef VAX
static int
#ifdef KR_headers
mantbits(d) double d;
#else
mantbits(double d)
#endif
{
ULong L;
#ifdef VAX
if (L)
#else
if ( (L = word1(d)) !=0)
#endif
return P - lo0bits(&L);
#ifdef VAX
#else
#endif
return P - 32 - lo0bits(&L);
}
#endif /* !VAX */
int
#ifdef KR_headers
#else
#endif
{
Long L;
ULong y, z;
e2 = 0; /* XXX gcc */
irv = STRTOG_Zero;
rvb = 0;
for(s = s00;;s++) switch(*s) {
case '-':
sign = 1;
/* FALLTHROUGH */
case '+':
if (*++s)
goto break2;
/* FALLTHROUGH */
case 0:
sign = 0;
s = s00;
goto ret;
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
case ' ':
continue;
default:
goto break2;
}
if (*s == '0') {
#ifndef NO_HEX_FP
switch(s[1]) {
case 'x':
case 'X':
if (irv == STRTOG_NoNumber) {
s = s00;
sign = 0;
}
goto ret;
}
#endif
nz0 = 1;
while(*++s == '0') ;
if (!*s)
goto ret;
}
s0 = s;
y = z = 0;
if (nd < 9)
y = 10*y + c - '0';
else if (nd < 16)
z = 10*z + c - '0';
#ifdef USE_LOCALE
if (c == *localeconv()->decimal_point)
#else
if (c == '.')
#endif
{
decpt = 1;
c = *++s;
if (!nd) {
for(; c == '0'; c = *++s)
nz++;
if (c > '0' && c <= '9') {
s0 = s;
nz = 0;
goto have_dig;
}
goto dig_done;
}
for(; c >= '0' && c <= '9'; c = *++s) {
nz++;
if (c -= '0') {
for(i = 1; i < nz; i++)
if (nd++ < 9)
y *= 10;
z *= 10;
if (nd++ < 9)
y = 10*y + c;
z = 10*z + c;
nz = 0;
}
}
}
e = 0;
if (c == 'e' || c == 'E') {
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';
/* 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) {
#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;
goto infnanexp;
}
break;
case 'n':
case 'N':
if (match(&s, "an")) {
irv = STRTOG_NaN;
#ifndef No_Hex_NaN
if (*s == '(') /*)*/
#endif
goto infnanexp;
}
}
#endif /* INFNAN_CHECK */
s = s00;
}
goto ret;
}
irv = STRTOG_Normal;
rd = 0;
case FPI_Round_up:
break;
case FPI_Round_zero:
rd = 1;
break;
case FPI_Round_down:
}
/* 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)
if (k > 9)
bd0 = 0;
if (!e) {
goto ret;
}
else if (e > 0) {
if (e <= Ten_pmax) {
#ifdef VAX
goto vax_ovfl_check;
#else
goto ret;
e1 -= e;
goto rv_notOK;
#endif
}
if (e <= Ten_pmax + i) {
/* A fancier test would sometimes let us do
* this for larger i values.
*/
e2 = e - i;
e1 -= i;
#ifdef VAX
/* VAX exponent range is so narrow we must
* worry about overflow here...
*/
goto rv_notOK;
#else
#endif
goto ret;
}
}
#ifndef Inaccurate_Divide
else if (e >= -Ten_pmax) {
goto ret;
e1 -= e;
}
#endif
}
/* Get starting approximation = rv * 10**e1 */
e2 = 0;
if (e1 > 0) {
if ( (i = e1 & 15) !=0)
if (e1 &= ~15) {
>> Exp_shift1) - Bias;
}
if (e1 & 1)
}
}
else if (e1 < 0) {
if ( (i = e1 & 15) !=0)
if (e1 &= ~15) {
>> Exp_shift1) - Bias;
}
if (e1 & 1)
}
}
#ifdef IBM
/* e2 is a correction to the (base 2) exponent of the return
* value, reflecting adjustments above to avoid overflow in the
* native arithmetic. For native IBM (base 16) arithmetic, we
* must multiply e2 by 4 to change from base 16 to 2.
*/
e2 <<= 2;
#endif
return STRTOG_NoMemory;
rve += j;
}
bb0 = 0; /* trailing zero bits in rvb */
goto huge;
denorm = 1;
if (j > 0) {
rvbits += j;
}
else if (j < 0) {
rvbits += j;
if (rvbits <= 0) {
if (rvbits < -1) {
ufl:
rvb->x[0] = 0;
goto ret;
}
}
else
}
goto ufl;
}
/* Now the hard part -- adjusting rv to the correct value.*/
/* Put digits into bd: true value = bd * 10^e */
for(;;) {
return STRTOG_NoMemory;
return STRTOG_NoMemory;
return STRTOG_NoMemory;
if (e >= 0) {
}
else {
}
if (bbe >= 0)
else
if (i < emin) /* denormal */
j += i - emin;
bb2 += j;
bd2 += j;
if (i > bs2)
i = bs2;
if (i > 0) {
bb2 -= i;
bd2 -= i;
bs2 -= i;
}
if (bb5 > 0) {
return STRTOG_NoMemory;
return STRTOG_NoMemory;
}
if (bb2 > 0) {
return STRTOG_NoMemory;
}
else if (bb2 < 0)
if (bd5 > 0) {
return STRTOG_NoMemory;
}
if (bd2 > 0) {
return STRTOG_NoMemory;
}
if (bs2 > 0) {
return STRTOG_NoMemory;
}
asub = 1;
return STRTOG_NoMemory;
break;
L = 0;
if (rd && i <= 0) {
irv = STRTOG_Normal;
if (dsign != 0) {
irv |= STRTOG_Inexhi;
goto adj1;
}
irv |= STRTOG_Inexlo;
goto adj1;
i++, j -= ULbits) {
goto adj1;
}
goto adj1;
return STRTOG_NoMemory;
break;
}
break;
}
if (i < 0) {
/* Error is less than half an ulp -- check for
* special case of mantissa a power of two.
*/
break;
return STRTOG_NoMemory;
goto drop_down;
}
break;
}
if (i == 0) {
/* exactly half-way between */
if (dsign) {
/*boundary case -- increment exponent*/
rvb->x[0] = 1;
denorm = 0;
break;
}
}
else if (bbbits == 1) {
irv = STRTOG_Normal;
/* boundary case -- decrement exponent */
sudden_underflow = 1;
break;
}
return STRTOG_NoMemory;
break;
}
else
break;
if (dsign) {
return STRTOG_NoMemory;
j = ULbits - j;
>> kshift])
!= j)
rvbits++;
}
else {
if (bbbits == 1)
goto undfl;
}
break;
}
adj1:
if (dsign) {
asub = 0;
}
break;
}
}
else {
if (dsign) {
asub = 0;
}
adj0 -= L;
switch(rd) {
case 0:
if (adj0 >= .5)
goto inc_L;
break;
case 1:
goto inc_L;
break;
case 2:
L++;
}
}
}
}
/* adj *= ulp(dval(rv)); */
/* if (asub) rv -= adj; else rv += adj; */
return STRTOG_NoMemory;
rve -= j;
}
return STRTOG_NoMemory;
if (abe < 0)
else if (abe > 0)
if (asub) {
/* rv -= adj; */
return STRTOG_NoMemory;
if (denorm)
/* do nothing */;
/* unlikely; can only have lost 1 high bit */
--rvbits;
denorm = 1;
}
else {
return STRTOG_NoMemory;
--rve;
--rve1;
L = finished = 0;
}
}
}
else {
return STRTOG_NoMemory;
if (denorm) {
denorm = 0;
}
else {
rve++;
rve1++;
L = 0;
}
}
}
if (finished)
break;
if (y == z && L) {
/* Can we stop now? */
break;
}
}
break;
}
}
}
if (j > 0)
else
rve -= j;
}
huge:
#ifndef NO_ERRNO
#endif
#ifdef INFNAN_CHECK
#endif
}
ret:
if (denorm) {
if (sudden_underflow) {
}
else {
if (irv & STRTOG_Inexact)
irv |= STRTOG_Underflow;
}
}
if (se)
if (sign)
irv |= STRTOG_Neg;
if (rvb) {
}
return irv;
}