cosmopolitan/third_party/gdtoa/strtod.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

690 lines
17 KiB
C
Raw Normal View History

2021-08-19 13:07:37 +00:00
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi
The author of this software is David M. Gay.
Please send bug reports to David M. Gay <dmg@acm.org>
or Justine Tunney <jtunney@gmail.com>
Copyright (C) 1998, 1999 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.
*/
2020-12-01 11:43:40 +00:00
#include "libc/errno.h"
2023-06-17 17:13:50 +00:00
#include "libc/fmt/conv.h"
2021-08-19 13:07:37 +00:00
#include "libc/runtime/fenv.h"
#include "third_party/gdtoa/gdtoa.internal.h"
2020-12-01 11:43:40 +00:00
#define Avoid_Underflow
2021-08-19 13:07:37 +00:00
#define dplen 1
/* The factor of 2^106 in tiny__gdtoa_tens[4] helps us avoid setting the underflow */
2020-12-01 11:43:40 +00:00
/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
static const double tiny__gdtoa_tens[] = {
2021-08-19 13:07:37 +00:00
1e-16, 1e-32, 1e-64, 1e-128,
9007199254740992.*9007199254740992.e-256
};
2020-12-01 11:43:40 +00:00
static double
__gdtoa_sulp(U *x, int scale)
2020-12-01 11:43:40 +00:00
{
U u;
int i;
2021-08-19 13:07:37 +00:00
double rv;
rv = __gdtoa_ulp(x);
2020-12-01 11:43:40 +00:00
if (!scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
return rv; /* Is there an example where i <= 0 ? */
word0(&u) = Exp_1 + (i << Exp_shift);
word1(&u) = 0;
return rv * u.d;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
double
2021-08-19 13:07:37 +00:00
strtod(const char *s00, char **se)
2020-12-01 11:43:40 +00:00
{
ThInfo *TI = 0;
2020-12-01 11:43:40 +00:00
int scale;
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, dsign,
2021-08-19 13:07:37 +00:00
e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
const char *s, *s0, *s1;
2020-12-01 11:43:40 +00:00
double aadj;
Long L;
U adj, aadj1, rv, rv0;
ULong y, z;
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
ULong Lsb, Lsb1;
int Rounding;
2021-08-19 13:07:37 +00:00
Rounding = FLT_ROUNDS;
2020-12-01 11:43:40 +00:00
sign = nz0 = nz = decpt = 0;
dval(&rv) = 0.;
for(s = s00;;s++) switch(*s) {
case '-':
sign = 1;
/* no break */
case '+':
if (*++s)
goto break2;
/* no break */
case 0:
goto ret0;
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
case ' ':
continue;
default:
goto break2;
}
2021-08-19 13:07:37 +00:00
break2:
2020-12-01 11:43:40 +00:00
if (*s == '0') {
{
2021-08-19 13:07:37 +00:00
static const FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ };
Long exp;
ULong bits[2];
switch(s[1]) {
case 'x':
case 'X':
2020-12-01 11:43:40 +00:00
{
2021-08-19 13:07:37 +00:00
FPI fpi1 = fpi;
fpi1.rounding = Rounding;
switch((i = __gdtoa_gethex(&s, &fpi1, &exp, &bb, sign, &TI)) & STRTOG_Retmask) {
2021-08-19 13:07:37 +00:00
case STRTOG_NoNumber:
s = s00;
sign = 0;
case STRTOG_Zero:
break;
default:
if (bb) {
__gdtoa_copybits(bits, fpi.nbits, bb);
__gdtoa_Bfree(bb, &TI);
2020-12-01 11:43:40 +00:00
}
__gdtoa_ULtod(((U*)&rv)->L, bits, exp, i);
2021-08-19 13:07:37 +00:00
}}
2020-12-01 11:43:40 +00:00
goto ret;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
}
nz0 = 1;
while(*++s == '0') ;
if (!*s)
goto ret;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
s0 = s;
y = z = 0;
for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
if (nd < 9)
y = 10*y + c - '0';
else if (nd < DBL_DIG + 2)
z = 10*z + c - '0';
nd0 = nd;
if (c == '.') {
c = *++s;
decpt = 1;
if (!nd) {
for(; c == '0'; c = *++s)
nz++;
if (c > '0' && c <= '9') {
s0 = s;
nf += nz;
nz = 0;
goto have_dig;
}
2021-08-19 13:07:37 +00:00
goto dig_done;
}
2020-12-01 11:43:40 +00:00
for(; c >= '0' && c <= '9'; c = *++s) {
2021-08-19 13:07:37 +00:00
have_dig:
2020-12-01 11:43:40 +00:00
nz++;
if (c -= '0') {
nf += nz;
for(i = 1; i < nz; i++)
if (nd++ < 9)
y *= 10;
else if (nd <= DBL_DIG + 2)
z *= 10;
if (nd++ < 9)
y = 10*y + c;
else if (nd <= DBL_DIG + 2)
z = 10*z + c;
nz = 0;
}
2021-08-19 13:07:37 +00:00
}
}/*}*/
dig_done:
2020-12-01 11:43:40 +00:00
e = 0;
if (c == 'e' || c == 'E') {
if (!nd && !nz && !nz0) {
goto ret0;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
s00 = s;
esign = 0;
switch(c = *++s) {
2021-08-19 13:07:37 +00:00
case '-':
esign = 1;
case '+':
c = *++s;
}
2020-12-01 11:43:40 +00:00
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;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else
e = 0;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else
s = s00;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (!nd) {
if (!nz && !nz0) {
/* Check for Nan and Infinity */
ULong bits[2];
static FPI fpinan = /* only 52 explicit bits */
{ 52, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ };
if (!decpt)
2021-08-19 13:07:37 +00:00
switch(c) {
case 'i':
case 'I':
if (__gdtoa_match(&s,"nf")) {
2021-08-19 13:07:37 +00:00
--s;
if (!__gdtoa_match(&s,"inity"))
2021-08-19 13:07:37 +00:00
++s;
word0(&rv) = 0x7ff00000;
word1(&rv) = 0;
goto ret;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
break;
case 'n':
case 'N':
if (__gdtoa_match(&s, "an")) {
2021-08-19 13:07:37 +00:00
if (*s == '(' /*)*/
&& __gdtoa_hexnan(&s, &fpinan, bits)
2021-08-19 13:07:37 +00:00
== STRTOG_NaNbits) {
word0(&rv) = 0x7ff00000 | bits[1];
word1(&rv) = bits[0];
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
else {
word0(&rv) = 0x7ff80000;
word1(&rv) = 0x0;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
goto ret;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
}
ret0:
2020-12-01 11:43:40 +00:00
s = s00;
sign = 0;
}
2021-08-19 13:07:37 +00:00
goto ret;
}
2020-12-01 11:43:40 +00:00
e1 = e -= nf;
/* 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 + 2 ? nd : DBL_DIG + 2;
dval(&rv) = y;
if (k > 9) {
dval(&rv) = __gdtoa_tens[k - 9] * dval(&rv) + z;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
bd0 = 0;
2021-08-19 13:07:37 +00:00
if (nd <= DBL_DIG) {
2020-12-01 11:43:40 +00:00
if (!e)
goto ret;
if (e > 0) {
if (e <= Ten_pmax) {
/* round correctly FLT_ROUNDS = 2 or 3 */
if (sign) {
rv.d = -rv.d;
sign = 0;
2021-08-19 13:07:37 +00:00
}
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e]);
2020-12-01 11:43:40 +00:00
goto ret;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
i = DBL_DIG - nd;
if (e <= Ten_pmax + i) {
/* A fancier test would sometimes let us do
* this for larger i values.
*/
/* round correctly FLT_ROUNDS = 2 or 3 */
if (sign) {
rv.d = -rv.d;
sign = 0;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
e -= i;
dval(&rv) *= __gdtoa_tens[i];
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e]);
2020-12-01 11:43:40 +00:00
goto ret;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else if (e >= -Ten_pmax) {
/* round correctly FLT_ROUNDS = 2 or 3 */
if (sign) {
rv.d = -rv.d;
sign = 0;
2021-08-19 13:07:37 +00:00
}
/* rv = */ rounded_quotient(dval(&rv), __gdtoa_tens[-e]);
2020-12-01 11:43:40 +00:00
goto ret;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
e1 += nd - k;
scale = 0;
if (Rounding >= 2) {
if (sign)
Rounding = Rounding == 2 ? 0 : 2;
else
if (Rounding != 2)
Rounding = 0;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
/* Get starting approximation = rv * 10**e1 */
if (e1 > 0) {
if ( (i = e1 & 15) !=0)
dval(&rv) *= __gdtoa_tens[i];
2020-12-01 11:43:40 +00:00
if (e1 &= ~15) {
if (e1 > DBL_MAX_10_EXP) {
2021-08-19 13:07:37 +00:00
ovfl:
2020-12-01 11:43:40 +00:00
/* Can't trust HUGE_VAL */
switch(Rounding) {
2021-08-19 13:07:37 +00:00
case 0: /* toward 0 */
case 3: /* toward -infinity */
2020-12-01 11:43:40 +00:00
word0(&rv) = Big0;
word1(&rv) = Big1;
break;
2021-08-19 13:07:37 +00:00
default:
2020-12-01 11:43:40 +00:00
word0(&rv) = Exp_mask;
word1(&rv) = 0;
2021-08-19 13:07:37 +00:00
}
range_err:
2020-12-01 11:43:40 +00:00
if (bd0) {
__gdtoa_Bfree(bb, &TI);
__gdtoa_Bfree(bd, &TI);
__gdtoa_Bfree(bs, &TI);
__gdtoa_Bfree(bd0, &TI);
__gdtoa_Bfree(delta, &TI);
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
errno = ERANGE;
goto ret;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
e1 >>= 4;
for(j = 0; e1 > 1; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= __gdtoa_bigtens[j];
/* The last __gdtoa_multiplication could overflow. */
2020-12-01 11:43:40 +00:00
word0(&rv) -= P*Exp_msk1;
dval(&rv) *= __gdtoa_bigtens[j];
2020-12-01 11:43:40 +00:00
if ((z = word0(&rv) & Exp_mask)
2021-08-19 13:07:37 +00:00
> Exp_msk1*(DBL_MAX_EXP+Bias-P))
2020-12-01 11:43:40 +00:00
goto ovfl;
if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
/* set to largest number */
/* (Can't trust DBL_MAX) */
word0(&rv) = Big0;
word1(&rv) = Big1;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else
word0(&rv) += P*Exp_msk1;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else if (e1 < 0) {
e1 = -e1;
if ( (i = e1 & 15) !=0)
dval(&rv) /= __gdtoa_tens[i];
2020-12-01 11:43:40 +00:00
if (e1 >>= 4) {
if (e1 >= 1 << n___gdtoa_bigtens)
2020-12-01 11:43:40 +00:00
goto undfl;
if (e1 & Scale_Bit)
scale = 2*P;
for(j = 0; e1 > 0; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= tiny__gdtoa_tens[j];
2020-12-01 11:43:40 +00:00
if (scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
2021-08-19 13:07:37 +00:00
>> Exp_shift)) > 0) {
2020-12-01 11:43:40 +00:00
/* scaled rv is denormal; zap j low bits */
if (j >= 32) {
word1(&rv) = 0;
if (j >= 53)
2021-08-19 13:07:37 +00:00
word0(&rv) = (P+2)*Exp_msk1;
2020-12-01 11:43:40 +00:00
else
2021-08-19 13:07:37 +00:00
word0(&rv) &= 0xffffffff << (j-32);
}
2020-12-01 11:43:40 +00:00
else
word1(&rv) &= 0xffffffff << j;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (!dval(&rv)) {
2021-08-19 13:07:37 +00:00
undfl:
dval(&rv) = 0.;
if (Rounding == 2)
word1(&rv) = 1;
goto range_err;
2020-12-01 11:43:40 +00:00
}
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
/* Now the hard part -- adjusting rv to the correct value.*/
/* Put digits into bd: true value = bd * 10^e */
bd0 = __gdtoa_s2b(s0, nd0, nd, y, dplen, &TI);
2020-12-01 11:43:40 +00:00
for(;;) {
bd = __gdtoa_Balloc(bd0->k, &TI);
2020-12-01 11:43:40 +00:00
Bcopy(bd, bd0);
bb = __gdtoa_d2b(dval(&rv), &bbe, &bbbits, &TI); /* rv = bb * 2^bbe */
bs = __gdtoa_i2b(1, &TI);
2020-12-01 11:43:40 +00:00
if (e >= 0) {
bb2 = bb5 = 0;
bd2 = bd5 = e;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else {
bb2 = bb5 = -e;
bd2 = bd5 = 0;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (bbe >= 0)
bb2 += bbe;
else
bd2 -= bbe;
bs2 = bb2;
if (Rounding != 1)
bs2++;
Lsb = LSB;
Lsb1 = 0;
j = bbe - scale;
i = j + bbbits - 1; /* logb(rv) */
j = P + 1 - bbbits;
if (i < Emin) { /* denormal */
i = Emin - i;
j -= i;
if (i < 32)
Lsb <<= i;
else
Lsb1 = Lsb << (i-32);
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
bb2 += j;
bd2 += j;
bd2 += scale;
i = bb2 < bd2 ? bb2 : bd2;
if (i > bs2)
i = bs2;
if (i > 0) {
bb2 -= i;
bd2 -= i;
bs2 -= i;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (bb5 > 0) {
bs = __gdtoa_pow5mult(bs, bb5, &TI);
bb1 = __gdtoa_mult(bs, bb, &TI);
__gdtoa_Bfree(bb, &TI);
2020-12-01 11:43:40 +00:00
bb = bb1;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (bb2 > 0)
bb = __gdtoa_lshift(bb, bb2, &TI);
2020-12-01 11:43:40 +00:00
if (bd5 > 0)
bd = __gdtoa_pow5mult(bd, bd5, &TI);
2020-12-01 11:43:40 +00:00
if (bd2 > 0)
bd = __gdtoa_lshift(bd, bd2, &TI);
2020-12-01 11:43:40 +00:00
if (bs2 > 0)
bs = __gdtoa_lshift(bs, bs2, &TI);
delta = __gdtoa_diff(bb, bd, &TI);
2020-12-01 11:43:40 +00:00
dsign = delta->sign;
delta->sign = 0;
i = __gdtoa_cmp(delta, bs);
2020-12-01 11:43:40 +00:00
if (Rounding != 1) {
if (i < 0) {
/* Error is less than an __gdtoa_ulp */
2020-12-01 11:43:40 +00:00
if (!delta->x[0] && delta->wds <= 1) {
/* exact */
break;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (Rounding) {
if (dsign) {
dval(&adj) = 1.;
goto apply_adj;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else if (!dsign) {
dval(&adj) = -1.;
if (!word1(&rv)
2021-08-19 13:07:37 +00:00
&& !(word0(&rv) & Frac_mask)) {
2020-12-01 11:43:40 +00:00
y = word0(&rv) & Exp_mask;
if (!scale || y > 2*P*Exp_msk1)
2021-08-19 13:07:37 +00:00
{
delta = __gdtoa_lshift(delta,Log2P,&TI);
if (__gdtoa_cmp(delta, bs) <= 0)
2021-08-19 13:07:37 +00:00
dval(&adj) = -0.5;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
}
apply_adj:
2020-12-01 11:43:40 +00:00
if (scale && (y = word0(&rv) & Exp_mask)
2021-08-19 13:07:37 +00:00
<= 2*P*Exp_msk1)
word0(&adj) += (2*P+1)*Exp_msk1 - y;
dval(&rv) += adj.d*__gdtoa_ulp(&rv);
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
break;
}
dval(&adj) = __gdtoa_ratio(delta, bs);
2020-12-01 11:43:40 +00:00
if (adj.d < 1.)
dval(&adj) = 1.;
if (adj.d <= 0x7ffffffe) {
/* dval(&adj) = Rounding ? ceil(&adj) : floor(&adj); */
y = adj.d;
if (y != adj.d) {
if (!((Rounding>>1) ^ dsign))
y++;
dval(&adj) = y;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
word0(&adj) += (2*P+1)*Exp_msk1 - y;
dval(&adj) *= __gdtoa_ulp(&rv); /* XXX */
2020-12-01 11:43:40 +00:00
if (dsign) {
if (word0(&rv) == Big0 && word1(&rv) == Big1)
goto ovfl;
dval(&rv) += adj.d;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else
dval(&rv) -= adj.d;
goto cont;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (i < 0) {
/* Error is less than half an __gdtoa_ulp -- check for
2020-12-01 11:43:40 +00:00
* special case of mantissa a power of two.
*/
2021-08-19 13:07:37 +00:00
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask ||
(word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1) {
2020-12-01 11:43:40 +00:00
break;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (!delta->x[0] && delta->wds <= 1) {
/* exact result */
break;
2021-08-19 13:07:37 +00:00
}
delta = __gdtoa_lshift(delta,Log2P,&TI);
if (__gdtoa_cmp(delta, bs) > 0)
2020-12-01 11:43:40 +00:00
goto drop_down;
break;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (i == 0) {
/* exactly half-way between */
if (dsign) {
if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
2021-08-19 13:07:37 +00:00
&& word1(&rv) == (
(scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
0xffffffff)) {
/*boundary case -- __gdtoa_increment exponent*/
2020-12-01 11:43:40 +00:00
if (word0(&rv) == Big0 && word1(&rv) == Big1)
goto ovfl;
word0(&rv) = (word0(&rv) & Exp_mask)
+ Exp_msk1
;
word1(&rv) = 0;
dsign = 0;
break;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
2021-08-19 13:07:37 +00:00
drop_down:
/* boundary case -- __gdtoa_decrement exponent */
2020-12-01 11:43:40 +00:00
if (scale) {
L = word0(&rv) & Exp_mask;
if (L <= (2*P+1)*Exp_msk1) {
if (L > (P+2)*Exp_msk1)
/* round even ==> */
/* accept rv */
break;
/* rv = smallest denormal */
goto undfl;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
L = (word0(&rv) & Exp_mask) - Exp_msk1;
word0(&rv) = L | Bndry_mask1;
word1(&rv) = 0xffffffff;
break;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
if (Lsb1) {
if (!(word0(&rv) & Lsb1))
break;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else if (!(word1(&rv) & Lsb))
break;
if (dsign)
dval(&rv) += __gdtoa_sulp(&rv, scale);
2020-12-01 11:43:40 +00:00
else {
dval(&rv) -= __gdtoa_sulp(&rv, scale);
2020-12-01 11:43:40 +00:00
if (!dval(&rv))
goto undfl;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
dsign = 1 - dsign;
break;
2021-08-19 13:07:37 +00:00
}
if ((aadj = __gdtoa_ratio(delta, bs)) <= 2.) {
2020-12-01 11:43:40 +00:00
if (dsign)
aadj = dval(&aadj1) = 1.;
else if (word1(&rv) || word0(&rv) & Bndry_mask) {
if (word1(&rv) == Tiny1 && !word0(&rv))
goto undfl;
aadj = 1.;
dval(&aadj1) = -1.;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else {
/* special case -- power of FLT_RADIX to be */
/* rounded down... */
if (aadj < 2./FLT_RADIX)
aadj = 1./FLT_RADIX;
else
aadj *= 0.5;
dval(&aadj1) = -aadj;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else {
aadj *= 0.5;
dval(&aadj1) = dsign ? aadj : -aadj;
switch(Rounding) {
2021-08-19 13:07:37 +00:00
case 2: /* towards +infinity */
dval(&aadj1) -= 0.5;
break;
case 0: /* towards 0 */
case 3: /* towards -infinity */
2020-12-01 11:43:40 +00:00
dval(&aadj1) += 0.5;
}
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
y = word0(&rv) & Exp_mask;
/* Check for overflow */
if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
dval(&rv0) = dval(&rv);
word0(&rv) -= P*Exp_msk1;
dval(&adj) = dval(&aadj1) * __gdtoa_ulp(&rv);
2020-12-01 11:43:40 +00:00
dval(&rv) += dval(&adj);
if ((word0(&rv) & Exp_mask) >=
2021-08-19 13:07:37 +00:00
Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2020-12-01 11:43:40 +00:00
if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
goto ovfl;
word0(&rv) = Big0;
word1(&rv) = Big1;
goto cont;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else
word0(&rv) += P*Exp_msk1;
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
else {
if (scale && y <= 2*P*Exp_msk1) {
if (aadj <= 0x7fffffff) {
if ((z = aadj) <= 0)
z = 1;
aadj = z;
dval(&aadj1) = dsign ? aadj : -aadj;
}
2021-08-19 13:07:37 +00:00
word0(&aadj1) += (2*P+1)*Exp_msk1 - y;
}
dval(&adj) = dval(&aadj1) * __gdtoa_ulp(&rv);
2020-12-01 11:43:40 +00:00
dval(&rv) += dval(&adj);
2021-08-19 13:07:37 +00:00
}
2020-12-01 11:43:40 +00:00
z = word0(&rv) & Exp_mask;
if (!scale)
2021-08-19 13:07:37 +00:00
if (y == z) {
/* Can we stop now? */
L = (Long)aadj;
aadj -= L;
/* The tolerances below are conservative. */
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
if (aadj < .4999999 || aadj > .5000001)
break;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
else if (aadj < .4999999/FLT_RADIX)
break;
2020-12-01 11:43:40 +00:00
}
2021-08-19 13:07:37 +00:00
cont:
__gdtoa_Bfree(bb, &TI);
__gdtoa_Bfree(bd, &TI);
__gdtoa_Bfree(bs, &TI);
__gdtoa_Bfree(delta, &TI);
2021-08-19 13:07:37 +00:00
}
__gdtoa_Bfree(bb, &TI);
__gdtoa_Bfree(bd, &TI);
__gdtoa_Bfree(bs, &TI);
__gdtoa_Bfree(bd0, &TI);
__gdtoa_Bfree(delta, &TI);
2020-12-01 11:43:40 +00:00
if (scale) {
word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
word1(&rv0) = 0;
dval(&rv) *= dval(&rv0);
/* try to avoid the bug of testing an 8087 register value */
if (!(word0(&rv) & Exp_mask))
errno = ERANGE;
2021-08-19 13:07:37 +00:00
}
ret:
2020-12-01 11:43:40 +00:00
if (se)
*se = (char *)s;
return sign ? -dval(&rv) : dval(&rv);
2021-08-19 13:07:37 +00:00
}
2023-06-17 17:13:50 +00:00
__weak_reference(strtod, strtod_l);