Improve memory safety

This commit makes numerous refinements to cosmopolitan memory handling.

The default stack size has been reduced from 2mb to 128kb. A new macro
is now provided so you can easily reconfigure the stack size to be any
value you want. Work around the breaking change by adding to your main:

    STATIC_STACK_SIZE(0x00200000);  // 2mb stack

If you're not sure how much stack you need, then you can use:

    STATIC_YOINK("stack_usage_logging");

After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test
suite, nothing in the Cosmopolitan repository (except for Python) needs
a stack size greater than 30kb. There are also new macros for detecting
the size and address of the stack at runtime, e.g. GetStackAddr(). We
also now support sigaltstack() so if you want to see nice looking crash
reports whenever a stack overflow happens, you can put this in main():

    ShowCrashReports();

Under `make MODE=dbg` and `make MODE=asan` the unit testing framework
will now automatically print backtraces of memory allocations when
things like memory leaks happen. Bugs are now fixed in ASAN global
variable overrun detection. The memtrack and asan runtimes also handle
edge cases now. The new tools helped to identify a few memory leaks,
which are fixed by this change.

This change should fix an issue reported in #288 with ARG_MAX limits.
Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
This commit is contained in:
Justine Tunney 2021-10-13 17:27:13 -07:00
parent a0b39f886c
commit 226aaf3547
317 changed files with 6474 additions and 3993 deletions

View file

@ -32,8 +32,16 @@
#include "third_party/gdtoa/gdtoa.internal.h"
/* clang-format off */
void
freedtoa(char *s)
{
Bigint *b = (Bigint *)((int *)s - 1);
b->maxwds = 1 << (b->k = *(int*)b);
__gdtoa_Bfree(b);
}
char *
rv_alloc(int i)
__gdtoa_rv_alloc(int i)
{
int j, k, *r;
j = sizeof(ULong);
@ -41,16 +49,16 @@ rv_alloc(int i)
(int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int)) + j <= i;
j <<= 1)
k++;
r = (int*)Balloc(k);
r = (int*)__gdtoa_Balloc(k);
*r = k;
return (char *)(r+1);
}
char *
nrv_alloc(char *s, char **rve, int n)
__gdtoa_nrv_alloc(char *s, char **rve, int n)
{
char *rv, *t;
t = rv = rv_alloc(n);
t = rv = __gdtoa_rv_alloc(n);
while((*t = *s++) !=0)
t++;
if (rve)
@ -58,16 +66,8 @@ nrv_alloc(char *s, char **rve, int n)
return rv;
}
void
freedtoa(char *s)
{
Bigint *b = (Bigint *)((int *)s - 1);
b->maxwds = 1 << (b->k = *(int*)b);
Bfree(b);
}
int
quorem(Bigint *b, Bigint *S)
__gdtoa_quorem(Bigint *b, Bigint *S)
{
int n;
ULong *bx, *bxe, q, *sx, *sxe;
@ -75,7 +75,7 @@ quorem(Bigint *b, Bigint *S)
n = S->wds;
#ifdef DEBUG
if (b->wds > n)
Bug("oversize b in quorem");
Bug("oversize b in __gdtoa_quorem");
#endif
if (b->wds < n)
return 0;
@ -86,7 +86,7 @@ quorem(Bigint *b, Bigint *S)
q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
#ifdef DEBUG
if (q > 9)
Bug("oversized quotient in quorem");
Bug("oversized quotient in __gdtoa_quorem");
#endif
if (q) {
borrow = 0;
@ -94,9 +94,9 @@ quorem(Bigint *b, Bigint *S)
do {
ys = *sx++ * (ULLong)q + carry;
carry = ys >> 32;
y = *bx - (ys & 0xffffffffUL) - borrow;
y = *bx - (ys & 0xffffffff) - borrow;
borrow = y >> 32 & 1UL;
*bx++ = y & 0xffffffffUL;
*bx++ = y & 0xffffffff;
}
while(sx <= sxe);
if (!*bxe) {
@ -106,7 +106,7 @@ quorem(Bigint *b, Bigint *S)
b->wds = n;
}
}
if (cmp(b, S) >= 0) {
if (__gdtoa_cmp(b, S) >= 0) {
q++;
borrow = 0;
carry = 0;
@ -115,9 +115,9 @@ quorem(Bigint *b, Bigint *S)
do {
ys = *sx++ + carry;
carry = ys >> 32;
y = *bx - (ys & 0xffffffffUL) - borrow;
y = *bx - (ys & 0xffffffff) - borrow;
borrow = y >> 32 & 1UL;
*bx++ = y & 0xffffffffUL;
*bx++ = y & 0xffffffff;
}
while(sx <= sxe);
bx = b->x;

View file

@ -41,12 +41,12 @@
* Modifications:
* 1. Rather than iterating, we use a simple numeric overestimate
* to determine k = floor(log10(d)). We scale relevant
* quantities using O(log2(k)) rather than O(k) multiplications.
* quantities using O(log2(k)) rather than O(k) __gdtoa_multiplications.
* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
* try to generate digits strictly left to right. Instead, we
* compute with fewer bits and propagate the carry if necessary
* when rounding the final digit up. This is often faster.
* 3. Under the assumption that input will be rounded nearest,
* 3. Under the as__gdtoa_sumption that input will be rounded nearest,
* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
* That is, we allow equality in stopping tests when the
* round-nearest rule will give the same floating-point value
@ -56,10 +56,10 @@
* quantities.
* 5. When converting floating-point integers less than 1e16,
* we use floating-point arithmetic rather than resorting
* to multiple-precision integers.
* to __gdtoa_multiple-precision integers.
* 6. When asked to produce fewer than 15 digits, we first try
* to get by with floating-point arithmetic; we resort to
* multiple-precision integer arithmetic only if we cannot
* __gdtoa_multiple-precision integer arithmetic only if we cannot
* guarantee that the floating-point calculation has given
* the correctly rounded result. For k requested digits and
* "uniformly" distributed input, the probability is
@ -128,12 +128,12 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Infinity or NaN */
*decpt = 9999;
if (!word1(&d) && !(word0(&d) & 0xfffff))
return nrv_alloc("Infinity", rve, 8);
return nrv_alloc("NaN", rve, 3);
return __gdtoa_nrv_alloc("Infinity", rve, 8);
return __gdtoa_nrv_alloc("NaN", rve, 3);
}
if (!dval(&d)) {
*decpt = 1;
return nrv_alloc("0", rve, 1);
return __gdtoa_nrv_alloc("0", rve, 1);
}
if (Rounding >= 2) {
if (*sign)
@ -142,7 +142,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (Rounding != 2)
Rounding = 0;
}
b = d2b(dval(&d), &be, &bbits);
b = __gdtoa_d2b(dval(&d), &be, &bbits);
if (( i = (int)(word0(&d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)) )!=0) {
dval(&d2) = dval(&d);
word0(&d2) &= Frac_mask1;
@ -160,7 +160,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
* We want k to be too large rather than too small.
* The error in the first-order Taylor series approximation
* is in our favor, so we just round up the constant enough
* to compensate for any error in the multiplication of
* to compensate for any error in the __gdtoa_multiplication of
* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
* adding 1e-13 to the constant term more than suffices.
@ -187,7 +187,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
k--; /* want k = floor(ds) */
k_check = 1;
if (k >= 0 && k <= Ten_pmax) {
if (dval(&d) < tens[k])
if (dval(&d) < __gdtoa_tens[k])
k--;
k_check = 0;
}
@ -245,7 +245,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (i <= 0)
i = 1;
}
s = s0 = rv_alloc(i);
s = s0 = __gdtoa_rv_alloc(i);
if (mode > 1 && Rounding != 1)
leftright = 0;
if (ilim >= 0 && ilim <= Quick_max && try_quick) {
@ -257,27 +257,27 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
ilim0 = ilim;
ieps = 2; /* conservative */
if (k > 0) {
ds = tens[k&0xf];
ds = __gdtoa_tens[k&0xf];
j = k >> 4;
if (j & Bletch) {
/* prevent overflows */
j &= Bletch - 1;
dval(&d) /= bigtens[n_bigtens-1];
dval(&d) /= __gdtoa_bigtens[n___gdtoa_bigtens-1];
ieps++;
}
for(; j; j >>= 1, i++)
if (j & 1) {
ieps++;
ds *= bigtens[i];
ds *= __gdtoa_bigtens[i];
}
dval(&d) /= ds;
}
else if (( j1 = -k )!=0) {
dval(&d) *= tens[j1 & 0xf];
dval(&d) *= __gdtoa_tens[j1 & 0xf];
for(j = j1 >> 4; j; j >>= 1, i++)
if (j & 1) {
ieps++;
dval(&d) *= bigtens[i];
dval(&d) *= __gdtoa_bigtens[i];
}
}
if (k_check && dval(&d) < 1. && ilim > 0) {
@ -303,14 +303,14 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Use Steele & White method of only
* generating digits needed.
*/
dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
dval(&eps) = 0.5/__gdtoa_tens[ilim-1] - dval(&eps);
if (k0 < 0 && j1 >= 307) {
eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
word0(&eps1) -= Exp_msk1 * (Bias+P-1);
dval(&eps1) *= tens[j1 & 0xf];
dval(&eps1) *= __gdtoa_tens[j1 & 0xf];
for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
if (j & 1)
dval(&eps1) *= bigtens[i];
dval(&eps1) *= __gdtoa_bigtens[i];
if (eps.d < eps1.d)
eps.d = eps1.d;
if (10. - d.d < 10.*eps.d && eps.d < 1.) {
@ -336,7 +336,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
}
else {
/* Generate ilim digits, then fix them up. */
dval(&eps) *= tens[ilim-1];
dval(&eps) *= __gdtoa_tens[ilim-1];
for(i = 1;; i++, dval(&d) *= 10.) {
L = (Long)(dval(&d));
if (!(dval(&d) -= L))
@ -361,7 +361,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Do we have a "small" integer? */
if (be >= 0 && k <= Int_max) {
/* Yes. */
ds = tens[k];
ds = __gdtoa_tens[k];
if (ndigits < 0 && ilim <= 0) {
S = mhi = 0;
if (ilim < 0 || dval(&d) <= 5*ds)
@ -409,7 +409,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
b2 += i;
s2 += i;
mhi = i2b(1);
mhi = __gdtoa_i2b(1);
}
if (m2 > 0 && s2 > 0) {
i = m2 < s2 ? m2 : s2;
@ -420,20 +420,20 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (b5 > 0) {
if (leftright) {
if (m5 > 0) {
mhi = pow5mult(mhi, m5);
b1 = mult(mhi, b);
Bfree(b);
mhi = __gdtoa_pow5mult(mhi, m5);
b1 = __gdtoa_mult(mhi, b);
__gdtoa_Bfree(b);
b = b1;
}
if (( j = b5 - m5 )!=0)
b = pow5mult(b, j);
b = __gdtoa_pow5mult(b, j);
}
else
b = pow5mult(b, b5);
b = __gdtoa_pow5mult(b, b5);
}
S = i2b(1);
S = __gdtoa_i2b(1);
if (s5 > 0)
S = pow5mult(S, s5);
S = __gdtoa_pow5mult(S, s5);
/* Check for special case that d is a normalized power of 2. */
spec_case = 0;
@ -451,7 +451,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
* shift left if necessary so divisor has 4 leading 0 bits.
*
* Perhaps we should just compute leading 28 bits of S once
* and for all and pass them and a shift to quorem, so it
* and for all and pass them and a shift to __gdtoa_quorem, so it
* can do shifts and ors to compute the numerator for q.
*/
if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f )!=0)
@ -469,20 +469,20 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
s2 += i;
}
if (b2 > 0)
b = lshift(b, b2);
b = __gdtoa_lshift(b, b2);
if (s2 > 0)
S = lshift(S, s2);
S = __gdtoa_lshift(S, s2);
if (k_check) {
if (cmp(b,S) < 0) {
if (__gdtoa_cmp(b,S) < 0) {
k--;
b = multadd(b, 10, 0); /* we botched the k estimate */
b = __gdtoa_multadd(b, 10, 0); /* we botched the k estimate */
if (leftright)
mhi = multadd(mhi, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
ilim = ilim1;
}
}
if (ilim <= 0 && (mode == 3 || mode == 5)) {
if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
if (ilim < 0 || __gdtoa_cmp(b,S = __gdtoa_multadd(S,5,0)) <= 0) {
/* no digits, fcvt style */
no_digits:
k = -1 - ndigits;
@ -495,25 +495,25 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
}
if (leftright) {
if (m2 > 0)
mhi = lshift(mhi, m2);
mhi = __gdtoa_lshift(mhi, m2);
/* Compute mlo -- check for special case
* that d is a normalized power of 2.
*/
mlo = mhi;
if (spec_case) {
mhi = Balloc(mhi->k);
mhi = __gdtoa_Balloc(mhi->k);
Bcopy(mhi, mlo);
mhi = lshift(mhi, Log2P);
mhi = __gdtoa_lshift(mhi, Log2P);
}
for(i = 1;;i++) {
dig = quorem(b,S) + '0';
dig = __gdtoa_quorem(b,S) + '0';
/* Do we yet have the shortest decimal string
* that will round to d?
*/
j = cmp(b, mlo);
delta = diff(S, mhi);
j1 = delta->sign ? 1 : cmp(b, delta);
Bfree(delta);
j = __gdtoa_cmp(b, mlo);
delta = __gdtoa_diff(S, mhi);
j1 = delta->sign ? 1 : __gdtoa_cmp(b, delta);
__gdtoa_Bfree(delta);
if (j1 == 0 && mode != 1 && !(word1(&d) & 1) && Rounding >= 1) {
if (dig == '9')
goto round_9_up;
@ -533,8 +533,8 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
case 2: goto keep_dig;
}
if (j1 > 0) {
b = lshift(b, 1);
j1 = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j1 = __gdtoa_cmp(b, S);
if ((j1 > 0 || (j1 == 0 && dig & 1))
&& dig++ == '9')
goto round_9_up;
@ -558,24 +558,24 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
*s++ = dig;
if (i == ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
if (mlo == mhi)
mlo = mhi = multadd(mhi, 10, 0);
mlo = mhi = __gdtoa_multadd(mhi, 10, 0);
else {
mlo = multadd(mlo, 10, 0);
mhi = multadd(mhi, 10, 0);
mlo = __gdtoa_multadd(mlo, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
}
}
}
else {
for(i = 1;; i++) {
*s++ = dig = quorem(b,S) + '0';
*s++ = dig = __gdtoa_quorem(b,S) + '0';
if (!b->x[0] && b->wds <= 1) {
goto ret;
}
if (i >= ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
}
}
@ -584,8 +584,8 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
case 0: goto trimzeros;
case 2: goto roundoff;
}
b = lshift(b, 1);
j = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j = __gdtoa_cmp(b, S);
if (j > 0 || (j == 0 && dig & 1))
{
roundoff:
@ -603,17 +603,17 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
s++;
}
ret:
Bfree(S);
__gdtoa_Bfree(S);
if (mhi) {
if (mlo && mlo != mhi)
Bfree(mlo);
Bfree(mhi);
__gdtoa_Bfree(mlo);
__gdtoa_Bfree(mhi);
}
retc:
while(s > s0 && s[-1] == '0')
--s;
ret1:
Bfree(b);
__gdtoa_Bfree(b);
*s = 0;
*decpt = k + 1;
if (rve)

View file

@ -40,8 +40,8 @@
#define ldus_QNAN3 0
#define ldus_QNAN4 0
const char *const InfName[6] = { "Infinity", "infinity", "INFINITY", "Inf", "inf", "INF" };
const char *const NanName[3] = { "NaN", "nan", "NAN" };
const char *const __gdtoa_InfName[6] = { "Infinity", "infinity", "INFINITY", "Inf", "inf", "INF" };
const char *const __gdtoa_NanName[3] = { "NaN", "nan", "NAN" };
const ULong __gdtoa_NanDflt_Q[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0x7fffffff };
const ULong __gdtoa_NanDflt_d[2] = { d_QNAN1, d_QNAN0 };
const ULong __gdtoa_NanDflt_f[1] = { f_QNAN };
@ -49,7 +49,7 @@ const ULong __gdtoa_NanDflt_xL[3] = { 1, 0x80000000, 0x7fff0000 };
const UShort __gdtoa_NanDflt_ldus[5] = { ldus_QNAN4, ldus_QNAN3, ldus_QNAN2, ldus_QNAN1, ldus_QNAN0 };
char *
g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
__gdtoa_g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
{
int i, j, k;
char *be, *s0;

View file

@ -84,38 +84,38 @@ g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize)
dd = ddx;
L = dd->L;
}
z = d2b(dval(&dd[0]), &ex, &bx);
z = __gdtoa_d2b(dval(&dd[0]), &ex, &bx);
if (dval(&dd[1]) == 0.)
goto no_y;
x = z;
y = d2b(dval(&dd[1]), &ey, &by);
y = __gdtoa_d2b(dval(&dd[1]), &ey, &by);
if ( (i = ex - ey) !=0) {
if (i > 0) {
x = lshift(x, i);
x = __gdtoa_lshift(x, i);
ex = ey;
}
else
y = lshift(y, -i);
y = __gdtoa_lshift(y, -i);
}
if ((L[1] ^ L[2+1]) & 0x80000000L) {
z = diff(x, y);
z = __gdtoa_diff(x, y);
if (L[1] & 0x80000000L)
z->sign = 1 - z->sign;
}
else {
z = sum(x, y);
z = __gdtoa_sum(x, y);
if (L[1] & 0x80000000L)
z->sign = 1;
}
Bfree(x);
Bfree(y);
__gdtoa_Bfree(x);
__gdtoa_Bfree(y);
no_y:
bits = zx = z->x;
for(i = 0; !*zx; zx++)
i += 32;
i += lo0bits(zx);
if (i) {
rshift(z, i);
__gdtoa_rshift(z, i);
ex += i;
}
fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
@ -132,7 +132,7 @@ no_y:
mode = 2;
if (ndig <= 0) {
if (bufsize < (size_t)(fpi.nbits * .301029995664) + 10) {
Bfree(z);
__gdtoa_Bfree(z);
return 0;
}
mode = 0;
@ -144,7 +144,7 @@ no_y:
fpi.int_max = Int_max;
i = STRTOG_Normal;
s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
b = g__fmt(buf, s, se, decpt, z->sign, bufsize);
Bfree(z);
b = __gdtoa_g__fmt(buf, s, se, decpt, z->sign, bufsize);
__gdtoa_Bfree(z);
return b;
}

View file

@ -59,7 +59,7 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik)
b = buf;
if (sign && nik < 18)
*b++ = '-';
b = stpcpy(b, NanName[nik%3]);
b = stpcpy(b, __gdtoa_NanName[nik%3]);
if (nik > 5 && (nik < 12
|| L[0] != __gdtoa_NanDflt_d[0]
|| (L[1] ^ __gdtoa_NanDflt_d[1]) & 0xfffff
@ -69,7 +69,7 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik)
bits0[1] = (L[2+1] & 0xfffff) | (L[0] << 20);
bits0[2] = (L[0] >> 12) | (L[1] << 20);
bits0[3] = (L[1] >> 12) & 0xff;
b = add_nanbits(b, bufsize - (b-buf), bits0, 4);
b = __gdtoa_add_nanbits(b, bufsize - (b-buf), bits0, 4);
}
return b;
}
@ -83,7 +83,7 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik)
b = buf;
if (L[1] & 0x80000000L)
*b++ = '-';
return stpcpy(b, InfName[nik%6]);
return stpcpy(b, __gdtoa_InfName[nik%6]);
}
if ((L[2+1] & 0x7ff00000) == 0x7ff00000) {
L += 2;
@ -105,38 +105,38 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik)
dd = ddx;
L = dd->L;
}
z = d2b(dval(&dd[0]), &ex, &bx);
z = __gdtoa_d2b(dval(&dd[0]), &ex, &bx);
if (dval(&dd[1]) == 0.)
goto no_y;
x = z;
y = d2b(dval(&dd[1]), &ey, &by);
y = __gdtoa_d2b(dval(&dd[1]), &ey, &by);
if ( (i = ex - ey) !=0) {
if (i > 0) {
x = lshift(x, i);
x = __gdtoa_lshift(x, i);
ex = ey;
}
else
y = lshift(y, -i);
y = __gdtoa_lshift(y, -i);
}
if ((L[1] ^ L[2+1]) & 0x80000000L) {
z = diff(x, y);
z = __gdtoa_diff(x, y);
if (L[1] & 0x80000000L)
z->sign = 1 - z->sign;
}
else {
z = sum(x, y);
z = __gdtoa_sum(x, y);
if (L[1] & 0x80000000L)
z->sign = 1;
}
Bfree(x);
Bfree(y);
__gdtoa_Bfree(x);
__gdtoa_Bfree(y);
no_y:
bits = zx = z->x;
for(i = 0; !*zx; zx++)
i += 32;
i += lo0bits(zx);
if (i) {
rshift(z, i);
__gdtoa_rshift(z, i);
ex += i;
}
fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]);
@ -153,7 +153,7 @@ no_y:
mode = 2;
if (ndig <= 0) {
if (bufsize < (size_t)(fpi.nbits * .301029995664) + 10) {
Bfree(z);
__gdtoa_Bfree(z);
return 0;
}
mode = 0;
@ -165,7 +165,7 @@ no_y:
fpi.int_max = Int_max;
i = STRTOG_Normal;
s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se);
b = g__fmt(buf, s, se, decpt, z->sign, bufsize);
Bfree(z);
b = __gdtoa_g__fmt(buf, s, se, decpt, z->sign, bufsize);
__gdtoa_Bfree(z);
return b;
}

View file

@ -80,5 +80,5 @@ g_dfmt(char *buf, double *d, int ndig, size_t bufsize)
if (sign)
i = STRTOG_Normal | STRTOG_Neg;
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -58,20 +58,20 @@ g_dfmt_p(char *buf, double *d, int ndig, size_t bufsize, int nik)
b = buf;
if (L[1] & 0x80000000L && nik < 18)
*b++ = '-';
b = stpcpy(b, NanName[nik%3]);
b = stpcpy(b, __gdtoa_NanName[nik%3]);
if (nik > 5 && (nik < 12
|| bits[0] != __gdtoa_NanDflt_d[0]
|| (bits[1] ^ __gdtoa_NanDflt_d[1]) & 0xfffff)) {
bits[0] = L[0];
bits[1] = L[1] & 0xfffff;
b = add_nanbits(b, bufsize - (b-buf), bits, 2);
b = __gdtoa_add_nanbits(b, bufsize - (b-buf), bits, 2);
}
return b;
}
b = buf;
if (sign)
*b++ = '-';
return stpcpy(b, InfName[nik%6]);
return stpcpy(b, __gdtoa_InfName[nik%6]);
}
if (L[0] == 0 && (L[1] ^ sign) == 0 /*d == 0.*/) {
b = buf;
@ -95,5 +95,5 @@ g_dfmt_p(char *buf, double *d, int ndig, size_t bufsize, int nik)
if (sign)
i = STRTOG_Normal | STRTOG_Neg;
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -78,5 +78,5 @@ g_ffmt(char *buf, float *f, int ndig, size_t bufsize)
}
i = STRTOG_Normal;
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -56,16 +56,16 @@ g_ffmt_p(char *buf, float *f, int ndig, size_t bufsize, int nik)
b = buf;
if (sign && nik < 18)
*b++ = '-';
b = stpcpy(b, NanName[nik%3]);
b = stpcpy(b, __gdtoa_NanName[nik%3]);
if (nik > 5 && (nik < 12
|| (bits[0] ^ __gdtoa_NanDflt_f[0]) & 0x7fffff))
b = add_nanbits(b, bufsize - (b-buf), bits, 1);
b = __gdtoa_add_nanbits(b, bufsize - (b-buf), bits, 1);
return b;
}
b = buf;
if (sign)
*b++ = '-';
return stpcpy(b, InfName[nik%6]);
return stpcpy(b, __gdtoa_InfName[nik%6]);
}
if (*f == 0.) {
b = buf;
@ -89,5 +89,5 @@ g_ffmt_p(char *buf, float *f, int ndig, size_t bufsize, int nik)
}
i = STRTOG_Normal;
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -84,5 +84,5 @@ g_xfmt(char *buf, void *V, int ndig, size_t bufsize)
mode = 0;
}
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -60,20 +60,20 @@ g_xfmt_p(char *buf, void *V, int ndig, size_t bufsize, int nik)
b = buf;
if (sign)
*b++ = '-';
b = stpcpy(b, InfName[nik%6]);
b = stpcpy(b, __gdtoa_InfName[nik%6]);
}
else {
b = buf;
if (sign && nik < 18)
*b++ = '-';
b = stpcpy(b, NanName[nik%3]);
b = stpcpy(b, __gdtoa_NanName[nik%3]);
if (nik > 5 && (nik < 12
|| L[3] != __gdtoa_NanDflt_ldus[3]
|| L[2] != __gdtoa_NanDflt_ldus[2]
|| L[1] != __gdtoa_NanDflt_ldus[1]
|| L[0] != __gdtoa_NanDflt_ldus[0])) {
bits[1] &= 0x7fffffff;
b = add_nanbits(b, bufsize - (b-buf), bits, 2);
b = __gdtoa_add_nanbits(b, bufsize - (b-buf), bits, 2);
}
}
return b;
@ -100,5 +100,5 @@ g_xfmt_p(char *buf, void *V, int ndig, size_t bufsize, int nik)
mode = 0;
}
s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se);
return g__fmt(buf, s, se, decpt, sign, bufsize);
return __gdtoa_g__fmt(buf, s, se, decpt, sign, bufsize);
}

View file

@ -44,7 +44,7 @@ bitstob(ULong *bits, int nbits, int *bbits)
i <<= 1;
k++;
}
b = Balloc(k);
b = __gdtoa_Balloc(k);
be = bits + ((nbits - 1) >> kshift);
x = x0 = b->x;
do {
@ -71,12 +71,12 @@ ret:
* Modifications:
* 1. Rather than iterating, we use a simple numeric overestimate
* to determine k = floor(log10(d)). We scale relevant
* quantities using O(log2(k)) rather than O(k) multiplications.
* quantities using O(log2(k)) rather than O(k) __gdtoa_multiplications.
* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
* try to generate digits strictly left to right. Instead, we
* compute with fewer bits and propagate the carry if necessary
* when rounding the final digit up. This is often faster.
* 3. Under the assumption that input will be rounded nearest,
* 3. Under the as__gdtoa_sumption that input will be rounded nearest,
* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
* That is, we allow equality in stopping tests when the
* round-nearest rule will give the same floating-point value
@ -86,10 +86,10 @@ ret:
* quantities.
* 5. When converting floating-point integers less than 1e16,
* we use floating-point arithmetic rather than resorting
* to multiple-precision integers.
* to __gdtoa_multiple-precision integers.
* 6. When asked to produce fewer than 15 digits, we first try
* to get by with floating-point arithmetic; we resort to
* multiple-precision integer arithmetic only if we cannot
* __gdtoa_multiple-precision integer arithmetic only if we cannot
* guarantee that the floating-point calculation has given
* the correctly rounded result. For k requested digits and
* "uniformly" distributed input, the probability is
@ -125,7 +125,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
2 + (mode & 1). These modes are mainly for
debugging; often they run slower but sometimes
faster than modes 2-3.
4,5,8,9 ==> left-to-right digit generation.
4,5,8,9 ==> left-to-right digit gene__gdtoa_ration.
6-9 ==> don't try fast floating-point estimate
(if applicable).
@ -153,27 +153,27 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
break;
case STRTOG_Infinite:
*decpt = -32768;
return nrv_alloc("Infinity", rve, 8);
return __gdtoa_nrv_alloc("Infinity", rve, 8);
case STRTOG_NaN:
*decpt = -32768;
return nrv_alloc("NaN", rve, 3);
return __gdtoa_nrv_alloc("NaN", rve, 3);
default:
return 0;
}
b = bitstob(bits, nbits = fpi->nbits, &bbits);
be0 = be;
if ( (i = trailz(b)) !=0) {
rshift(b, i);
if ( (i = __gdtoa_trailz(b)) !=0) {
__gdtoa_rshift(b, i);
be += i;
bbits -= i;
}
if (!b->wds) {
Bfree(b);
__gdtoa_Bfree(b);
ret_zero:
*decpt = 1;
return nrv_alloc("0", rve, 1);
return __gdtoa_nrv_alloc("0", rve, 1);
}
dval(&d) = b2d(b, &i);
dval(&d) = __gdtoa_b2d(b, &i);
i = be + bbits - 1;
word0(&d) &= Frac_mask1;
word0(&d) |= Exp_11;
@ -190,7 +190,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
* We want k to be too large rather than too small.
* The error in the first-order Taylor series approximation
* is in our favor, so we just round up the constant enough
* to compensate for any error in the multiplication of
* to compensate for any error in the __gdtoa_multiplication of
* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
* adding 1e-13 to the constant term more than suffices.
@ -199,7 +199,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
* but this is probably not worthwhile.)
*/
ds = (dval(&d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
/* correct assumption about exponent range */
/* correct as__gdtoa_sumption about exponent range */
if ((j = i) < 0)
j = -j;
if ((j -= 1077) > 0)
@ -226,7 +226,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
// 401173: _start at libc/crt/crt.S:67
word0(&d) += (unsigned)(be + bbits - 1) << Exp_shift;
if (k >= 0 && k <= Ten_pmax) {
if (dval(&d) < tens[k])
if (dval(&d) < __gdtoa_tens[k])
k--;
k_check = 0;
}
@ -285,7 +285,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
if (i <= 0)
i = 1;
}
s = s0 = rv_alloc(i);
s = s0 = __gdtoa_rv_alloc(i);
if (mode <= 1)
rdir = 0;
else if ( (rdir = fpi->rounding - 1) !=0) {
@ -303,28 +303,28 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
ilim0 = ilim;
ieps = 2; /* conservative */
if (k > 0) {
ds = tens[k&0xf];
ds = __gdtoa_tens[k&0xf];
j = k >> 4;
if (j & Bletch) {
/* prevent overflows */
j &= Bletch - 1;
dval(&d) /= bigtens[n_bigtens-1];
dval(&d) /= __gdtoa_bigtens[n___gdtoa_bigtens-1];
ieps++;
}
for(; j; j >>= 1, i++)
if (j & 1) {
ieps++;
ds *= bigtens[i];
ds *= __gdtoa_bigtens[i];
}
}
else {
ds = 1.;
if ( (j1 = -k) !=0) {
dval(&d) *= tens[j1 & 0xf];
dval(&d) *= __gdtoa_tens[j1 & 0xf];
for(j = j1 >> 4; j; j >>= 1, i++)
if (j & 1) {
ieps++;
dval(&d) *= bigtens[i];
dval(&d) *= __gdtoa_bigtens[i];
}
}
}
@ -351,7 +351,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
/* Use Steele & White method of only
* generating digits needed.
*/
dval(&eps) = ds*0.5/tens[ilim-1] - dval(&eps);
dval(&eps) = ds*0.5/__gdtoa_tens[ilim-1] - dval(&eps);
for(i = 0;;) {
L = (Long)(dval(&d)/ds);
dval(&d) -= L*ds;
@ -371,7 +371,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
}
else {
/* Generate ilim digits, then fix them up. */
dval(&eps) *= tens[ilim-1];
dval(&eps) *= __gdtoa_tens[ilim-1];
for(i = 1;; i++, dval(&d) *= 10.) {
if ( (L = (Long)(dval(&d)/ds)) !=0)
dval(&d) -= L*ds;
@ -398,7 +398,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
/* Do we have a "small" integer? */
if (be >= 0 && k <= fpi->int_max) {
/* Yes. */
ds = tens[k];
ds = __gdtoa_tens[k];
if (ndigits < 0 && ilim <= 0) {
S = mhi = 0;
if (ilim < 0 || dval(&d) <= 5*ds)
@ -471,7 +471,7 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
}
b2 += i;
s2 += i;
mhi = i2b(1);
mhi = __gdtoa_i2b(1);
}
if (m2 > 0 && s2 > 0) {
i = m2 < s2 ? m2 : s2;
@ -482,20 +482,20 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
if (b5 > 0) {
if (leftright) {
if (m5 > 0) {
mhi = pow5mult(mhi, m5);
b1 = mult(mhi, b);
Bfree(b);
mhi = __gdtoa_pow5mult(mhi, m5);
b1 = __gdtoa_mult(mhi, b);
__gdtoa_Bfree(b);
b = b1;
}
if ( (j = b5 - m5) !=0)
b = pow5mult(b, j);
b = __gdtoa_pow5mult(b, j);
}
else
b = pow5mult(b, b5);
b = __gdtoa_pow5mult(b, b5);
}
S = i2b(1);
S = __gdtoa_i2b(1);
if (s5 > 0)
S = pow5mult(S, s5);
S = __gdtoa_pow5mult(S, s5);
/* Check for special case that d is a normalized power of 2. */
spec_case = 0;
if (mode < 2) {
@ -510,26 +510,26 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
* shift left if necessary so divisor has 4 leading 0 bits.
*
* Perhaps we should just compute leading 28 bits of S once
* and for all and pass them and a shift to quorem, so it
* and for all and pass them and a shift to __gdtoa_quorem, so it
* can do shifts and ors to compute the numerator for q.
*/
i = ((s5 ? hi0bits(S->x[S->wds-1]) : ULbits - 1) - s2 - 4) & kmask;
m2 += i;
if ((b2 += i) > 0)
b = lshift(b, b2);
b = __gdtoa_lshift(b, b2);
if ((s2 += i) > 0)
S = lshift(S, s2);
S = __gdtoa_lshift(S, s2);
if (k_check) {
if (cmp(b,S) < 0) {
if (__gdtoa_cmp(b,S) < 0) {
k--;
b = multadd(b, 10, 0); /* we botched the k estimate */
b = __gdtoa_multadd(b, 10, 0); /* we botched the k estimate */
if (leftright)
mhi = multadd(mhi, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
ilim = ilim1;
}
}
if (ilim <= 0 && mode > 2) {
if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
if (ilim < 0 || __gdtoa_cmp(b,S = __gdtoa_multadd(S,5,0)) <= 0) {
/* no digits, fcvt style */
no_digits:
k = -1 - ndigits;
@ -544,25 +544,25 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
}
if (leftright) {
if (m2 > 0)
mhi = lshift(mhi, m2);
mhi = __gdtoa_lshift(mhi, m2);
/* Compute mlo -- check for special case
* that d is a normalized power of 2.
*/
mlo = mhi;
if (spec_case) {
mhi = Balloc(mhi->k);
mhi = __gdtoa_Balloc(mhi->k);
Bcopy(mhi, mlo);
mhi = lshift(mhi, 1);
mhi = __gdtoa_lshift(mhi, 1);
}
for(i = 1;;i++) {
dig = quorem(b,S) + '0';
dig = __gdtoa_quorem(b,S) + '0';
/* Do we yet have the shortest decimal string
* that will round to d?
*/
j = cmp(b, mlo);
delta = diff(S, mhi);
j1 = delta->sign ? 1 : cmp(b, delta);
Bfree(delta);
j = __gdtoa_cmp(b, mlo);
delta = __gdtoa_diff(S, mhi);
j1 = delta->sign ? 1 : __gdtoa_cmp(b, delta);
__gdtoa_Bfree(delta);
if (j1 == 0 && !mode && !(bits[0] & 1) && !rdir) {
if (dig == '9')
goto round_9_up;
@ -583,14 +583,14 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
inex = STRTOG_Inexlo;
goto accept;
}
while (cmp(S,mhi) > 0) {
while (__gdtoa_cmp(S,mhi) > 0) {
*s++ = dig;
mhi1 = multadd(mhi, 10, 0);
mhi1 = __gdtoa_multadd(mhi, 10, 0);
if (mlo == mhi)
mlo = mhi1;
mhi = mhi1;
b = multadd(b, 10, 0);
dig = quorem(b,S) + '0';
b = __gdtoa_multadd(b, 10, 0);
dig = __gdtoa_quorem(b,S) + '0';
}
if (dig++ == '9')
goto round_9_up;
@ -598,8 +598,8 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
goto accept;
}
if (j1 > 0) {
b = lshift(b, 1);
j1 = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j1 = __gdtoa_cmp(b, S);
if ((j1 > 0 || (j1 == 0 && dig & 1)) && dig++ == '9')
goto round_9_up;
inex = STRTOG_Inexhi;
@ -624,21 +624,21 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
*s++ = dig;
if (i == ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
if (mlo == mhi)
mlo = mhi = multadd(mhi, 10, 0);
mlo = mhi = __gdtoa_multadd(mhi, 10, 0);
else {
mlo = multadd(mlo, 10, 0);
mhi = multadd(mhi, 10, 0);
mlo = __gdtoa_multadd(mlo, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
}
}
}
else
for(i = 1;; i++) {
*s++ = dig = quorem(b,S) + '0';
*s++ = dig = __gdtoa_quorem(b,S) + '0';
if (i >= ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
}
/* Round off last digit */
if (rdir) {
@ -646,8 +646,8 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
goto chopzeros;
goto roundoff;
}
b = lshift(b, 1);
j = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j = __gdtoa_cmp(b, S);
if (j > 0 || (j == 0 && dig & 1))
{
roundoff:
@ -666,16 +666,16 @@ gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in
inex = STRTOG_Inexlo;
}
ret:
Bfree(S);
__gdtoa_Bfree(S);
if (mhi) {
if (mlo && mlo != mhi)
Bfree(mlo);
Bfree(mhi);
__gdtoa_Bfree(mlo);
__gdtoa_Bfree(mhi);
}
ret1:
while(s > s0 && s[-1] == '0')
--s;
Bfree(b);
__gdtoa_Bfree(b);
*s = 0;
*decpt = k + 1;
if (rve)

View file

@ -169,9 +169,6 @@ asm(".include \"libc/disclaimer.inc\"");
* floating-point constants.
* #define -DNO_ERRNO to suppress setting errno (in strtod.c and
* strtodg.c).
* #define NO_STRING_H to use private versions of memcpy.
* On some K&R systems, it may also be necessary to
* #define DECLARE_SIZE_T in this case.
* #define USE_LOCALE to use the current locale's decimal_point value.
*/
@ -197,24 +194,6 @@ typedef unsigned short UShort;
}
#endif
/* #ifdef KR_headers */
/* #define Char char */
/* #else */
#define Char void
/* #endif */
#ifdef MALLOC
extern Char *MALLOC(size_t);
#else
#define MALLOC malloc
#endif
#ifdef REALLOC
extern Char *REALLOC(Char *, size_t);
#else
#define REALLOC realloc
#endif
#undef IEEE_Arith
#undef Avoid_Underflow
#ifdef IEEE_MC68k
@ -230,16 +209,8 @@ extern Char *REALLOC(Char *, size_t);
#endif /* Bad_float_h */
#ifdef IEEE_Arith
#define Scale_Bit 0x10
#define n_bigtens 5
#endif
#ifdef IBM
#define n_bigtens 3
#endif
#ifdef VAX
#define n_bigtens 2
#define Scale_Bit 0x10
#define n___gdtoa_bigtens 5
#endif
typedef union {
@ -334,7 +305,7 @@ extern double rnd_prod(double, double), rnd_quot(double, double);
#define Pack_16
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
* This makes some inner loops simpler and sometimes saves work
* during multiplications, but it often seems to make things slightly
* during __gdtoa_multiplications, but it often seems to make things slightly
* slower. Hence the default is now to store 32 bits per Long.
*/
#endif
@ -374,111 +345,56 @@ typedef struct ThInfo {
Bigint *P5s;
} ThInfo;
#ifdef NO_STRING_H
#ifdef DECLARE_SIZE_T
typedef unsigned int size_t;
#endif
extern void __gdtoa_memcpy(void *, const void *, size_t);
#define Bcopy(x, y) \
__gdtoa_memcpy(&x->sign, &y->sign, y->wds * sizeof(ULong) + 2 * sizeof(int))
#else /* !NO_STRING_H */
#define Bcopy(x, y) \
memcpy(&x->sign, &y->sign, y->wds * sizeof(ULong) + 2 * sizeof(int))
#endif /* NO_STRING_H */
#define Balloc __gdtoa_Balloc
#define Bfree __gdtoa_Bfree
#define InfName __gdtoa_InfName
#define NanName __gdtoa_NanName
#define ULtoQ __gdtoa_ULtoQ
#define ULtof __gdtoa_ULtof
#define ULtod __gdtoa_ULtod
#define ULtodd __gdtoa_ULtodd
#define ULtox __gdtoa_ULtox
#define ULtoxL __gdtoa_ULtoxL
#define add_nanbits __gdtoa_add_nanbits
#define any_on __gdtoa_any_on
#define b2d __gdtoa_b2d
#define bigtens __gdtoa_bigtens
#define cmp __gdtoa_cmp
#define copybits __gdtoa_copybits
#define d2b __gdtoa_d2b
#define decrement __gdtoa_decrement
#define diff __gdtoa_diff
#define dtoa_result __gdtoa_dtoa_result
#define g__fmt __gdtoa_g__fmt
#define gethex __gdtoa_gethex
#define hexdig __gdtoa_hexdig
#define hexnan __gdtoa_hexnan
#define i2b __gdtoa_i2b
#define increment __gdtoa_increment
#define lshift __gdtoa_lshift
#define match __gdtoa_match
#define mult __gdtoa_mult
#define multadd __gdtoa_multadd
#define nrv_alloc __gdtoa_nrv_alloc
#define pow5mult __gdtoa_pow5mult
#define quorem __gdtoa_quorem
#define ratio __gdtoa_ratio
#define rshift __gdtoa_rshift
#define rv_alloc __gdtoa_rv_alloc
#define s2b __gdtoa_s2b
#define set_ones __gdtoa_set_ones
#define strtoIg __gdtoa_strtoIg
#define sum __gdtoa_sum
#define tens __gdtoa_tens
#define tinytens __gdtoa_tinytens
#define tinytens __gdtoa_tinytens
#define trailz __gdtoa_trailz
#define ulp __gdtoa_ulp
hidden extern char *__gdtoa_dtoa_result;
hidden extern const double __gdtoa_bigtens[];
hidden extern const double __gdtoa_tens[];
hidden extern const double __gdtoa_tinytens[];
hidden extern const unsigned char __gdtoa_hexdig[];
hidden extern const char *const __gdtoa_InfName[6];
hidden extern const char *const __gdtoa_NanName[3];
extern char *add_nanbits(char *, size_t, ULong *, int);
hidden extern char *dtoa_result;
hidden extern const double bigtens[];
hidden extern const double tens[];
hidden extern const double tinytens[];
hidden extern const unsigned char hexdig[];
hidden extern const char *const InfName[6];
hidden extern const char *const NanName[3];
Bigint *Balloc(int);
void Bfree(Bigint *);
void ULtof(ULong *, ULong *, Long, int);
void ULtod(ULong *, ULong *, Long, int);
void ULtodd(ULong *, ULong *, Long, int);
void ULtoQ(ULong *, ULong *, Long, int);
void ULtox(UShort *, ULong *, Long, int);
void ULtoxL(ULong *, ULong *, Long, int);
ULong any_on(Bigint *, int);
double b2d(Bigint *, int *);
int cmp(Bigint *, Bigint *);
void copybits(ULong *, int, Bigint *);
Bigint *d2b(double, int *, int *);
void decrement(Bigint *);
Bigint *diff(Bigint *, Bigint *);
char *g__fmt(char *, char *, char *, int, ULong, size_t);
int gethex(const char **, const FPI *, Long *, Bigint **, int);
Bigint *__gdtoa_Balloc(int);
Bigint *__gdtoa_d2b(double, int *, int *);
Bigint *__gdtoa_diff(Bigint *, Bigint *);
Bigint *__gdtoa_i2b(int);
Bigint *__gdtoa_increment(Bigint *);
Bigint *__gdtoa_lshift(Bigint *, int);
Bigint *__gdtoa_mult(Bigint *, Bigint *);
Bigint *__gdtoa_multadd(Bigint *, int, int);
Bigint *__gdtoa_s2b(const char *, int, int, ULong, int);
Bigint *__gdtoa_set_ones(Bigint *, int);
Bigint *__gdtoa_sum(Bigint *, Bigint *);
Bigint *__gdtoa_pow5mult(Bigint *, int);
ULong __gdtoa_any_on(Bigint *, int);
char *__gdtoa_add_nanbits(char *, size_t, ULong *, int);
char *__gdtoa_g__fmt(char *, char *, char *, int, ULong, size_t);
char *__gdtoa_nrv_alloc(char *, char **, int);
char *__gdtoa_rv_alloc(int);
double __gdtoa_b2d(Bigint *, int *);
double __gdtoa_ratio(Bigint *, Bigint *);
double __gdtoa_ulp(U *);
int __gdtoa_cmp(Bigint *, Bigint *);
int __gdtoa_gethex(const char **, const FPI *, Long *, Bigint **, int);
int __gdtoa_hexnan(const char **, const FPI *, ULong *);
int __gdtoa_match(const char **, char *);
int __gdtoa_quorem(Bigint *, Bigint *);
int __gdtoa_strtoIg(const char *, char **, const FPI *, Long *, Bigint **,
int *);
int __gdtoa_trailz(Bigint *);
void __gdtoa_Bfree(Bigint *);
void __gdtoa_ULtoQ(ULong *, ULong *, Long, int);
void __gdtoa_ULtod(ULong *, ULong *, Long, int);
void __gdtoa_ULtodd(ULong *, ULong *, Long, int);
void __gdtoa_ULtof(ULong *, ULong *, Long, int);
void __gdtoa_ULtox(UShort *, ULong *, Long, int);
void __gdtoa_ULtoxL(ULong *, ULong *, Long, int);
void __gdtoa_copybits(ULong *, int, Bigint *);
void __gdtoa_decrement(Bigint *);
void __gdtoa_hexdig_init(void);
int hexnan(const char **, const FPI *, ULong *);
Bigint *i2b(int);
Bigint *increment(Bigint *);
Bigint *lshift(Bigint *, int);
int match(const char **, char *);
Bigint *mult(Bigint *, Bigint *);
Bigint *multadd(Bigint *, int, int);
char *nrv_alloc(char *, char **, int);
Bigint *pow5mult(Bigint *, int);
int quorem(Bigint *, Bigint *);
double ratio(Bigint *, Bigint *);
void rshift(Bigint *, int);
char *rv_alloc(int);
Bigint *s2b(const char *, int, int, ULong, int);
Bigint *set_ones(Bigint *, int);
int strtoIg(const char *, char **, const FPI *, Long *, Bigint **, int *);
Bigint *sum(Bigint *, Bigint *);
int trailz(Bigint *);
double ulp(U *);
void __gdtoa_rshift(Bigint *, int);
forceinline int lo0bits(ULong *y) {
int k;

View file

@ -34,14 +34,15 @@
/* clang-format off */
int
gethex( const char **sp, const FPI *fpi, Long *exp, Bigint **bp, int sign)
__gdtoa_gethex(const char **sp, const FPI *fpi,
Long *exp, Bigint **bp, int sign)
{
Bigint *b;
const unsigned char *decpt, *s0, *s, *s1;
int big, esign, havedig, irv, j, k, n, n0, nbits, up, zret;
ULong L, lostbits, *x;
Long e, e1;
/**** if (!hexdig['0']) __gdtoa_hexdig_init(); ****/
/**** if (!__gdtoa_hexdig['0']) __gdtoa_hexdig_init(); ****/
*bp = 0;
havedig = 0;
s0 = *(const unsigned char **)sp + 2;
@ -52,27 +53,27 @@ gethex( const char **sp, const FPI *fpi, Long *exp, Bigint **bp, int sign)
decpt = 0;
zret = 0;
e = 0;
if (hexdig[*s])
if (__gdtoa_hexdig[*s])
havedig++;
else {
zret = 1;
if (*s != '.')
goto pcheck;
decpt = ++s;
if (!hexdig[*s])
if (!__gdtoa_hexdig[*s])
goto pcheck;
while(*s == '0')
s++;
if (hexdig[*s])
if (__gdtoa_hexdig[*s])
zret = 0;
havedig = 1;
s0 = s;
}
while(hexdig[*s])
while(__gdtoa_hexdig[*s])
s++;
if (*s == '.' && !decpt) {
decpt = ++s;
while(hexdig[*s])
while(__gdtoa_hexdig[*s])
s++;
}/*}*/
if (decpt)
@ -90,12 +91,12 @@ pcheck:
case '+':
s++;
}
if ((n = hexdig[*s]) == 0 || n > 0x19) {
if ((n = __gdtoa_hexdig[*s]) == 0 || n > 0x19) {
s = s1;
break;
}
e1 = n - 0x10;
while((n = hexdig[*++s]) !=0 && n <= 0x19) {
while((n = __gdtoa_hexdig[*++s]) !=0 && n <= 0x19) {
if (e1 & 0xf8000000)
big = 1;
e1 = 10*e1 + n - 0x10;
@ -123,7 +124,7 @@ pcheck:
}
goto retz;
ret_tiny:
b = Balloc(0);
b = __gdtoa_Balloc(0);
b->wds = 1;
b->x[0] = 1;
goto dret;
@ -145,7 +146,7 @@ pcheck:
if (nbits & kmask)
++n;
for(j = n, k = 0; j >>= 1; ++k);
*bp = b = Balloc(k);
*bp = b = __gdtoa_Balloc(k);
b->wds = n;
for(j = 0; j < n0; ++j)
b->x[j] = ALL_ON;
@ -157,7 +158,7 @@ pcheck:
n = s1 - s0 - 1;
for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
k++;
b = Balloc(k);
b = __gdtoa_Balloc(k);
x = b->x;
n = 0;
L = 0;
@ -169,7 +170,7 @@ pcheck:
L = 0;
n = 0;
}
L |= (hexdig[*s1] & 0x0f) << n;
L |= (__gdtoa_hexdig[*s1] & 0x0f) << n;
n += 4;
}
*x++ = L;
@ -180,27 +181,27 @@ pcheck:
x = b->x;
if (n > nbits) {
n -= nbits;
if (any_on(b,n)) {
if (__gdtoa_any_on(b,n)) {
lostbits = 1;
k = n - 1;
if (x[k>>kshift] & 1 << (k & kmask)) {
lostbits = 2;
if (k > 0 && any_on(b,k))
if (k > 0 && __gdtoa_any_on(b,k))
lostbits = 3;
}
}
rshift(b, n);
__gdtoa_rshift(b, n);
e += n;
}
else if (n < nbits) {
n = nbits - n;
b = lshift(b, n);
b = __gdtoa_lshift(b, n);
e -= n;
x = b->x;
}
if (e > fpi->emax) {
ovfl:
Bfree(b);
__gdtoa_Bfree(b);
ovfl1:
errno = ERANGE;
switch (fpi->rounding) {
@ -223,7 +224,7 @@ pcheck:
if (n >= nbits) {
switch (fpi->rounding) {
case FPI_Round_near:
if (n == nbits && (n < 2 || lostbits || any_on(b,n-1)))
if (n == nbits && (n < 2 || lostbits || __gdtoa_any_on(b,n-1)))
goto one_bit;
break;
case FPI_Round_up:
@ -242,7 +243,7 @@ pcheck:
| STRTOG_Underflow;
}
}
Bfree(b);
__gdtoa_Bfree(b);
retz:
errno = ERANGE;
return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
@ -251,11 +252,11 @@ pcheck:
if (lostbits)
lostbits = 1;
else if (k > 0)
lostbits = any_on(b,k);
lostbits = __gdtoa_any_on(b,k);
if (x[k>>kshift] & 1 << (k & kmask))
lostbits |= 2;
nbits -= n;
rshift(b,n);
__gdtoa_rshift(b,n);
e = fpi->emin;
}
if (lostbits) {
@ -276,7 +277,7 @@ pcheck:
}
if (up) {
k = b->wds;
b = increment(b);
b = __gdtoa_increment(b);
x = b->x;
if (irv == STRTOG_Denormal) {
if (nbits == fpi->nbits - 1
@ -286,7 +287,7 @@ pcheck:
else if (b->wds > k
|| ((n = nbits & kmask) !=0
&& hi0bits(x[k-1]) < 32-n)) {
rshift(b,1);
__gdtoa_rshift(b,1);
if (++e > fpi->emax)
goto ovfl;
}

View file

@ -33,7 +33,7 @@
/* clang-format off */
void
rshift(Bigint *b, int k)
__gdtoa_rshift(Bigint *b, int k)
{
ULong *x, *x1, *xe, y;
int n;
@ -61,7 +61,7 @@ rshift(Bigint *b, int k)
}
int
trailz(Bigint *b)
__gdtoa_trailz(Bigint *b)
{
ULong L, *x, *xe;
int n = 0;

View file

@ -30,45 +30,22 @@
*/
#include "third_party/gdtoa/gdtoa.internal.h"
/* clang-format off */
#if 0
unsigned char hexdig[256];
static void
htinit(unsigned char *h, unsigned char *s, int inc)
{
int i, j;
for(i = 0; (j = s[i]) !=0; i++)
h[j] = i + inc;
}
void
__gdtoa_hexdig_init(Void) /* Use of hexdig_init omitted 20121220 to avoid a */
/* race condition when multiple threads are used. */
{
#define USC (unsigned char *)
htinit(hexdig, USC "0123456789", 0x10);
htinit(hexdig, USC "abcdef", 0x10 + 10);
htinit(hexdig, USC "ABCDEF", 0x10 + 10);
}
#else
const unsigned char hexdig[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
#endif
const unsigned char __gdtoa_hexdig[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, //
0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
};

View file

@ -46,12 +46,12 @@ L_shift(ULong *x, ULong *x1, int i)
}
int
hexnan( const char **sp, const FPI *fpi, ULong *x0)
__gdtoa_hexnan( const char **sp, const FPI *fpi, ULong *x0)
{
ULong c, h, *x, *x1, *xe;
const char *s;
int havedig, hd0, i, nbits;
/**** if (!hexdig['0']) __gdtoa_hexdig_init(); ****/
/**** if (!__gdtoa_hexdig['0']) __gdtoa_hexdig_init(); ****/
nbits = fpi->nbits;
x = x0 + (nbits >> kshift);
if (nbits & kmask)
@ -70,7 +70,7 @@ hexnan( const char **sp, const FPI *fpi, ULong *x0)
&& *(const unsigned char*)(s+3) > ' ')
s += 2;
while((c = *(const unsigned char*)++s)) {
if (!(h = hexdig[c])) {
if (!(h = __gdtoa_hexdig[c])) {
if (c <= ' ') {
if (hd0 < havedig) {
if (x < x1 && i < 8)

View file

@ -29,43 +29,85 @@
THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "third_party/gdtoa/gdtoa.internal.h"
/* clang-format off */
static ThInfo TI0;
Bigint *
Balloc(int k)
static void
__gdtoa_Brelease(Bigint *rv)
{
if (!rv) return;
__gdtoa_Brelease(rv->next);
free(rv);
}
static void
Bclear(void)
{
int i;
for (i = 0; i < ARRAYLEN(TI0.Freelist); ++i)
__gdtoa_Brelease(TI0.Freelist[i]);
bzero(&TI0.Freelist, sizeof(TI0.Freelist));
}
Bigint *
__gdtoa_Balloc(int k)
{
#if 0
int x;
Bigint *rv;
if (k <= Kmax && (rv = TI0.Freelist[k]) != 0)
x = 1 << k;
rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(ULong));
rv->k = k;
rv->maxwds = x;
rv->sign = 0;
rv->wds = 0;
return rv;
#else
int x;
Bigint *rv;
static char once;
if (!once) {
atexit(Bclear);
once = 1;
}
if (k <= Kmax && (rv = TI0.Freelist[k]) != 0) {
TI0.Freelist[k] = rv->next;
else {
} else {
x = 1 << k;
rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(ULong));
rv->k = k;
rv->maxwds = x;
}
rv->sign = rv->wds = 0;
rv->sign = 0;
rv->wds = 0;
return rv;
#endif
}
void
Bfree(Bigint *v)
__gdtoa_Bfree(Bigint *v)
{
#if 0
free(v);
#else
if (v) {
if (v->k > Kmax)
if (v->k > Kmax) {
free((void*)v);
else {
} else {
v->next = TI0.Freelist[v->k];
TI0.Freelist[v->k] = v;
}
}
#endif
}
Bigint *
multadd(Bigint *b, int m, int a) /* multiply by m and add a */
__gdtoa_multadd(Bigint *b, int m, int a) /* multiply by m and add a */
{
int i, wds;
ULong *x;
@ -83,9 +125,9 @@ multadd(Bigint *b, int m, int a) /* multiply by m and add a */
while(++i < wds);
if (carry) {
if (wds >= b->maxwds) {
b1 = Balloc(b->k+1);
b1 = __gdtoa_Balloc(b->k+1);
Bcopy(b1, b);
Bfree(b);
__gdtoa_Bfree(b);
b = b1;
}
b->x[wds++] = carry;
@ -95,17 +137,17 @@ multadd(Bigint *b, int m, int a) /* multiply by m and add a */
}
Bigint *
i2b(int i)
__gdtoa_i2b(int i)
{
Bigint *b;
b = Balloc(1);
b = __gdtoa_Balloc(1);
b->x[0] = i;
b->wds = 1;
return b;
}
Bigint *
mult(Bigint *a, Bigint *b)
__gdtoa_mult(Bigint *a, Bigint *b)
{
Bigint *c;
int k, wa, wb, wc;
@ -123,7 +165,7 @@ mult(Bigint *a, Bigint *b)
wc = wa + wb;
if (wc > a->maxwds)
k++;
c = Balloc(k);
c = __gdtoa_Balloc(k);
for(x = c->x, xa = x + wc; x < xa; x++)
*x = 0;
xa = a->x;
@ -150,31 +192,37 @@ mult(Bigint *a, Bigint *b)
return c;
}
static void
__gdtoa_pow5mult_destroy(void) {
__gdtoa_Brelease(TI0.P5s);
TI0.P5s = 0;
}
Bigint *
pow5mult(Bigint *b, int k)
__gdtoa_pow5mult(Bigint *b, int k)
{
Bigint *b1, *p5, *p51;
int i;
static const int p05[3] = { 5, 25, 125 };
if ( (i = k & 3) !=0)
b = multadd(b, p05[i-1], 0);
if ((i = k & 3))
b = __gdtoa_multadd(b, p05[i-1], 0);
if (!(k >>= 2))
return b;
if ((p5 = TI0.P5s) == 0) {
/* first time */
p5 = TI0.P5s = i2b(625);
if (!(p5 = TI0.P5s)) {
p5 = TI0.P5s = __gdtoa_i2b(625);
p5->next = 0;
atexit(__gdtoa_pow5mult_destroy);
}
for(;;) {
if (k & 1) {
b1 = mult(b, p5);
Bfree(b);
b1 = __gdtoa_mult(b, p5);
__gdtoa_Bfree(b);
b = b1;
}
if (!(k >>= 1))
break;
if ((p51 = p5->next) == 0) {
p51 = p5->next = mult(p5,p5);
p51 = p5->next = __gdtoa_mult(p5,p5);
p51->next = 0;
}
p5 = p51;
@ -183,7 +231,7 @@ pow5mult(Bigint *b, int k)
}
Bigint *
lshift(Bigint *b, int k)
__gdtoa_lshift(Bigint *b, int k)
{
int i, k1, n, n1;
Bigint *b1;
@ -193,7 +241,7 @@ lshift(Bigint *b, int k)
n1 = n + b->wds + 1;
for(i = b->maxwds; n1 > i; i <<= 1)
k1++;
b1 = Balloc(k1);
b1 = __gdtoa_Balloc(k1);
x1 = b1->x;
for(i = 0; i < n; i++)
*x1++ = 0;
@ -214,12 +262,12 @@ lshift(Bigint *b, int k)
*x1++ = *x++;
while(x < xe);
b1->wds = n1 - 1;
Bfree(b);
__gdtoa_Bfree(b);
return b1;
}
int
cmp(Bigint *a, Bigint *b)
__gdtoa_cmp(Bigint *a, Bigint *b)
{
ULong *xa, *xa0, *xb, *xb0;
int i, j;
@ -227,9 +275,9 @@ cmp(Bigint *a, Bigint *b)
j = b->wds;
#ifdef DEBUG
if (i > 1 && !a->x[i-1])
Bug("cmp called with a->x[a->wds-1] == 0");
Bug("__gdtoa_cmp called with a->x[a->wds-1] == 0");
if (j > 1 && !b->x[j-1])
Bug("cmp called with b->x[b->wds-1] == 0");
Bug("__gdtoa_cmp called with b->x[b->wds-1] == 0");
#endif
if (i -= j)
return i;
@ -247,15 +295,15 @@ cmp(Bigint *a, Bigint *b)
}
Bigint *
diff(Bigint *a, Bigint *b)
__gdtoa_diff(Bigint *a, Bigint *b)
{
Bigint *c;
int i, wa, wb;
ULong *xa, *xae, *xb, *xbe, *xc;
ULLong borrow, y;
i = cmp(a,b);
i = __gdtoa_cmp(a,b);
if (!i) {
c = Balloc(0);
c = __gdtoa_Balloc(0);
c->wds = 1;
c->x[0] = 0;
return c;
@ -268,7 +316,7 @@ diff(Bigint *a, Bigint *b)
}
else
i = 0;
c = Balloc(a->k);
c = __gdtoa_Balloc(a->k);
c->sign = i;
wa = a->wds;
xa = a->x;
@ -296,7 +344,7 @@ diff(Bigint *a, Bigint *b)
}
double
b2d(Bigint *a, int *e)
__gdtoa_b2d(Bigint *a, int *e)
{
ULong *xa, *xa0, w, y, z;
int k;
@ -305,7 +353,7 @@ b2d(Bigint *a, int *e)
xa = xa0 + a->wds;
y = *--xa;
#ifdef DEBUG
if (!y) Bug("zero y in b2d");
if (!y) Bug("zero y in __gdtoa_b2d");
#endif
k = hi0bits(y);
*e = 32 - k;
@ -330,7 +378,7 @@ ret_d:
}
Bigint *
d2b(double dd, int *e, int *bits)
__gdtoa_d2b(double dd, int *e, int *bits)
{
Bigint *b;
U d;
@ -338,7 +386,7 @@ d2b(double dd, int *e, int *bits)
int de, k;
ULong *x, y, z;
d.d = dd;
b = Balloc(1);
b = __gdtoa_Balloc(1);
x = b->x;
z = word0(&d) & Frac_mask;
word0(&d) &= 0x7fffffff; /* clear sign bit, which we ignore */
@ -371,13 +419,13 @@ d2b(double dd, int *e, int *bits)
}
const double
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
__gdtoa_bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
const double
tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
__gdtoa_tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
const double
tens[] = {
__gdtoa_tens[] = {
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1e20, 1e21, 1e22

View file

@ -33,37 +33,37 @@
/* clang-format off */
Bigint *
s2b(const char *s, int nd0, int nd, ULong y9, int dplen)
__gdtoa_s2b(const char *s, int nd0, int nd, ULong y9, int dplen)
{
Bigint *b;
int i, k;
Long x, y;
x = (nd + 8) / 9;
for(k = 0, y = 1; x > y; y <<= 1, k++) ;
b = Balloc(k);
b = __gdtoa_Balloc(k);
b->x[0] = y9;
b->wds = 1;
i = 9;
if (9 < nd0) {
s += 9;
do b = multadd(b, 10, *s++ - '0');
do b = __gdtoa_multadd(b, 10, *s++ - '0');
while(++i < nd0);
s += dplen;
}
else
s += dplen + 9;
for(; i < nd; i++)
b = multadd(b, 10, *s++ - '0');
b = __gdtoa_multadd(b, 10, *s++ - '0');
return b;
}
double
ratio(Bigint *a, Bigint *b)
__gdtoa_ratio(Bigint *a, Bigint *b)
{
U da, db;
int k, ka, kb;
dval(&da) = b2d(a, &ka);
dval(&db) = b2d(b, &kb);
dval(&da) = __gdtoa_b2d(a, &ka);
dval(&db) = __gdtoa_b2d(b, &kb);
k = ka - kb + ULbits*(a->wds - b->wds);
if (k > 0)
word0(&da) += k*Exp_msk1;
@ -75,7 +75,7 @@ ratio(Bigint *a, Bigint *b)
}
int
match(const char **sp, char *t)
__gdtoa_match(const char **sp, char *t)
{
int c, d;
const char *s = *sp;
@ -90,7 +90,7 @@ match(const char **sp, char *t)
}
void
copybits(ULong *c, int n, Bigint *b)
__gdtoa_copybits(ULong *c, int n, Bigint *b)
{
ULong *ce, *x, *xe;
ce = c + ((n-1) >> kshift) + 1;
@ -103,7 +103,7 @@ copybits(ULong *c, int n, Bigint *b)
}
ULong
any_on(Bigint *b, int k)
__gdtoa_any_on(Bigint *b, int k)
{
int n, nwds;
ULong *x, *x0, x1, x2;

View file

@ -39,14 +39,14 @@ strtoId(const char *s, char **sp, double *f0, double *f1)
Long exp[2];
Bigint *B[2];
int k, rv[2];
B[0] = Balloc(1);
B[0] = __gdtoa_Balloc(1);
B[0]->wds = 2;
k = strtoIg(s, sp, &fpi, exp, B, rv);
ULtod((ULong*)f0, B[0]->x, exp[0], rv[0]);
Bfree(B[0]);
k = __gdtoa_strtoIg(s, sp, &fpi, exp, B, rv);
__gdtoa_ULtod((ULong*)f0, B[0]->x, exp[0], rv[0]);
__gdtoa_Bfree(B[0]);
if (B[1]) {
ULtod((ULong*)f1, B[1]->x, exp[1], rv[1]);
Bfree(B[1]);
__gdtoa_ULtod((ULong*)f1, B[1]->x, exp[1], rv[1]);
__gdtoa_Bfree(B[1]);
}
else {
((ULong*)f1)[0] = ((ULong*)f0)[0];

View file

@ -39,14 +39,14 @@ strtoIdd(const char *s, char **sp, double *f0, double *f1)
Long exp[2];
Bigint *B[2];
int k, rv[2];
B[0] = Balloc(2);
B[0] = __gdtoa_Balloc(2);
B[0]->wds = 4;
k = strtoIg(s, sp, &fpi, exp, B, rv);
ULtodd((ULong*)f0, B[0]->x, exp[0], rv[0]);
Bfree(B[0]);
k = __gdtoa_strtoIg(s, sp, &fpi, exp, B, rv);
__gdtoa_ULtodd((ULong*)f0, B[0]->x, exp[0], rv[0]);
__gdtoa_Bfree(B[0]);
if (B[1]) {
ULtodd((ULong*)f1, B[1]->x, exp[1], rv[1]);
Bfree(B[1]);
__gdtoa_ULtodd((ULong*)f1, B[1]->x, exp[1], rv[1]);
__gdtoa_Bfree(B[1]);
}
else {
((ULong*)f1)[0] = ((ULong*)f0)[0];

View file

@ -39,14 +39,14 @@ strtoIf(const char *s, char **sp, float *f0, float *f1)
Long exp[2];
Bigint *B[2];
int k, rv[2];
B[0] = Balloc(0);
B[0] = __gdtoa_Balloc(0);
B[0]->wds = 1;
k = strtoIg(s, sp, &fpi, exp, B, rv);
ULtof((ULong*)f0, B[0]->x, exp[0], rv[0]);
Bfree(B[0]);
k = __gdtoa_strtoIg(s, sp, &fpi, exp, B, rv);
__gdtoa_ULtof((ULong*)f0, B[0]->x, exp[0], rv[0]);
__gdtoa_Bfree(B[0]);
if (B[1]) {
ULtof((ULong*)f1, B[1]->x, exp[1], rv[1]);
Bfree(B[1]);
__gdtoa_ULtof((ULong*)f1, B[1]->x, exp[1], rv[1]);
__gdtoa_Bfree(B[1]);
}
else
*(ULong*)f1 = *(ULong*)f0;

View file

@ -33,7 +33,7 @@
/* clang-format off */
int
strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *rvp)
__gdtoa_strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *rvp)
{
Bigint *b, *b1;
int i, nb, nw, nw1, rv, rv1, swap;
@ -47,7 +47,7 @@ strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *
}
e1 = exp[0];
rv1 = rv ^ STRTOG_Inexact;
b1 = Balloc(b->k);
b1 = __gdtoa_Balloc(b->k);
Bcopy(b1, b);
nb = fpi->nbits;
nb1 = nb & 31;
@ -56,7 +56,7 @@ strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *
nw1 = nw - 1;
if (rv & STRTOG_Inexlo) {
swap = 0;
b1 = increment(b1);
b1 = __gdtoa_increment(b1);
if ((rv & STRTOG_Retmask) == STRTOG_Zero) {
if (fpi->sudden_underflow) {
b1->x[0] = 0;
@ -73,7 +73,7 @@ strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *
|| (nb1 && b1->x[nw1] & 1L << nb1)) {
if (++e1 > fpi->emax)
rv1 = STRTOG_Infinite | STRTOG_Inexhi;
rshift(b1, 1);
__gdtoa_rshift(b1, 1);
}
else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
if (b1->x[nw1] & 1L << nb11) {
@ -85,12 +85,12 @@ strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *
else {
swap = STRTOG_Neg;
if ((rv & STRTOG_Retmask) == STRTOG_Infinite) {
b1 = set_ones(b1, nb);
b1 = __gdtoa_set_ones(b1, nb);
e1 = fpi->emax;
rv1 = STRTOG_Normal | STRTOG_Inexlo | (rv & STRTOG_Neg);
goto swapcheck;
}
decrement(b1);
__gdtoa_decrement(b1);
if ((rv & STRTOG_Retmask) == STRTOG_Denormal) {
for(i = nw1; !b1->x[i]; --i)
if (!i) {
@ -108,7 +108,7 @@ strtoIg(const char *s00, char **se, const FPI *fpi, Long *exp, Bigint **B, int *
rv1 |= STRTOG_Underflow;
}
else {
b1 = lshift(b1, 1);
b1 = __gdtoa_lshift(b1, 1);
b1->x[0] |= 1;
--e1;
}

View file

@ -40,14 +40,14 @@ strtoIx(const char *s, char **sp, void *a, void *b)
Bigint *B[2];
int k, rv[2];
UShort *L = (UShort *)a, *M = (UShort *)b;
B[0] = Balloc(1);
B[0] = __gdtoa_Balloc(1);
B[0]->wds = 2;
k = strtoIg(s, sp, &fpi, exp, B, rv);
ULtox(L, B[0]->x, exp[0], rv[0]);
Bfree(B[0]);
k = __gdtoa_strtoIg(s, sp, &fpi, exp, B, rv);
__gdtoa_ULtox(L, B[0]->x, exp[0], rv[0]);
__gdtoa_Bfree(B[0]);
if (B[1]) {
ULtox(M, B[1]->x, exp[1], rv[1]);
Bfree(B[1]);
__gdtoa_ULtox(M, B[1]->x, exp[1], rv[1]);
__gdtoa_Bfree(B[1]);
}
else {
M[0] = L[0];

View file

@ -36,22 +36,21 @@
#define Avoid_Underflow
#define dplen 1
#undef tinytens
/* The factor of 2^106 in tinytens[4] helps us avoid setting the underflow */
/* The factor of 2^106 in tiny__gdtoa_tens[4] helps us avoid setting the underflow */
/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
static const double tinytens[] = {
static const double tiny__gdtoa_tens[] = {
1e-16, 1e-32, 1e-64, 1e-128,
9007199254740992.*9007199254740992.e-256
};
static double
sulp(U *x, int scale)
s__gdtoa_ulp(U *x, int scale)
{
U u;
int i;
double rv;
rv = ulp(x);
rv = __gdtoa_ulp(x);
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);
@ -108,7 +107,7 @@ break2:
{
FPI fpi1 = fpi;
fpi1.rounding = Rounding;
switch((i = gethex(&s, &fpi1, &exp, &bb, sign)) & STRTOG_Retmask) {
switch((i = __gdtoa_gethex(&s, &fpi1, &exp, &bb, sign)) & STRTOG_Retmask) {
case STRTOG_NoNumber:
s = s00;
sign = 0;
@ -116,10 +115,10 @@ break2:
break;
default:
if (bb) {
copybits(bits, fpi.nbits, bb);
Bfree(bb);
__gdtoa_copybits(bits, fpi.nbits, bb);
__gdtoa_Bfree(bb);
}
ULtod(((U*)&rv)->L, bits, exp, i);
__gdtoa_ULtod(((U*)&rv)->L, bits, exp, i);
}}
goto ret;
}
@ -217,9 +216,9 @@ dig_done:
switch(c) {
case 'i':
case 'I':
if (match(&s,"nf")) {
if (__gdtoa_match(&s,"nf")) {
--s;
if (!match(&s,"inity"))
if (!__gdtoa_match(&s,"inity"))
++s;
word0(&rv) = 0x7ff00000;
word1(&rv) = 0;
@ -228,9 +227,9 @@ dig_done:
break;
case 'n':
case 'N':
if (match(&s, "an")) {
if (__gdtoa_match(&s, "an")) {
if (*s == '(' /*)*/
&& hexnan(&s, &fpinan, bits)
&& __gdtoa_hexnan(&s, &fpinan, bits)
== STRTOG_NaNbits) {
word0(&rv) = 0x7ff00000 | bits[1];
word1(&rv) = bits[0];
@ -258,7 +257,7 @@ dig_done:
k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
dval(&rv) = y;
if (k > 9) {
dval(&rv) = tens[k - 9] * dval(&rv) + z;
dval(&rv) = __gdtoa_tens[k - 9] * dval(&rv) + z;
}
bd0 = 0;
if (nd <= DBL_DIG) {
@ -271,7 +270,7 @@ dig_done:
rv.d = -rv.d;
sign = 0;
}
/* rv = */ rounded_product(dval(&rv), tens[e]);
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e]);
goto ret;
}
i = DBL_DIG - nd;
@ -285,8 +284,8 @@ dig_done:
sign = 0;
}
e -= i;
dval(&rv) *= tens[i];
/* rv = */ rounded_product(dval(&rv), tens[e]);
dval(&rv) *= __gdtoa_tens[i];
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e]);
goto ret;
}
}
@ -296,7 +295,7 @@ dig_done:
rv.d = -rv.d;
sign = 0;
}
/* rv = */ rounded_quotient(dval(&rv), tens[-e]);
/* rv = */ rounded_quotient(dval(&rv), __gdtoa_tens[-e]);
goto ret;
}
}
@ -312,7 +311,7 @@ dig_done:
/* Get starting approximation = rv * 10**e1 */
if (e1 > 0) {
if ( (i = e1 & 15) !=0)
dval(&rv) *= tens[i];
dval(&rv) *= __gdtoa_tens[i];
if (e1 &= ~15) {
if (e1 > DBL_MAX_10_EXP) {
ovfl:
@ -329,11 +328,11 @@ dig_done:
}
range_err:
if (bd0) {
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(bd0);
Bfree(delta);
__gdtoa_Bfree(bb);
__gdtoa_Bfree(bd);
__gdtoa_Bfree(bs);
__gdtoa_Bfree(bd0);
__gdtoa_Bfree(delta);
}
errno = ERANGE;
goto ret;
@ -341,10 +340,10 @@ dig_done:
e1 >>= 4;
for(j = 0; e1 > 1; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= bigtens[j];
/* The last multiplication could overflow. */
dval(&rv) *= __gdtoa_bigtens[j];
/* The last __gdtoa_multiplication could overflow. */
word0(&rv) -= P*Exp_msk1;
dval(&rv) *= bigtens[j];
dval(&rv) *= __gdtoa_bigtens[j];
if ((z = word0(&rv) & Exp_mask)
> Exp_msk1*(DBL_MAX_EXP+Bias-P))
goto ovfl;
@ -361,15 +360,15 @@ dig_done:
else if (e1 < 0) {
e1 = -e1;
if ( (i = e1 & 15) !=0)
dval(&rv) /= tens[i];
dval(&rv) /= __gdtoa_tens[i];
if (e1 >>= 4) {
if (e1 >= 1 << n_bigtens)
if (e1 >= 1 << n___gdtoa_bigtens)
goto undfl;
if (e1 & Scale_Bit)
scale = 2*P;
for(j = 0; e1 > 0; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= tinytens[j];
dval(&rv) *= tiny__gdtoa_tens[j];
if (scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
>> Exp_shift)) > 0) {
/* scaled rv is denormal; zap j low bits */
@ -394,12 +393,12 @@ dig_done:
}
/* Now the hard part -- adjusting rv to the correct value.*/
/* Put digits into bd: true value = bd * 10^e */
bd0 = s2b(s0, nd0, nd, y, dplen);
bd0 = __gdtoa_s2b(s0, nd0, nd, y, dplen);
for(;;) {
bd = Balloc(bd0->k);
bd = __gdtoa_Balloc(bd0->k);
Bcopy(bd, bd0);
bb = d2b(dval(&rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
bs = i2b(1);
bb = __gdtoa_d2b(dval(&rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
bs = __gdtoa_i2b(1);
if (e >= 0) {
bb2 = bb5 = 0;
bd2 = bd5 = e;
@ -440,26 +439,26 @@ dig_done:
bs2 -= i;
}
if (bb5 > 0) {
bs = pow5mult(bs, bb5);
bb1 = mult(bs, bb);
Bfree(bb);
bs = __gdtoa_pow5mult(bs, bb5);
bb1 = __gdtoa_mult(bs, bb);
__gdtoa_Bfree(bb);
bb = bb1;
}
if (bb2 > 0)
bb = lshift(bb, bb2);
bb = __gdtoa_lshift(bb, bb2);
if (bd5 > 0)
bd = pow5mult(bd, bd5);
bd = __gdtoa_pow5mult(bd, bd5);
if (bd2 > 0)
bd = lshift(bd, bd2);
bd = __gdtoa_lshift(bd, bd2);
if (bs2 > 0)
bs = lshift(bs, bs2);
delta = diff(bb, bd);
bs = __gdtoa_lshift(bs, bs2);
delta = __gdtoa_diff(bb, bd);
dsign = delta->sign;
delta->sign = 0;
i = cmp(delta, bs);
i = __gdtoa_cmp(delta, bs);
if (Rounding != 1) {
if (i < 0) {
/* Error is less than an ulp */
/* Error is less than an __gdtoa_ulp */
if (!delta->x[0] && delta->wds <= 1) {
/* exact */
break;
@ -477,8 +476,8 @@ dig_done:
y = word0(&rv) & Exp_mask;
if (!scale || y > 2*P*Exp_msk1)
{
delta = lshift(delta,Log2P);
if (cmp(delta, bs) <= 0)
delta = __gdtoa_lshift(delta,Log2P);
if (__gdtoa_cmp(delta, bs) <= 0)
dval(&adj) = -0.5;
}
}
@ -486,11 +485,11 @@ dig_done:
if (scale && (y = word0(&rv) & Exp_mask)
<= 2*P*Exp_msk1)
word0(&adj) += (2*P+1)*Exp_msk1 - y;
dval(&rv) += adj.d*ulp(&rv);
dval(&rv) += adj.d*__gdtoa_ulp(&rv);
}
break;
}
dval(&adj) = ratio(delta, bs);
dval(&adj) = __gdtoa_ratio(delta, bs);
if (adj.d < 1.)
dval(&adj) = 1.;
if (adj.d <= 0x7ffffffe) {
@ -504,7 +503,7 @@ dig_done:
}
if (scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
word0(&adj) += (2*P+1)*Exp_msk1 - y;
dval(&adj) *= ulp(&rv); /* XXX */
dval(&adj) *= __gdtoa_ulp(&rv); /* XXX */
if (dsign) {
if (word0(&rv) == Big0 && word1(&rv) == Big1)
goto ovfl;
@ -515,7 +514,7 @@ dig_done:
goto cont;
}
if (i < 0) {
/* Error is less than half an ulp -- check for
/* Error is less than half an __gdtoa_ulp -- check for
* special case of mantissa a power of two.
*/
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask ||
@ -526,8 +525,8 @@ dig_done:
/* exact result */
break;
}
delta = lshift(delta,Log2P);
if (cmp(delta, bs) > 0)
delta = __gdtoa_lshift(delta,Log2P);
if (__gdtoa_cmp(delta, bs) > 0)
goto drop_down;
break;
}
@ -539,7 +538,7 @@ dig_done:
(scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
0xffffffff)) {
/*boundary case -- increment exponent*/
/*boundary case -- __gdtoa_increment exponent*/
if (word0(&rv) == Big0 && word1(&rv) == Big1)
goto ovfl;
word0(&rv) = (word0(&rv) & Exp_mask)
@ -552,7 +551,7 @@ dig_done:
}
else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
drop_down:
/* boundary case -- decrement exponent */
/* boundary case -- __gdtoa_decrement exponent */
if (scale) {
L = word0(&rv) & Exp_mask;
if (L <= (2*P+1)*Exp_msk1) {
@ -576,16 +575,16 @@ dig_done:
else if (!(word1(&rv) & Lsb))
break;
if (dsign)
dval(&rv) += sulp(&rv, scale);
dval(&rv) += s__gdtoa_ulp(&rv, scale);
else {
dval(&rv) -= sulp(&rv, scale);
dval(&rv) -= s__gdtoa_ulp(&rv, scale);
if (!dval(&rv))
goto undfl;
}
dsign = 1 - dsign;
break;
}
if ((aadj = ratio(delta, bs)) <= 2.) {
if ((aadj = __gdtoa_ratio(delta, bs)) <= 2.) {
if (dsign)
aadj = dval(&aadj1) = 1.;
else if (word1(&rv) || word0(&rv) & Bndry_mask) {
@ -621,7 +620,7 @@ dig_done:
if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
dval(&rv0) = dval(&rv);
word0(&rv) -= P*Exp_msk1;
dval(&adj) = dval(&aadj1) * ulp(&rv);
dval(&adj) = dval(&aadj1) * __gdtoa_ulp(&rv);
dval(&rv) += dval(&adj);
if ((word0(&rv) & Exp_mask) >=
Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
@ -644,7 +643,7 @@ dig_done:
}
word0(&aadj1) += (2*P+1)*Exp_msk1 - y;
}
dval(&adj) = dval(&aadj1) * ulp(&rv);
dval(&adj) = dval(&aadj1) * __gdtoa_ulp(&rv);
dval(&rv) += dval(&adj);
}
z = word0(&rv) & Exp_mask;
@ -662,16 +661,16 @@ dig_done:
break;
}
cont:
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(delta);
__gdtoa_Bfree(bb);
__gdtoa_Bfree(bd);
__gdtoa_Bfree(bs);
__gdtoa_Bfree(delta);
}
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(bd0);
Bfree(delta);
__gdtoa_Bfree(bb);
__gdtoa_Bfree(bd);
__gdtoa_Bfree(bs);
__gdtoa_Bfree(bd0);
__gdtoa_Bfree(delta);
if (scale) {
word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
word1(&rv0) = 0;

View file

@ -33,11 +33,11 @@
/* clang-format off */
static double
ulpdown(U *d)
__gdtoa_ulpdown(U *d)
{
double u;
ULong *L = d->L;
u = ulp(d);
u = __gdtoa_ulp(d);
if (!(L[0] | (L[1] & 0xfffff))
&& (L[1] & 0x7ff00000) > 0x00100000)
u *= 0.5;
@ -77,11 +77,11 @@ strtodI(const char *s, char **sp, double *dd)
}
switch(j) {
case STRTOG_Inexlo:
dval(&u[1]) = dval(&u[0]) + ulp(&u[0]);
dval(&u[1]) = dval(&u[0]) + __gdtoa_ulp(&u[0]);
break;
case STRTOG_Inexhi:
dval(&u[1]) = dval(&u[0]);
dval(&u[0]) -= ulpdown(u);
dval(&u[0]) -= __gdtoa_ulpdown(u);
break;
default:
dval(&u[1]) = dval(&u[0]);

View file

@ -39,7 +39,7 @@ fivesbits[] = { 0, 3, 5, 7, 10, 12, 14, 17, 19, 21,
47, 49, 52 };
Bigint *
increment(Bigint *b)
__gdtoa_increment(Bigint *b)
{
ULong *x, *xe;
Bigint *b1;
@ -54,9 +54,9 @@ increment(Bigint *b)
} while(x < xe);
{
if (b->wds >= b->maxwds) {
b1 = Balloc(b->k+1);
b1 = __gdtoa_Balloc(b->k+1);
Bcopy(b1,b);
Bfree(b);
__gdtoa_Bfree(b);
b = b1;
}
b->x[b->wds++] = 1;
@ -65,7 +65,7 @@ increment(Bigint *b)
}
void
decrement(Bigint *b)
__gdtoa_decrement(Bigint *b)
{
ULong *x, *xe;
x = b->x;
@ -95,14 +95,14 @@ all_on(Bigint *b, int n)
}
Bigint *
set_ones(Bigint *b, int n)
__gdtoa_set_ones(Bigint *b, int n)
{
int k;
ULong *x, *xe;
k = (n + ((1 << kshift) - 1)) >> kshift;
if (b->k < k) {
Bfree(b);
b = Balloc(k);
__gdtoa_Bfree(b);
b = __gdtoa_Balloc(k);
}
k = n >> kshift;
if (n &= kmask)
@ -124,7 +124,7 @@ rvOK(U *d, const FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv)
ULong carry, inex, lostbits;
int bdif, e, j, k, k1, nb, rv;
carry = rv = 0;
b = d2b(dval(d), &e, &bdif);
b = __gdtoa_d2b(dval(d), &e, &bdif);
bdif -= nb = fpi->nbits;
e += bdif;
if (bdif <= 0) {
@ -161,24 +161,24 @@ rvOK(U *d, const FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv)
trunc:
inex = lostbits = 0;
if (bdif > 0) {
if ( (lostbits = any_on(b, bdif)) !=0)
if ( (lostbits = __gdtoa_any_on(b, bdif)) !=0)
inex = STRTOG_Inexlo;
rshift(b, bdif);
__gdtoa_rshift(b, bdif);
if (carry) {
inex = STRTOG_Inexhi;
b = increment(b);
b = __gdtoa_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);
__gdtoa_rshift(b, 1);
e++;
}
}
}
else if (bdif < 0)
b = lshift(b, -bdif);
b = __gdtoa_lshift(b, -bdif);
if (e < fpi->emin) {
k = fpi->emin - e;
e = fpi->emin;
@ -189,15 +189,15 @@ trunc:
else {
k1 = k - 1;
if (k1 > 0 && !lostbits)
lostbits = any_on(b, k1);
lostbits = __gdtoa_any_on(b, k1);
if (!lostbits && !exact)
goto ret;
lostbits |=
carry = b->x[k1>>kshift] & (1 << (k1 & kmask));
rshift(b, k);
__gdtoa_rshift(b, k);
*irv = STRTOG_Denormal;
if (carry) {
b = increment(b);
b = __gdtoa_increment(b);
inex = STRTOG_Inexhi | STRTOG_Underflow;
}
else if (lostbits)
@ -211,11 +211,11 @@ trunc:
b->wds = inex = 0;
}
*exp = e;
copybits(bits, nb, b);
__gdtoa_copybits(bits, nb, b);
*irv |= inex;
rv = 1;
ret:
Bfree(b);
__gdtoa_Bfree(b);
return rv;
}
@ -276,7 +276,7 @@ break2:
switch(s[1]) {
case 'x':
case 'X':
irv = gethex(&s, fpi, exp, &rvb, sign);
irv = __gdtoa_gethex(&s, fpi, exp, &rvb, sign);
if (irv == STRTOG_NoNumber) {
s = s00;
sign = 0;
@ -376,9 +376,9 @@ dig_done:
switch(c) {
case 'i':
case 'I':
if (match(&s,"nf")) {
if (__gdtoa_match(&s,"nf")) {
--s;
if (!match(&s,"inity"))
if (!__gdtoa_match(&s,"inity"))
++s;
irv = STRTOG_Infinite;
goto infnanexp;
@ -386,11 +386,11 @@ dig_done:
break;
case 'n':
case 'N':
if (match(&s, "an")) {
if (__gdtoa_match(&s, "an")) {
irv = STRTOG_NaN;
*exp = fpi->emax + 1;
if (*s == '(')
irv = hexnan(&s, fpi, bits);
irv = __gdtoa_hexnan(&s, fpi, bits);
goto infnanexp;
}
}
@ -421,7 +421,7 @@ dig_done:
k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
dval(&rv) = y;
if (k > 9)
dval(&rv) = tens[k - 9] * dval(&rv) + z;
dval(&rv) = __gdtoa_tens[k - 9] * dval(&rv) + z;
bd0 = 0;
if (nbits <= P && nd <= DBL_DIG) {
if (!e) {
@ -431,7 +431,7 @@ dig_done:
else if (e > 0) {
if (e <= Ten_pmax) {
i = fivesbits[e] + mantbits(&rv) <= P;
/* rv = */ rounded_product(dval(&rv), tens[e]);
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e]);
if (rvOK(&rv, fpi, exp, bits, i, rd, &irv))
goto ret;
e1 -= e;
@ -444,15 +444,15 @@ dig_done:
*/
e2 = e - i;
e1 -= i;
dval(&rv) *= tens[i];
/* rv = */ rounded_product(dval(&rv), tens[e2]);
dval(&rv) *= __gdtoa_tens[i];
/* rv = */ rounded_product(dval(&rv), __gdtoa_tens[e2]);
if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv))
goto ret;
e1 -= e2;
}
}
else if (e >= -Ten_pmax) {
/* rv = */ rounded_quotient(dval(&rv), tens[-e]);
/* rv = */ rounded_quotient(dval(&rv), __gdtoa_tens[-e]);
if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv))
goto ret;
e1 -= e;
@ -464,51 +464,51 @@ rv_notOK:
e2 = 0;
if (e1 > 0) {
if ( (i = e1 & 15) !=0)
dval(&rv) *= tens[i];
dval(&rv) *= __gdtoa_tens[i];
if (e1 &= ~15) {
e1 >>= 4;
while(e1 >= (1 << (n_bigtens-1))) {
while(e1 >= (1 << (n___gdtoa_bigtens-1))) {
e2 += ((word0(&rv) & Exp_mask)
>> Exp_shift1) - Bias;
word0(&rv) &= ~Exp_mask;
word0(&rv) |= Bias << Exp_shift1;
dval(&rv) *= bigtens[n_bigtens-1];
e1 -= 1 << (n_bigtens-1);
dval(&rv) *= __gdtoa_bigtens[n___gdtoa_bigtens-1];
e1 -= 1 << (n___gdtoa_bigtens-1);
}
e2 += ((word0(&rv) & Exp_mask) >> Exp_shift1) - Bias;
word0(&rv) &= ~Exp_mask;
word0(&rv) |= Bias << Exp_shift1;
for(j = 0; e1 > 0; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= bigtens[j];
dval(&rv) *= __gdtoa_bigtens[j];
}
}
else if (e1 < 0) {
e1 = -e1;
if ( (i = e1 & 15) !=0)
dval(&rv) /= tens[i];
dval(&rv) /= __gdtoa_tens[i];
if (e1 &= ~15) {
e1 >>= 4;
while(e1 >= (1 << (n_bigtens-1))) {
while(e1 >= (1 << (n___gdtoa_bigtens-1))) {
e2 += ((word0(&rv) & Exp_mask)
>> Exp_shift1) - Bias;
word0(&rv) &= ~Exp_mask;
word0(&rv) |= Bias << Exp_shift1;
dval(&rv) *= tinytens[n_bigtens-1];
e1 -= 1 << (n_bigtens-1);
dval(&rv) *= __gdtoa_tinytens[n___gdtoa_bigtens-1];
e1 -= 1 << (n___gdtoa_bigtens-1);
}
e2 += ((word0(&rv) & Exp_mask) >> Exp_shift1) - Bias;
word0(&rv) &= ~Exp_mask;
word0(&rv) |= Bias << Exp_shift1;
for(j = 0; e1 > 0; j++, e1 >>= 1)
if (e1 & 1)
dval(&rv) *= tinytens[j];
dval(&rv) *= __gdtoa_tinytens[j];
}
}
rvb = d2b(dval(&rv), &rve, &rvbits); /* rv = rvb * 2^rve */
rvb = __gdtoa_d2b(dval(&rv), &rve, &rvbits); /* rv = rvb * 2^rve */
rve += e2;
if ((j = rvbits - nbits) > 0) {
rshift(rvb, j);
__gdtoa_rshift(rvb, j);
rvbits = nbits;
rve += j;
}
@ -521,7 +521,7 @@ rv_notOK:
denorm = 1;
j = rve - emin;
if (j > 0) {
rvb = lshift(rvb, j);
rvb = __gdtoa_lshift(rvb, j);
rvbits += j;
}
else if (j < 0) {
@ -549,7 +549,7 @@ rv_notOK:
rvb->x[0] = rvb->wds = rvbits = 1;
}
else
rshift(rvb, -j);
__gdtoa_rshift(rvb, -j);
}
rve = rve1 = emin;
if (sudden_underflow && e2 + 1 < emin)
@ -557,15 +557,15 @@ rv_notOK:
}
/* Now the hard part -- adjusting rv to the correct value.*/
/* Put digits into bd: true value = bd * 10^e */
bd0 = s2b(s0, nd0, nd, y, 1);
bd0 = __gdtoa_s2b(s0, nd0, nd, y, 1);
for(;;) {
bd = Balloc(bd0->k);
bd = __gdtoa_Balloc(bd0->k);
Bcopy(bd, bd0);
bb = Balloc(rvb->k);
bb = __gdtoa_Balloc(rvb->k);
Bcopy(bb, rvb);
bbbits = rvbits - bb0;
bbe = rve + bb0;
bs = i2b(1);
bs = __gdtoa_i2b(1);
if (e >= 0) {
bb2 = bb5 = 0;
bd2 = bd5 = e;
@ -594,31 +594,31 @@ rv_notOK:
bs2 -= i;
}
if (bb5 > 0) {
bs = pow5mult(bs, bb5);
bb1 = mult(bs, bb);
Bfree(bb);
bs = __gdtoa_pow5mult(bs, bb5);
bb1 = __gdtoa_mult(bs, bb);
__gdtoa_Bfree(bb);
bb = bb1;
}
bb2 -= bb0;
if (bb2 > 0)
bb = lshift(bb, bb2);
bb = __gdtoa_lshift(bb, bb2);
else if (bb2 < 0)
rshift(bb, -bb2);
__gdtoa_rshift(bb, -bb2);
if (bd5 > 0)
bd = pow5mult(bd, bd5);
bd = __gdtoa_pow5mult(bd, bd5);
if (bd2 > 0)
bd = lshift(bd, bd2);
bd = __gdtoa_lshift(bd, bd2);
if (bs2 > 0)
bs = lshift(bs, bs2);
bs = __gdtoa_lshift(bs, bs2);
asub = 1;
inex = STRTOG_Inexhi;
delta = diff(bb, bd);
delta = __gdtoa_diff(bb, bd);
if (delta->wds <= 1 && !delta->x[0])
break;
dsign = delta->sign;
delta->sign = finished = 0;
L = 0;
i = cmp(delta, bs);
i = __gdtoa_cmp(delta, bs);
if (rd && i <= 0) {
irv = STRTOG_Normal;
if ( (finished = dsign ^ (rd&1)) !=0) {
@ -637,14 +637,14 @@ rv_notOK:
if (j > 1 && lo0bits(rvb->x + i) < j - 1)
goto adj1;
rve = rve1 - 1;
rvb = set_ones(rvb, rvbits = nbits);
rvb = __gdtoa_set_ones(rvb, rvbits = nbits);
break;
}
irv |= dsign ? STRTOG_Inexlo : STRTOG_Inexhi;
break;
}
if (i < 0) {
/* Error is less than half an ulp -- check for
/* Error is less than half an __gdtoa_ulp -- check for
* special case of mantissa a power of two.
*/
irv = dsign
@ -652,8 +652,8 @@ rv_notOK:
: STRTOG_Normal | STRTOG_Inexhi;
if (dsign || bbbits > 1 || denorm || rve1 == emin)
break;
delta = lshift(delta,1);
if (cmp(delta, bs) > 0) {
delta = __gdtoa_lshift(delta,1);
if (__gdtoa_cmp(delta, bs) > 0) {
irv = STRTOG_Normal | STRTOG_Inexlo;
goto drop_down;
}
@ -663,7 +663,7 @@ rv_notOK:
/* exactly half-way between */
if (dsign) {
if (denorm && all_on(rvb, rvbits)) {
/*boundary case -- increment exponent*/
/*boundary case -- __gdtoa_increment exponent*/
rvb->wds = 1;
rvb->x[0] = 1;
rve = emin + nbits - (rvbits = 1);
@ -676,7 +676,7 @@ rv_notOK:
else if (bbbits == 1) {
irv = STRTOG_Normal;
drop_down:
/* boundary case -- decrement exponent */
/* boundary case -- __gdtoa_decrement exponent */
if (rve1 == emin) {
irv = STRTOG_Normal | STRTOG_Inexhi;
if (rvb->wds == 1 && rvb->x[0] == 1)
@ -684,7 +684,7 @@ rv_notOK:
break;
}
rve -= nbits;
rvb = set_ones(rvb, rvbits = nbits);
rvb = __gdtoa_set_ones(rvb, rvbits = nbits);
break;
}
else
@ -692,7 +692,7 @@ rv_notOK:
if ((bbbits < nbits && !denorm) || !(rvb->x[0] & 1))
break;
if (dsign) {
rvb = increment(rvb);
rvb = __gdtoa_increment(rvb);
j = kmask & (ULbits - (rvbits & kmask));
if (hi0bits(rvb->x[rvb->wds - 1]) != j)
rvbits++;
@ -701,12 +701,12 @@ rv_notOK:
else {
if (bbbits == 1)
goto undfl;
decrement(rvb);
__gdtoa_decrement(rvb);
irv = STRTOG_Normal | STRTOG_Inexlo;
}
break;
}
if ((dval(&adj) = ratio(delta, bs)) <= 2.) {
if ((dval(&adj) = __gdtoa_ratio(delta, bs)) <= 2.) {
adj1:
inex = STRTOG_Inexlo;
if (dsign) {
@ -756,23 +756,23 @@ rv_notOK:
}
}
y = rve + rvbits;
/* adj *= ulp(dval(&rv)); */
/* adj *= __gdtoa_ulp(dval(&rv)); */
/* if (asub) rv -= adj; else rv += adj; */
if (!denorm && rvbits < nbits) {
rvb = lshift(rvb, j = nbits - rvbits);
rvb = __gdtoa_lshift(rvb, j = nbits - rvbits);
rve -= j;
rvbits = nbits;
}
ab = d2b(dval(&adj), &abe, &abits);
ab = __gdtoa_d2b(dval(&adj), &abe, &abits);
if (abe < 0)
rshift(ab, -abe);
__gdtoa_rshift(ab, -abe);
else if (abe > 0)
ab = lshift(ab, abe);
ab = __gdtoa_lshift(ab, abe);
rvb0 = rvb;
if (asub) {
/* rv -= adj; */
j = hi0bits(rvb->x[rvb->wds-1]);
rvb = diff(rvb, ab);
rvb = __gdtoa_diff(rvb, ab);
k = rvb0->wds - 1;
if (denorm)
/* do nothing */;
@ -785,7 +785,7 @@ rv_notOK:
denorm = 1;
}
else {
rvb = lshift(rvb, 1);
rvb = __gdtoa_lshift(rvb, 1);
--rve;
--rve1;
L = finished = 0;
@ -793,7 +793,7 @@ rv_notOK:
}
}
else {
rvb = sum(rvb, ab);
rvb = __gdtoa_sum(rvb, ab);
k = rvb->wds - 1;
if (k >= rvb0->wds
|| hi0bits(rvb->x[k]) < hi0bits(rvb0->x[k])) {
@ -802,15 +802,15 @@ rv_notOK:
denorm = 0;
}
else {
rshift(rvb, 1);
__gdtoa_rshift(rvb, 1);
rve++;
rve1++;
L = 0;
}
}
}
Bfree(ab);
Bfree(rvb0);
__gdtoa_Bfree(ab);
__gdtoa_Bfree(rvb0);
if (finished)
break;
z = rve + rvbits;
@ -829,28 +829,28 @@ rv_notOK:
break;
}
}
bb0 = denorm ? 0 : trailz(rvb);
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(delta);
bb0 = denorm ? 0 : __gdtoa_trailz(rvb);
__gdtoa_Bfree(bb);
__gdtoa_Bfree(bd);
__gdtoa_Bfree(bs);
__gdtoa_Bfree(delta);
}
if (!denorm && (j = nbits - rvbits)) {
if (j > 0)
rvb = lshift(rvb, j);
rvb = __gdtoa_lshift(rvb, j);
else
rshift(rvb, -j);
__gdtoa_rshift(rvb, -j);
rve -= j;
}
*exp = rve;
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(bd0);
Bfree(delta);
__gdtoa_Bfree(bb);
__gdtoa_Bfree(bd);
__gdtoa_Bfree(bs);
__gdtoa_Bfree(bd0);
__gdtoa_Bfree(delta);
if (rve > fpi->emax) {
huge:
Bfree(rvb);
__gdtoa_Bfree(rvb);
rvb = 0;
errno = ERANGE;
switch(fpi->rounding & 3) {
@ -903,8 +903,8 @@ ret:
if (sign)
irv |= STRTOG_Neg;
if (rvb) {
copybits(bits, nbits, rvb);
Bfree(rvb);
__gdtoa_copybits(bits, nbits, rvb);
__gdtoa_Bfree(rvb);
}
return irv;
}

View file

@ -41,6 +41,6 @@ strtopd(const char *s, char **sp, double *d)
int k;
#include "third_party/gdtoa/gdtoa_fltrnds.inc"
k = strtodg(s, sp, fpi, &exp, bits);
ULtod((ULong*)d, bits, exp, k);
__gdtoa_ULtod((ULong*)d, bits, exp, k);
return k;
}

View file

@ -35,7 +35,7 @@
extern ULong __gdtoa_NanDflt_d[2];
void
ULtod(ULong *L, ULong *bits, Long exp, int k)
__gdtoa_ULtod(ULong *L, ULong *bits, Long exp, int k)
{
switch(k & STRTOG_Retmask) {
case STRTOG_NoNumber:
@ -78,6 +78,6 @@ strtord(const char *s, char **sp, int rounding, double *d)
fpi = &fpi1;
}
k = strtodg(s, sp, fpi, &exp, bits);
ULtod((ULong*)d, bits, exp, k);
__gdtoa_ULtod((ULong*)d, bits, exp, k);
return k;
}

View file

@ -35,7 +35,7 @@
extern ULong __gdtoa_NanDflt_d[2];
void
ULtodd(ULong *L, ULong *bits, Long exp, int k)
__gdtoa_ULtodd(ULong *L, ULong *bits, Long exp, int k)
{
int i, j;
switch(k & STRTOG_Retmask) {
@ -176,6 +176,6 @@ strtordd(const char *s, char **sp, int rounding, double *dd)
fpi = &fpi1;
}
k = strtodg(s, sp, fpi, &exp, bits);
ULtodd((ULong*)dd, bits, exp, k);
__gdtoa_ULtodd((ULong*)dd, bits, exp, k);
return k;
}

View file

@ -35,7 +35,7 @@
extern ULong __gdtoa_NanDflt_f[1];
void
ULtof(ULong *L, ULong *bits, Long exp, int k)
__gdtoa_ULtof(ULong *L, ULong *bits, Long exp, int k)
{
switch(k & STRTOG_Retmask) {
case STRTOG_NoNumber:
@ -74,6 +74,6 @@ strtorf(const char *s, char **sp, int rounding, float *f)
fpi = &fpi1;
}
k = strtodg(s, sp, fpi, &exp, bits);
ULtof((ULong*)f, bits, exp, k);
__gdtoa_ULtof((ULong*)f, bits, exp, k);
return k;
}

View file

@ -35,7 +35,7 @@
extern UShort __gdtoa_NanDflt_ldus[5];
void
ULtox(UShort *L, ULong *bits, Long exp, int k)
__gdtoa_ULtox(UShort *L, ULong *bits, Long exp, int k)
{
switch(k & STRTOG_Retmask) {
case STRTOG_NoNumber:
@ -85,6 +85,6 @@ strtorx(const char *s, char **sp, int rounding, void *L)
fpi = &fpi1;
}
k = strtodg(s, sp, fpi, &exp, bits);
ULtox((UShort*)L, bits, exp, k);
__gdtoa_ULtox((UShort*)L, bits, exp, k);
return k;
}

View file

@ -33,7 +33,7 @@
/* clang-format off */
Bigint *
sum(Bigint *a, Bigint *b)
__gdtoa_sum(Bigint *a, Bigint *b)
{
Bigint *c;
ULong carry, *xc, *xa, *xb, *xe, y;
@ -41,7 +41,7 @@ sum(Bigint *a, Bigint *b)
if (a->wds < b->wds) {
c = b; b = a; a = c;
}
c = Balloc(a->k);
c = __gdtoa_Balloc(a->k);
c->wds = a->wds;
carry = 0;
xa = a->x;
@ -66,9 +66,9 @@ sum(Bigint *a, Bigint *b)
}
if (carry) {
if (c->wds == c->maxwds) {
b = Balloc(c->k + 1);
b = __gdtoa_Balloc(c->k + 1);
Bcopy(b, c);
Bfree(c);
__gdtoa_Bfree(c);
c = b;
}
c->x[c->wds++] = 1;

View file

@ -33,7 +33,7 @@
/* clang-format off */
double
ulp(U *x)
__gdtoa_ulp(U *x)
{
Long L;
U a;