From da45c7c80b50eae0bb638c1f92aae0dcf33f5aaa Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 19 Aug 2021 06:07:37 -0700 Subject: [PATCH] Clean up gdtoa code --- test/libc/tinymath/strtod_test.c | 9 - third_party/gdtoa/dmisc.c | 179 ++---- third_party/gdtoa/dtoa.c | 587 +++++++------------ third_party/gdtoa/g__fmt.c | 150 ++--- third_party/gdtoa/g_ddfmt.c | 178 +++--- third_party/gdtoa/g_ddfmt_p.c | 197 +++---- third_party/gdtoa/g_dfmt.c | 98 ++-- third_party/gdtoa/g_dfmt_p.c | 106 ++-- third_party/gdtoa/g_ffmt.c | 84 ++- third_party/gdtoa/g_ffmt_p.c | 84 ++- third_party/gdtoa/g_xfmt.c | 118 ++-- third_party/gdtoa/g_xfmt_p.c | 126 ++-- third_party/gdtoa/gdtoa.c | 443 ++++++--------- third_party/gdtoa/gdtoa.h | 6 +- third_party/gdtoa/gdtoa.internal.h | 251 ++------ third_party/gdtoa/gdtoa_fltrnds.inc | 17 +- third_party/gdtoa/gethex.c | 251 ++++---- third_party/gdtoa/gmisc.c | 76 ++- third_party/gdtoa/hd_init.c | 62 +- third_party/gdtoa/hexnan.c | 124 ++-- third_party/gdtoa/misc.c | 692 ++++------------------ third_party/gdtoa/smisc.c | 135 ++--- third_party/gdtoa/strtoId.c | 80 ++- third_party/gdtoa/strtoIdd.c | 84 ++- third_party/gdtoa/strtoIf.c | 78 ++- third_party/gdtoa/strtoIg.c | 106 ++-- third_party/gdtoa/strtoIx.c | 80 ++- third_party/gdtoa/strtod.c | 852 ++++++++-------------------- third_party/gdtoa/strtodI.c | 147 ++--- third_party/gdtoa/strtodg.c | 641 ++++++++------------- third_party/gdtoa/strtodnrp.c | 110 ++-- third_party/gdtoa/strtof.c | 93 ++- third_party/gdtoa/strtold.c | 40 +- third_party/gdtoa/strtopd.c | 71 ++- third_party/gdtoa/strtopdd.c | 188 +++--- third_party/gdtoa/strtopf.c | 93 ++- third_party/gdtoa/strtopx.c | 147 ++--- third_party/gdtoa/strtord.c | 111 ++-- third_party/gdtoa/strtordd.c | 206 ++++--- third_party/gdtoa/strtorf.c | 93 ++- third_party/gdtoa/strtorx.c | 143 ++--- third_party/gdtoa/sum.c | 102 ++-- third_party/gdtoa/ulp.c | 78 ++- third_party/lua/lua.main.c | 3 + third_party/quickjs/quickjs.mk | 2 +- third_party/sqlite3/sqlite3.mk | 2 +- tool/emacs/cosmo-c-constants.el | 3 +- tool/emacs/cosmo-cpp-constants.el | 3 +- 48 files changed, 2808 insertions(+), 4721 deletions(-) diff --git a/test/libc/tinymath/strtod_test.c b/test/libc/tinymath/strtod_test.c index d10f93fcb..e1fce65a8 100644 --- a/test/libc/tinymath/strtod_test.c +++ b/test/libc/tinymath/strtod_test.c @@ -22,13 +22,6 @@ #include "libc/x/x.h" #include "third_party/gdtoa/gdtoa.h" -TEST(strtod, testNearest) { - EXPECT_STREQ("-1.79769313486231e+308", - gc(xasprintf("%.15g", strtod("-1.79769313486231e+308", NULL)))); -} - -#if 0 /* TODO(jart): Why doesn't this work on Travis CI? */ - int oldround; void SetUp(void) { @@ -68,5 +61,3 @@ TEST(strtod, testTowardzero) { free(p); } } - -#endif diff --git a/third_party/gdtoa/dmisc.c b/third_party/gdtoa/dmisc.c index 59057ff94..c2dc60feb 100644 --- a/third_party/gdtoa/dmisc.c +++ b/third_party/gdtoa/dmisc.c @@ -1,112 +1,81 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -#ifndef MULTIPLE_THREADS - char *dtoa_result; -#endif char * -rv_alloc(int i MTd) +rv_alloc(int i) { int j, k, *r; - j = sizeof(ULong); for(k = 0; - (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int)) + j <= i; - j <<= 1) - k++; - r = (int*)Balloc(k MTa); + (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int)) + j <= i; + j <<= 1) + k++; + r = (int*)Balloc(k); *r = k; - return -#ifndef MULTIPLE_THREADS - dtoa_result = -#endif - (char *)(r+1); - } + return (char *)(r+1); +} char * -nrv_alloc(char *s, char **rve, int n MTd) +nrv_alloc(char *s, char **rve, int n) { char *rv, *t; - - t = rv = rv_alloc(n MTa); + t = rv = rv_alloc(n); while((*t = *s++) !=0) t++; if (rve) *rve = t; return rv; - } - -/* freedtoa(s) must be used to free values s returned by dtoa - * when MULTIPLE_THREADS is #defined. It should be used in all cases, - * but for consistency with earlier versions of dtoa, it is optional - * when MULTIPLE_THREADS is not defined. - */ +} void freedtoa(char *s) { -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif Bigint *b = (Bigint *)((int *)s - 1); b->maxwds = 1 << (b->k = *(int*)b); - Bfree(b MTb); -#ifndef MULTIPLE_THREADS - if (s == dtoa_result) - dtoa_result = 0; -#endif - } + Bfree(b); +} int quorem(Bigint *b, Bigint *S) { int n; ULong *bx, *bxe, q, *sx, *sxe; -#ifdef ULLong ULLong borrow, carry, y, ys; -#else - ULong borrow, carry, y, ys; -#ifdef Pack_32 - ULong si, z, zs; -#endif -#endif - n = S->wds; #ifdef DEBUG - /*debug*/ if (b->wds > n) - /*debug*/ Bug("oversize b in quorem"); + if (b->wds > n) + Bug("oversize b in quorem"); #endif if (b->wds < n) return 0; @@ -116,47 +85,27 @@ quorem(Bigint *b, Bigint *S) bxe = bx + n; q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ #ifdef DEBUG - /*debug*/ if (q > 9) - /*debug*/ Bug("oversized quotient in quorem"); + if (q > 9) + Bug("oversized quotient in quorem"); #endif if (q) { borrow = 0; carry = 0; do { -#ifdef ULLong ys = *sx++ * (ULLong)q + carry; carry = ys >> 32; y = *bx - (ys & 0xffffffffUL) - borrow; borrow = y >> 32 & 1UL; *bx++ = y & 0xffffffffUL; -#else -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) * q + carry; - zs = (si >> 16) * q + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*bx >> 16) - (zs & 0xffff) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(bx, z, y); -#else - ys = *sx++ * q + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - *bx++ = y & 0xffff; -#endif -#endif - } - while(sx <= sxe); + } + while(sx <= sxe); if (!*bxe) { bx = b->x; while(--bxe > bx && !*bxe) --n; b->wds = n; - } } + } if (cmp(b, S) >= 0) { q++; borrow = 0; @@ -164,40 +113,20 @@ quorem(Bigint *b, Bigint *S) bx = b->x; sx = S->x; do { -#ifdef ULLong ys = *sx++ + carry; carry = ys >> 32; y = *bx - (ys & 0xffffffffUL) - borrow; borrow = y >> 32 & 1UL; *bx++ = y & 0xffffffffUL; -#else -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) + carry; - zs = (si >> 16) + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*bx >> 16) - (zs & 0xffff) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(bx, z, y); -#else - ys = *sx++ + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - *bx++ = y & 0xffff; -#endif -#endif - } - while(sx <= sxe); + } + while(sx <= sxe); bx = b->x; bxe = bx + n; if (!*bxe) { while(--bxe > bx && !*bxe) --n; b->wds = n; - } } - return q; } + return q; +} diff --git a/third_party/gdtoa/dtoa.c b/third_party/gdtoa/dtoa.c index 7884a8f85..7d66a0938 100644 --- a/third_party/gdtoa/dtoa.c +++ b/third_party/gdtoa/dtoa.c @@ -1,36 +1,37 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/runtime/fenv.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -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. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. * @@ -66,149 +67,86 @@ THIS SOFTWARE. * calculation. */ -#ifdef Honor_FLT_ROUNDS -#undef Check_FLT_ROUNDS -#define Check_FLT_ROUNDS -#else -#define Rounding Flt_Rounds -#endif - char * dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) { - /* Arguments ndigits, decpt, sign are similar to those - of ecvt and fcvt; trailing zeros are suppressed from - the returned string. If not null, *rve is set to point - to the end of the return value. If d is +-Infinity or NaN, - then *decpt is set to 9999. + /* Arguments ndigits, decpt, sign are similar to those + of ecvt and fcvt; trailing zeros are suppressed from + the returned string. If not null, *rve is set to point + to the end of the return value. If d is +-Infinity or NaN, + then *decpt is set to 9999. - mode: + mode: 0 ==> shortest string that yields d when read in - and rounded to nearest. + and rounded to nearest. 1 ==> like 0, but with Steele & White stopping rule; - e.g. with IEEE P754 arithmetic , mode 0 gives - 1e23 whereas mode 1 gives 9.999999999999999e22. + e.g. with IEEE P754 arithmetic , mode 0 gives + 1e23 whereas mode 1 gives 9.999999999999999e22. 2 ==> max(1,ndigits) significant digits. This gives a - return value similar to that of ecvt, except - that trailing zeros are suppressed. + return value similar to that of ecvt, except + that trailing zeros are suppressed. 3 ==> through ndigits past the decimal point. This - gives a return value similar to that from fcvt, - except that trailing zeros are suppressed, and - ndigits can be negative. + gives a return value similar to that from fcvt, + except that trailing zeros are suppressed, and + ndigits can be negative. 4,5 ==> similar to 2 and 3, respectively, but (in - round-nearest mode) with the tests of mode 0 to - possibly return a shorter string that rounds to d. - With IEEE arithmetic and compilation with - -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same - as modes 2 and 3 when FLT_ROUNDS != 1. + round-nearest mode) with the tests of mode 0 to + possibly return a shorter string that rounds to d. + With IEEE arithmetic and compilation with + -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same + as modes 2 and 3 when FLT_ROUNDS != 1. 6-9 ==> Debugging modes similar to mode - 4: don't try - fast floating-point estimate (if applicable). + fast floating-point estimate (if applicable). Values of mode other than 0-9 are treated as mode 0. Sufficient space is allocated to the return value to hold the suppressed trailing zeros. */ - -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, spec_case, try_quick; Long L; -#ifndef Sudden_Underflow int denorm; ULong x; -#endif Bigint *b, *b1, *delta, *mlo, *mhi, *S; U d, d2, eps, eps1; double ds; char *s, *s0; -#ifdef SET_INEXACT - int inexact, oldinexact; -#endif -#ifdef Honor_FLT_ROUNDS /*{*/ int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ -#endif /*}*/ - -#ifndef MULTIPLE_THREADS - if (dtoa_result) { - freedtoa(dtoa_result); - dtoa_result = 0; - } -#endif + Rounding = FLT_ROUNDS; d.d = d0; if (word0(&d) & Sign_bit) { /* set sign for everything, including 0's and NaNs */ *sign = 1; word0(&d) &= ~Sign_bit; /* clear sign bit */ - } + } else *sign = 0; - -#if defined(IEEE_Arith) + defined(VAX) -#ifdef IEEE_Arith if ((word0(&d) & Exp_mask) == Exp_mask) -#else - if (word0(&d) == 0x8000) -#endif - { + { /* Infinity or NaN */ *decpt = 9999; -#ifdef IEEE_Arith if (!word1(&d) && !(word0(&d) & 0xfffff)) - return nrv_alloc("Infinity", rve, 8 MTb); -#endif - return nrv_alloc("NaN", rve, 3 MTb); - } -#endif -#ifdef IBM - dval(&d) += 0; /* normalize */ -#endif + return nrv_alloc("Infinity", rve, 8); + return nrv_alloc("NaN", rve, 3); + } if (!dval(&d)) { *decpt = 1; - return nrv_alloc("0", rve, 1 MTb); - } - -#ifdef SET_INEXACT - try_quick = oldinexact = get_inexact(); - inexact = 1; -#endif -#ifdef Honor_FLT_ROUNDS + return nrv_alloc("0", rve, 1); + } if (Rounding >= 2) { if (*sign) Rounding = Rounding == 2 ? 0 : 2; else if (Rounding != 2) Rounding = 0; - } -#endif - - b = d2b(dval(&d), &be, &bbits MTb); -#ifdef Sudden_Underflow - i = (int)(word0(&d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); -#else + } + b = d2b(dval(&d), &be, &bbits); if (( i = (int)(word0(&d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)) )!=0) { -#endif dval(&d2) = dval(&d); word0(&d2) &= Frac_mask1; word0(&d2) |= Exp_11; -#ifdef IBM - if (( j = 11 - hi0bits(word0(&d2) & Frac_mask) )!=0) - dval(&d2) /= 1 << j; -#endif - /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 * log10(x) = log(x) / log(10) * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) @@ -230,27 +168,19 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) * (We could get a more accurate k by invoking log10, * but this is probably not worthwhile.) */ - i -= Bias; -#ifdef IBM - i <<= 2; - i += j; -#endif -#ifndef Sudden_Underflow denorm = 0; - } + } else { /* d is denormalized */ - i = bbits + be + (Bias + (P-1) - 1); x = i > 32 ? word0(&d) << (64 - i) | word1(&d) >> (i - 32) - : word1(&d) << (32 - i); + : word1(&d) << (32 - i); dval(&d2) = x; word0(&d2) -= 31*Exp_msk1; /* adjust exponent */ i -= (Bias + (P-1) - 1) + 1; denorm = 1; - } -#endif + } ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; k = (int)ds; if (ds < 0. && ds != k) @@ -260,79 +190,66 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) if (dval(&d) < tens[k]) k--; k_check = 0; - } + } j = bbits - i - 1; if (j >= 0) { b2 = 0; s2 = j; - } + } else { b2 = -j; s2 = 0; - } + } if (k >= 0) { b5 = 0; s5 = k; s2 += k; - } + } else { b2 -= k; b5 = -k; s5 = 0; - } + } if (mode < 0 || mode > 9) mode = 0; - -#ifndef SET_INEXACT -#ifdef Check_FLT_ROUNDS try_quick = Rounding == 1; -#else - try_quick = 1; -#endif -#endif /*SET_INEXACT*/ - if (mode > 5) { mode -= 4; try_quick = 0; - } + } + leftright = 1; ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */ /* silence erroneous "gcc -Wall" warning. */ switch(mode) { - case 0: - case 1: - i = 18; - ndigits = 0; - break; - case 2: - leftright = 0; - /* no break */ - case 4: - if (ndigits <= 0) - ndigits = 1; - ilim = ilim1 = i = ndigits; - break; - case 3: - leftright = 0; - /* no break */ - case 5: - i = ndigits + k + 1; - ilim = i; - ilim1 = i - 1; - if (i <= 0) - i = 1; - } - s = s0 = rv_alloc(i MTb); - -#ifdef Honor_FLT_ROUNDS + case 0: + case 1: + i = 18; + ndigits = 0; + break; + case 2: + leftright = 0; + /* no break */ + case 4: + if (ndigits <= 0) + ndigits = 1; + ilim = ilim1 = i = ndigits; + break; + case 3: + leftright = 0; + /* no break */ + case 5: + i = ndigits + k + 1; + ilim = i; + ilim1 = i - 1; + if (i <= 0) + i = 1; + } + s = s0 = rv_alloc(i); if (mode > 1 && Rounding != 1) leftright = 0; -#endif - if (ilim >= 0 && ilim <= Quick_max && try_quick) { - /* Try to get by with floating-point arithmetic. */ - i = 0; j1 = 0; dval(&d2) = dval(&d); @@ -347,22 +264,22 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) j &= Bletch - 1; dval(&d) /= bigtens[n_bigtens-1]; ieps++; - } + } for(; j; j >>= 1, i++) if (j & 1) { ieps++; ds *= bigtens[i]; - } + } dval(&d) /= ds; - } + } else if (( j1 = -k )!=0) { dval(&d) *= tens[j1 & 0xf]; for(j = j1 >> 4; j; j >>= 1, i++) if (j & 1) { ieps++; dval(&d) *= bigtens[i]; - } - } + } + } if (k_check && dval(&d) < 1. && ilim > 0) { if (ilim1 <= 0) goto fast_failed; @@ -370,7 +287,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) k--; dval(&d) *= 10.; ieps++; - } + } dval(&eps) = ieps*dval(&d) + 7.; word0(&eps) -= (P-1)*Exp_msk1; if (ilim == 0) { @@ -381,8 +298,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) if (dval(&d) < -dval(&eps)) goto no_digits; goto fast_failed; - } -#ifndef No_leftright + } if (leftright) { /* Use Steele & White method of only * generating digits needed. @@ -402,8 +318,8 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) *s++ = '1'; ++k; goto ret1; - } } + } for(i = 0;;) { L = dval(&d); dval(&d) -= L; @@ -416,10 +332,9 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) break; dval(&eps) *= 10.; dval(&d) *= 10.; - } } + } else { -#endif /* Generate ilim digits, then fix them up. */ dval(&eps) *= tens[ilim-1]; for(i = 1;; i++, dval(&d) *= 10.) { @@ -433,20 +348,17 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) else if (dval(&d) < 0.5 - dval(&eps)) goto retc; break; - } } -#ifndef No_leftright } -#endif - fast_failed: + } + fast_failed: s = s0; dval(&d) = dval(&d2); k = k0; ilim = ilim0; - } + } /* Do we have a "small" integer? */ - if (be >= 0 && k <= Int_max) { /* Yes. */ ds = tens[k]; @@ -455,114 +367,85 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) if (ilim < 0 || dval(&d) <= 5*ds) goto no_digits; goto one_digit; - } + } for(i = 1;; i++, dval(&d) *= 10.) { L = (Long)(dval(&d) / ds); dval(&d) -= L*ds; -#ifdef Check_FLT_ROUNDS /* If FLT_ROUNDS == 2, L will usually be high by 1 */ if (dval(&d) < 0) { L--; dval(&d) += ds; - } -#endif + } *s++ = '0' + (int)L; if (!dval(&d)) { -#ifdef SET_INEXACT - inexact = 0; -#endif break; - } + } if (i == ilim) { -#ifdef Honor_FLT_ROUNDS if (mode > 1) - switch(Rounding) { - case 0: goto retc; - case 2: goto bump_up; - } -#endif + switch(Rounding) { + case 0: goto retc; + case 2: goto bump_up; + } dval(&d) += dval(&d); -#ifdef ROUND_BIASED - if (dval(&d) >= ds) -#else - if (dval(&d) > ds || (dval(&d) == ds && L & 1)) -#endif - { - bump_up: + if (dval(&d) > ds || (dval(&d) == ds && L & 1)) { + bump_up: while(*--s == '9') if (s == s0) { k++; *s = '0'; break; - } + } ++*s++; - } - break; } + break; } - goto retc; } - + goto retc; + } m2 = b2; m5 = b5; mhi = mlo = 0; if (leftright) { - i = -#ifndef Sudden_Underflow - denorm ? be + (Bias + (P-1) - 1 + 1) : -#endif -#ifdef IBM - 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); -#else - 1 + P - bbits; -#endif + i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits; b2 += i; s2 += i; - mhi = i2b(1 MTb); - } + mhi = i2b(1); + } if (m2 > 0 && s2 > 0) { i = m2 < s2 ? m2 : s2; b2 -= i; m2 -= i; s2 -= i; - } + } if (b5 > 0) { if (leftright) { if (m5 > 0) { - mhi = pow5mult(mhi, m5 MTb); - b1 = mult(mhi, b MTb); - Bfree(b MTb); + mhi = pow5mult(mhi, m5); + b1 = mult(mhi, b); + Bfree(b); b = b1; - } - if (( j = b5 - m5 )!=0) - b = pow5mult(b, j MTb); } - else - b = pow5mult(b, b5 MTb); + if (( j = b5 - m5 )!=0) + b = pow5mult(b, j); } - S = i2b(1 MTb); + else + b = pow5mult(b, b5); + } + S = i2b(1); if (s5 > 0) - S = pow5mult(S, s5 MTb); + S = pow5mult(S, s5); /* Check for special case that d is a normalized power of 2. */ - spec_case = 0; - if ((mode < 2 || leftright) -#ifdef Honor_FLT_ROUNDS - && Rounding == 1 -#endif - ) { - if (!word1(&d) && !(word0(&d) & Bndry_mask) -#ifndef Sudden_Underflow - && word0(&d) & (Exp_mask & ~Exp_msk1) -#endif - ) { + if ((mode < 2 || leftright) && Rounding == 1) { + if (!word1(&d) && !(word0(&d) & Bndry_mask) && + word0(&d) & (Exp_mask & ~Exp_msk1)) { /* The special case */ b2 += Log2P; s2 += Log2P; spec_case = 1; - } } + } /* Arrange for convenient computation of quotients: * shift left if necessary so divisor has 4 leading 0 bits. @@ -571,225 +454,169 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve) * and for all and pass them and a shift to quorem, so it * can do shifts and ors to compute the numerator for q. */ -#ifdef Pack_32 if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f )!=0) i = 32 - i; -#else - if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf )!=0) - i = 16 - i; -#endif if (i > 4) { i -= 4; b2 += i; m2 += i; s2 += i; - } + } else if (i < 4) { i += 28; b2 += i; m2 += i; s2 += i; - } + } if (b2 > 0) - b = lshift(b, b2 MTb); + b = lshift(b, b2); if (s2 > 0) - S = lshift(S, s2 MTb); + S = lshift(S, s2); if (k_check) { if (cmp(b,S) < 0) { k--; - b = multadd(b, 10, 0 MTb); /* we botched the k estimate */ + b = multadd(b, 10, 0); /* we botched the k estimate */ if (leftright) - mhi = multadd(mhi, 10, 0 MTb); + mhi = multadd(mhi, 10, 0); ilim = ilim1; - } } + } if (ilim <= 0 && (mode == 3 || mode == 5)) { - if (ilim < 0 || cmp(b,S = multadd(S,5,0 MTb)) <= 0) { + if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { /* no digits, fcvt style */ - no_digits: + no_digits: k = -1 - ndigits; goto ret; - } - one_digit: + } + one_digit: *s++ = '1'; k++; goto ret; - } + } if (leftright) { if (m2 > 0) - mhi = lshift(mhi, m2 MTb); - + mhi = 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 MTb); + mhi = Balloc(mhi->k); Bcopy(mhi, mlo); - mhi = lshift(mhi, Log2P MTb); - } - + mhi = lshift(mhi, Log2P); + } for(i = 1;;i++) { dig = 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 MTb); + delta = diff(S, mhi); j1 = delta->sign ? 1 : cmp(b, delta); - Bfree(delta MTb); -#ifndef ROUND_BIASED - if (j1 == 0 && mode != 1 && !(word1(&d) & 1) -#ifdef Honor_FLT_ROUNDS - && Rounding >= 1 -#endif - ) { + Bfree(delta); + if (j1 == 0 && mode != 1 && !(word1(&d) & 1) && Rounding >= 1) { if (dig == '9') goto round_9_up; if (j > 0) dig++; -#ifdef SET_INEXACT - else if (!b->x[0] && b->wds <= 1) - inexact = 0; -#endif *s++ = dig; goto ret; - } -#endif - if (j < 0 || (j == 0 && mode != 1 -#ifndef ROUND_BIASED - && !(word1(&d) & 1) -#endif - )) { + } + if (j < 0 || (j == 0 && mode != 1 && !(word1(&d) & 1) + )) { if (!b->x[0] && b->wds <= 1) { -#ifdef SET_INEXACT - inexact = 0; -#endif goto accept_dig; - } -#ifdef Honor_FLT_ROUNDS + } if (mode > 1) - switch(Rounding) { - case 0: goto accept_dig; - case 2: goto keep_dig; - } -#endif /*Honor_FLT_ROUNDS*/ - if (j1 > 0) { - b = lshift(b, 1 MTb); - j1 = cmp(b, S); -#ifdef ROUND_BIASED - if (j1 >= 0 /*)*/ -#else - if ((j1 > 0 || (j1 == 0 && dig & 1)) -#endif - && dig++ == '9') - goto round_9_up; + switch(Rounding) { + case 0: goto accept_dig; + case 2: goto keep_dig; } - accept_dig: + if (j1 > 0) { + b = lshift(b, 1); + j1 = cmp(b, S); + if ((j1 > 0 || (j1 == 0 && dig & 1)) + && dig++ == '9') + goto round_9_up; + } + accept_dig: *s++ = dig; goto ret; - } + } if (j1 > 0) { -#ifdef Honor_FLT_ROUNDS if (!Rounding && mode > 1) goto accept_dig; -#endif if (dig == '9') { /* possible if i == 1 */ - round_9_up: + round_9_up: *s++ = '9'; goto roundoff; - } + } *s++ = dig + 1; goto ret; - } -#ifdef Honor_FLT_ROUNDS - keep_dig: -#endif + } + keep_dig: *s++ = dig; if (i == ilim) break; - b = multadd(b, 10, 0 MTb); + b = multadd(b, 10, 0); if (mlo == mhi) - mlo = mhi = multadd(mhi, 10, 0 MTb); + mlo = mhi = multadd(mhi, 10, 0); else { - mlo = multadd(mlo, 10, 0 MTb); - mhi = multadd(mhi, 10, 0 MTb); - } + mlo = multadd(mlo, 10, 0); + mhi = multadd(mhi, 10, 0); } } - else + } + else { for(i = 1;; i++) { *s++ = dig = quorem(b,S) + '0'; if (!b->x[0] && b->wds <= 1) { -#ifdef SET_INEXACT - inexact = 0; -#endif goto ret; - } + } if (i >= ilim) break; - b = multadd(b, 10, 0 MTb); - } + b = multadd(b, 10, 0); + } + } /* Round off last digit */ - -#ifdef Honor_FLT_ROUNDS switch(Rounding) { - case 0: goto trimzeros; - case 2: goto roundoff; - } -#endif - b = lshift(b, 1 MTb); + case 0: goto trimzeros; + case 2: goto roundoff; + } + b = lshift(b, 1); j = cmp(b, S); -#ifdef ROUND_BIASED - if (j >= 0) -#else if (j > 0 || (j == 0 && dig & 1)) -#endif - { - roundoff: + { + roundoff: while(*--s == '9') if (s == s0) { k++; *s++ = '1'; goto ret; - } + } ++*s++; - } + } else { -#ifdef Honor_FLT_ROUNDS - trimzeros: -#endif + trimzeros: while(*--s == '0'); s++; - } - ret: - Bfree(S MTb); + } +ret: + Bfree(S); if (mhi) { if (mlo && mlo != mhi) - Bfree(mlo MTb); - Bfree(mhi MTb); - } - retc: + Bfree(mlo); + Bfree(mhi); + } +retc: while(s > s0 && s[-1] == '0') --s; - ret1: -#ifdef SET_INEXACT - if (inexact) { - if (!oldinexact) { - word0(&d) = Exp_1 + (70 << Exp_shift); - word1(&d) = 0; - dval(&d) += 1.; - } - } - else if (!oldinexact) - clear_inexact(); -#endif - Bfree(b MTb); +ret1: + Bfree(b); *s = 0; *decpt = k + 1; if (rve) *rve = s; return s0; - } +} diff --git a/third_party/gdtoa/g__fmt.c b/third_party/gdtoa/g__fmt.c index 65cc693d5..c6fa7419a 100644 --- a/third_party/gdtoa/g__fmt.c +++ b/third_party/gdtoa/g__fmt.c @@ -1,52 +1,44 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -#ifndef ldus_QNAN0 +#define dlen 0 +#define HEXDIG "0123456789abcdef" #define ldus_QNAN0 0x7fff -#endif -#ifndef ldus_QNAN1 #define ldus_QNAN1 0xc000 -#endif -#ifndef ldus_QNAN2 #define ldus_QNAN2 0 -#endif -#ifndef ldus_QNAN3 #define ldus_QNAN3 0 -#endif -#ifndef ldus_QNAN4 #define ldus_QNAN4 0 -#endif const char *const InfName[6] = { "Infinity", "infinity", "INFINITY", "Inf", "inf", "INF" }; const char *const NanName[3] = { "NaN", "nan", "NAN" }; @@ -62,27 +54,6 @@ g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen) int i, j, k; char *be, *s0; size_t len; -#ifdef USE_LOCALE -#ifdef NO_LOCALE_CACHE - char *decimalpoint = localeconv()->decimal_point; - size_t dlen = strlen(decimalpoint); -#else - char *decimalpoint; - static char *decimalpoint_cache; - static size_t dlen; - if (!(s0 = decimalpoint_cache)) { - s0 = localeconv()->decimal_point; - dlen = strlen(s0); - if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) { - strcpy(decimalpoint_cache, s0); - s0 = decimalpoint_cache; - } - } - decimalpoint = s0; -#endif -#else -#define dlen 0 -#endif s0 = s; len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */ if (blen < len) @@ -93,21 +64,16 @@ g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen) if (decpt <= -4 || decpt > se - s + 5) { *b++ = *s++; if (*s) { -#ifdef USE_LOCALE - while((*b = *decimalpoint++)) - ++b; -#else *b++ = '.'; -#endif while((*b = *s++) !=0) b++; - } + } *b++ = 'e'; /* sprintf(b, "%+.2d", decpt - 1); */ if (--decpt < 0) { *b++ = '-'; decpt = -decpt; - } + } else *b++ = '+'; for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){} @@ -120,58 +86,46 @@ g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen) break; decpt -= i*k; decpt *= 10; - } - *b = 0; } + *b = 0; + } else if (decpt <= 0) { -#ifdef USE_LOCALE - while((*b = *decimalpoint++)) - ++b; -#else *b++ = '.'; -#endif if (be < b - decpt + (se - s)) goto ret0; for(; decpt < 0; decpt++) *b++ = '0'; while((*b = *s++) != 0) b++; - } + } else { while((*b = *s++) != 0) { b++; if (--decpt == 0 && *s) { -#ifdef USE_LOCALE - while(*b = *decimalpoint++) - ++b; -#else *b++ = '.'; -#endif - } } + } if (b + decpt > be) { - ret0: + ret0: b = 0; goto ret; - } + } for(; decpt > 0; decpt--) *b++ = '0'; *b = 0; - } - ret: + } +ret: freedtoa(s0); return b; - } +} - char * +char * __gdtoa_add_nanbits(char *b, size_t blen, ULong *bits, int nb) { ULong t; char *rv; int i, j; size_t L; - static char Hexdig[16] = "0123456789abcdef"; - while(!bits[--nb]) if (!nb) return b; @@ -183,14 +137,14 @@ __gdtoa_add_nanbits(char *b, size_t blen, ULong *bits, int nb) b += L; *--b = 0; rv = b; - *--b = /*(*/ ')'; + *--b = ')'; for(i = 0; i < nb; ++i) { t = bits[i]; for(j = 0; j < 8; ++j, t >>= 4) - *--b = Hexdig[t & 0xf]; - } - t = bits[nb]; - do *--b = Hexdig[t & 0xf]; while(t >>= 4); - *--b = '('; /*)*/ - return rv; + *--b = HEXDIG[t & 0xf]; } + t = bits[nb]; + do *--b = HEXDIG[t & 0xf]; while(t >>= 4); + *--b = '('; + return rv; +} diff --git a/third_party/gdtoa/g_ddfmt.c b/third_party/gdtoa/g_ddfmt.c index 547316688..3e6b0e9de 100644 --- a/third_party/gdtoa/g_ddfmt.c +++ b/third_party/gdtoa/g_ddfmt.c @@ -1,35 +1,37 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/runtime/fenv.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg@acm.org). */ char * g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize) @@ -40,96 +42,74 @@ g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize) int bx, by, decpt, ex, ey, i, j, mode; Bigint *x, *y, *z; U *dd, ddx[2]; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif -#ifdef Honor_FLT_ROUNDS /*{{*/ - int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ -#else /*}{*/ -#define Rounding FPI_Round_near -#endif /*}}*/ - if (bufsize < 10 || bufsize < (size_t)(ndig + 8)) return 0; - dd = (U*)dd0; L = dd->L; - if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) { + if ((L[1] & 0x7ff00000L) == 0x7ff00000L) { /* Infinity or NaN */ - if (L[_0] & 0xfffff || L[_1]) { - nanret: - return strcp(buf, "NaN"); - } - if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { - if (L[2+_0] & 0xfffff || L[2+_1]) - goto nanret; - if ((L[_0] ^ L[2+_0]) & 0x80000000L) - goto nanret; /* Infinity - Infinity */ - } - infret: - b = buf; - if (L[_0] & 0x80000000L) - *b++ = '-'; - return strcp(b, "Infinity"); + if (L[1] & 0xfffff || L[0]) { + nanret: + return stpcpy(buf, "NaN"); } - if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { + if ((L[2+1] & 0x7ff00000) == 0x7ff00000) { + if (L[2+1] & 0xfffff || L[2+0]) + goto nanret; + if ((L[1] ^ L[2+1]) & 0x80000000L) + goto nanret; /* Infinity - Infinity */ + } + infret: + b = buf; + if (L[1] & 0x80000000L) + *b++ = '-'; + return stpcpy(b, "Infinity"); + } + if ((L[2+1] & 0x7ff00000) == 0x7ff00000) { L += 2; - if (L[_0] & 0xfffff || L[_1]) + if (L[1] & 0xfffff || L[0]) goto nanret; goto infret; - } + } if (dval(&dd[0]) + dval(&dd[1]) == 0.) { b = buf; -#ifndef IGNORE_ZERO_SIGN - if (L[_0] & L[2+_0] & 0x80000000L) + if (L[1] & L[2+1] & 0x80000000L) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } - if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) { + } + if ((L[1] & 0x7ff00000L) < (L[2+1] & 0x7ff00000L)) { dval(&ddx[1]) = dval(&dd[0]); dval(&ddx[0]) = dval(&dd[1]); dd = ddx; L = dd->L; - } - z = d2b(dval(&dd[0]), &ex, &bx MTb); + } + z = d2b(dval(&dd[0]), &ex, &bx); if (dval(&dd[1]) == 0.) goto no_y; x = z; - y = d2b(dval(&dd[1]), &ey, &by MTb); + y = d2b(dval(&dd[1]), &ey, &by); if ( (i = ex - ey) !=0) { if (i > 0) { - x = lshift(x, i MTb); + x = lshift(x, i); ex = ey; - } + } else - y = lshift(y, -i MTb); - } - if ((L[_0] ^ L[2+_0]) & 0x80000000L) { - z = diff(x, y MTb); - if (L[_0] & 0x80000000L) + y = lshift(y, -i); + } + if ((L[1] ^ L[2+1]) & 0x80000000L) { + z = diff(x, y); + if (L[1] & 0x80000000L) z->sign = 1 - z->sign; - } + } else { - z = sum(x, y MTb); - if (L[_0] & 0x80000000L) + z = sum(x, y); + if (L[1] & 0x80000000L) z->sign = 1; - } - Bfree(x MTb); - Bfree(y MTb); - no_y: + } + Bfree(x); + Bfree(y); +no_y: bits = zx = z->x; for(i = 0; !*zx; zx++) i += 32; @@ -137,7 +117,7 @@ g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize) if (i) { rshift(z, i); ex += i; - } + } fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]); if (fpi.nbits < 106) { fpi.nbits = 106; @@ -147,24 +127,24 @@ g_ddfmt(char *buf, double *dd0, int ndig, size_t bufsize) while(i < 4) bits0[i++] = 0; bits = bits0; - } } + } mode = 2; if (ndig <= 0) { if (bufsize < (size_t)(fpi.nbits * .301029995664) + 10) { - Bfree(z MTb); + Bfree(z); return 0; - } - mode = 0; } + mode = 0; + } fpi.emin = 1-1023-53+1; fpi.emax = 2046-1023-106+1; - fpi.rounding = Rounding; + fpi.rounding = FLT_ROUNDS; fpi.sudden_underflow = 0; 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 MTb); + Bfree(z); return b; - } +} diff --git a/third_party/gdtoa/g_ddfmt_p.c b/third_party/gdtoa/g_ddfmt_p.c index 4e294d3fe..9d03084ae 100644 --- a/third_party/gdtoa/g_ddfmt_p.c +++ b/third_party/gdtoa/g_ddfmt_p.c @@ -1,35 +1,38 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/runtime/fenv.h" +#include "libc/str/str.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg@acm.org). */ extern ULong __gdtoa_NanDflt_d[2]; @@ -42,114 +45,92 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik) int bx, by, decpt, ex, ey, i, j, mode; Bigint *x, *y, *z; U *dd, ddx[2]; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif -#ifdef Honor_FLT_ROUNDS /*{{*/ - int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ -#else /*}{*/ -#define Rounding FPI_Round_near -#endif /*}}*/ - if (bufsize < 10 || bufsize < (size_t)(ndig + 8)) return 0; - dd = (U*)dd0; L = dd->L; - sign = L[_0] & L[2+_0] & 0x80000000L; + sign = L[1] & L[2+1] & 0x80000000L; if (nik < 0 || nik > 35) nik = 0; - if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) { + if ((L[1] & 0x7ff00000L) == 0x7ff00000L) { /* Infinity or NaN */ - if (L[_0] & 0xfffff || L[_1]) { - nanret: + if (L[1] & 0xfffff || L[0]) { + nanret: b = buf; if (sign && nik < 18) *b++ = '-'; - b = strcp(b, NanName[nik%3]); + b = stpcpy(b, NanName[nik%3]); if (nik > 5 && (nik < 12 - || L[_1] != __gdtoa_NanDflt_d[0] - || (L[_0] ^ __gdtoa_NanDflt_d[1]) & 0xfffff - || L[2+_1] != __gdtoa_NanDflt_d[0] - || (L[2+_0] ^ __gdtoa_NanDflt_d[1]) & 0xfffff)) { - bits0[0] = L[2+_1]; - bits0[1] = (L[2+_0] & 0xfffff) | (L[_1] << 20); - bits0[2] = (L[_1] >> 12) | (L[_0] << 20); - bits0[3] = (L[_0] >> 12) & 0xff; + || L[0] != __gdtoa_NanDflt_d[0] + || (L[1] ^ __gdtoa_NanDflt_d[1]) & 0xfffff + || L[2+0] != __gdtoa_NanDflt_d[0] + || (L[2+1] ^ __gdtoa_NanDflt_d[1]) & 0xfffff)) { + bits0[0] = L[2+0]; + 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); - } + } return b; - } - if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { - if (L[2+_0] & 0xfffff || L[2+_1]) - goto nanret; - if ((L[_0] ^ L[2+_0]) & 0x80000000L) - goto nanret; /* Infinity - Infinity */ - } - infret: - b = buf; - if (L[_0] & 0x80000000L) - *b++ = '-'; - return strcp(b, InfName[nik%6]); } - if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { + if ((L[2+1] & 0x7ff00000) == 0x7ff00000) { + if (L[2+1] & 0xfffff || L[2+0]) + goto nanret; + if ((L[1] ^ L[2+1]) & 0x80000000L) + goto nanret; /* Infinity - Infinity */ + } + infret: + b = buf; + if (L[1] & 0x80000000L) + *b++ = '-'; + return stpcpy(b, InfName[nik%6]); + } + if ((L[2+1] & 0x7ff00000) == 0x7ff00000) { L += 2; - if (L[_0] & 0xfffff || L[_1]) + if (L[1] & 0xfffff || L[0]) goto nanret; goto infret; - } + } if (dval(&dd[0]) + dval(&dd[1]) == 0.) { b = buf; -#ifndef IGNORE_ZERO_SIGN if (sign) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } - if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) { + } + if ((L[1] & 0x7ff00000L) < (L[2+1] & 0x7ff00000L)) { dval(&ddx[1]) = dval(&dd[0]); dval(&ddx[0]) = dval(&dd[1]); dd = ddx; L = dd->L; - } - z = d2b(dval(&dd[0]), &ex, &bx MTb); + } + z = d2b(dval(&dd[0]), &ex, &bx); if (dval(&dd[1]) == 0.) goto no_y; x = z; - y = d2b(dval(&dd[1]), &ey, &by MTb); + y = d2b(dval(&dd[1]), &ey, &by); if ( (i = ex - ey) !=0) { if (i > 0) { - x = lshift(x, i MTb); + x = lshift(x, i); ex = ey; - } + } else - y = lshift(y, -i MTb); - } - if ((L[_0] ^ L[2+_0]) & 0x80000000L) { - z = diff(x, y MTb); - if (L[_0] & 0x80000000L) + y = lshift(y, -i); + } + if ((L[1] ^ L[2+1]) & 0x80000000L) { + z = diff(x, y); + if (L[1] & 0x80000000L) z->sign = 1 - z->sign; - } + } else { - z = sum(x, y MTb); - if (L[_0] & 0x80000000L) + z = sum(x, y); + if (L[1] & 0x80000000L) z->sign = 1; - } - Bfree(x MTb); - Bfree(y MTb); - no_y: + } + Bfree(x); + Bfree(y); +no_y: bits = zx = z->x; for(i = 0; !*zx; zx++) i += 32; @@ -157,7 +138,7 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik) if (i) { rshift(z, i); ex += i; - } + } fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]); if (fpi.nbits < 106) { fpi.nbits = 106; @@ -167,24 +148,24 @@ g_ddfmt_p(char *buf, double *dd0, int ndig, size_t bufsize, int nik) while(i < 4) bits0[i++] = 0; bits = bits0; - } } + } mode = 2; if (ndig <= 0) { if (bufsize < (size_t)(fpi.nbits * .301029995664) + 10) { - Bfree(z MTb); + Bfree(z); return 0; - } - mode = 0; } + mode = 0; + } fpi.emin = 1-1023-53+1; fpi.emax = 2046-1023-106+1; - fpi.rounding = Rounding; + fpi.rounding = FLT_ROUNDS; fpi.sudden_underflow = 0; 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 MTb); + Bfree(z); return b; - } +} diff --git a/third_party/gdtoa/g_dfmt.c b/third_party/gdtoa/g_dfmt.c index 70d518354..a6e8b0012 100644 --- a/third_party/gdtoa/g_dfmt.c +++ b/third_party/gdtoa/g_dfmt.c @@ -1,36 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ char* g_dfmt(char *buf, double *d, int ndig, size_t bufsize) @@ -39,44 +39,36 @@ g_dfmt(char *buf, double *d, int ndig, size_t bufsize) char *b, *s, *se; ULong bits[2], *L, sign; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (ULong*)d; - sign = L[_0] & 0x80000000L; - if ((L[_0] & 0x7ff00000) == 0x7ff00000) { + sign = L[1] & 0x80000000L; + if ((L[1] & 0x7ff00000) == 0x7ff00000) { /* Infinity or NaN */ if (bufsize < 10) return 0; - if (L[_0] & 0xfffff || L[_1]) { - return strcp(buf, "NaN"); - } + if (L[1] & 0xfffff || L[0]) { + return stpcpy(buf, "NaN"); + } b = buf; if (sign) *b++ = '-'; - return strcp(b, "Infinity"); - } - if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) { + return stpcpy(b, "Infinity"); + } + if (L[0] == 0 && (L[1] ^ sign) == 0 /*d == 0.*/) { b = buf; -#ifndef IGNORE_ZERO_SIGN - if (L[_0] & 0x80000000L) + if (L[1] & 0x80000000L) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } - bits[0] = L[_1]; - bits[1] = L[_0] & 0xfffff; - if ( (ex = (L[_0] >> 20) & 0x7ff) !=0) + } + bits[0] = L[0]; + bits[1] = L[1] & 0xfffff; + if ( (ex = (L[1] >> 20) & 0x7ff) !=0) bits[1] |= 0x100000; else ex = 1; @@ -89,4 +81,4 @@ g_dfmt(char *buf, double *d, int ndig, size_t bufsize) i = STRTOG_Normal | STRTOG_Neg; s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/g_dfmt_p.c b/third_party/gdtoa/g_dfmt_p.c index 1cf5c89d4..e998ad42c 100644 --- a/third_party/gdtoa/g_dfmt_p.c +++ b/third_party/gdtoa/g_dfmt_p.c @@ -1,36 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ extern ULong __gdtoa_NanDflt_d[2]; @@ -41,57 +41,49 @@ g_dfmt_p(char *buf, double *d, int ndig, size_t bufsize, int nik) char *b, *s, *se; ULong bits[2], *L, sign; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (ULong*)d; - sign = L[_0] & 0x80000000L; - if ((L[_0] & 0x7ff00000) == 0x7ff00000) { + sign = L[1] & 0x80000000L; + if ((L[1] & 0x7ff00000) == 0x7ff00000) { /* Infinity or NaN */ if (nik < 0 || nik > 35) nik = 0; if (bufsize < 10) return 0; - if (L[_0] & 0xfffff || L[_1]) { + if (L[1] & 0xfffff || L[0]) { b = buf; - if (L[_0] & 0x80000000L && nik < 18) + if (L[1] & 0x80000000L && nik < 18) *b++ = '-'; - b = strcp(b, NanName[nik%3]); + b = stpcpy(b, NanName[nik%3]); if (nik > 5 && (nik < 12 || bits[0] != __gdtoa_NanDflt_d[0] || (bits[1] ^ __gdtoa_NanDflt_d[1]) & 0xfffff)) { - bits[0] = L[_1]; - bits[1] = L[_0] & 0xfffff; + bits[0] = L[0]; + bits[1] = L[1] & 0xfffff; b = add_nanbits(b, bufsize - (b-buf), bits, 2); - } - return b; } + return b; + } b = buf; if (sign) *b++ = '-'; - return strcp(b, InfName[nik%6]); - } - if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) { + return stpcpy(b, InfName[nik%6]); + } + if (L[0] == 0 && (L[1] ^ sign) == 0 /*d == 0.*/) { b = buf; -#ifndef IGNORE_ZERO_SIGN - if (L[_0] & 0x80000000L) + if (L[1] & 0x80000000L) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } - bits[0] = L[_1]; - bits[1] = L[_0] & 0xfffff; - if ( (ex = (L[_0] >> 20) & 0x7ff) !=0) + } + bits[0] = L[0]; + bits[1] = L[1] & 0xfffff; + if ( (ex = (L[1] >> 20) & 0x7ff) !=0) bits[1] |= 0x100000; else ex = 1; @@ -104,4 +96,4 @@ g_dfmt_p(char *buf, double *d, int ndig, size_t bufsize, int nik) i = STRTOG_Normal | STRTOG_Neg; s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/g_ffmt.c b/third_party/gdtoa/g_ffmt.c index f91401f3e..df17c9600 100644 --- a/third_party/gdtoa/g_ffmt.c +++ b/third_party/gdtoa/g_ffmt.c @@ -1,36 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ char* g_ffmt(char *buf, float *f, int ndig, size_t bufsize) @@ -39,39 +39,31 @@ g_ffmt(char *buf, float *f, int ndig, size_t bufsize) char *b, *s, *se; ULong bits[1], *L, sign; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (ULong*)f; sign = L[0] & 0x80000000L; if ((L[0] & 0x7f800000) == 0x7f800000) { /* Infinity or NaN */ if (L[0] & 0x7fffff) { - return strcp(buf, "NaN"); - } + return stpcpy(buf, "NaN"); + } b = buf; if (sign) *b++ = '-'; - return strcp(b, "Infinity"); - } + return stpcpy(b, "Infinity"); + } if (*f == 0.) { b = buf; -#ifndef IGNORE_ZERO_SIGN if (L[0] & 0x80000000L) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } + } bits[0] = L[0] & 0x7fffff; if ( (ex = (L[0] >> 23) & 0xff) !=0) bits[0] |= 0x800000; @@ -83,8 +75,8 @@ g_ffmt(char *buf, float *f, int ndig, size_t bufsize) if (bufsize < 16) return 0; mode = 0; - } + } i = STRTOG_Normal; s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/g_ffmt_p.c b/third_party/gdtoa/g_ffmt_p.c index 0ba53cbea..48e22858f 100644 --- a/third_party/gdtoa/g_ffmt_p.c +++ b/third_party/gdtoa/g_ffmt_p.c @@ -1,36 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ extern ULong __gdtoa_NanDflt_f[1]; @@ -41,17 +41,11 @@ g_ffmt_p(char *buf, float *f, int ndig, size_t bufsize, int nik) char *b, *s, *se; ULong bits[1], *L, sign; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (ULong*)f; sign = L[0] & 0x80000000L; if ((L[0] & 0x7f800000) == 0x7f800000) { @@ -62,27 +56,25 @@ g_ffmt_p(char *buf, float *f, int ndig, size_t bufsize, int nik) b = buf; if (sign && nik < 18) *b++ = '-'; - b = strcp(b, NanName[nik%3]); + b = stpcpy(b, NanName[nik%3]); if (nik > 5 && (nik < 12 || (bits[0] ^ __gdtoa_NanDflt_f[0]) & 0x7fffff)) b = add_nanbits(b, bufsize - (b-buf), bits, 1); return b; - } + } b = buf; if (sign) *b++ = '-'; - return strcp(b, InfName[nik%6]); - } + return stpcpy(b, InfName[nik%6]); + } if (*f == 0.) { b = buf; -#ifndef IGNORE_ZERO_SIGN if (L[0] & 0x80000000L) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } + } bits[0] = L[0] & 0x7fffff; if ( (ex = (L[0] >> 23) & 0xff) !=0) bits[0] |= 0x800000; @@ -94,8 +86,8 @@ g_ffmt_p(char *buf, float *f, int ndig, size_t bufsize, int nik) if (bufsize < 16) return 0; mode = 0; - } + } i = STRTOG_Normal; s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/g_xfmt.c b/third_party/gdtoa/g_xfmt.c index b2439ab95..117ed9c1d 100644 --- a/third_party/gdtoa/g_xfmt.c +++ b/third_party/gdtoa/g_xfmt.c @@ -1,56 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -#undef _0 -#undef _1 - -/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ - -#ifdef IEEE_MC68k -#define _0 0 -#define _1 1 -#define _2 2 -#define _3 3 -#define _4 4 -#endif -#ifdef IEEE_8087 -#define _0 4 -#define _1 3 -#define _2 2 -#define _3 1 -#define _4 0 -#endif char* g_xfmt(char *buf, void *V, int ndig, size_t bufsize) @@ -60,57 +40,49 @@ g_xfmt(char *buf, void *V, int ndig, size_t bufsize) ULong bits[2], sign; UShort *L; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (UShort *)V; - sign = L[_0] & 0x8000; - bits[1] = ((ULong)L[_1] << 16) | L[_2]; - bits[0] = ((ULong)L[_3] << 16) | L[_4]; - if ( (ex = L[_0] & 0x7fff) !=0) { + sign = L[4] & 0x8000; + bits[1] = ((ULong)L[3] << 16) | L[2]; + bits[0] = ((ULong)L[1] << 16) | L[0]; + if ( (ex = L[4] & 0x7fff) !=0) { if (ex == 0x7fff) { /* Infinity or NaN */ if (!bits[0] && bits[1]== 0x80000000) { b = buf; if (sign) *b++ = '-'; - b = strcp(b, "Infinity"); - } - else - b = strcp(buf, "NaN"); - return b; + b = stpcpy(b, "Infinity"); } - i = STRTOG_Normal; + else + b = stpcpy(buf, "NaN"); + return b; } + i = STRTOG_Normal; + } else if (bits[0] | bits[1]) { i = STRTOG_Denormal; ex = 1; - } + } else { b = buf; -#ifndef IGNORE_ZERO_SIGN if (sign) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } + } ex -= 0x3fff + 63; mode = 2; if (ndig <= 0) { if (bufsize < 32) return 0; mode = 0; - } + } s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/g_xfmt_p.c b/third_party/gdtoa/g_xfmt_p.c index fb8856290..35906e34e 100644 --- a/third_party/gdtoa/g_xfmt_p.c +++ b/third_party/gdtoa/g_xfmt_p.c @@ -1,59 +1,39 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ extern UShort __gdtoa_NanDflt_ldus[5]; -#undef _0 -#undef _1 - -/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ - -#ifdef IEEE_MC68k -#define _0 0 -#define _1 1 -#define _2 2 -#define _3 3 -#define _4 4 -#endif -#ifdef IEEE_8087 -#define _0 4 -#define _1 3 -#define _2 2 -#define _3 1 -#define _4 0 -#endif - char* g_xfmt_p(char *buf, void *V, int ndig, size_t bufsize, int nik) { @@ -62,22 +42,16 @@ g_xfmt_p(char *buf, void *V, int ndig, size_t bufsize, int nik) ULong bits[2], sign; UShort *L; int decpt, ex, i, mode; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - if (ndig < 0) ndig = 0; if (bufsize < (size_t)(ndig + 10)) return 0; - L = (UShort *)V; - sign = L[_0] & 0x8000; - bits[1] = ((ULong)L[_1] << 16) | L[_2]; - bits[0] = ((ULong)L[_3] << 16) | L[_4]; - if ( (ex = L[_0] & 0x7fff) !=0) { + sign = L[4] & 0x8000; + bits[1] = ((ULong)L[3] << 16) | L[2]; + bits[0] = ((ULong)L[1] << 16) | L[0]; + if ( (ex = L[4] & 0x7fff) !=0) { if (ex == 0x7fff) { /* Infinity or NaN */ if (nik < 0 || nik > 35) @@ -86,47 +60,45 @@ g_xfmt_p(char *buf, void *V, int ndig, size_t bufsize, int nik) b = buf; if (sign) *b++ = '-'; - b = strcp(b, InfName[nik%6]); - } + b = stpcpy(b, InfName[nik%6]); + } else { b = buf; if (sign && nik < 18) *b++ = '-'; - b = strcp(b, NanName[nik%3]); + b = stpcpy(b, NanName[nik%3]); if (nik > 5 && (nik < 12 - || L[_1] != __gdtoa_NanDflt_ldus[3] - || L[_2] != __gdtoa_NanDflt_ldus[2] - || L[_3] != __gdtoa_NanDflt_ldus[1] - || L[_4] != __gdtoa_NanDflt_ldus[0])) { + || 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); - } } - return b; } - i = STRTOG_Normal; + return b; } + i = STRTOG_Normal; + } else if (bits[0] | bits[1]) { i = STRTOG_Denormal; ex = 1; - } + } else { b = buf; -#ifndef IGNORE_ZERO_SIGN if (sign) *b++ = '-'; -#endif *b++ = '0'; *b = 0; return b; - } + } ex -= 0x3fff + 63; mode = 2; if (ndig <= 0) { if (bufsize < 32) return 0; mode = 0; - } + } s = gdtoa(fpi, ex, bits, &i, mode, ndig, &decpt, &se); return g__fmt(buf, s, se, decpt, sign, bufsize); - } +} diff --git a/third_party/gdtoa/gdtoa.c b/third_party/gdtoa/gdtoa.c index bb46f89f3..2d0dc45dd 100644 --- a/third_party/gdtoa/gdtoa.c +++ b/third_party/gdtoa/gdtoa.c @@ -1,75 +1,67 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -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. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ static Bigint * -bitstob(ULong *bits, int nbits, int *bbits MTd) +bitstob(ULong *bits, int nbits, int *bbits) { int i, k; Bigint *b; ULong *be, *x, *x0; - i = ULbits; k = 0; while(i < nbits) { i <<= 1; k++; - } -#ifndef Pack_32 - if (!k) - k = 1; -#endif - b = Balloc(k MTa); + } + b = Balloc(k); be = bits + ((nbits - 1) >> kshift); x = x0 = b->x; do { *x++ = *bits & ALL_ON; -#ifdef Pack_16 - *x++ = (*bits >> 16) & ALL_ON; -#endif - } while(++bits <= be); + } while(++bits <= be); i = x - x0; while(!x0[--i]) if (!i) { b->wds = 0; *bbits = 0; goto ret; - } + } b->wds = i + 1; *bbits = i*ULbits + 32 - hi0bits(b->x[i]); - ret: +ret: return b; - } +} /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. * @@ -106,7 +98,7 @@ bitstob(ULong *bits, int nbits, int *bbits MTd) */ char * -gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve) +gdtoa(const FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve) { /* Arguments ndigits and decpt are similar to the second and third arguments of ecvt and fcvt; trailing zeros are suppressed from @@ -143,9 +135,6 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in to hold the suppressed trailing zeros. */ -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif int bbits, b2, b5, be0, dig, i, ieps, ilim, ilim0, ilim1, inex; int j, j1, k, k0, k_check, kind, leftright, m2, m5, nbits; int rdir, s2, s5, spec_case, try_quick; @@ -154,53 +143,40 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in double d2, ds; char *s, *s0; U d, eps; - -#ifndef MULTIPLE_THREADS - if (dtoa_result) { - freedtoa(dtoa_result); - dtoa_result = 0; - } -#endif inex = 0; kind = *kindp &= ~STRTOG_Inexact; switch(kind & STRTOG_Retmask) { - case STRTOG_Zero: + case STRTOG_Zero: goto ret_zero; - case STRTOG_Normal: - case STRTOG_Denormal: + case STRTOG_Normal: + case STRTOG_Denormal: break; - case STRTOG_Infinite: + case STRTOG_Infinite: *decpt = -32768; - return nrv_alloc("Infinity", rve, 8 MTb); - case STRTOG_NaN: + return nrv_alloc("Infinity", rve, 8); + case STRTOG_NaN: *decpt = -32768; - return nrv_alloc("NaN", rve, 3 MTb); - default: + return nrv_alloc("NaN", rve, 3); + default: return 0; - } - b = bitstob(bits, nbits = fpi->nbits, &bbits MTb); + } + b = bitstob(bits, nbits = fpi->nbits, &bbits); be0 = be; if ( (i = trailz(b)) !=0) { rshift(b, i); be += i; bbits -= i; - } + } if (!b->wds) { - Bfree(b MTb); - ret_zero: + Bfree(b); + ret_zero: *decpt = 1; - return nrv_alloc("0", rve, 1 MTb); - } - + return nrv_alloc("0", rve, 1); + } dval(&d) = b2d(b, &i); i = be + bbits - 1; word0(&d) &= Frac_mask1; word0(&d) |= Exp_11; -#ifdef IBM - if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0) - dval(&d) /= 1 << j; -#endif - /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 * log10(x) = log(x) / log(10) * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) @@ -222,25 +198,16 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in * (We could get a more accurate k by invoking log10, * but this is probably not worthwhile.) */ - ds = (dval(&d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; - /* correct assumption about exponent range */ if ((j = i) < 0) j = -j; if ((j -= 1077) > 0) ds += j * 7e-17; - k = (int)ds; if (ds < 0. && ds != k) k--; /* want k = floor(ds) */ k_check = 1; -#ifdef IBM - j = be + bbits - 1; - if ( (j1 = j & 3) !=0) - dval(&d) *= 1 << j1; - word0(&d) += j << Exp_shift - 2 & Exp_mask; -#else // TODO: word0(&d) += (be + bbits - 1) << Exp_shift; // error: third_party/gdtoa/gdtoa.c:244: left shift of negative value -6 'int' 20 'int' // 4161d8: __die at libc/log/die.c:33 @@ -258,69 +225,67 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in // 401d30: cosmo at libc/runtime/cosmo.S:65 // 401173: _start at libc/crt/crt.S:67 word0(&d) += (unsigned)(be + bbits - 1) << Exp_shift; -#endif if (k >= 0 && k <= Ten_pmax) { if (dval(&d) < tens[k]) k--; k_check = 0; - } + } j = bbits - i - 1; if (j >= 0) { b2 = 0; s2 = j; - } + } else { b2 = -j; s2 = 0; - } + } if (k >= 0) { b5 = 0; s5 = k; s2 += k; - } + } else { b2 -= k; b5 = -k; s5 = 0; - } + } if (mode < 0 || mode > 9) mode = 0; try_quick = 1; if (mode > 5) { mode -= 4; try_quick = 0; - } + } else if (i >= -4 - Emin || i < Emin) try_quick = 0; leftright = 1; ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */ /* silence erroneous "gcc -Wall" warning. */ switch(mode) { - case 0: - case 1: - i = (int)(nbits * .30103) + 3; - ndigits = 0; - break; - case 2: - leftright = 0; - /* no break */ - case 4: - if (ndigits <= 0) - ndigits = 1; - ilim = ilim1 = i = ndigits; - break; - case 3: - leftright = 0; - /* no break */ - case 5: - i = ndigits + k + 1; - ilim = i; - ilim1 = i - 1; - if (i <= 0) - i = 1; - } - s = s0 = rv_alloc(i MTb); - + case 0: + case 1: + i = (int)(nbits * .30103) + 3; + ndigits = 0; + break; + case 2: + leftright = 0; + /* no break */ + case 4: + if (ndigits <= 0) + ndigits = 1; + ilim = ilim1 = i = ndigits; + break; + case 3: + leftright = 0; + /* no break */ + case 5: + i = ndigits + k + 1; + ilim = i; + ilim1 = i - 1; + if (i <= 0) + i = 1; + } + s = s0 = rv_alloc(i); if (mode <= 1) rdir = 0; else if ( (rdir = fpi->rounding - 1) !=0) { @@ -328,18 +293,10 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in rdir = 2; if (kind & STRTOG_Neg) rdir = 3 - rdir; - } - + } /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */ - - if (ilim >= 0 && ilim <= Quick_max && try_quick && !rdir -#ifndef IMPRECISE_INEXACT - && k == 0 -#endif - ) { - + if (ilim >= 0 && ilim <= Quick_max && try_quick && !rdir && k == 0) { /* Try to get by with floating-point arithmetic. */ - i = 0; d2 = dval(&d); k0 = k; @@ -353,13 +310,13 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in j &= Bletch - 1; dval(&d) /= bigtens[n_bigtens-1]; ieps++; - } + } for(; j; j >>= 1, i++) if (j & 1) { ieps++; ds *= bigtens[i]; - } - } + } + } else { ds = 1.; if ( (j1 = -k) !=0) { @@ -368,9 +325,9 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in if (j & 1) { ieps++; dval(&d) *= bigtens[i]; - } - } + } } + } if (k_check && dval(&d) < 1. && ilim > 0) { if (ilim1 <= 0) goto fast_failed; @@ -378,7 +335,7 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in k--; dval(&d) *= 10.; ieps++; - } + } dval(&eps) = ieps*dval(&d) + 7.; word0(&eps) -= (P-1)*Exp_msk1; if (ilim == 0) { @@ -389,8 +346,7 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in if (dval(&d) < -dval(&eps)) goto no_digits; goto fast_failed; - } -#ifndef No_leftright + } if (leftright) { /* Use Steele & White method of only * generating digits needed. @@ -404,17 +360,16 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in if (dval(&d)) inex = STRTOG_Inexlo; goto ret1; - } + } if (ds - dval(&d) < dval(&eps)) goto bump_up; if (++i >= ilim) break; dval(&eps) *= 10.; dval(&d) *= 10.; - } } + } else { -#endif /* Generate ilim digits, then fix them up. */ dval(&eps) *= tens[ilim-1]; for(i = 1;; i++, dval(&d) *= 10.) { @@ -429,22 +384,18 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in if (dval(&d)) inex = STRTOG_Inexlo; goto ret1; - } - break; } + break; } -#ifndef No_leftright } -#endif - fast_failed: + } + fast_failed: s = s0; dval(&d) = d2; k = k0; ilim = ilim0; - } - + } /* Do we have a "small" integer? */ - if (be >= 0 && k <= fpi->int_max) { /* Yes. */ ds = tens[k]; @@ -453,17 +404,15 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in if (ilim < 0 || dval(&d) <= 5*ds) goto no_digits; goto one_digit; - } + } for(i = 1;; i++, dval(&d) *= 10.) { L = dval(&d) / ds; dval(&d) -= L*ds; -#ifdef Check_FLT_ROUNDS /* If FLT_ROUNDS == 2, L will usually be high by 1 */ if (dval(&d) < 0) { L--; dval(&d) += ds; - } -#endif + } *s++ = '0' + (int)L; if (dval(&d) == 0.) break; @@ -473,32 +422,27 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in goto bump_up; inex = STRTOG_Inexlo; goto ret1; - } + } dval(&d) += dval(&d); -#ifdef ROUND_BIASED - if (dval(&d) >= ds) -#else if (dval(&d) > ds || (dval(&d) == ds && L & 1)) -#endif - { - bump_up: + { + bump_up: inex = STRTOG_Inexhi; while(*--s == '9') if (s == s0) { k++; *s = '0'; break; - } + } ++*s++; - } + } else inex = STRTOG_Inexlo; break; - } } - goto ret1; } - + goto ret1; + } m2 = b2; m5 = b5; mhi = mlo = 0; @@ -509,9 +453,9 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in i = be - fpi->emin + 1; if (mode >= 2 && ilim > 0 && ilim < i) goto small_ilim; - } + } else if (mode >= 2) { - small_ilim: + small_ilim: j = ilim - 1; if (m5 >= j) m5 -= j; @@ -519,42 +463,40 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in s5 += j -= m5; b5 += j; m5 = 0; - } + } if ((i = ilim) < 0) { m2 -= i; i = 0; - } } + } b2 += i; s2 += i; - mhi = i2b(1 MTb); - } + mhi = i2b(1); + } if (m2 > 0 && s2 > 0) { i = m2 < s2 ? m2 : s2; b2 -= i; m2 -= i; s2 -= i; - } + } if (b5 > 0) { if (leftright) { if (m5 > 0) { - mhi = pow5mult(mhi, m5 MTb); - b1 = mult(mhi, b MTb); - Bfree(b MTb); + mhi = pow5mult(mhi, m5); + b1 = mult(mhi, b); + Bfree(b); b = b1; - } - if ( (j = b5 - m5) !=0) - b = pow5mult(b, j MTb); } - else - b = pow5mult(b, b5 MTb); + if ( (j = b5 - m5) !=0) + b = pow5mult(b, j); } - S = i2b(1 MTb); + else + b = pow5mult(b, b5); + } + S = i2b(1); if (s5 > 0) - S = pow5mult(S, s5 MTb); - + S = pow5mult(S, s5); /* Check for special case that d is a normalized power of 2. */ - spec_case = 0; if (mode < 2) { if (bbits == 1 && be0 > fpi->emin + 1) { @@ -562,9 +504,8 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in b2++; s2++; spec_case = 1; - } } - + } /* Arrange for convenient computation of quotients: * shift left if necessary so divisor has 4 leading 0 bits. * @@ -575,190 +516,170 @@ gdtoa(CONST FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, in i = ((s5 ? hi0bits(S->x[S->wds-1]) : ULbits - 1) - s2 - 4) & kmask; m2 += i; if ((b2 += i) > 0) - b = lshift(b, b2 MTb); + b = lshift(b, b2); if ((s2 += i) > 0) - S = lshift(S, s2 MTb); + S = lshift(S, s2); if (k_check) { if (cmp(b,S) < 0) { k--; - b = multadd(b, 10, 0 MTb); /* we botched the k estimate */ + b = multadd(b, 10, 0); /* we botched the k estimate */ if (leftright) - mhi = multadd(mhi, 10, 0 MTb); + mhi = multadd(mhi, 10, 0); ilim = ilim1; - } } + } if (ilim <= 0 && mode > 2) { - if (ilim < 0 || cmp(b,S = multadd(S,5,0 MTb)) <= 0) { + if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { /* no digits, fcvt style */ - no_digits: + no_digits: k = -1 - ndigits; inex = STRTOG_Inexlo; goto ret; - } - one_digit: + } + one_digit: inex = STRTOG_Inexhi; *s++ = '1'; k++; goto ret; - } + } if (leftright) { if (m2 > 0) - mhi = lshift(mhi, m2 MTb); - + mhi = 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 MTb); + mhi = Balloc(mhi->k); Bcopy(mhi, mlo); - mhi = lshift(mhi, 1 MTb); - } - + mhi = lshift(mhi, 1); + } for(i = 1;;i++) { dig = 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 MTb); + delta = diff(S, mhi); j1 = delta->sign ? 1 : cmp(b, delta); - Bfree(delta MTb); -#ifndef ROUND_BIASED + Bfree(delta); if (j1 == 0 && !mode && !(bits[0] & 1) && !rdir) { if (dig == '9') goto round_9_up; if (j <= 0) { if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; - } + } else { dig++; inex = STRTOG_Inexhi; - } + } *s++ = dig; goto ret; - } -#endif - if (j < 0 || (j == 0 && !mode -#ifndef ROUND_BIASED - && !(bits[0] & 1) -#endif - )) { + } + if (j < 0 || (j == 0 && !mode && !(bits[0] & 1))) { if (rdir && (b->wds > 1 || b->x[0])) { if (rdir == 2) { inex = STRTOG_Inexlo; goto accept; - } + } while (cmp(S,mhi) > 0) { *s++ = dig; - mhi1 = multadd(mhi, 10, 0 MTb); + mhi1 = multadd(mhi, 10, 0); if (mlo == mhi) mlo = mhi1; mhi = mhi1; - b = multadd(b, 10, 0 MTb); + b = multadd(b, 10, 0); dig = quorem(b,S) + '0'; - } + } if (dig++ == '9') goto round_9_up; inex = STRTOG_Inexhi; goto accept; - } + } if (j1 > 0) { - b = lshift(b, 1 MTb); + b = lshift(b, 1); j1 = cmp(b, S); -#ifdef ROUND_BIASED - if (j1 >= 0 /*)*/ -#else - if ((j1 > 0 || (j1 == 0 && dig & 1)) -#endif - && dig++ == '9') + if ((j1 > 0 || (j1 == 0 && dig & 1)) && dig++ == '9') goto round_9_up; inex = STRTOG_Inexhi; - } + } if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; - accept: + accept: *s++ = dig; goto ret; - } + } if (j1 > 0 && rdir != 2) { if (dig == '9') { /* possible if i == 1 */ - round_9_up: + round_9_up: *s++ = '9'; inex = STRTOG_Inexhi; goto roundoff; - } + } inex = STRTOG_Inexhi; *s++ = dig + 1; goto ret; - } + } *s++ = dig; if (i == ilim) break; - b = multadd(b, 10, 0 MTb); + b = multadd(b, 10, 0); if (mlo == mhi) - mlo = mhi = multadd(mhi, 10, 0 MTb); + mlo = mhi = multadd(mhi, 10, 0); else { - mlo = multadd(mlo, 10, 0 MTb); - mhi = multadd(mhi, 10, 0 MTb); - } + mlo = multadd(mlo, 10, 0); + mhi = multadd(mhi, 10, 0); } } + } else for(i = 1;; i++) { *s++ = dig = quorem(b,S) + '0'; if (i >= ilim) break; - b = multadd(b, 10, 0 MTb); - } - + b = multadd(b, 10, 0); + } /* Round off last digit */ - if (rdir) { if (rdir == 2 || (b->wds <= 1 && !b->x[0])) goto chopzeros; goto roundoff; - } - b = lshift(b, 1 MTb); + } + b = lshift(b, 1); j = cmp(b, S); -#ifdef ROUND_BIASED - if (j >= 0) -#else if (j > 0 || (j == 0 && dig & 1)) -#endif - { - roundoff: + { + roundoff: inex = STRTOG_Inexhi; while(*--s == '9') if (s == s0) { k++; *s++ = '1'; goto ret; - } + } ++*s++; - } + } else { - chopzeros: + chopzeros: if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; - } - ret: - Bfree(S MTb); + } +ret: + Bfree(S); if (mhi) { if (mlo && mlo != mhi) - Bfree(mlo MTb); - Bfree(mhi MTb); - } - ret1: + Bfree(mlo); + Bfree(mhi); + } +ret1: while(s > s0 && s[-1] == '0') --s; - Bfree(b MTb); + Bfree(b); *s = 0; *decpt = k + 1; if (rve) *rve = s; *kindp |= inex; return s0; - } +} diff --git a/third_party/gdtoa/gdtoa.h b/third_party/gdtoa/gdtoa.h index 4f3f373b1..42b9a9ac5 100644 --- a/third_party/gdtoa/gdtoa.h +++ b/third_party/gdtoa/gdtoa.h @@ -13,7 +13,6 @@ enum { STRTOG_NaNbits = 5, STRTOG_NoNumber = 6, STRTOG_Retmask = 7, - /* The following may be or-ed into one of the above values. */ STRTOG_Neg = 0x08, /* does not affect STRTOG_Inexlo or STRTOG_Inexhi */ STRTOG_Inexlo = 0x10, /* returned result rounded toward zero */ @@ -40,9 +39,8 @@ enum { FPI_Round_down = 3 }; -char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve); -char *gdtoa(const FPI *fpi, int be, unsigned *bits, int *kindp, int mode, - int ndigits, int *decpt, char **rve); +char *dtoa(double, int, int, int *, int *, char **); +char *gdtoa(const FPI *, int, unsigned *, int *, int, int, int *, char **); void freedtoa(char *); double atof(const char *); diff --git a/third_party/gdtoa/gdtoa.internal.h b/third_party/gdtoa/gdtoa.internal.h index 9c120c511..433bbc141 100644 --- a/third_party/gdtoa/gdtoa.internal.h +++ b/third_party/gdtoa/gdtoa.internal.h @@ -11,54 +11,14 @@ Kudos go to Guy L. Steele, Jr. and Jon L. White\\n\ Copyright (C) 1997, 1998, 2000 by Lucent Technologies\""); asm(".include \"libc/disclaimer.inc\""); -#define IEEE_Arith 1 -#define IEEE_8087 1 -#define Honor_FLT_ROUNDS 1 -#define f_QNAN 0x7fc00000 -#define d_QNAN0 0x7ff80000 -#define d_QNAN1 0x0 - -#define Check_FLT_ROUNDS 1 -#define Trust_FLT_ROUNDS 1 - -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998-2000 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. - -****************************************************************/ - -/* This is a variation on dtoa.c that converts arbitary binary - floating-point formats to and from decimal notation. It uses - double-precision arithmetic internally, so there are still - various #ifdefs that adapt the calculations to the native - double-precision arithmetic (any of IEEE, VAX D_floating, - or IBM mainframe arithmetic). - - Please send bug reports to David M. Gay (dmg at acm dot org, - with " at " changed at "@" and " dot " changed to "."). - */ +#define IEEE_Arith 1 +#define IEEE_8087 1 +#define Honor_FLT_ROUNDS 1 +#define f_QNAN 0x7fc00000 +#define d_QNAN0 0x7ff80000 +#define d_QNAN1 0x0 +#define Omit_Private_Memory 1 +#define Check_FLT_ROUNDS 1 /* On a machine with IEEE extended-precision registers, it is * necessary to specify double-precision (53-bit) rounding precision @@ -184,7 +144,6 @@ THIS SOFTWARE. * probability of wasting memory, but would otherwise be harmless.) * You must also invoke freedtoa(s) to free the value s returned by * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. - * When MULTIPLE_THREADS is #defined, source file misc.c provides * void set_max_gdtoa_threads(unsigned int n); * and expects @@ -202,7 +161,6 @@ THIS SOFTWARE. * with m <= n has has no effect, but a call with m > n is honored. * Such a call invokes REALLOC (assumed to be "realloc" if REALLOC * is not #defined) to extend the size of the relevant array. - * #define IMPRECISE_INEXACT if you do not care about the setting of * the STRTOG_Inexact bits in the special case of doing IEEE double * precision conversions (which could also be done by the strtod in @@ -227,9 +185,9 @@ typedef unsigned Long ULong; typedef unsigned short UShort; #endif -#ifndef CONST -#define CONST const -#endif /* CONST */ +#ifndef const +#define const const +#endif /* const */ #ifdef DEBUG #define Bug(x) \ @@ -284,10 +242,6 @@ extern Char *REALLOC(Char *, size_t); #define n_bigtens 2 #endif -#ifdef __cplusplus -extern "C" { -#endif - typedef union { double d; ULong L[2]; @@ -322,7 +276,6 @@ typedef union { /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ -#ifdef IEEE_Arith #define Exp_shift 20 #define Exp_shift1 20 #define Exp_msk1 0x100000 @@ -348,72 +301,6 @@ typedef union { #define Quick_max 14 #define Int_max 14 -#ifndef Flt_Rounds -#ifdef FLT_ROUNDS -#define Flt_Rounds FLT_ROUNDS -#else -#define Flt_Rounds 1 -#endif -#endif /*Flt_Rounds*/ - -#else /* ifndef IEEE_Arith */ -#undef Sudden_Underflow -#define Sudden_Underflow -#ifdef IBM -#undef Flt_Rounds -#define Flt_Rounds 0 -#define Exp_shift 24 -#define Exp_shift1 24 -#define Exp_msk1 0x1000000 -#define Exp_msk11 0x1000000 -#define Exp_mask 0x7f000000 -#define P 14 -#define Bias 65 -#define Exp_1 0x41000000 -#define Exp_11 0x41000000 -#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ -#define Frac_mask 0xffffff -#define Frac_mask1 0xffffff -#define Bletch 4 -#define Ten_pmax 22 -#define Bndry_mask 0xefffff -#define Bndry_mask1 0xffffff -#define LSB 1 -#define Sign_bit 0x80000000 -#define Log2P 4 -#define Tiny0 0x100000 -#define Tiny1 0 -#define Quick_max 14 -#define Int_max 15 -#else /* VAX */ -#undef Flt_Rounds -#define Flt_Rounds 1 -#define Exp_shift 23 -#define Exp_shift1 7 -#define Exp_msk1 0x80 -#define Exp_msk11 0x800000 -#define Exp_mask 0x7f80 -#define P 56 -#define Bias 129 -#define Exp_1 0x40800000 -#define Exp_11 0x4080 -#define Ebits 8 -#define Frac_mask 0x7fffff -#define Frac_mask1 0xffff007f -#define Ten_pmax 24 -#define Bletch 2 -#define Bndry_mask 0xffff007f -#define Bndry_mask1 0xffff007f -#define LSB 0x10000 -#define Sign_bit 0x8000 -#define Log2P 1 -#define Tiny0 0x80 -#define Tiny1 0 -#define Quick_max 15 -#define Int_max 15 -#endif /* IBM, VAX */ -#endif /* IEEE_Arith */ - #ifndef IEEE_Arith #define ROUND_BIASED #else @@ -472,23 +359,6 @@ extern double rnd_prod(double, double), rnd_quot(double, double); #define ALL_ON 0xffff #endif -#ifdef MULTIPLE_THREADS /*{{*/ -#define MTa , PTI -#define MTb , &TI -#define MTd , ThInfo **PTI -#define MTk ThInfo **PTI; -extern void ACQUIRE_DTOA_LOCK(unsigned int); -extern void FREE_DTOA_LOCK(unsigned int); -extern unsigned int dtoa_get_threadno(void); -#else /*}{*/ -#define ACQUIRE_DTOA_LOCK(n) /*nothing*/ -#define FREE_DTOA_LOCK(n) /*nothing*/ -#define MTa /*nothing*/ -#define MTb /*nothing*/ -#define MTd /*nothing*/ -#define MTk /*nothing*/ -#endif /*}}*/ - #define Kmax 9 struct Bigint { @@ -554,7 +424,6 @@ extern void __gdtoa_memcpy(void *, const void *, size_t); #define rv_alloc __gdtoa_rv_alloc #define s2b __gdtoa_s2b #define set_ones __gdtoa_set_ones -#define strcp __gdtoa_strcp #define strtoIg __gdtoa_strtoIg #define sum __gdtoa_sum #define tens __gdtoa_tens @@ -566,52 +435,50 @@ extern void __gdtoa_memcpy(void *, const void *, size_t); 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 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]; -extern Bigint *Balloc(int MTd); -extern void Bfree(Bigint *MTd); -extern void ULtof(ULong *, ULong *, Long, int); -extern void ULtod(ULong *, ULong *, Long, int); -extern void ULtodd(ULong *, ULong *, Long, int); -extern void ULtoQ(ULong *, ULong *, Long, int); -extern void ULtox(UShort *, ULong *, Long, int); -extern void ULtoxL(ULong *, ULong *, Long, int); -extern ULong any_on(Bigint *, int); -extern double b2d(Bigint *, int *); -extern int cmp(Bigint *, Bigint *); -extern void copybits(ULong *, int, Bigint *); -extern Bigint *d2b(double, int *, int *MTd); -extern void decrement(Bigint *); -extern Bigint *diff(Bigint *, Bigint *MTd); -extern char *g__fmt(char *, char *, char *, int, ULong, size_t); -extern int gethex(CONST char **, CONST FPI *, Long *, Bigint **, int MTd); -extern void __gdtoa_hexdig_init(void); -extern int hexnan(CONST char **, CONST FPI *, ULong *); -extern Bigint *i2b(int MTd); -extern Bigint *increment(Bigint *MTd); -extern Bigint *lshift(Bigint *, int MTd); -extern int match(CONST char **, char *); -extern Bigint *mult(Bigint *, Bigint *MTd); -extern Bigint *multadd(Bigint *, int, int MTd); -extern char *nrv_alloc(char *, char **, int MTd); -extern Bigint *pow5mult(Bigint *, int MTd); -extern int quorem(Bigint *, Bigint *); -extern double ratio(Bigint *, Bigint *); -extern void rshift(Bigint *, int); -extern char *rv_alloc(int MTd); -extern Bigint *s2b(CONST char *, int, int, ULong, int MTd); -extern Bigint *set_ones(Bigint *, int MTd); -extern char *strcp(char *, const char *); -extern int strtoIg(CONST char *, char **, CONST FPI *, Long *, Bigint **, - int *); -extern Bigint *sum(Bigint *, Bigint *MTd); -extern int trailz(Bigint *); -extern double ulp(U *); +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); +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 *); forceinline int lo0bits(ULong *y) { int k; @@ -628,9 +495,6 @@ forceinline int hi0bits(ULong x) { return x ? __builtin_clz(x) : 32; } -#ifdef __cplusplus -} -#endif /* * NAN_WORD0 and NAN_WORD1 are only referenced in strtod.c. Prior to * 20050115, they used to be hard-wired here (to 0x7ff80000 and 0, @@ -645,25 +509,12 @@ forceinline int hi0bits(ULong x) { #undef INFNAN_CHECK #define INFNAN_CHECK #endif -#ifdef IEEE_MC68k -#define _0 0 -#define _1 1 -#ifndef NAN_WORD0 -#define NAN_WORD0 d_QNAN0 -#endif -#ifndef NAN_WORD1 -#define NAN_WORD1 d_QNAN1 -#endif -#else -#define _0 1 -#define _1 0 #ifndef NAN_WORD0 #define NAN_WORD0 d_QNAN1 #endif #ifndef NAN_WORD1 #define NAN_WORD1 d_QNAN0 #endif -#endif #else #undef INFNAN_CHECK #endif diff --git a/third_party/gdtoa/gdtoa_fltrnds.inc b/third_party/gdtoa/gdtoa_fltrnds.inc index 33e5f9e53..919674f35 100644 --- a/third_party/gdtoa/gdtoa_fltrnds.inc +++ b/third_party/gdtoa/gdtoa_fltrnds.inc @@ -1,18 +1,11 @@ +/* clang-format off */ + FPI *fpi, fpi1; int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ + Rounding = FLT_ROUNDS; fpi = &fpi0; - if (Rounding != 1) { + if (Rounding != FPI_Round_near) { fpi1 = fpi0; fpi = &fpi1; fpi1.rounding = Rounding; - } + } diff --git a/third_party/gdtoa/gethex.c b/third_party/gdtoa/gethex.c index c9dcb2c18..00f702041 100644 --- a/third_party/gdtoa/gethex.c +++ b/third_party/gdtoa/gethex.c @@ -1,68 +1,50 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) +gethex( const char **sp, const FPI *fpi, Long *exp, Bigint **bp, int sign) { Bigint *b; - CONST unsigned char *decpt, *s0, *s, *s1; + 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; -#ifdef USE_LOCALE - int i; -#ifdef NO_LOCALE_CACHE - const unsigned char *decimalpoint = (unsigned char*)localeconv()->decimal_point; -#else - const unsigned char *decimalpoint; - static unsigned char *decimalpoint_cache; - if (!(s0 = decimalpoint_cache)) { - s0 = (unsigned char*)localeconv()->decimal_point; - if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) { - strcpy(decimalpoint_cache, s0); - s0 = decimalpoint_cache; - } - } - decimalpoint = s0; -#endif -#endif - /**** if (!hexdig['0']) __gdtoa_hexdig_init(); ****/ *bp = 0; havedig = 0; - s0 = *(CONST unsigned char **)sp + 2; + s0 = *(const unsigned char **)sp + 2; while(s0[havedig] == '0') havedig++; s0 += havedig; @@ -74,17 +56,9 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) havedig++; else { zret = 1; -#ifdef USE_LOCALE - for(i = 0; decimalpoint[i]; ++i) { - if (s[i] != decimalpoint[i]) - goto pcheck; - } - decpt = s += i; -#else if (*s != '.') goto pcheck; decpt = ++s; -#endif if (!hexdig[*s]) goto pcheck; while(*s == '0') @@ -93,52 +67,43 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) zret = 0; havedig = 1; s0 = s; - } + } while(hexdig[*s]) s++; -#ifdef USE_LOCALE - if (*s == *decimalpoint && !decpt) { - for(i = 1; decimalpoint[i]; ++i) { - if (s[i] != decimalpoint[i]) - goto pcheck; - } - decpt = s += i; -#else if (*s == '.' && !decpt) { decpt = ++s; -#endif while(hexdig[*s]) s++; - }/*}*/ + }/*}*/ if (decpt) e = -(((Long)(s-decpt)) << 2); - pcheck: +pcheck: s1 = s; big = esign = 0; switch(*s) { - case 'p': - case 'P': + case 'p': + case 'P': switch(*++s) { - case '-': + case '-': esign = 1; /* no break */ - case '+': + case '+': s++; - } + } if ((n = hexdig[*s]) == 0 || n > 0x19) { s = s1; break; - } + } e1 = n - 0x10; while((n = hexdig[*++s]) !=0 && n <= 0x19) { if (e1 & 0xf8000000) big = 1; e1 = 10*e1 + n - 0x10; - } + } if (esign) e1 = -e1; e += e1; - } + } *sp = (char*)s; if (!havedig) *sp = (char*)s0 - 1; @@ -147,40 +112,40 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) if (big) { if (esign) { switch(fpi->rounding) { - case FPI_Round_up: + case FPI_Round_up: if (sign) break; goto ret_tiny; - case FPI_Round_down: + case FPI_Round_down: if (!sign) break; goto ret_tiny; - } + } goto retz; - ret_tiny: - b = Balloc(0 MTa); + ret_tiny: + b = Balloc(0); b->wds = 1; b->x[0] = 1; goto dret; - } + } switch(fpi->rounding) { - case FPI_Round_near: + case FPI_Round_near: goto ovfl1; - case FPI_Round_up: + case FPI_Round_up: if (!sign) goto ovfl1; goto ret_big; - case FPI_Round_down: + case FPI_Round_down: if (sign) goto ovfl1; - } - ret_big: + } + ret_big: nbits = fpi->nbits; n0 = n = nbits >> kshift; if (nbits & kmask) ++n; for(j = n, k = 0; j >>= 1; ++k); - *bp = b = Balloc(k MTa); + *bp = b = Balloc(k); b->wds = n; for(j = 0; j < n0; ++j) b->x[j] = ALL_ON; @@ -188,35 +153,25 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) b->x[j] = ALL_ON >> (ULbits - (nbits & kmask)); *exp = fpi->emax; return STRTOG_Normal | STRTOG_Inexlo; - } + } n = s1 - s0 - 1; for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1) k++; - b = Balloc(k MTa); + b = Balloc(k); x = b->x; n = 0; L = 0; -#ifdef USE_LOCALE - for(i = 0; decimalpoint[i+1]; ++i); -#endif while(s1 > s0) { -#ifdef USE_LOCALE - if (*--s1 == decimalpoint[i]) { - s1 -= i; - continue; - } -#else if (*--s1 == '.') continue; -#endif if (n == ULbits) { *x++ = L; L = 0; n = 0; - } + } L |= (hexdig[*s1] & 0x0f) << n; n += 4; - } + } *x++ = L; b->wds = n = x - b->x; n = ULbits*n - hi0bits(L); @@ -232,72 +187,66 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) lostbits = 2; if (k > 0 && any_on(b,k)) lostbits = 3; - } } + } rshift(b, n); e += n; - } + } else if (n < nbits) { n = nbits - n; - b = lshift(b, n MTa); + b = lshift(b, n); e -= n; x = b->x; - } + } if (e > fpi->emax) { - ovfl: - Bfree(b MTa); - ovfl1: -#ifndef NO_ERRNO + ovfl: + Bfree(b); + ovfl1: errno = ERANGE; -#endif switch (fpi->rounding) { - case FPI_Round_zero: + case FPI_Round_zero: goto ret_big; - case FPI_Round_down: + case FPI_Round_down: if (!sign) goto ret_big; break; - case FPI_Round_up: + case FPI_Round_up: if (sign) goto ret_big; - } - return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; } + return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; + } irv = STRTOG_Normal; if (e < fpi->emin) { irv = STRTOG_Denormal; n = fpi->emin - e; if (n >= nbits) { switch (fpi->rounding) { - case FPI_Round_near: + case FPI_Round_near: if (n == nbits && (n < 2 || lostbits || any_on(b,n-1))) goto one_bit; break; - case FPI_Round_up: + case FPI_Round_up: if (!sign) goto one_bit; break; - case FPI_Round_down: + case FPI_Round_down: if (sign) { - one_bit: + one_bit: x[0] = b->wds = 1; - dret: + dret: *bp = b; *exp = fpi->emin; -#ifndef NO_ERRNO errno = ERANGE; -#endif return STRTOG_Denormal | STRTOG_Inexhi | STRTOG_Underflow; - } - } - Bfree(b MTa); - retz: -#ifndef NO_ERRNO - errno = ERANGE; -#endif - return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow; + } } + Bfree(b); + retz: + errno = ERANGE; + return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow; + } k = n - 1; if (lostbits) lostbits = 1; @@ -308,45 +257,45 @@ gethex( CONST char **sp, CONST FPI *fpi, Long *exp, Bigint **bp, int sign MTd) nbits -= n; rshift(b,n); e = fpi->emin; - } + } if (lostbits) { up = 0; switch(fpi->rounding) { - case FPI_Round_zero: + case FPI_Round_zero: break; - case FPI_Round_near: + case FPI_Round_near: if (lostbits & 2 - && (lostbits | x[0]) & 1) + && (lostbits | x[0]) & 1) up = 1; break; - case FPI_Round_up: + case FPI_Round_up: up = 1 - sign; break; - case FPI_Round_down: + case FPI_Round_down: up = sign; - } + } if (up) { k = b->wds; - b = increment(b MTa); + b = increment(b); x = b->x; if (irv == STRTOG_Denormal) { if (nbits == fpi->nbits - 1 - && x[nbits >> kshift] & 1 << (nbits & kmask)) + && x[nbits >> kshift] & 1 << (nbits & kmask)) irv = STRTOG_Normal; - } + } else if (b->wds > k - || ((n = nbits & kmask) !=0 - && hi0bits(x[k-1]) < 32-n)) { + || ((n = nbits & kmask) !=0 + && hi0bits(x[k-1]) < 32-n)) { rshift(b,1); if (++e > fpi->emax) goto ovfl; - } - irv |= STRTOG_Inexhi; } + irv |= STRTOG_Inexhi; + } else irv |= STRTOG_Inexlo; - } + } *bp = b; *exp = e; return irv; - } +} diff --git a/third_party/gdtoa/gmisc.c b/third_party/gdtoa/gmisc.c index 1c3b692a7..1a045c513 100644 --- a/third_party/gdtoa/gmisc.c +++ b/third_party/gdtoa/gmisc.c @@ -1,43 +1,42 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ void rshift(Bigint *b, int k) { ULong *x, *x1, *xe, y; int n; - x = x1 = b->x; n = k >> kshift; if (n < b->wds) { @@ -49,24 +48,23 @@ rshift(Bigint *b, int k) while(x < xe) { *x1++ = (y | (*x << n)) & ALL_ON; y = *x++ >> k; - } + } if ((*x1 = y) !=0) x1++; - } + } else while(x < xe) *x1++ = *x++; - } + } if ((b->wds = x1 - b->x) == 0) b->x[0] = 0; - } +} int trailz(Bigint *b) { ULong L, *x, *xe; int n = 0; - x = b->x; xe = x + b->wds; for(n = 0; x < xe && !*x; x++) @@ -74,6 +72,6 @@ trailz(Bigint *b) if (x < xe) { L = *x; n += lo0bits(&L); - } - return n; } + return n; +} diff --git a/third_party/gdtoa/hd_init.c b/third_party/gdtoa/hd_init.c index 161e85f90..ec3971bfe 100644 --- a/third_party/gdtoa/hd_init.c +++ b/third_party/gdtoa/hd_init.c @@ -1,36 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ #if 0 unsigned char hexdig[256]; diff --git a/third_party/gdtoa/hexnan.c b/third_party/gdtoa/hexnan.c index 422999dd2..0830324ba 100644 --- a/third_party/gdtoa/hexnan.c +++ b/third_party/gdtoa/hexnan.c @@ -1,58 +1,56 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ static void L_shift(ULong *x, ULong *x1, int i) { int j; - i = 8 - i; i <<= 2; j = ULbits - i; do { *x |= x[1] << j; x[1] >>= i; - } while(++x < x1); - } + } while(++x < x1); +} int -hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0) +hexnan( const char **sp, const FPI *fpi, ULong *x0) { ULong c, h, *x, *x1, *xe; - CONST char *s; + const char *s; int havedig, hd0, i, nbits; - /**** if (!hexdig['0']) __gdtoa_hexdig_init(); ****/ nbits = fpi->nbits; x = x0 + (nbits >> kshift); @@ -63,15 +61,15 @@ hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0) havedig = hd0 = i = 0; s = *sp; /* allow optional initial 0x or 0X */ - while((c = *(CONST unsigned char*)(s+1)) && c <= ' ') { + while((c = *(const unsigned char*)(s+1)) && c <= ' ') { if (!c) goto retnan; ++s; - } + } if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X') - && *(CONST unsigned char*)(s+3) > ' ') + && *(const unsigned char*)(s+3) > ' ') s += 2; - while((c = *(CONST unsigned char*)++s)) { + while((c = *(const unsigned char*)++s)) { if (!(h = hexdig[c])) { if (c <= ' ') { if (hd0 < havedig) { @@ -80,49 +78,45 @@ hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0) if (x <= x0) { i = 8; continue; - } + } hd0 = havedig; *--x = 0; x1 = x; i = 0; - } - while((c = *(CONST unsigned char*)(s+1)) <= ' ') { + } + while((c = *(const unsigned char*)(s+1)) <= ' ') { if (!c) goto retnan; ++s; - } + } if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X') - && *(CONST unsigned char*)(s+3) > ' ') + && *(const unsigned char*)(s+3) > ' ') s += 2; continue; - } + } if (/*(*/ c == ')' && havedig) { *sp = s + 1; break; - } -#ifndef GDTOA_NON_PEDANTIC_NANCHECK + } do { if (/*(*/ c == ')') { *sp = s + 1; goto break2; - } - } while((c = *++s)); -#endif - retnan: + } + } while((c = *++s)); + retnan: return STRTOG_NaN; - } + } havedig++; if (++i > 8) { if (x <= x0) continue; i = 1; *--x = 0; - } - *x = (*x << 4) | (h & 0xf); } -#ifndef GDTOA_NON_PEDANTIC_NANCHECK - break2: -#endif + *x = (*x << 4) | (h & 0xf); + } +break2: if (!havedig) return STRTOG_NaN; if (x < x1 && i < 8) @@ -130,22 +124,22 @@ hexnan( CONST char **sp, CONST FPI *fpi, ULong *x0) if (x > x0) { x1 = x0; do *x1++ = *x++; - while(x <= xe); + while(x <= xe); do *x1++ = 0; - while(x1 <= xe); - } + while(x1 <= xe); + } else { /* truncate high-order word if necessary */ if ( (i = nbits & (ULbits-1)) !=0) *xe &= ((ULong)0xffffffff) >> (ULbits - i); - } + } for(x1 = xe;; --x1) { if (*x1 != 0) break; if (x1 == x0) { *x1 = 1; break; - } } - return STRTOG_NaNbits; } + return STRTOG_NaNbits; +} diff --git a/third_party/gdtoa/misc.c b/third_party/gdtoa/misc.c index ffcea5b4d..814d9a46c 100644 --- a/third_party/gdtoa/misc.c +++ b/third_party/gdtoa/misc.c @@ -1,273 +1,129 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -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. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -#ifndef Omit_Private_Memory -#ifndef PRIVATE_MEM -#define PRIVATE_MEM 2304 -#endif -#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) -static double private_mem[PRIVATE_mem], *pmem_next; -#endif - - static ThInfo TI0; - -#ifdef MULTIPLE_THREADS /*{{*/ - static unsigned int maxthreads = 0; - static ThInfo *TI1; - static int TI0_used; - - void -set_max_gdtoa_threads(unsigned int n) -{ - size_t L; - - if (n > maxthreads) { - L = n*sizeof(ThInfo); - if (TI1) { - TI1 = (ThInfo*)REALLOC(TI1, L); - memset(TI1 + maxthreads, 0, (n-maxthreads)*sizeof(ThInfo)); - } - else { - TI1 = (ThInfo*)MALLOC(L); - if (TI0_used) { - memcpy(TI1, &TI0, sizeof(ThInfo)); - if (n > 1) - memset(TI1 + 1, 0, L - sizeof(ThInfo)); - memset(&TI0, 0, sizeof(ThInfo)); - } - else - memset(TI1, 0, L); - } - maxthreads = n; - } - } - - static ThInfo* -get_TI(void) -{ - unsigned int thno = dtoa_get_threadno(); - if (thno < maxthreads) - return TI1 + thno; - if (thno == 0) - TI0_used = 1; - return &TI0; - } -#define freelist TI->Freelist -#define p5s TI->P5s -#else /*}{*/ -#define freelist TI0.Freelist -#define p5s TI0.P5s -#endif /*}}*/ +static ThInfo TI0; Bigint * -Balloc(int k MTd) +Balloc(int k) { int x; Bigint *rv; -#ifndef Omit_Private_Memory - unsigned int len; -#endif - - static bool once; - if (!once) { - pmem_next = private_mem; - once = true; - } - -#ifdef MULTIPLE_THREADS - ThInfo *TI; - - if (!(TI = *PTI)) - *PTI = TI = get_TI(); - if (TI == &TI0) - ACQUIRE_DTOA_LOCK(0); -#endif - /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */ - /* but this case seems very unlikely. */ - if (k <= Kmax && (rv = freelist[k]) !=0) { - freelist[k] = rv->next; - } + if (k <= Kmax && (rv = TI0.Freelist[k]) != 0) + TI0.Freelist[k] = rv->next; else { x = 1 << k; -#ifdef Omit_Private_Memory - rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); -#else - len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) - /sizeof(double); - if (k <= Kmax && pmem_next - private_mem + len - PRIVATE_mem <= 0 -#ifdef MULTIPLE_THREADS - && TI == TI1 -#endif - ) { - rv = (Bigint*)pmem_next; - pmem_next += len; - } - else - rv = (Bigint*)MALLOC(len*sizeof(double)); -#endif + rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(ULong)); rv->k = k; rv->maxwds = x; - } -#ifdef MULTIPLE_THREADS - if (TI == &TI0) - FREE_DTOA_LOCK(0); -#endif + } rv->sign = rv->wds = 0; return rv; - } +} void -Bfree(Bigint *v MTd) +Bfree(Bigint *v) { -#ifdef MULTIPLE_THREADS - ThInfo *TI; -#endif if (v) { if (v->k > Kmax) -#ifdef FREE - FREE((void*)v); -#else free((void*)v); -#endif else { -#ifdef MULTIPLE_THREADS - if (!(TI = *PTI)) - *PTI = TI = get_TI(); - if (TI == &TI0) - ACQUIRE_DTOA_LOCK(0); -#endif - v->next = freelist[v->k]; - freelist[v->k] = v; -#ifdef MULTIPLE_THREADS - if (TI == &TI0) - FREE_DTOA_LOCK(0); -#endif - } + v->next = TI0.Freelist[v->k]; + TI0.Freelist[v->k] = v; } } +} Bigint * -multadd(Bigint *b, int m, int a MTd) /* multiply by m and add a */ +multadd(Bigint *b, int m, int a) /* multiply by m and add a */ { int i, wds; -#ifdef ULLong ULong *x; ULLong carry, y; -#else - ULong carry, *x, y; -#ifdef Pack_32 - ULong xi, z; -#endif -#endif Bigint *b1; - wds = b->wds; x = b->x; i = 0; carry = a; do { -#ifdef ULLong y = *x * (ULLong)m + carry; carry = y >> 32; *x++ = y & 0xffffffffUL; -#else -#ifdef Pack_32 - xi = *x; - y = (xi & 0xffff) * m + carry; - z = (xi >> 16) * m + (y >> 16); - carry = z >> 16; - *x++ = (z << 16) + (y & 0xffff); -#else - y = *x * m + carry; - carry = y >> 16; - *x++ = y & 0xffff; -#endif -#endif - } - while(++i < wds); + } + while(++i < wds); if (carry) { if (wds >= b->maxwds) { - b1 = Balloc(b->k+1 MTa); + b1 = Balloc(b->k+1); Bcopy(b1, b); - Bfree(b MTa); + Bfree(b); b = b1; - } + } b->x[wds++] = carry; b->wds = wds; - } - return b; } + return b; +} Bigint * -i2b(int i MTd) +i2b(int i) { Bigint *b; - - b = Balloc(1 MTa); + b = Balloc(1); b->x[0] = i; b->wds = 1; return b; - } +} Bigint * -mult(Bigint *a, Bigint *b MTd) +mult(Bigint *a, Bigint *b) { Bigint *c; int k, wa, wb, wc; ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; ULong y; -#ifdef ULLong ULLong carry, z; -#else - ULong carry, z; -#ifdef Pack_32 - ULong z2; -#endif -#endif - if (a->wds < b->wds) { c = a; a = b; b = c; - } + } k = a->k; wa = a->wds; wb = b->wds; wc = wa + wb; if (wc > a->maxwds) k++; - c = Balloc(k MTa); + c = Balloc(k); for(x = c->x, xa = x + wc; x < xa; x++) *x = 0; xa = a->x; @@ -275,7 +131,6 @@ mult(Bigint *a, Bigint *b MTd) xb = b->x; xbe = xb + wb; xc0 = c->x; -#ifdef ULLong for(; xb < xbe; xc0++) { if ( (y = *xb++) !=0) { x = xa; @@ -285,188 +140,89 @@ mult(Bigint *a, Bigint *b MTd) z = *x++ * (ULLong)y + *xc + carry; carry = z >> 32; *xc++ = z & 0xffffffffUL; - } - while(x < xae); + } + while(x < xae); *xc = carry; - } } -#else -#ifdef Pack_32 - for(; xb < xbe; xb++, xc0++) { - if ( (y = *xb & 0xffff) !=0) { - x = xa; - xc = xc0; - carry = 0; - do { - z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; - carry = z >> 16; - z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; - carry = z2 >> 16; - Storeinc(xc, z2, z); - } - while(x < xae); - *xc = carry; - } - if ( (y = *xb >> 16) !=0) { - x = xa; - xc = xc0; - carry = 0; - z2 = *xc; - do { - z = (*x & 0xffff) * y + (*xc >> 16) + carry; - carry = z >> 16; - Storeinc(xc, z, z2); - z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; - carry = z2 >> 16; - } - while(x < xae); - *xc = z2; - } - } -#else - for(; xb < xbe; xc0++) { - if ( (y = *xb++) !=0) { - x = xa; - xc = xc0; - carry = 0; - do { - z = *x++ * y + *xc + carry; - carry = z >> 16; - *xc++ = z & 0xffff; - } - while(x < xae); - *xc = carry; - } - } -#endif -#endif + } for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; c->wds = wc; return c; - } +} Bigint * -pow5mult(Bigint *b, int k MTd) +pow5mult(Bigint *b, int k) { Bigint *b1, *p5, *p51; -#ifdef MULTIPLE_THREADS - ThInfo *TI; -#endif int i; static const int p05[3] = { 5, 25, 125 }; - if ( (i = k & 3) !=0) - b = multadd(b, p05[i-1], 0 MTa); - + b = multadd(b, p05[i-1], 0); if (!(k >>= 2)) return b; -#ifdef MULTIPLE_THREADS - if (!(TI = *PTI)) - *PTI = TI = get_TI(); -#endif - if ((p5 = p5s) == 0) { + if ((p5 = TI0.P5s) == 0) { /* first time */ -#ifdef MULTIPLE_THREADS - if (!(TI = *PTI)) - *PTI = TI = get_TI(); - if (TI == &TI0) - ACQUIRE_DTOA_LOCK(1); - if (!(p5 = p5s)) { - p5 = p5s = i2b(625 MTa); - p5->next = 0; - } - if (TI == &TI0) - FREE_DTOA_LOCK(1); -#else - p5 = p5s = i2b(625); + p5 = TI0.P5s = i2b(625); p5->next = 0; -#endif - } + } for(;;) { if (k & 1) { - b1 = mult(b, p5 MTa); - Bfree(b MTa); + b1 = mult(b, p5); + Bfree(b); b = b1; - } + } if (!(k >>= 1)) break; if ((p51 = p5->next) == 0) { -#ifdef MULTIPLE_THREADS - if (!TI && !(TI = *PTI)) - *PTI = TI = get_TI(); - if (TI == &TI0) - ACQUIRE_DTOA_LOCK(1); - if (!(p51 = p5->next)) { - p51 = p5->next = mult(p5,p5 MTa); - p51->next = 0; - } - if (TI == &TI0) - FREE_DTOA_LOCK(1); -#else - p51 = p5->next = mult(p5,p5 MTa); + p51 = p5->next = mult(p5,p5); p51->next = 0; -#endif - } - p5 = p51; } - return b; + p5 = p51; } + return b; +} Bigint * -lshift(Bigint *b, int k MTd) +lshift(Bigint *b, int k) { int i, k1, n, n1; Bigint *b1; ULong *x, *x1, *xe, z; - n = k >> kshift; k1 = b->k; n1 = n + b->wds + 1; for(i = b->maxwds; n1 > i; i <<= 1) k1++; - b1 = Balloc(k1 MTa); + b1 = Balloc(k1); x1 = b1->x; for(i = 0; i < n; i++) *x1++ = 0; x = b->x; xe = x + b->wds; if (k &= kmask) { -#ifdef Pack_32 k1 = 32 - k; z = 0; do { *x1++ = *x << k | z; z = *x++ >> k1; - } - while(x < xe); + } + while(x < xe); if ((*x1 = z) !=0) ++n1; -#else - k1 = 16 - k; - z = 0; - do { - *x1++ = *x << k & 0xffff | z; - z = *x++ >> k1; - } - while(x < xe); - if (*x1 = z) - ++n1; -#endif - } + } else do - *x1++ = *x++; + *x1++ = *x++; while(x < xe); b1->wds = n1 - 1; - Bfree(b MTa); + Bfree(b); return b1; - } +} int cmp(Bigint *a, Bigint *b) { ULong *xa, *xa0, *xb, *xb0; int i, j; - i = a->wds; j = b->wds; #ifdef DEBUG @@ -486,41 +242,33 @@ cmp(Bigint *a, Bigint *b) return *xa < *xb ? -1 : 1; if (xa <= xa0) break; - } - return 0; } + return 0; +} Bigint * -diff(Bigint *a, Bigint *b MTd) +diff(Bigint *a, Bigint *b) { Bigint *c; int i, wa, wb; ULong *xa, *xae, *xb, *xbe, *xc; -#ifdef ULLong ULLong borrow, y; -#else - ULong borrow, y; -#ifdef Pack_32 - ULong z; -#endif -#endif - i = cmp(a,b); if (!i) { - c = Balloc(0 MTa); + c = Balloc(0); c->wds = 1; c->x[0] = 0; return c; - } + } if (i < 0) { c = a; a = b; b = c; i = 1; - } + } else i = 0; - c = Balloc(a->k MTa); + c = Balloc(a->k); c->sign = i; wa = a->wds; xa = a->x; @@ -530,54 +278,22 @@ diff(Bigint *a, Bigint *b MTd) xbe = xb + wb; xc = c->x; borrow = 0; -#ifdef ULLong do { y = (ULLong)*xa++ - *xb++ - borrow; borrow = y >> 32 & 1UL; *xc++ = y & 0xffffffffUL; - } - while(xb < xbe); + } + while(xb < xbe); while(xa < xae) { y = *xa++ - borrow; borrow = y >> 32 & 1UL; *xc++ = y & 0xffffffffUL; - } -#else -#ifdef Pack_32 - do { - y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(xc, z, y); - } - while(xb < xbe); - while(xa < xae) { - y = (*xa & 0xffff) - borrow; - borrow = (y & 0x10000) >> 16; - z = (*xa++ >> 16) - borrow; - borrow = (z & 0x10000) >> 16; - Storeinc(xc, z, y); - } -#else - do { - y = *xa++ - *xb++ - borrow; - borrow = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } - while(xb < xbe); - while(xa < xae) { - y = *xa++ - borrow; - borrow = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } -#endif -#endif + } while(!*--xc) wa--; c->wds = wa; return c; - } +} double b2d(Bigint *a, int *e) @@ -585,13 +301,6 @@ b2d(Bigint *a, int *e) ULong *xa, *xa0, w, y, z; int k; U d; -#ifdef VAX - ULong d0, d1; -#else -#define d0 word0(&d) -#define d1 word1(&d) -#endif - xa0 = a->x; xa = xa0 + a->wds; y = *--xa; @@ -600,225 +309,76 @@ b2d(Bigint *a, int *e) #endif k = hi0bits(y); *e = 32 - k; -#ifdef Pack_32 if (k < Ebits) { - d0 = Exp_1 | y >> (Ebits - k); + word0(&d) = Exp_1 | y >> (Ebits - k); w = xa > xa0 ? *--xa : 0; - d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); + word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k); goto ret_d; - } + } z = xa > xa0 ? *--xa : 0; if (k -= Ebits) { - d0 = Exp_1 | y << k | z >> (32 - k); + word0(&d) = Exp_1 | y << k | z >> (32 - k); y = xa > xa0 ? *--xa : 0; - d1 = z << k | y >> (32 - k); - } - else { - d0 = Exp_1 | y; - d1 = z; - } -#else - if (k < Ebits + 16) { - z = xa > xa0 ? *--xa : 0; - d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; - w = xa > xa0 ? *--xa : 0; - y = xa > xa0 ? *--xa : 0; - d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; - goto ret_d; - } - z = xa > xa0 ? *--xa : 0; - w = xa > xa0 ? *--xa : 0; - k -= Ebits + 16; - d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; - y = xa > xa0 ? *--xa : 0; - d1 = w << k + 16 | y << k; -#endif - ret_d: -#ifdef VAX - word0(&d) = d0 >> 16 | d0 << 16; - word1(&d) = d1 >> 16 | d1 << 16; -#endif - return dval(&d); + word1(&d) = z << k | y >> (32 - k); } -#undef d0 -#undef d1 + else { + word0(&d) = Exp_1 | y; + word1(&d) = z; + } +ret_d: + return dval(&d); +} Bigint * -d2b(double dd, int *e, int *bits MTd) +d2b(double dd, int *e, int *bits) { Bigint *b; U d; -#ifndef Sudden_Underflow int i; -#endif int de, k; ULong *x, y, z; -#ifdef VAX - ULong d0, d1; -#else -#define d0 word0(&d) -#define d1 word1(&d) -#endif d.d = dd; -#ifdef VAX - d0 = word0(&d) >> 16 | word0(&d) << 16; - d1 = word1(&d) >> 16 | word1(&d) << 16; -#endif - -#ifdef Pack_32 - b = Balloc(1 MTa); -#else - b = Balloc(2 MTa); -#endif + b = Balloc(1); x = b->x; - - z = d0 & Frac_mask; - d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ -#ifdef Sudden_Underflow - de = (int)(d0 >> Exp_shift); -#ifndef IBM - z |= Exp_msk11; -#endif -#else - if ( (de = (int)(d0 >> Exp_shift)) !=0) + z = word0(&d) & Frac_mask; + word0(&d) &= 0x7fffffff; /* clear sign bit, which we ignore */ + if ( (de = (int)(word0(&d) >> Exp_shift)) !=0) z |= Exp_msk1; -#endif -#ifdef Pack_32 - if ( (y = d1) !=0) { + if ( (y = word1(&d)) !=0) { if ( (k = lo0bits(&y)) !=0) { x[0] = y | z << (32 - k); z >>= k; - } + } else x[0] = y; -#ifndef Sudden_Underflow - i = -#endif - b->wds = (x[1] = z) !=0 ? 2 : 1; - } + i = b->wds = (x[1] = z) !=0 ? 2 : 1; + } else { k = lo0bits(&z); x[0] = z; -#ifndef Sudden_Underflow - i = -#endif - b->wds = 1; + i = b->wds = 1; k += 32; - } -#else - if ( (y = d1) !=0) { - if ( (k = lo0bits(&y)) !=0) - if (k >= 16) { - x[0] = y | z << 32 - k & 0xffff; - x[1] = z >> k - 16 & 0xffff; - x[2] = z >> k; - i = 2; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16 | z << 16 - k & 0xffff; - x[2] = z >> k & 0xffff; - x[3] = z >> k+16; - i = 3; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16; - x[2] = z & 0xffff; - x[3] = z >> 16; - i = 3; - } - } - else { -#ifdef DEBUG - if (!z) - Bug("Zero passed to d2b"); -#endif - k = lo0bits(&z); - if (k >= 16) { - x[0] = z; - i = 0; - } - else { - x[0] = z & 0xffff; - x[1] = z >> 16; - i = 1; - } - k += 32; - } - while(!x[i]) - --i; - b->wds = i + 1; -#endif -#ifndef Sudden_Underflow + } if (de) { -#endif -#ifdef IBM - *e = (de - Bias - (P-1) << 2) + k; - *bits = 4*P + 8 - k - hi0bits(word0(&d) & Frac_mask); -#else *e = de - Bias - (P-1) + k; *bits = P - k; -#endif -#ifndef Sudden_Underflow - } + } else { *e = de - Bias - (P-1) + 1 + k; -#ifdef Pack_32 *bits = 32*i - hi0bits(x[i-1]); -#else - *bits = (i+2)*16 - hi0bits(x[i]); -#endif - } -#endif + } return b; - } -#undef d0 -#undef d1 +} -CONST double -#ifdef IEEE_Arith +const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; -CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 - }; -#else -#ifdef IBM -bigtens[] = { 1e16, 1e32, 1e64 }; -CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; -#else -bigtens[] = { 1e16, 1e32 }; -CONST double tinytens[] = { 1e-16, 1e-32 }; -#endif -#endif -CONST double +const double +tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; + +const double tens[] = { - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, - 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, - 1e20, 1e21, 1e22 -#ifdef VAX - , 1e23, 1e24 -#endif - }; - -char * -__gdtoa_strcp(char *a, CONST char *b) -{ - while((*a = *b++)) - a++; - return a; - } - -#ifdef NO_STRING_H - -Char * -__gdtoa_memcpy(void *a1, void *b1, size_t len) -{ - char *a = (char*)a1, *ae = a + len; - char *b = (char*)b1, *a0 = a; - while(a < ae) - *a++ = *b++; - return a0; - } - -#endif /* NO_STRING_H */ + 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, + 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, + 1e20, 1e21, 1e22 +}; diff --git a/third_party/gdtoa/smisc.c b/third_party/gdtoa/smisc.c index 49a6bed7a..57227e73c 100644 --- a/third_party/gdtoa/smisc.c +++ b/third_party/gdtoa/smisc.c @@ -1,153 +1,112 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -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. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ Bigint * -s2b(CONST char *s, int nd0, int nd, ULong y9, int dplen MTd) +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++) ; -#ifdef Pack_32 - b = Balloc(k MTa); + b = Balloc(k); b->x[0] = y9; b->wds = 1; -#else - b = Balloc(k+1 MTa); - b->x[0] = y9 & 0xffff; - b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; -#endif - i = 9; if (9 < nd0) { s += 9; - do b = multadd(b, 10, *s++ - '0' MTa); - while(++i < nd0); + do b = multadd(b, 10, *s++ - '0'); + while(++i < nd0); s += dplen; - } + } else s += dplen + 9; for(; i < nd; i++) - b = multadd(b, 10, *s++ - '0' MTa); + b = multadd(b, 10, *s++ - '0'); return b; - } +} double ratio(Bigint *a, Bigint *b) { U da, db; int k, ka, kb; - dval(&da) = b2d(a, &ka); dval(&db) = b2d(b, &kb); k = ka - kb + ULbits*(a->wds - b->wds); -#ifdef IBM - if (k > 0) { - word0(&da) += (k >> 2)*Exp_msk1; - if (k &= 3) - dval(&da) *= 1 << k; - } - else { - k = -k; - word0(&db) += (k >> 2)*Exp_msk1; - if (k &= 3) - dval(&db) *= 1 << k; - } -#else if (k > 0) word0(&da) += k*Exp_msk1; else { k = -k; word0(&db) += k*Exp_msk1; - } -#endif - return dval(&da) / dval(&db); } - -#ifdef INFNAN_CHECK + return dval(&da) / dval(&db); +} int -match(CONST char **sp, char *t) +match(const char **sp, char *t) { int c, d; - CONST char *s = *sp; - + const char *s = *sp; while( (d = *t++) !=0) { if ((c = *++s) >= 'A' && c <= 'Z') c += 'a' - 'A'; if (c != d) return 0; - } + } *sp = s + 1; return 1; - } -#endif /* INFNAN_CHECK */ +} void copybits(ULong *c, int n, Bigint *b) { ULong *ce, *x, *xe; -#ifdef Pack_16 - int nw, nw1; -#endif - ce = c + ((n-1) >> kshift) + 1; x = b->x; -#ifdef Pack_32 xe = x + b->wds; while(x < xe) *c++ = *x++; -#else - nw = b->wds; - nw1 = nw & 1; - for(xe = x + (nw - nw1); x < xe; x += 2) - Storeinc(c, x[1], x[0]); - if (nw1) - *c++ = *x; -#endif while(c < ce) *c++ = 0; - } +} ULong any_on(Bigint *b, int k) { int n, nwds; ULong *x, *x0, x1, x2; - x = b->x; nwds = b->wds; n = k >> kshift; @@ -159,11 +118,11 @@ any_on(Bigint *b, int k) x1 <<= k; if (x1 != x2) return 1; - } + } x0 = x; x += n; while(x > x0) if (*--x) return 1; return 0; - } +} diff --git a/third_party/gdtoa/strtoId.c b/third_party/gdtoa/strtoId.c index d9b9ff692..2c6cd8e95 100644 --- a/third_party/gdtoa/strtoId.c +++ b/third_party/gdtoa/strtoId.c @@ -1,60 +1,56 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtoId(CONST char *s, char **sp, double *f0, double *f1) +strtoId(const char *s, char **sp, double *f0, double *f1) { static const FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ }; Long exp[2]; Bigint *B[2]; int k, rv[2]; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif - - B[0] = Balloc(1 MTb); + B[0] = 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] MTb); + Bfree(B[0]); if (B[1]) { ULtod((ULong*)f1, B[1]->x, exp[1], rv[1]); - Bfree(B[1] MTb); - } + Bfree(B[1]); + } else { ((ULong*)f1)[0] = ((ULong*)f0)[0]; ((ULong*)f1)[1] = ((ULong*)f0)[1]; - } - return k; } + return k; +} diff --git a/third_party/gdtoa/strtoIdd.c b/third_party/gdtoa/strtoIdd.c index 652a80711..00219a88e 100644 --- a/third_party/gdtoa/strtoIdd.c +++ b/third_party/gdtoa/strtoIdd.c @@ -1,66 +1,58 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtoIdd(CONST char *s, char **sp, double *f0, double *f1) +strtoIdd(const char *s, char **sp, double *f0, double *f1) { -#ifdef Sudden_Underflow - static const FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1, 0 /*unused*/ }; -#else static const FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0, 0 /*unused*/ }; -#endif Long exp[2]; Bigint *B[2]; int k, rv[2]; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif - - B[0] = Balloc(2 MTb); + B[0] = 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] MTb); + Bfree(B[0]); if (B[1]) { ULtodd((ULong*)f1, B[1]->x, exp[1], rv[1]); - Bfree(B[1] MTb); - } + Bfree(B[1]); + } else { ((ULong*)f1)[0] = ((ULong*)f0)[0]; ((ULong*)f1)[1] = ((ULong*)f0)[1]; ((ULong*)f1)[2] = ((ULong*)f0)[2]; ((ULong*)f1)[3] = ((ULong*)f0)[3]; - } - return k; } + return k; +} diff --git a/third_party/gdtoa/strtoIf.c b/third_party/gdtoa/strtoIf.c index 59b0b0949..a103d27c2 100644 --- a/third_party/gdtoa/strtoIf.c +++ b/third_party/gdtoa/strtoIf.c @@ -1,58 +1,54 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtoIf(CONST char *s, char **sp, float *f0, float *f1) +strtoIf(const char *s, char **sp, float *f0, float *f1) { static const FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI, 0 /*unused*/ }; Long exp[2]; Bigint *B[2]; int k, rv[2]; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif - - B[0] = Balloc(0 MTb); + B[0] = 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] MTb); + Bfree(B[0]); if (B[1]) { ULtof((ULong*)f1, B[1]->x, exp[1], rv[1]); - Bfree(B[1] MTb); - } + Bfree(B[1]); + } else *(ULong*)f1 = *(ULong*)f0; return k; - } +} diff --git a/third_party/gdtoa/strtoIg.c b/third_party/gdtoa/strtoIg.c index 7acf713c0..56654b4c6 100644 --- a/third_party/gdtoa/strtoIg.c +++ b/third_party/gdtoa/strtoIg.c @@ -1,57 +1,53 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtoIg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, Bigint **B, int *rvp) +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; unsigned int nb1, nb11; Long e1; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif - b = *B; rv = strtodg(s00, se, fpi, exp, b->x); if (!(rv & STRTOG_Inexact)) { B[1] = 0; return *rvp = rv; - } + } e1 = exp[0]; rv1 = rv ^ STRTOG_Inexact; - b1 = Balloc(b->k MTb); + b1 = Balloc(b->k); Bcopy(b1, b); nb = fpi->nbits; nb1 = nb & 31; @@ -60,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 MTb); + b1 = increment(b1); if ((rv & STRTOG_Retmask) == STRTOG_Zero) { if (fpi->sudden_underflow) { b1->x[0] = 0; @@ -68,41 +64,41 @@ strtoIg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, Bigint **B, int * rv1 += STRTOG_Normal - STRTOG_Zero; rv1 &= ~STRTOG_Underflow; goto swapcheck; - } + } rv1 &= STRTOG_Inexlo | STRTOG_Underflow | STRTOG_Zero | STRTOG_Neg; rv1 |= STRTOG_Inexhi | STRTOG_Denormal; goto swapcheck; - } + } if (b1->wds > nw - || (nb1 && b1->x[nw1] & 1L << nb1)) { + || (nb1 && b1->x[nw1] & 1L << nb1)) { if (++e1 > fpi->emax) rv1 = STRTOG_Infinite | STRTOG_Inexhi; rshift(b1, 1); - } + } else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { if (b1->x[nw1] & 1L << nb11) { rv1 += STRTOG_Normal - STRTOG_Denormal; rv1 &= ~STRTOG_Underflow; - } } } + } else { swap = STRTOG_Neg; if ((rv & STRTOG_Retmask) == STRTOG_Infinite) { - b1 = set_ones(b1, nb MTb); + b1 = set_ones(b1, nb); e1 = fpi->emax; rv1 = STRTOG_Normal | STRTOG_Inexlo | (rv & STRTOG_Neg); goto swapcheck; - } + } decrement(b1); if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { for(i = nw1; !b1->x[i]; --i) if (!i) { rv1 = STRTOG_Zero | STRTOG_Inexlo | (rv & STRTOG_Neg); break; - } + } goto swapcheck; - } + } if (!(b1->x[nw1] & 1L << nb11)) { if (e1 == fpi->emin) { if (fpi->sudden_underflow) @@ -110,15 +106,15 @@ strtoIg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, Bigint **B, int * else rv1 += STRTOG_Denormal - STRTOG_Normal; rv1 |= STRTOG_Underflow; - } + } else { - b1 = lshift(b1, 1 MTb); + b1 = lshift(b1, 1); b1->x[0] |= 1; --e1; - } } } - swapcheck: + } +swapcheck: if (swap ^ (rv & STRTOG_Neg)) { rvp[0] = rv1; rvp[1] = rv; @@ -126,12 +122,12 @@ strtoIg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, Bigint **B, int * B[1] = b; exp[1] = exp[0]; exp[0] = e1; - } + } else { rvp[0] = rv; rvp[1] = rv1; B[1] = b1; exp[1] = e1; - } - return rv; } + return rv; +} diff --git a/third_party/gdtoa/strtoIx.c b/third_party/gdtoa/strtoIx.c index de588353e..4ce3329ed 100644 --- a/third_party/gdtoa/strtoIx.c +++ b/third_party/gdtoa/strtoIx.c @@ -1,64 +1,60 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtoIx(CONST char *s, char **sp, void *a, void *b) +strtoIx(const char *s, char **sp, void *a, void *b) { static const FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI, 0 /*unused*/ }; Long exp[2]; Bigint *B[2]; int k, rv[2]; UShort *L = (UShort *)a, *M = (UShort *)b; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif - - B[0] = Balloc(1 MTb); + B[0] = 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] MTb); + Bfree(B[0]); if (B[1]) { ULtox(M, B[1]->x, exp[1], rv[1]); - Bfree(B[1] MTb); - } + Bfree(B[1]); + } else { M[0] = L[0]; M[1] = L[1]; M[2] = L[2]; M[3] = L[3]; M[4] = L[4]; - } - return k; } + return k; +} diff --git a/third_party/gdtoa/strtod.c b/third_party/gdtoa/strtod.c index 19f2de989..cb46eb4d6 100644 --- a/third_party/gdtoa/strtod.c +++ b/third_party/gdtoa/strtod.c @@ -1,130 +1,79 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" +#include "libc/runtime/fenv.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998-2001 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -#ifdef IEEE_Arith -#ifndef NO_IEEE_Scale #define Avoid_Underflow +#define dplen 1 #undef tinytens + /* The factor of 2^106 in tinytens[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[] = { 1e-16, 1e-32, 1e-64, 1e-128, - 9007199254740992.*9007199254740992.e-256 - }; -#endif -#endif +static const double tinytens[] = { + 1e-16, 1e-32, 1e-64, 1e-128, + 9007199254740992.*9007199254740992.e-256 +}; -#ifdef Honor_FLT_ROUNDS -#undef Check_FLT_ROUNDS -#define Check_FLT_ROUNDS -#else -#define Rounding Flt_Rounds -#endif - -#ifdef Avoid_Underflow /*{*/ static double sulp(U *x, int scale) { U u; - double rv; int i; - + double rv; rv = 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); word1(&u) = 0; return rv * u.d; - } -#endif /*}*/ +} double -strtod(CONST char *s00, char **se) +strtod(const char *s00, char **se) { -#ifdef Avoid_Underflow int scale; -#endif int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, dsign, - e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; - CONST char *s, *s0, *s1; + e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; + const char *s, *s0, *s1; double aadj; Long L; U adj, aadj1, rv, rv0; ULong y, z; Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; -#ifdef Avoid_Underflow ULong Lsb, Lsb1; -#endif -#ifdef SET_INEXACT - int inexact, oldinexact; -#endif -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif -#ifdef USE_LOCALE /*{{*/ -#ifdef NO_LOCALE_CACHE - char *decimalpoint = localeconv()->decimal_point; - int dplen = strlen(decimalpoint); -#else - char *decimalpoint; - static char *decimalpoint_cache; - static int dplen; - if (!(s0 = decimalpoint_cache)) { - s0 = localeconv()->decimal_point; - if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) { - strcpy(decimalpoint_cache, s0); - s0 = decimalpoint_cache; - } - dplen = strlen(s0); - } - decimalpoint = (char*)s0; -#endif /*NO_LOCALE_CACHE*/ -#else /*USE_LOCALE}{*/ -#define dplen 1 -#endif /*USE_LOCALE}}*/ - -#ifdef Honor_FLT_ROUNDS /*{*/ int Rounding; -#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ - Rounding = Flt_Rounds; -#else /*}{*/ - Rounding = 1; - switch(fegetround()) { - case FE_TOWARDZERO: Rounding = 0; break; - case FE_UPWARD: Rounding = 2; break; - case FE_DOWNWARD: Rounding = 3; - } -#endif /*}}*/ -#endif /*}*/ - + Rounding = FLT_ROUNDS; sign = nz0 = nz = decpt = 0; dval(&rv) = 0.; for(s = s00;;s++) switch(*s) { @@ -147,45 +96,39 @@ strtod(CONST char *s00, char **se) default: goto break2; } - break2: +break2: if (*s == '0') { -#ifndef NO_HEX_FP /*{*/ { - 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': + 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': { -#ifdef Honor_FLT_ROUNDS - FPI fpi1 = fpi; - fpi1.rounding = Rounding; -#else -#define fpi1 fpi -#endif - switch((i = gethex(&s, &fpi1, &exp, &bb, sign MTb)) & STRTOG_Retmask) { - case STRTOG_NoNumber: - s = s00; - sign = 0; - case STRTOG_Zero: - break; - default: - if (bb) { - copybits(bits, fpi.nbits, bb); - Bfree(bb MTb); + FPI fpi1 = fpi; + fpi1.rounding = Rounding; + switch((i = gethex(&s, &fpi1, &exp, &bb, sign)) & STRTOG_Retmask) { + case STRTOG_NoNumber: + s = s00; + sign = 0; + case STRTOG_Zero: + break; + default: + if (bb) { + copybits(bits, fpi.nbits, bb); + Bfree(bb); } - ULtod(((U*)&rv)->L, bits, exp, i); - }} + ULtod(((U*)&rv)->L, bits, exp, i); + }} goto ret; - } + } } -#endif /*}*/ nz0 = 1; while(*++s == '0') ; if (!*s) goto ret; - } + } s0 = s; y = z = 0; for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) @@ -194,17 +137,8 @@ strtod(CONST char *s00, char **se) else if (nd < DBL_DIG + 2) z = 10*z + c - '0'; nd0 = nd; -#ifdef USE_LOCALE - if (c == *decimalpoint) { - for(i = 1; decimalpoint[i]; ++i) - if (s[i] != decimalpoint[i]) - goto dig_done; - s += i; - c = *s; -#else if (c == '.') { c = *++s; -#endif decpt = 1; if (!nd) { for(; c == '0'; c = *++s) @@ -214,11 +148,11 @@ strtod(CONST char *s00, char **se) nf += nz; nz = 0; goto have_dig; - } - goto dig_done; } + goto dig_done; + } for(; c >= '0' && c <= '9'; c = *++s) { - have_dig: + have_dig: nz++; if (c -= '0') { nf += nz; @@ -232,23 +166,23 @@ strtod(CONST char *s00, char **se) else if (nd <= DBL_DIG + 2) z = 10*z + c; nz = 0; - } } - }/*}*/ - dig_done: + } + }/*}*/ +dig_done: e = 0; if (c == 'e' || c == 'E') { if (!nd && !nz && !nz0) { goto ret0; - } + } s00 = s; esign = 0; switch(c = *++s) { - case '-': - esign = 1; - case '+': - c = *++s; - } + case '-': + esign = 1; + case '+': + c = *++s; + } if (c >= '0' && c <= '9') { while(c == '0') c = *++s; @@ -266,240 +200,164 @@ strtod(CONST char *s00, char **se) e = (int)L; if (esign) e = -e; - } + } else e = 0; - } + } else s = s00; - } + } if (!nd) { if (!nz && !nz0) { -#ifdef INFNAN_CHECK /* Check for Nan and Infinity */ 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) - switch(c) { - case 'i': - case 'I': - if (match(&s,"nf")) { - --s; - if (!match(&s,"inity")) - ++s; - word0(&rv) = 0x7ff00000; - word1(&rv) = 0; - goto ret; + switch(c) { + case 'i': + case 'I': + if (match(&s,"nf")) { + --s; + if (!match(&s,"inity")) + ++s; + word0(&rv) = 0x7ff00000; + word1(&rv) = 0; + goto ret; } - break; - case 'n': - case 'N': - if (match(&s, "an")) { -#ifndef No_Hex_NaN - if (*s == '(' /*)*/ - && hexnan(&s, &fpinan, bits) - == STRTOG_NaNbits) { - word0(&rv) = 0x7ff00000 | bits[1]; - word1(&rv) = bits[0]; + break; + case 'n': + case 'N': + if (match(&s, "an")) { + if (*s == '(' /*)*/ + && hexnan(&s, &fpinan, bits) + == STRTOG_NaNbits) { + word0(&rv) = 0x7ff00000 | bits[1]; + word1(&rv) = bits[0]; } - else { -#endif - word0(&rv) = NAN_WORD0; - word1(&rv) = NAN_WORD1; -#ifndef No_Hex_NaN + else { + word0(&rv) = NAN_WORD0; + word1(&rv) = NAN_WORD1; } -#endif - goto ret; + goto ret; } - } -#endif /* INFNAN_CHECK */ - ret0: + } + ret0: s = s00; sign = 0; - } - goto ret; } + goto ret; + } 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) { -#ifdef SET_INEXACT - if (k > DBL_DIG) - oldinexact = get_inexact(); -#endif dval(&rv) = tens[k - 9] * dval(&rv) + z; - } + } bd0 = 0; - if (nd <= DBL_DIG -#ifndef RND_PRODQUOT -#ifndef Honor_FLT_ROUNDS - && Flt_Rounds == 1 -#endif -#endif - ) { + if (nd <= DBL_DIG) { if (!e) goto ret; -#ifndef ROUND_BIASED_without_Round_Up if (e > 0) { if (e <= Ten_pmax) { -#ifdef VAX - goto vax_ovfl_check; -#else -#ifdef Honor_FLT_ROUNDS /* round correctly FLT_ROUNDS = 2 or 3 */ if (sign) { rv.d = -rv.d; sign = 0; - } -#endif + } /* rv = */ rounded_product(dval(&rv), tens[e]); goto ret; -#endif - } + } i = DBL_DIG - nd; if (e <= Ten_pmax + i) { /* A fancier test would sometimes let us do * this for larger i values. */ -#ifdef Honor_FLT_ROUNDS /* round correctly FLT_ROUNDS = 2 or 3 */ if (sign) { rv.d = -rv.d; sign = 0; - } -#endif + } e -= i; dval(&rv) *= tens[i]; -#ifdef VAX - /* VAX exponent range is so narrow we must - * worry about overflow here... - */ - vax_ovfl_check: - word0(&rv) -= P*Exp_msk1; /* rv = */ rounded_product(dval(&rv), tens[e]); - if ((word0(&rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) - goto ovfl; - word0(&rv) += P*Exp_msk1; -#else - /* rv = */ rounded_product(dval(&rv), tens[e]); -#endif goto ret; - } } -#ifndef Inaccurate_Divide + } else if (e >= -Ten_pmax) { -#ifdef Honor_FLT_ROUNDS /* round correctly FLT_ROUNDS = 2 or 3 */ if (sign) { rv.d = -rv.d; sign = 0; - } -#endif + } /* rv = */ rounded_quotient(dval(&rv), tens[-e]); goto ret; - } -#endif -#endif /* ROUND_BIASED_without_Round_Up */ } + } e1 += nd - k; - -#ifdef IEEE_Arith -#ifdef SET_INEXACT - inexact = 1; - if (k <= DBL_DIG) - oldinexact = get_inexact(); -#endif -#ifdef Avoid_Underflow scale = 0; -#endif -#ifdef Honor_FLT_ROUNDS if (Rounding >= 2) { if (sign) Rounding = Rounding == 2 ? 0 : 2; else if (Rounding != 2) Rounding = 0; - } -#endif -#endif /*IEEE_Arith*/ - + } /* Get starting approximation = rv * 10**e1 */ - if (e1 > 0) { if ( (i = e1 & 15) !=0) dval(&rv) *= tens[i]; if (e1 &= ~15) { if (e1 > DBL_MAX_10_EXP) { - ovfl: + ovfl: /* Can't trust HUGE_VAL */ -#ifdef IEEE_Arith -#ifdef Honor_FLT_ROUNDS switch(Rounding) { - case 0: /* toward 0 */ - case 3: /* toward -infinity */ + case 0: /* toward 0 */ + case 3: /* toward -infinity */ word0(&rv) = Big0; word1(&rv) = Big1; break; - default: + default: word0(&rv) = Exp_mask; word1(&rv) = 0; - } -#else /*Honor_FLT_ROUNDS*/ - word0(&rv) = Exp_mask; - word1(&rv) = 0; -#endif /*Honor_FLT_ROUNDS*/ -#ifdef SET_INEXACT - /* set overflow bit */ - dval(&rv0) = 1e300; - dval(&rv0) *= dval(&rv0); -#endif -#else /*IEEE_Arith*/ - word0(&rv) = Big0; - word1(&rv) = Big1; -#endif /*IEEE_Arith*/ - range_err: - if (bd0) { - Bfree(bb MTb); - Bfree(bd MTb); - Bfree(bs MTb); - Bfree(bd0 MTb); - Bfree(delta MTb); - } -#ifndef NO_ERRNO - errno = ERANGE; -#endif - goto ret; } + range_err: + if (bd0) { + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(bd0); + Bfree(delta); + } + errno = ERANGE; + goto ret; + } e1 >>= 4; for(j = 0; e1 > 1; j++, e1 >>= 1) if (e1 & 1) dval(&rv) *= bigtens[j]; - /* The last multiplication could overflow. */ + /* The last multiplication could overflow. */ word0(&rv) -= P*Exp_msk1; dval(&rv) *= bigtens[j]; if ((z = word0(&rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-P)) + > Exp_msk1*(DBL_MAX_EXP+Bias-P)) 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; - } + } else word0(&rv) += P*Exp_msk1; - } } + } else if (e1 < 0) { e1 = -e1; if ( (i = e1 & 15) !=0) @@ -507,86 +365,56 @@ strtod(CONST char *s00, char **se) if (e1 >>= 4) { if (e1 >= 1 << n_bigtens) goto undfl; -#ifdef Avoid_Underflow if (e1 & Scale_Bit) scale = 2*P; for(j = 0; e1 > 0; j++, e1 >>= 1) if (e1 & 1) dval(&rv) *= tinytens[j]; if (scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask) - >> Exp_shift)) > 0) { + >> Exp_shift)) > 0) { /* scaled rv is denormal; zap j low bits */ if (j >= 32) { word1(&rv) = 0; if (j >= 53) - word0(&rv) = (P+2)*Exp_msk1; + word0(&rv) = (P+2)*Exp_msk1; else - word0(&rv) &= 0xffffffff << (j-32); - } + word0(&rv) &= 0xffffffff << (j-32); + } else word1(&rv) &= 0xffffffff << j; - } -#else - for(j = 0; e1 > 1; j++, e1 >>= 1) - if (e1 & 1) - dval(&rv) *= tinytens[j]; - /* The last multiplication could underflow. */ - dval(&rv0) = dval(&rv); - dval(&rv) *= tinytens[j]; + } if (!dval(&rv)) { - dval(&rv) = 2.*dval(&rv0); - dval(&rv) *= tinytens[j]; -#endif - if (!dval(&rv)) { - undfl: - dval(&rv) = 0.; -#ifdef Honor_FLT_ROUNDS - if (Rounding == 2) - word1(&rv) = 1; -#endif - goto range_err; - } -#ifndef Avoid_Underflow - word0(&rv) = Tiny0; - word1(&rv) = Tiny1; - /* The refinement below will clean - * this approximation up. - */ - } -#endif + undfl: + dval(&rv) = 0.; + if (Rounding == 2) + word1(&rv) = 1; + goto range_err; } } - + } /* 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 MTb); - + bd0 = s2b(s0, nd0, nd, y, dplen); for(;;) { - bd = Balloc(bd0->k MTb); + bd = Balloc(bd0->k); Bcopy(bd, bd0); - bb = d2b(dval(&rv), &bbe, &bbbits MTb); /* rv = bb * 2^bbe */ - bs = i2b(1 MTb); - + bb = d2b(dval(&rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ + bs = i2b(1); if (e >= 0) { bb2 = bb5 = 0; bd2 = bd5 = e; - } + } else { bb2 = bb5 = -e; bd2 = bd5 = 0; - } + } if (bbe >= 0) bb2 += bbe; else bd2 -= bbe; bs2 = bb2; -#ifdef Honor_FLT_ROUNDS if (Rounding != 1) bs2++; -#endif -#ifdef Avoid_Underflow Lsb = LSB; Lsb1 = 0; j = bbe - scale; @@ -599,28 +427,10 @@ strtod(CONST char *s00, char **se) Lsb <<= i; else Lsb1 = Lsb << (i-32); - } -#else /*Avoid_Underflow*/ -#ifdef Sudden_Underflow -#ifdef IBM - j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); -#else - j = P + 1 - bbbits; -#endif -#else /*Sudden_Underflow*/ - j = bbe; - i = j + bbbits - 1; /* logb(&rv) */ - if (i < Emin) /* denormal */ - j += P - Emin; - else - j = P + 1 - bbbits; -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ + } bb2 += j; bd2 += j; -#ifdef Avoid_Underflow bd2 += scale; -#endif i = bb2 < bd2 ? bb2 : bd2; if (i > bs2) i = bs2; @@ -628,78 +438,58 @@ strtod(CONST char *s00, char **se) bb2 -= i; bd2 -= i; bs2 -= i; - } + } if (bb5 > 0) { - bs = pow5mult(bs, bb5 MTb); - bb1 = mult(bs, bb MTb); - Bfree(bb MTb); + bs = pow5mult(bs, bb5); + bb1 = mult(bs, bb); + Bfree(bb); bb = bb1; - } + } if (bb2 > 0) - bb = lshift(bb, bb2 MTb); + bb = lshift(bb, bb2); if (bd5 > 0) - bd = pow5mult(bd, bd5 MTb); + bd = pow5mult(bd, bd5); if (bd2 > 0) - bd = lshift(bd, bd2 MTb); + bd = lshift(bd, bd2); if (bs2 > 0) - bs = lshift(bs, bs2 MTb); - delta = diff(bb, bd MTb); + bs = lshift(bs, bs2); + delta = diff(bb, bd); dsign = delta->sign; delta->sign = 0; i = cmp(delta, bs); -#ifdef Honor_FLT_ROUNDS if (Rounding != 1) { if (i < 0) { /* Error is less than an ulp */ if (!delta->x[0] && delta->wds <= 1) { /* exact */ -#ifdef SET_INEXACT - inexact = 0; -#endif break; - } + } if (Rounding) { if (dsign) { dval(&adj) = 1.; goto apply_adj; - } } + } else if (!dsign) { dval(&adj) = -1.; if (!word1(&rv) - && !(word0(&rv) & Frac_mask)) { + && !(word0(&rv) & Frac_mask)) { y = word0(&rv) & Exp_mask; -#ifdef Avoid_Underflow if (!scale || y > 2*P*Exp_msk1) -#else - if (y) -#endif - { - delta = lshift(delta,Log2P MTb); - if (cmp(delta, bs) <= 0) - dval(&adj) = -0.5; - } + { + delta = lshift(delta,Log2P); + if (cmp(delta, bs) <= 0) + dval(&adj) = -0.5; } - apply_adj: -#ifdef Avoid_Underflow - if (scale && (y = word0(&rv) & Exp_mask) - <= 2*P*Exp_msk1) - word0(&adj) += (2*P+1)*Exp_msk1 - y; -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= - P*Exp_msk1) { - word0(&rv) += P*Exp_msk1; - dval(&rv) += adj*ulp(&rv); - word0(&rv) -= P*Exp_msk1; - } - else -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ - dval(&rv) += adj.d*ulp(&rv); } - break; + apply_adj: + 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); } + break; + } dval(&adj) = ratio(delta, bs); if (adj.d < 1.) dval(&adj) = 1.; @@ -710,112 +500,59 @@ strtod(CONST char *s00, char **se) if (!((Rounding>>1) ^ dsign)) y++; dval(&adj) = y; - } } -#ifdef Avoid_Underflow + } if (scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) word0(&adj) += (2*P+1)*Exp_msk1 - y; -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { - word0(&rv) += P*Exp_msk1; - dval(&adj) *= ulp(&rv); - if (dsign) - dval(&rv) += adj; - else - dval(&rv) -= adj; - word0(&rv) -= P*Exp_msk1; - goto cont; - } -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ dval(&adj) *= ulp(&rv); /* XXX */ if (dsign) { if (word0(&rv) == Big0 && word1(&rv) == Big1) goto ovfl; dval(&rv) += adj.d; - } + } else dval(&rv) -= adj.d; goto cont; - } -#endif /*Honor_FLT_ROUNDS*/ - + } if (i < 0) { /* Error is less than half an ulp -- check for * special case of mantissa a power of two. */ - if (dsign || word1(&rv) || word0(&rv) & Bndry_mask -#ifdef IEEE_Arith -#ifdef Avoid_Underflow - || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1 -#else - || (word0(&rv) & Exp_mask) <= Exp_msk1 -#endif -#endif - ) { -#ifdef SET_INEXACT - if (!delta->x[0] && delta->wds <= 1) - inexact = 0; -#endif + if (dsign || word1(&rv) || word0(&rv) & Bndry_mask || + (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1) { break; - } + } if (!delta->x[0] && delta->wds <= 1) { /* exact result */ -#ifdef SET_INEXACT - inexact = 0; -#endif break; - } - delta = lshift(delta,Log2P MTb); + } + delta = lshift(delta,Log2P); if (cmp(delta, bs) > 0) goto drop_down; break; - } + } if (i == 0) { /* exactly half-way between */ if (dsign) { if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 - && word1(&rv) == ( -#ifdef Avoid_Underflow - (scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) - ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : -#endif - 0xffffffff)) { + && word1(&rv) == ( + (scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) + ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : + 0xffffffff)) { /*boundary case -- increment exponent*/ if (word0(&rv) == Big0 && word1(&rv) == Big1) goto ovfl; word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1 -#ifdef IBM - | Exp_msk1 >> 4 -#endif ; word1(&rv) = 0; -#ifdef Avoid_Underflow dsign = 0; -#endif break; - } } + } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) { - drop_down: + drop_down: /* boundary case -- decrement exponent */ -#ifdef Sudden_Underflow /*{{*/ - L = word0(&rv) & Exp_mask; -#ifdef IBM - if (L < Exp_msk1) -#else -#ifdef Avoid_Underflow - if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) -#else - if (L <= Exp_msk1) -#endif /*Avoid_Underflow*/ -#endif /*IBM*/ - goto undfl; - L -= Exp_msk1; -#else /*Sudden_Underflow}{*/ -#ifdef Avoid_Underflow if (scale) { L = word0(&rv) & Exp_mask; if (L <= (2*P+1)*Exp_msk1) { @@ -825,239 +562,126 @@ strtod(CONST char *s00, char **se) break; /* rv = smallest denormal */ goto undfl; - } } -#endif /*Avoid_Underflow*/ + } L = (word0(&rv) & Exp_mask) - Exp_msk1; -#endif /*Sudden_Underflow}}*/ word0(&rv) = L | Bndry_mask1; word1(&rv) = 0xffffffff; -#ifdef IBM - goto cont; -#else break; -#endif - } -#ifndef ROUND_BIASED -#ifdef Avoid_Underflow + } if (Lsb1) { if (!(word0(&rv) & Lsb1)) break; - } + } else if (!(word1(&rv) & Lsb)) break; -#else - if (!(word1(&rv) & LSB)) - break; -#endif -#endif if (dsign) -#ifdef Avoid_Underflow dval(&rv) += sulp(&rv, scale); -#else - dval(&rv) += ulp(&rv); -#endif -#ifndef ROUND_BIASED else { -#ifdef Avoid_Underflow dval(&rv) -= sulp(&rv, scale); -#else - dval(&rv) -= ulp(&rv); -#endif -#ifndef Sudden_Underflow if (!dval(&rv)) goto undfl; -#endif - } -#ifdef Avoid_Underflow - dsign = 1 - dsign; -#endif -#endif - break; } + dsign = 1 - dsign; + break; + } if ((aadj = ratio(delta, bs)) <= 2.) { if (dsign) aadj = dval(&aadj1) = 1.; else if (word1(&rv) || word0(&rv) & Bndry_mask) { -#ifndef Sudden_Underflow if (word1(&rv) == Tiny1 && !word0(&rv)) goto undfl; -#endif aadj = 1.; dval(&aadj1) = -1.; - } + } 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; - } } + } else { aadj *= 0.5; dval(&aadj1) = dsign ? aadj : -aadj; -#ifdef Check_FLT_ROUNDS switch(Rounding) { - case 2: /* towards +infinity */ - dval(&aadj1) -= 0.5; - break; - case 0: /* towards 0 */ - case 3: /* towards -infinity */ - dval(&aadj1) += 0.5; - } -#else - if (Flt_Rounds == 0) + case 2: /* towards +infinity */ + dval(&aadj1) -= 0.5; + break; + case 0: /* towards 0 */ + case 3: /* towards -infinity */ dval(&aadj1) += 0.5; -#endif /*Check_FLT_ROUNDS*/ } + } 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) * ulp(&rv); dval(&rv) += dval(&adj); if ((word0(&rv) & Exp_mask) >= - Exp_msk1*(DBL_MAX_EXP+Bias-P)) { + Exp_msk1*(DBL_MAX_EXP+Bias-P)) { if (word0(&rv0) == Big0 && word1(&rv0) == Big1) goto ovfl; word0(&rv) = Big0; word1(&rv) = Big1; goto cont; - } + } else word0(&rv) += P*Exp_msk1; - } + } else { -#ifdef Avoid_Underflow if (scale && y <= 2*P*Exp_msk1) { if (aadj <= 0x7fffffff) { if ((z = aadj) <= 0) z = 1; aadj = z; dval(&aadj1) = dsign ? aadj : -aadj; - } - word0(&aadj1) += (2*P+1)*Exp_msk1 - y; } + word0(&aadj1) += (2*P+1)*Exp_msk1 - y; + } dval(&adj) = dval(&aadj1) * ulp(&rv); dval(&rv) += dval(&adj); -#else -#ifdef Sudden_Underflow - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) { - dval(&rv0) = dval(&rv); - word0(&rv) += P*Exp_msk1; - dval(&adj) = dval(&aadj1) * ulp(&rv); - dval(&rv) += adj; -#ifdef IBM - if ((word0(&rv) & Exp_mask) < P*Exp_msk1) -#else - if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) -#endif - { - if (word0(&rv0) == Tiny0 - && word1(&rv0) == Tiny1) - goto undfl; - word0(&rv) = Tiny0; - word1(&rv) = Tiny1; - goto cont; - } - else - word0(&rv) -= P*Exp_msk1; - } - else { - dval(&adj) = dval(&aadj1) * ulp(&rv); - dval(&rv) += adj; - } -#else /*Sudden_Underflow*/ - /* Compute dval(&adj) so that the IEEE rounding rules will - * correctly round rv + dval(&adj) in some half-way cases. - * If rv * ulp(&rv) is denormalized (i.e., - * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid - * trouble from bits lost to denormalization; - * example: 1.2e-307 . - */ - if (y <= (P-1)*Exp_msk1 && aadj > 1.) { - dval(&aadj1) = (double)(int)(aadj + 0.5); - if (!dsign) - dval(&aadj1) = -dval(&aadj1); - } - dval(&adj) = dval(&aadj1) * ulp(&rv); - dval(&rv) += adj; -#endif /*Sudden_Underflow*/ -#endif /*Avoid_Underflow*/ - } + } z = word0(&rv) & Exp_mask; -#ifndef SET_INEXACT -#ifdef Avoid_Underflow if (!scale) -#endif - 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; + 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; } - else if (aadj < .4999999/FLT_RADIX) - break; + else if (aadj < .4999999/FLT_RADIX) + break; } -#endif - cont: - Bfree(bb MTb); - Bfree(bd MTb); - Bfree(bs MTb); - Bfree(delta MTb); - } - Bfree(bb MTb); - Bfree(bd MTb); - Bfree(bs MTb); - Bfree(bd0 MTb); - Bfree(delta MTb); -#ifdef SET_INEXACT - if (inexact) { - if (!oldinexact) { - word0(&rv0) = Exp_1 + (70 << Exp_shift); - word1(&rv0) = 0; - dval(&rv0) += 1.; - } - } - else if (!oldinexact) - clear_inexact(); -#endif -#ifdef Avoid_Underflow + cont: + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(delta); + } + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(bd0); + Bfree(delta); if (scale) { word0(&rv0) = Exp_1 - 2*P*Exp_msk1; word1(&rv0) = 0; dval(&rv) *= dval(&rv0); -#ifndef NO_ERRNO /* try to avoid the bug of testing an 8087 register value */ -#ifdef IEEE_Arith if (!(word0(&rv) & Exp_mask)) -#else - if (word0(&rv) == 0 && word1(&rv) == 0) -#endif errno = ERANGE; -#endif - } -#endif /* Avoid_Underflow */ -#ifdef SET_INEXACT - if (inexact && !(word0(&rv) & Exp_mask)) { - /* set underflow bit */ - dval(&rv0) = 1e-300; - dval(&rv0) *= dval(&rv0); - } -#endif - ret: + } +ret: if (se) *se = (char *)s; return sign ? -dval(&rv) : dval(&rv); - } - +} diff --git a/third_party/gdtoa/strtodI.c b/third_party/gdtoa/strtodI.c index dc4d718f3..48967b58f 100644 --- a/third_party/gdtoa/strtodI.c +++ b/third_party/gdtoa/strtodI.c @@ -1,156 +1,113 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ static double ulpdown(U *d) { double u; ULong *L = d->L; - u = ulp(d); - if (!(L[_1] | (L[_0] & 0xfffff)) - && (L[_0] & 0x7ff00000) > 0x00100000) + if (!(L[0] | (L[1] & 0xfffff)) + && (L[1] & 0x7ff00000) > 0x00100000) u *= 0.5; return u; - } +} int -strtodI(CONST char *s, char **sp, double *dd) +strtodI(const char *s, char **sp, double *dd) { static const FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ }; ULong bits[2], sign; Long exp; int j, k; U *u; - k = strtodg(s, sp, &fpi, &exp, bits); u = (U*)dd; sign = k & STRTOG_Neg ? 0x80000000L : 0; switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: + case STRTOG_NoNumber: dval(&u[0]) = dval(&u[1]) = 0.; break; - - case STRTOG_Zero: + case STRTOG_Zero: dval(&u[0]) = dval(&u[1]) = 0.; -#ifdef Sudden_Underflow - if (k & STRTOG_Inexact) { - if (sign) - word0(&u[0]) = 0x80100000L; - else - word0(&u[1]) = 0x100000L; - } - break; -#else goto contain; -#endif - - case STRTOG_Denormal: + case STRTOG_Denormal: word1(&u[0]) = bits[0]; word0(&u[0]) = bits[1]; goto contain; - - case STRTOG_Normal: + case STRTOG_Normal: word1(&u[0]) = bits[0]; word0(&u[0]) = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); - contain: + contain: j = k & STRTOG_Inexact; if (sign) { word0(&u[0]) |= sign; j = STRTOG_Inexact - j; - } + } switch(j) { - case STRTOG_Inexlo: -#ifdef Sudden_Underflow - if ((u->L[_0] & 0x7ff00000) < 0x3500000) { - word0(&u[1]) = word0(&u[0]) + 0x3500000; - word1(&u[1]) = word1(&u[0]); - dval(&u[1]) += ulp(&u[1]); - word0(&u[1]) -= 0x3500000; - if (!(word0(&u[1]) & 0x7ff00000)) { - word0(&u[1]) = sign; - word1(&u[1]) = 0; - } - } - else -#endif + case STRTOG_Inexlo: dval(&u[1]) = dval(&u[0]) + ulp(&u[0]); break; - case STRTOG_Inexhi: + case STRTOG_Inexhi: dval(&u[1]) = dval(&u[0]); -#ifdef Sudden_Underflow - if ((word0(&u[0]) & 0x7ff00000) < 0x3500000) { - word0(&u[0]) += 0x3500000; - dval(&u[0]) -= ulpdown(u); - word0(&u[0]) -= 0x3500000; - if (!(word0(&u[0]) & 0x7ff00000)) { - word0(&u[0]) = sign; - word1(&u[0]) = 0; - } - } - else -#endif dval(&u[0]) -= ulpdown(u); break; - default: + default: dval(&u[1]) = dval(&u[0]); - } + } break; - - case STRTOG_Infinite: + case STRTOG_Infinite: word0(&u[0]) = word0(&u[1]) = sign | 0x7ff00000; word1(&u[0]) = word1(&u[1]) = 0; if (k & STRTOG_Inexact) { if (sign) { word0(&u[1]) = 0xffefffffL; word1(&u[1]) = 0xffffffffL; - } + } else { word0(&u[0]) = 0x7fefffffL; word1(&u[0]) = 0xffffffffL; - } } + } break; - - case STRTOG_NaN: + case STRTOG_NaN: u->L[0] = (u+1)->L[0] = d_QNAN0; u->L[1] = (u+1)->L[1] = d_QNAN1; break; - - case STRTOG_NaNbits: + case STRTOG_NaNbits: word0(&u[0]) = word0(&u[1]) = 0x7ff00000 | sign | bits[1]; word1(&u[0]) = word1(&u[1]) = bits[0]; - } - return k; } + return k; +} diff --git a/third_party/gdtoa/strtodg.c b/third_party/gdtoa/strtodg.c index 8a22801f2..897e6304e 100644 --- a/third_party/gdtoa/strtodg.c +++ b/third_party/gdtoa/strtodg.c @@ -1,121 +1,89 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998-2001 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - - static CONST int -fivesbits[] = { 0, 3, 5, 7, 10, 12, 14, 17, 19, 21, - 24, 26, 28, 31, 33, 35, 38, 40, 42, 45, - 47, 49, 52 -#ifdef VAX - , 54, 56 -#endif - }; +static const int +fivesbits[] = { 0, 3, 5, 7, 10, 12, 14, 17, 19, 21, + 24, 26, 28, 31, 33, 35, 38, 40, 42, 45, + 47, 49, 52 }; Bigint * -increment(Bigint *b MTd) +increment(Bigint *b) { ULong *x, *xe; Bigint *b1; -#ifdef Pack_16 - ULong carry = 1, y; -#endif - x = b->x; xe = x + b->wds; -#ifdef Pack_32 do { if (*x < (ULong)0xffffffffL) { ++*x; return b; - } + } *x++ = 0; - } while(x < xe); -#else - do { - y = *x + carry; - carry = y >> 16; - *x++ = y & 0xffff; - if (!carry) - return b; - } while(x < xe); - if (carry) -#endif + } while(x < xe); { if (b->wds >= b->maxwds) { - b1 = Balloc(b->k+1 MTa); + b1 = Balloc(b->k+1); Bcopy(b1,b); - Bfree(b MTa); + Bfree(b); b = b1; - } - b->x[b->wds++] = 1; } - return b; + b->x[b->wds++] = 1; } + return b; +} void decrement(Bigint *b) { ULong *x, *xe; -#ifdef Pack_16 - ULong borrow = 1, y; -#endif - x = b->x; xe = x + b->wds; -#ifdef Pack_32 do { if (*x) { --*x; break; - } - *x++ = 0xffffffffL; } - while(x < xe); -#else - do { - y = *x - borrow; - borrow = (y & 0x10000) >> 16; - *x++ = y & 0xffff; - } while(borrow && x < xe); -#endif + *x++ = 0xffffffffL; } + while(x < xe); +} static int all_on(Bigint *b, int n) { ULong *x, *xe; - x = b->x; xe = x + (n >> kshift); while(x < xe) @@ -124,19 +92,18 @@ all_on(Bigint *b, int n) if (n &= kmask) return ((*x | (ALL_ON << n)) & ALL_ON) == ALL_ON; return 1; - } +} Bigint * -set_ones(Bigint *b, int n MTd) +set_ones(Bigint *b, int n) { int k; ULong *x, *xe; - k = (n + ((1 << kshift) - 1)) >> kshift; if (b->k < k) { - Bfree(b MTa); - b = Balloc(k MTa); - } + Bfree(b); + b = Balloc(k); + } k = n >> kshift; if (n &= kmask) k++; @@ -148,44 +115,33 @@ set_ones(Bigint *b, int n MTd) if (n) x[-1] >>= ULbits - n; return b; - } +} static int -rvOK(U *d, CONST FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv MTd) +rvOK(U *d, const FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv) { Bigint *b; ULong carry, inex, lostbits; int bdif, e, j, k, k1, nb, rv; - carry = rv = 0; - b = d2b(dval(d), &e, &bdif MTa); + b = d2b(dval(d), &e, &bdif); bdif -= nb = fpi->nbits; e += bdif; if (bdif <= 0) { if (exact) goto trunc; goto ret; - } + } if (P == nb) { - if ( -#ifndef IMPRECISE_INEXACT - exact && -#endif - fpi->rounding == -#ifdef RND_PRODQUOT - FPI_Round_near -#else - Flt_Rounds -#endif - ) goto trunc; + if (exact && fpi->rounding == FLT_ROUNDS) goto trunc; goto ret; - } + } switch(rd) { - case 1: /* round down (toward -Infinity) */ + case 1: /* round down (toward -Infinity) */ goto trunc; - case 2: /* round up (toward +Infinity) */ + case 2: /* round up (toward +Infinity) */ break; - default: /* round near */ + default: /* round near */ k = bdif - 1; if (k < 0) goto trunc; @@ -195,14 +151,14 @@ rvOK(U *d, CONST FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv M if (b->x[0] & 2) break; goto trunc; - } + } if (b->x[k>>kshift] & ((ULong)1 << (k & kmask))) break; goto trunc; - } + } /* "break" cases: round up 1 bit, then truncate; bdif > 0 */ carry = 1; - trunc: +trunc: inex = lostbits = 0; if (bdif > 0) { if ( (lostbits = any_on(b, bdif)) !=0) @@ -210,7 +166,7 @@ rvOK(U *d, CONST FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv M rshift(b, bdif); if (carry) { inex = STRTOG_Inexhi; - b = increment(b MTa); + b = increment(b); if ( (j = nb & kmask) !=0) j = ULbits - j; if (hi0bits(b->x[b->wds - 1]) != j) { @@ -218,18 +174,18 @@ rvOK(U *d, CONST FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv M lostbits = b->x[0] & 1; rshift(b, 1); e++; - } } } + } else if (bdif < 0) - b = lshift(b, -bdif MTa); + b = lshift(b, -bdif); if (e < fpi->emin) { k = fpi->emin - e; e = fpi->emin; if (k > nb || fpi->sudden_underflow) { b->wds = inex = 0; *irv = STRTOG_Underflow | STRTOG_Inexlo; - } + } else { k1 = k - 1; if (k1 > 0 && !lostbits) @@ -237,92 +193,56 @@ rvOK(U *d, CONST FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv M if (!lostbits && !exact) goto ret; lostbits |= - carry = b->x[k1>>kshift] & (1 << (k1 & kmask)); + carry = b->x[k1>>kshift] & (1 << (k1 & kmask)); rshift(b, k); *irv = STRTOG_Denormal; if (carry) { - b = increment(b MTa); + b = increment(b); inex = STRTOG_Inexhi | STRTOG_Underflow; - } + } else if (lostbits) inex = STRTOG_Inexlo | STRTOG_Underflow; - } } + } else if (e > fpi->emax) { e = fpi->emax + 1; *irv = STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; -#ifndef NO_ERRNO errno = ERANGE; -#endif b->wds = inex = 0; - } + } *exp = e; copybits(bits, nb, b); *irv |= inex; rv = 1; - ret: - Bfree(b MTa); +ret: + Bfree(b); return rv; - } +} static int mantbits(U *d) { ULong L; -#ifdef VAX - L = word1(d) << 16 | word1(d) >> 16; - if (L) -#else if ( (L = word1(d)) !=0) -#endif return P - lo0bits(&L); -#ifdef VAX - L = word0(d) << 16 | word0(d) >> 16 | Exp_msk11; -#else L = word0(d) | Exp_msk1; -#endif return P - 32 - lo0bits(&L); - } +} int -strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) +strtodg(const char *s00, char **se, const FPI *fpi, Long *exp, ULong *bits) { int abe, abits, asub; int bb0, bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, denorm; int dsign, e, e1, e2, emin, esign, finished, i, inex, irv, j, k; int nbits, nd, nd0, nf, nz, nz0, rd, rvbits, rve, rve1, sign; int sudden_underflow; - CONST char *s, *s0, *s1; + const char *s, *s0, *s1; double adj0, tol; Long L; U adj, rv; ULong *b, *be, y, z; Bigint *ab, *bb, *bb1, *bd, *bd0, *bs, *delta, *rvb, *rvb0; -#ifdef MULTIPLE_THREADS - ThInfo *TI = 0; -#endif -#ifdef USE_LOCALE /*{{*/ -#ifdef NO_LOCALE_CACHE - char *decimalpoint = localeconv()->decimal_point; - int dplen = strlen(decimalpoint); -#else - char *decimalpoint; - static char *decimalpoint_cache; - static int dplen; - if (!(s0 = decimalpoint_cache)) { - s0 = localeconv()->decimal_point; - if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) { - strcpy(decimalpoint_cache, s0); - s0 = decimalpoint_cache; - } - dplen = strlen(s0); - } - decimalpoint = (char*)s0; -#endif /*NO_LOCALE_CACHE*/ -#else /*USE_LOCALE}{*/ -#define dplen 1 -#endif /*USE_LOCALE}}*/ - irv = STRTOG_Zero; denorm = sign = nz0 = nz = 0; dval(&rv) = 0.; @@ -351,25 +271,23 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) default: goto break2; } - break2: +break2: if (*s == '0') { -#ifndef NO_HEX_FP switch(s[1]) { - case 'x': - case 'X': - irv = gethex(&s, fpi, exp, &rvb, sign MTb); + case 'x': + case 'X': + irv = gethex(&s, fpi, exp, &rvb, sign); if (irv == STRTOG_NoNumber) { s = s00; sign = 0; - } + } goto ret; - } -#endif + } nz0 = 1; while(*++s == '0') ; if (!*s) goto ret; - } + } sudden_underflow = fpi->sudden_underflow; s0 = s; y = z = 0; @@ -379,17 +297,8 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) else if (nd < DBL_DIG + 2) z = 10*z + c - '0'; nd0 = nd; -#ifdef USE_LOCALE - if (c == *decimalpoint) { - for(i = 1; decimalpoint[i]; ++i) - if (s[i] != decimalpoint[i]) - goto dig_done; - s += i; - c = *s; -#else if (c == '.') { c = *++s; -#endif decpt = 1; if (!nd) { for(; c == '0'; c = *++s) @@ -399,11 +308,11 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) nf += nz; nz = 0; goto have_dig; - } - goto dig_done; } + goto dig_done; + } for(; c >= '0' && c <= '9'; c = *++s) { - have_dig: + have_dig: nz++; if (c -= '0') { nf += nz; @@ -417,25 +326,25 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) else if (nd <= DBL_DIG + 2) z = 10*z + c; nz = 0; - } } - }/*}*/ - dig_done: + } + }/*}*/ +dig_done: e = 0; if (c == 'e' || c == 'E') { if (!nd && !nz && !nz0) { irv = STRTOG_NoNumber; s = s00; goto ret; - } + } s00 = s; esign = 0; switch(c = *++s) { - case '-': - esign = 1; - case '+': - c = *++s; - } + case '-': + esign = 1; + case '+': + c = *++s; + } if (c >= '0' && c <= '9') { while(c == '0') c = *++s; @@ -453,67 +362,60 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) e = (int)L; if (esign) e = -e; - } + } else e = 0; - } + } else s = s00; - } + } if (!nd) { if (!nz && !nz0) { -#ifdef INFNAN_CHECK /* Check for Nan and Infinity */ if (!decpt) - switch(c) { - case 'i': - case 'I': - if (match(&s,"nf")) { - --s; - if (!match(&s,"inity")) - ++s; - irv = STRTOG_Infinite; - goto infnanexp; + switch(c) { + case 'i': + case 'I': + if (match(&s,"nf")) { + --s; + if (!match(&s,"inity")) + ++s; + irv = STRTOG_Infinite; + goto infnanexp; } - break; - case 'n': - case 'N': - if (match(&s, "an")) { - irv = STRTOG_NaN; - *exp = fpi->emax + 1; -#ifndef No_Hex_NaN - if (*s == '(') /*)*/ - irv = hexnan(&s, fpi, bits); -#endif - goto infnanexp; + break; + case 'n': + case 'N': + if (match(&s, "an")) { + irv = STRTOG_NaN; + *exp = fpi->emax + 1; + if (*s == '(') + irv = hexnan(&s, fpi, bits); + goto infnanexp; } - } -#endif /* INFNAN_CHECK */ + } irv = STRTOG_NoNumber; s = s00; - } - goto ret; } - + goto ret; + } irv = STRTOG_Normal; e1 = e -= nf; rd = 0; switch(fpi->rounding & 3) { - case FPI_Round_up: + case FPI_Round_up: rd = 2 - sign; break; - case FPI_Round_zero: + case FPI_Round_zero: rd = 1; break; - case FPI_Round_down: + case FPI_Round_down: rd = 1 + sign; - } - + } /* Now we have nd0 digits, starting at s0, followed by a * decimal point, followed by nd-nd0 digits. The number we're * after is the integer represented by those digits times * 10**e */ - if (!nd0) nd0 = nd; k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2; @@ -523,22 +425,18 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) bd0 = 0; if (nbits <= P && nd <= DBL_DIG) { if (!e) { - if (rvOK(&rv, fpi, exp, bits, 1, rd, &irv MTb)) + if (rvOK(&rv, fpi, exp, bits, 1, rd, &irv)) goto ret; - } + } else if (e > 0) { if (e <= Ten_pmax) { -#ifdef VAX - goto vax_ovfl_check; -#else i = fivesbits[e] + mantbits(&rv) <= P; /* rv = */ rounded_product(dval(&rv), tens[e]); - if (rvOK(&rv, fpi, exp, bits, i, rd, &irv MTb)) + if (rvOK(&rv, fpi, exp, bits, i, rd, &irv)) goto ret; e1 -= e; goto rv_notOK; -#endif - } + } i = DBL_DIG - nd; if (e <= Ten_pmax + i) { /* A fancier test would sometimes let us do @@ -547,41 +445,22 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) e2 = e - i; e1 -= i; dval(&rv) *= tens[i]; -#ifdef VAX - /* VAX exponent range is so narrow we must - * worry about overflow here... - */ - vax_ovfl_check: - dval(&adj) = dval(&rv); - word0(&adj) -= P*Exp_msk1; - /* adj = */ rounded_product(dval(&adj), tens[e2]); - if ((word0(&adj) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) - goto rv_notOK; - word0(&adj) += P*Exp_msk1; - dval(&rv) = dval(&adj); -#else /* rv = */ rounded_product(dval(&rv), tens[e2]); -#endif - if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv MTb)) + if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv)) goto ret; e1 -= e2; - } } -#ifndef Inaccurate_Divide + } else if (e >= -Ten_pmax) { /* rv = */ rounded_quotient(dval(&rv), tens[-e]); - if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv MTb)) + if (rvOK(&rv, fpi, exp, bits, 0, rd, &irv)) goto ret; e1 -= e; - } -#endif } - rv_notOK: + } +rv_notOK: e1 += nd - k; - /* Get starting approximation = rv * 10**e1 */ - e2 = 0; if (e1 > 0) { if ( (i = e1 & 15) !=0) @@ -590,20 +469,20 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) e1 >>= 4; while(e1 >= (1 << (n_bigtens-1))) { e2 += ((word0(&rv) & Exp_mask) - >> Exp_shift1) - Bias; + >> Exp_shift1) - Bias; word0(&rv) &= ~Exp_mask; word0(&rv) |= Bias << Exp_shift1; dval(&rv) *= bigtens[n_bigtens-1]; e1 -= 1 << (n_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]; - } } + } else if (e1 < 0) { e1 = -e1; if ( (i = e1 & 15) !=0) @@ -612,35 +491,27 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) e1 >>= 4; while(e1 >= (1 << (n_bigtens-1))) { e2 += ((word0(&rv) & Exp_mask) - >> Exp_shift1) - Bias; + >> Exp_shift1) - Bias; word0(&rv) &= ~Exp_mask; word0(&rv) |= Bias << Exp_shift1; dval(&rv) *= tinytens[n_bigtens-1]; e1 -= 1 << (n_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]; - } } -#ifdef IBM - /* e2 is a correction to the (base 2) exponent of the return - * value, reflecting adjustments above to avoid overflow in the - * native arithmetic. For native IBM (base 16) arithmetic, we - * must multiply e2 by 4 to change from base 16 to 2. - */ - e2 <<= 2; -#endif - rvb = d2b(dval(&rv), &rve, &rvbits MTb); /* rv = rvb * 2^rve */ + } + rvb = d2b(dval(&rv), &rve, &rvbits); /* rv = rvb * 2^rve */ rve += e2; if ((j = rvbits - nbits) > 0) { rshift(rvb, j); rvbits = nbits; rve += j; - } + } bb0 = 0; /* trailing zero bits in rvb */ e2 = rve + rvbits - nbits; if (e2 > fpi->emax + 1) @@ -650,64 +521,59 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) denorm = 1; j = rve - emin; if (j > 0) { - rvb = lshift(rvb, j MTb); + rvb = lshift(rvb, j); rvbits += j; - } + } else if (j < 0) { rvbits += j; if (rvbits <= 0) { if (rvbits < -1) { - ufl: + ufl: rvb->wds = 0; rvb->x[0] = 0; switch(fpi->rounding) { - case FPI_Round_up: + case FPI_Round_up: if (!sign) goto ret_tiny; break; - case FPI_Round_down: + case FPI_Round_down: if (sign) { - ret_tiny: + ret_tiny: rvb->wds = rvb->x[0] = 1; - } - } + } + } *exp = emin; irv = STRTOG_Underflow | STRTOG_Inexlo; goto ret; - } - rvb->x[0] = rvb->wds = rvbits = 1; } + rvb->x[0] = rvb->wds = rvbits = 1; + } else rshift(rvb, -j); - } + } rve = rve1 = emin; if (sudden_underflow && e2 + 1 < emin) goto ufl; - } - + } /* 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 MTb); - + bd0 = s2b(s0, nd0, nd, y, 1); for(;;) { - bd = Balloc(bd0->k MTb); + bd = Balloc(bd0->k); Bcopy(bd, bd0); - bb = Balloc(rvb->k MTb); + bb = Balloc(rvb->k); Bcopy(bb, rvb); bbbits = rvbits - bb0; bbe = rve + bb0; - bs = i2b(1 MTb); - + bs = i2b(1); if (e >= 0) { bb2 = bb5 = 0; bd2 = bd5 = e; - } + } else { bb2 = bb5 = -e; bd2 = bd5 = 0; - } + } if (bbe >= 0) bb2 += bbe; else @@ -726,27 +592,27 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) bb2 -= i; bd2 -= i; bs2 -= i; - } + } if (bb5 > 0) { - bs = pow5mult(bs, bb5 MTb); - bb1 = mult(bs, bb MTb); - Bfree(bb MTb); + bs = pow5mult(bs, bb5); + bb1 = mult(bs, bb); + Bfree(bb); bb = bb1; - } + } bb2 -= bb0; if (bb2 > 0) - bb = lshift(bb, bb2 MTb); + bb = lshift(bb, bb2); else if (bb2 < 0) rshift(bb, -bb2); if (bd5 > 0) - bd = pow5mult(bd, bd5 MTb); + bd = pow5mult(bd, bd5); if (bd2 > 0) - bd = lshift(bd, bd2 MTb); + bd = lshift(bd, bd2); if (bs2 > 0) - bs = lshift(bs, bs2 MTb); + bs = lshift(bs, bs2); asub = 1; inex = STRTOG_Inexhi; - delta = diff(bb, bd MTb); + delta = diff(bb, bd); if (delta->wds <= 1 && !delta->x[0]) break; dsign = delta->sign; @@ -759,24 +625,24 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) if (dsign != 0) { irv |= STRTOG_Inexhi; goto adj1; - } + } irv |= STRTOG_Inexlo; if (rve1 == emin) goto adj1; for(i = 0, j = nbits; j >= ULbits; - i++, j -= ULbits) { + i++, j -= ULbits) { if (rvb->x[i] & ALL_ON) goto adj1; - } + } if (j > 1 && lo0bits(rvb->x + i) < j - 1) goto adj1; rve = rve1 - 1; - rvb = set_ones(rvb, rvbits = nbits MTb); + rvb = 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 * special case of mantissa a power of two. @@ -786,13 +652,13 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) : STRTOG_Normal | STRTOG_Inexhi; if (dsign || bbbits > 1 || denorm || rve1 == emin) break; - delta = lshift(delta,1 MTb); + delta = lshift(delta,1); if (cmp(delta, bs) > 0) { irv = STRTOG_Normal | STRTOG_Inexlo; goto drop_down; - } - break; } + break; + } if (i == 0) { /* exactly half-way between */ if (dsign) { @@ -804,51 +670,51 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) irv = STRTOG_Normal | STRTOG_Inexhi; denorm = 0; break; - } - irv = STRTOG_Normal | STRTOG_Inexlo; } + irv = STRTOG_Normal | STRTOG_Inexlo; + } else if (bbbits == 1) { irv = STRTOG_Normal; - drop_down: + drop_down: /* boundary case -- decrement exponent */ if (rve1 == emin) { irv = STRTOG_Normal | STRTOG_Inexhi; if (rvb->wds == 1 && rvb->x[0] == 1) sudden_underflow = 1; break; - } - rve -= nbits; - rvb = set_ones(rvb, rvbits = nbits MTb); - break; } + rve -= nbits; + rvb = set_ones(rvb, rvbits = nbits); + break; + } else irv = STRTOG_Normal | STRTOG_Inexhi; if ((bbbits < nbits && !denorm) || !(rvb->x[0] & 1)) break; if (dsign) { - rvb = increment(rvb MTb); + rvb = increment(rvb); j = kmask & (ULbits - (rvbits & kmask)); if (hi0bits(rvb->x[rvb->wds - 1]) != j) rvbits++; irv = STRTOG_Normal | STRTOG_Inexhi; - } + } else { if (bbbits == 1) goto undfl; decrement(rvb); irv = STRTOG_Normal | STRTOG_Inexlo; - } - break; } + break; + } if ((dval(&adj) = ratio(delta, bs)) <= 2.) { - adj1: + adj1: inex = STRTOG_Inexlo; if (dsign) { asub = 0; inex = STRTOG_Inexhi; - } + } else if (denorm && bbbits <= 1) { - undfl: + undfl: rvb->wds = 0; rve = emin; irv = STRTOG_Underflow | STRTOG_Inexlo; @@ -856,100 +722,97 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) rvb->wds = 1; rvb->x[0] = 1; irv = STRTOG_Underflow | STRTOG_Inexhi; - } - break; } - adj0 = dval(&adj) = 1.; + break; } + adj0 = dval(&adj) = 1.; + } else { adj0 = dval(&adj) *= 0.5; if (dsign) { asub = 0; inex = STRTOG_Inexlo; - } + } if (dval(&adj) < 2147483647.) { L = adj0; adj0 -= L; switch(rd) { - case 0: + case 0: if (adj0 >= .5) goto inc_L; break; - case 1: + case 1: if (asub && adj0 > 0.) goto inc_L; break; - case 2: + case 2: if (!asub && adj0 > 0.) { - inc_L: + inc_L: L++; inex = STRTOG_Inexact - inex; - } - } - dval(&adj) = L; + } } + dval(&adj) = L; } + } y = rve + rvbits; - /* adj *= ulp(dval(&rv)); */ /* if (asub) rv -= adj; else rv += adj; */ - if (!denorm && rvbits < nbits) { - rvb = lshift(rvb, j = nbits - rvbits MTb); + rvb = lshift(rvb, j = nbits - rvbits); rve -= j; rvbits = nbits; - } - ab = d2b(dval(&adj), &abe, &abits MTb); + } + ab = d2b(dval(&adj), &abe, &abits); if (abe < 0) rshift(ab, -abe); else if (abe > 0) - ab = lshift(ab, abe MTb); + ab = lshift(ab, abe); rvb0 = rvb; if (asub) { /* rv -= adj; */ j = hi0bits(rvb->x[rvb->wds-1]); - rvb = diff(rvb, ab MTb); + rvb = diff(rvb, ab); k = rvb0->wds - 1; if (denorm) /* do nothing */; else if (rvb->wds <= k - || hi0bits( rvb->x[k]) > - hi0bits(rvb0->x[k])) { + || hi0bits( rvb->x[k]) > + hi0bits(rvb0->x[k])) { /* unlikely; can only have lost 1 high bit */ if (rve1 == emin) { --rvbits; denorm = 1; - } + } else { - rvb = lshift(rvb, 1 MTb); + rvb = lshift(rvb, 1); --rve; --rve1; L = finished = 0; - } } } + } else { - rvb = sum(rvb, ab MTb); + rvb = sum(rvb, ab); k = rvb->wds - 1; if (k >= rvb0->wds - || hi0bits(rvb->x[k]) < hi0bits(rvb0->x[k])) { + || hi0bits(rvb->x[k]) < hi0bits(rvb0->x[k])) { if (denorm) { if (++rvbits == nbits) denorm = 0; - } + } else { rshift(rvb, 1); rve++; rve1++; L = 0; - } } } - Bfree(ab MTb); - Bfree(rvb0 MTb); + } + Bfree(ab); + Bfree(rvb0); if (finished) break; - z = rve + rvbits; if (y == z && L) { /* Can we stop now? */ @@ -959,58 +822,56 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) if (adj0 > tol) { irv |= inex; break; - } } + } else if (dval(&adj) > tol && adj0 < 1. - tol) { irv |= inex; break; - } } - bb0 = denorm ? 0 : trailz(rvb); - Bfree(bb MTb); - Bfree(bd MTb); - Bfree(bs MTb); - Bfree(delta MTb); } + bb0 = denorm ? 0 : trailz(rvb); + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(delta); + } if (!denorm && (j = nbits - rvbits)) { if (j > 0) - rvb = lshift(rvb, j MTb); + rvb = lshift(rvb, j); else rshift(rvb, -j); rve -= j; - } + } *exp = rve; - Bfree(bb MTb); - Bfree(bd MTb); - Bfree(bs MTb); - Bfree(bd0 MTb); - Bfree(delta MTb); + Bfree(bb); + Bfree(bd); + Bfree(bs); + Bfree(bd0); + Bfree(delta); if (rve > fpi->emax) { - huge: - Bfree(rvb MTb); + huge: + Bfree(rvb); rvb = 0; -#ifndef NO_ERRNO errno = ERANGE; -#endif switch(fpi->rounding & 3) { - case FPI_Round_up: + case FPI_Round_up: if (!sign) goto ret_inf; break; - case FPI_Round_down: + case FPI_Round_down: if (!sign) break; - case FPI_Round_near: - ret_inf: + case FPI_Round_near: + ret_inf: irv = STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; k = nbits >> kshift; if (nbits & kmask) ++k; memset(bits, 0, k*sizeof(ULong)); - infnanexp: + infnanexp: *exp = fpi->emax + 1; goto ret; - } + } /* Round to largest representable magnitude */ irv = STRTOG_Normal | STRTOG_Inexlo; *exp = fpi->emax; @@ -1020,34 +881,30 @@ strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *exp, ULong *bits) *b++ = -1; if ((j = fpi->nbits & 0x1f)) *--be >>= (32 - j); - } - ret: + } +ret: if (denorm) { if (sudden_underflow) { rvb->wds = 0; irv = STRTOG_Underflow | STRTOG_Inexlo; -#ifndef NO_ERRNO errno = ERANGE; -#endif - } + } else { irv = (irv & ~STRTOG_Retmask) | (rvb->wds > 0 ? STRTOG_Denormal : STRTOG_Zero); if (irv & STRTOG_Inexact) { irv |= STRTOG_Underflow; -#ifndef NO_ERRNO errno = ERANGE; -#endif - } } } + } if (se) *se = (char *)s; if (sign) irv |= STRTOG_Neg; if (rvb) { copybits(bits, nbits, rvb); - Bfree(rvb MTb); - } - return irv; + Bfree(rvb); } + return irv; +} diff --git a/third_party/gdtoa/strtodnrp.c b/third_party/gdtoa/strtodnrp.c index 2b020e161..8fc40d469 100644 --- a/third_party/gdtoa/strtodnrp.c +++ b/third_party/gdtoa/strtodnrp.c @@ -1,84 +1,76 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 2004 by David M. Gay. -All Rights Reserved -Based on material in the rest of /netlib/fp/gdota.tar.gz, -which is copyright (C) 1998, 2000 by Lucent Technologies. - -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. - -****************************************************************/ /* This is a variant of strtod that works on Intel ia32 systems */ /* with the default extended-precision arithmetic -- it does not */ /* require setting the precision control to 53 bits. */ -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - double -strtod(CONST char *s, char **sp) +strtod(const char *s, char **sp) { static const FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ }; ULong bits[2]; Long exp; int k; union { ULong L[2]; double d; } u; - k = strtodg(s, sp, &fpi, &exp, bits); switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: u.L[0] = u.L[1] = 0; break; - - case STRTOG_Normal: - u.L[_1] = bits[0]; - u.L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); + case STRTOG_Normal: + u.L[0] = bits[0]; + u.L[1] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); break; - - case STRTOG_Denormal: - u.L[_1] = bits[0]; - u.L[_0] = bits[1]; + case STRTOG_Denormal: + u.L[0] = bits[0]; + u.L[1] = bits[1]; break; - - case STRTOG_Infinite: - u.L[_0] = 0x7ff00000; - u.L[_1] = 0; + case STRTOG_Infinite: + u.L[1] = 0x7ff00000; + u.L[0] = 0; break; - - case STRTOG_NaN: + case STRTOG_NaN: u.L[0] = d_QNAN0; u.L[1] = d_QNAN1; break; - - case STRTOG_NaNbits: - u.L[_0] = 0x7ff00000 | bits[1]; - u.L[_1] = bits[0]; - } - if (k & STRTOG_Neg) - u.L[_0] |= 0x80000000L; - return u.d; + case STRTOG_NaNbits: + u.L[1] = 0x7ff00000 | bits[1]; + u.L[0] = bits[0]; } + if (k & STRTOG_Neg) + u.L[1] |= 0x80000000L; + return u.d; +} diff --git a/third_party/gdtoa/strtof.c b/third_party/gdtoa/strtof.c index 4c95df4d6..3fb2f2518 100644 --- a/third_party/gdtoa/strtof.c +++ b/third_party/gdtoa/strtof.c @@ -1,76 +1,67 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ float -strtof(CONST char *s, char **sp) +strtof(const char *s, char **sp) { static FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI, 0 /*unused*/ }; ULong bits[1]; Long exp; int k; union { ULong L[1]; float f; } u; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - k = strtodg(s, sp, fpi, &exp, bits); switch(k & STRTOG_Retmask) { - default: /* unused */ - case STRTOG_NoNumber: - case STRTOG_Zero: + default: /* unused */ + case STRTOG_NoNumber: + case STRTOG_Zero: u.L[0] = 0; break; - - case STRTOG_Normal: - case STRTOG_NaNbits: + case STRTOG_Normal: + case STRTOG_NaNbits: u.L[0] = (bits[0] & 0x7fffff) | ((exp + 0x7f + 23) << 23); break; - - case STRTOG_Denormal: + case STRTOG_Denormal: u.L[0] = bits[0]; break; - - case STRTOG_Infinite: + case STRTOG_Infinite: u.L[0] = 0x7f800000; break; - - case STRTOG_NaN: + case STRTOG_NaN: u.L[0] = f_QNAN; - } + } if (k & STRTOG_Neg) u.L[0] |= 0x80000000L; return u.f; - } +} diff --git a/third_party/gdtoa/strtold.c b/third_party/gdtoa/strtold.c index edb431e00..e9abe0f31 100644 --- a/third_party/gdtoa/strtold.c +++ b/third_party/gdtoa/strtold.c @@ -1,12 +1,34 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.h" /** diff --git a/third_party/gdtoa/strtopd.c b/third_party/gdtoa/strtopd.c index 6a4741faa..56d2af3d5 100644 --- a/third_party/gdtoa/strtopd.c +++ b/third_party/gdtoa/strtopd.c @@ -1,51 +1,46 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtopd(CONST char *s, char **sp, double *d) +strtopd(const char *s, char **sp, double *d) { static const FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ }; ULong bits[2]; Long exp; int k; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - k = strtodg(s, sp, fpi, &exp, bits); ULtod((ULong*)d, bits, exp, k); return k; - } +} diff --git a/third_party/gdtoa/strtopdd.c b/third_party/gdtoa/strtopdd.c index 6a24a9e00..e5c9661e0 100644 --- a/third_party/gdtoa/strtopdd.c +++ b/third_party/gdtoa/strtopdd.c @@ -1,111 +1,100 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtopdd(CONST char *s, char **sp, double *dd) +strtopdd(const char *s, char **sp, double *dd) { -#ifdef Sudden_Underflow - static const FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1, 0 /*unused*/ }; -#else static const FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0, 0 /*unused*/ }; -#endif ULong bits[4]; Long exp; int i, j, rv; typedef union { double d[2]; ULong L[4]; - } U; + } U; U *u; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - rv = strtodg(s, sp, fpi, &exp, bits); u = (U*)dd; switch(rv & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: u->d[0] = u->d[1] = 0.; break; - - case STRTOG_Normal: - u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL; - u->L[_0] = (bits[2] >> 21) | ((bits[3] << 11) & 0xfffff) - | ((exp + 0x3ff + 105) << 20); + case STRTOG_Normal: + u->L[0] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL; + u->L[1] = (bits[2] >> 21) | ((bits[3] << 11) & 0xfffff) + | ((exp + 0x3ff + 105) << 20); exp += 0x3ff + 52; if (bits[1] &= 0x1fffff) { i = hi0bits(bits[1]) - 11; if (i >= exp) { i = exp - 1; exp = 0; - } + } else exp -= i; if (i > 0) { bits[1] = bits[1] << i | bits[0] >> (32-i); bits[0] = bits[0] << i & 0xffffffffL; - } } + } else if (bits[0]) { i = hi0bits(bits[0]) + 21; if (i >= exp) { i = exp - 1; exp = 0; - } + } else exp -= i; if (i < 32) { bits[1] = bits[0] >> (32 - i); bits[0] = bits[0] << i & 0xffffffffL; - } + } else { bits[1] = bits[0] << (i - 32); bits[0] = 0; - } } + } else { u->L[2] = u->L[3] = 0; break; - } - u->L[2+_1] = bits[0]; - u->L[2+_0] = (bits[1] & 0xfffff) | (exp << 20); + } + u->L[2+0] = bits[0]; + u->L[2+1] = (bits[1] & 0xfffff) | (exp << 20); break; - - case STRTOG_Denormal: + case STRTOG_Denormal: if (bits[3]) goto nearly_normal; if (bits[2]) @@ -114,67 +103,62 @@ strtopdd(CONST char *s, char **sp, double *dd) goto hardly_normal; /* completely denormal */ u->L[2] = u->L[3] = 0; - u->L[_1] = bits[0]; - u->L[_0] = bits[1]; + u->L[0] = bits[0]; + u->L[1] = bits[1]; break; - - nearly_normal: + nearly_normal: i = hi0bits(bits[3]) - 11; /* i >= 12 */ j = 32 - i; - u->L[_0] = ((bits[3] << i | bits[2] >> j) & 0xfffff) + u->L[1] = ((bits[3] << i | bits[2] >> j) & 0xfffff) | ((65 - i) << 20); - u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; - u->L[2+_0] = bits[1] & ((1L << j) - 1); - u->L[2+_1] = bits[0]; + u->L[0] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + u->L[2+1] = bits[1] & ((1L << j) - 1); + u->L[2+0] = bits[0]; break; - - partly_normal: + partly_normal: i = hi0bits(bits[2]) - 11; if (i < 0) { j = -i; i += 32; - u->L[_0] = (bits[2] >> j & 0xfffff) | (33 + j) << 20; - u->L[_1] = ((bits[2] << i) | (bits[1] >> j)) & 0xffffffffL; - u->L[2+_0] = bits[1] & ((1L << j) - 1); - u->L[2+_1] = bits[0]; + u->L[1] = (bits[2] >> j & 0xfffff) | (33 + j) << 20; + u->L[0] = ((bits[2] << i) | (bits[1] >> j)) & 0xffffffffL; + u->L[2+1] = bits[1] & ((1L << j) - 1); + u->L[2+0] = bits[0]; break; - } + } if (i == 0) { - u->L[_0] = (bits[2] & 0xfffff) | (33 << 20); - u->L[_1] = bits[1]; - u->L[2+_0] = 0; - u->L[2+_1] = bits[0]; + u->L[1] = (bits[2] & 0xfffff) | (33 << 20); + u->L[0] = bits[1]; + u->L[2+1] = 0; + u->L[2+0] = bits[0]; break; - } + } j = 32 - i; - u->L[_0] = (((bits[2] << i) | (bits[1] >> j)) & 0xfffff) - | ((j + 1) << 20); - u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; - u->L[2+_0] = 0; - u->L[2+_1] = bits[0] & ((1L << j) - 1); + u->L[1] = (((bits[2] << i) | (bits[1] >> j)) & 0xfffff) + | ((j + 1) << 20); + u->L[0] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + u->L[2+1] = 0; + u->L[2+0] = bits[0] & ((1L << j) - 1); break; - - hardly_normal: + hardly_normal: j = 11 - hi0bits(bits[1]); i = 32 - j; - u->L[_0] = (bits[1] >> j & 0xfffff) | ((j + 1) << 20); - u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; - u->L[2+_0] = 0; - u->L[2+_1] = bits[0] & ((1L << j) - 1); + u->L[1] = (bits[1] >> j & 0xfffff) | ((j + 1) << 20); + u->L[0] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + u->L[2+1] = 0; + u->L[2+0] = bits[0] & ((1L << j) - 1); break; - - case STRTOG_Infinite: - u->L[_0] = u->L[2+_0] = 0x7ff00000; - u->L[_1] = u->L[2+_1] = 0; + case STRTOG_Infinite: + u->L[1] = u->L[2+1] = 0x7ff00000; + u->L[0] = u->L[2+0] = 0; break; - - case STRTOG_NaN: + case STRTOG_NaN: u->L[0] = u->L[2] = d_QNAN0; u->L[1] = u->L[3] = d_QNAN1; - } - if (rv & STRTOG_Neg) { - u->L[ _0] |= 0x80000000L; - u->L[2+_0] |= 0x80000000L; - } - return rv; } + if (rv & STRTOG_Neg) { + u->L[ 1] |= 0x80000000L; + u->L[2+1] |= 0x80000000L; + } + return rv; +} diff --git a/third_party/gdtoa/strtopf.c b/third_party/gdtoa/strtopf.c index 8cf73acfe..40e6c73c9 100644 --- a/third_party/gdtoa/strtopf.c +++ b/third_party/gdtoa/strtopf.c @@ -1,75 +1,66 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ int -strtopf(CONST char *s, char **sp, float *f) +strtopf(const char *s, char **sp, float *f) { - static const FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI, 0 /*unused*/ }; + static const FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI, 0 /*unused*/ }; ULong bits[1], *L; Long exp; int k; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - k = strtodg(s, sp, fpi, &exp, bits); L = (ULong*)f; switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: L[0] = 0; break; - - case STRTOG_Normal: - case STRTOG_NaNbits: + case STRTOG_Normal: + case STRTOG_NaNbits: L[0] = (bits[0] & 0x7fffff) | ((exp + 0x7f + 23) << 23); break; - - case STRTOG_Denormal: + case STRTOG_Denormal: L[0] = bits[0]; break; - - case STRTOG_Infinite: + case STRTOG_Infinite: L[0] = 0x7f800000; break; - - case STRTOG_NaN: + case STRTOG_NaN: L[0] = f_QNAN; - } + } if (k & STRTOG_Neg) L[0] |= 0x80000000L; return k; - } +} diff --git a/third_party/gdtoa/strtopx.c b/third_party/gdtoa/strtopx.c index f1c594745..664720a01 100644 --- a/third_party/gdtoa/strtopx.c +++ b/third_party/gdtoa/strtopx.c @@ -1,108 +1,79 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - - extern UShort __gdtoa_NanDflt_ldus[5]; - -#undef _0 -#undef _1 - -/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ - -#ifdef IEEE_MC68k -#define _0 0 -#define _1 1 -#define _2 2 -#define _3 3 -#define _4 4 -#endif -#ifdef IEEE_8087 -#define _0 4 -#define _1 3 -#define _2 2 -#define _3 1 -#define _4 0 -#endif +extern UShort __gdtoa_NanDflt_ldus[5]; int -strtopx(CONST char *s, char **sp, void *V) +strtopx(const char *s, char **sp, void *V) { const static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI, 0 /*unused*/ }; ULong bits[2]; Long exp; int k; UShort *L = (UShort*)V; -#ifdef Honor_FLT_ROUNDS #include "third_party/gdtoa/gdtoa_fltrnds.inc" -#else -#define fpi &fpi0 -#endif - k = strtodg(s, sp, fpi, &exp, bits); switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: L[0] = L[1] = L[2] = L[3] = L[4] = 0; break; - - case STRTOG_Denormal: - L[_0] = 0; + case STRTOG_Denormal: + L[4] = 0; goto normal_bits; - - case STRTOG_Normal: - case STRTOG_NaNbits: - L[_0] = exp + 0x3fff + 63; - normal_bits: - L[_4] = (UShort)bits[0]; - L[_3] = (UShort)(bits[0] >> 16); - L[_2] = (UShort)bits[1]; - L[_1] = (UShort)(bits[1] >> 16); + case STRTOG_Normal: + case STRTOG_NaNbits: + L[4] = exp + 0x3fff + 63; + normal_bits: + L[0] = (UShort)bits[0]; + L[1] = (UShort)(bits[0] >> 16); + L[2] = (UShort)bits[1]; + L[3] = (UShort)(bits[1] >> 16); break; - - case STRTOG_Infinite: - L[_0] = 0x7fff; - L[_1] = 0x8000; - L[_2] = L[_3] = L[_4] = 0; + case STRTOG_Infinite: + L[4] = 0x7fff; + L[3] = 0x8000; + L[2] = L[1] = L[0] = 0; break; - - case STRTOG_NaN: - L[_4] = __gdtoa_NanDflt_ldus[0]; - L[_3] = __gdtoa_NanDflt_ldus[1]; - L[_2] = __gdtoa_NanDflt_ldus[2]; - L[_1] = __gdtoa_NanDflt_ldus[3]; - L[_0] = __gdtoa_NanDflt_ldus[4]; - } - if (k & STRTOG_Neg) - L[_0] |= 0x8000; - return k; + case STRTOG_NaN: + L[0] = __gdtoa_NanDflt_ldus[0]; + L[1] = __gdtoa_NanDflt_ldus[1]; + L[2] = __gdtoa_NanDflt_ldus[2]; + L[3] = __gdtoa_NanDflt_ldus[3]; + L[4] = __gdtoa_NanDflt_ldus[4]; } + if (k & STRTOG_Neg) + L[4] |= 0x8000; + return k; +} diff --git a/third_party/gdtoa/strtord.c b/third_party/gdtoa/strtord.c index 361b44a77..02d819653 100644 --- a/third_party/gdtoa/strtord.c +++ b/third_party/gdtoa/strtord.c @@ -1,88 +1,83 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - - extern ULong __gdtoa_NanDflt_d[2]; +extern ULong __gdtoa_NanDflt_d[2]; void ULtod(ULong *L, ULong *bits, Long exp, int k) { switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: L[0] = L[1] = 0; break; - - case STRTOG_Denormal: - L[_1] = bits[0]; - L[_0] = bits[1]; + case STRTOG_Denormal: + L[0] = bits[0]; + L[1] = bits[1]; break; - - case STRTOG_Normal: - case STRTOG_NaNbits: - L[_1] = bits[0]; - L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); + case STRTOG_Normal: + case STRTOG_NaNbits: + L[0] = bits[0]; + L[1] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); break; - - case STRTOG_Infinite: - L[_0] = 0x7ff00000; - L[_1] = 0; + case STRTOG_Infinite: + L[1] = 0x7ff00000; + L[0] = 0; break; - - case STRTOG_NaN: - L[_0] = __gdtoa_NanDflt_d[1]; - L[_1] = __gdtoa_NanDflt_d[0]; - } - if (k & STRTOG_Neg) - L[_0] |= 0x80000000L; + case STRTOG_NaN: + L[1] = __gdtoa_NanDflt_d[1]; + L[0] = __gdtoa_NanDflt_d[0]; } + if (k & STRTOG_Neg) + L[1] |= 0x80000000L; +} int -strtord(CONST char *s, char **sp, int rounding, double *d) +strtord(const char *s, char **sp, int rounding, double *d) { static const FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI, 0 /*unused*/ }; FPI *fpi, fpi1; ULong bits[2]; Long exp; int k; - fpi = &fpi0; if (rounding != FPI_Round_near) { fpi1 = fpi0; fpi1.rounding = rounding; fpi = &fpi1; - } + } k = strtodg(s, sp, fpi, &exp, bits); ULtod((ULong*)d, bits, exp, k); return k; - } +} diff --git a/third_party/gdtoa/strtordd.c b/third_party/gdtoa/strtordd.c index 25a27774c..4a22335f7 100644 --- a/third_party/gdtoa/strtordd.c +++ b/third_party/gdtoa/strtordd.c @@ -1,94 +1,91 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - - extern ULong __gdtoa_NanDflt_d[2]; +extern ULong __gdtoa_NanDflt_d[2]; void ULtodd(ULong *L, ULong *bits, Long exp, int k) { int i, j; - switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: L[0] = L[1] = L[2] = L[3] = 0; break; - - case STRTOG_Normal: - L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; - L[_0] = (bits[2] >> 21) | (bits[3] << 11 & 0xfffff) - | ((exp + 0x3ff + 105) << 20); + case STRTOG_Normal: + L[0] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; + L[1] = (bits[2] >> 21) | (bits[3] << 11 & 0xfffff) + | ((exp + 0x3ff + 105) << 20); exp += 0x3ff + 52; if (bits[1] &= 0x1fffff) { i = hi0bits(bits[1]) - 11; if (i >= exp) { i = exp - 1; exp = 0; - } + } else exp -= i; if (i > 0) { bits[1] = bits[1] << i | bits[0] >> (32-i); bits[0] = bits[0] << i & (ULong)0xffffffffL; - } } + } else if (bits[0]) { i = hi0bits(bits[0]) + 21; if (i >= exp) { i = exp - 1; exp = 0; - } + } else exp -= i; if (i < 32) { bits[1] = bits[0] >> (32 - i); bits[0] = bits[0] << i & (ULong)0xffffffffL; - } + } else { bits[1] = bits[0] << (i - 32); bits[0] = 0; - } } + } else { L[2] = L[3] = 0; break; - } - L[2+_1] = bits[0]; - L[2+_0] = (bits[1] & 0xfffff) | (exp << 20); + } + L[2+0] = bits[0]; + L[2+1] = (bits[1] & 0xfffff) | (exp << 20); break; - - case STRTOG_Denormal: + case STRTOG_Denormal: if (bits[3]) goto nearly_normal; if (bits[2]) @@ -97,99 +94,88 @@ ULtodd(ULong *L, ULong *bits, Long exp, int k) goto hardly_normal; /* completely denormal */ L[2] = L[3] = 0; - L[_1] = bits[0]; - L[_0] = bits[1]; + L[0] = bits[0]; + L[1] = bits[1]; break; - - nearly_normal: + nearly_normal: i = hi0bits(bits[3]) - 11; /* i >= 12 */ j = 32 - i; - L[_0] = ((bits[3] << i | bits[2] >> j) & 0xfffff) + L[1] = ((bits[3] << i | bits[2] >> j) & 0xfffff) | ((65 - i) << 20); - L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; - L[2+_0] = bits[1] & (((ULong)1L << j) - 1); - L[2+_1] = bits[0]; + L[0] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + L[2+1] = bits[1] & (((ULong)1L << j) - 1); + L[2+0] = bits[0]; break; - - partly_normal: + partly_normal: i = hi0bits(bits[2]) - 11; if (i < 0) { j = -i; i += 32; - L[_0] = (bits[2] >> j & 0xfffff) | ((33 + j) << 20); - L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; - L[2+_0] = bits[1] & (((ULong)1L << j) - 1); - L[2+_1] = bits[0]; + L[1] = (bits[2] >> j & 0xfffff) | ((33 + j) << 20); + L[0] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; + L[2+1] = bits[1] & (((ULong)1L << j) - 1); + L[2+0] = bits[0]; break; - } + } if (i == 0) { - L[_0] = (bits[2] & 0xfffff) | (33 << 20); - L[_1] = bits[1]; - L[2+_0] = 0; - L[2+_1] = bits[0]; + L[1] = (bits[2] & 0xfffff) | (33 << 20); + L[0] = bits[1]; + L[2+1] = 0; + L[2+0] = bits[0]; break; - } + } j = 32 - i; - L[_0] = (((bits[2] << i) | (bits[1] >> j)) & 0xfffff) - | ((j + 1) << 20); - L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; - L[2+_0] = 0; - L[2+_1] = bits[0] & ((1L << j) - 1); + L[1] = (((bits[2] << i) | (bits[1] >> j)) & 0xfffff) + | ((j + 1) << 20); + L[0] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + L[2+1] = 0; + L[2+0] = bits[0] & ((1L << j) - 1); break; - - hardly_normal: + hardly_normal: j = 11 - hi0bits(bits[1]); i = 32 - j; - L[_0] = (bits[1] >> j & 0xfffff) | ((j + 1) << 20); - L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; - L[2+_0] = 0; - L[2+_1] = bits[0] & (((ULong)1L << j) - 1); + L[1] = (bits[1] >> j & 0xfffff) | ((j + 1) << 20); + L[0] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; + L[2+1] = 0; + L[2+0] = bits[0] & (((ULong)1L << j) - 1); break; - - case STRTOG_Infinite: - L[_0] = L[2+_0] = 0x7ff00000; - L[_1] = L[2+_1] = 0; + case STRTOG_Infinite: + L[1] = L[2+1] = 0x7ff00000; + L[0] = L[2+0] = 0; break; - - case STRTOG_NaN: - L[_0] = L[_0+2] = __gdtoa_NanDflt_d[1]; - L[_1] = L[_1+2] = __gdtoa_NanDflt_d[0]; + case STRTOG_NaN: + L[1] = L[1+2] = __gdtoa_NanDflt_d[1]; + L[0] = L[0+2] = __gdtoa_NanDflt_d[0]; break; - - case STRTOG_NaNbits: - L[_1] = (bits[1] >> 20 | bits[2] << 12) & (ULong)0xffffffffL; - L[_0] = bits[2] >> 20 | bits[3] << 12; - L[_0] |= (L[_1] | L[_0]) ? (ULong)0x7ff00000L : (ULong)0x7ff80000L; - L[2+_1] = bits[0] & (ULong)0xffffffffL; - L[2+_0] = bits[1] & 0xfffffL; - L[2+_0] |= (L[2+_1] | L[2+_0]) ? (ULong)0x7ff00000L : (ULong)0x7ff80000L; - } - if (k & STRTOG_Neg) { - L[_0] |= 0x80000000L; - L[2+_0] |= 0x80000000L; - } + case STRTOG_NaNbits: + L[0] = (bits[1] >> 20 | bits[2] << 12) & (ULong)0xffffffffL; + L[1] = bits[2] >> 20 | bits[3] << 12; + L[1] |= (L[0] | L[1]) ? (ULong)0x7ff00000L : (ULong)0x7ff80000L; + L[2+0] = bits[0] & (ULong)0xffffffffL; + L[2+1] = bits[1] & 0xfffffL; + L[2+1] |= (L[2+0] | L[2+1]) ? (ULong)0x7ff00000L : (ULong)0x7ff80000L; } + if (k & STRTOG_Neg) { + L[1] |= 0x80000000L; + L[2+1] |= 0x80000000L; + } +} int -strtordd(CONST char *s, char **sp, int rounding, double *dd) +strtordd(const char *s, char **sp, int rounding, double *dd) { -#ifdef Sudden_Underflow - static const FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1, 0 /*unused*/ }; -#else static const FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0, 0 /*unused*/ }; -#endif FPI *fpi, fpi1; ULong bits[4]; Long exp; int k; - fpi = &fpi0; if (rounding != FPI_Round_near) { fpi1 = fpi0; fpi1.rounding = rounding; fpi = &fpi1; - } + } k = strtodg(s, sp, fpi, &exp, bits); ULtodd((ULong*)dd, bits, exp, k); return k; - } +} diff --git a/third_party/gdtoa/strtorf.c b/third_party/gdtoa/strtorf.c index e971e4c93..47740e446 100644 --- a/third_party/gdtoa/strtorf.c +++ b/third_party/gdtoa/strtorf.c @@ -1,84 +1,79 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - - extern ULong __gdtoa_NanDflt_f[1]; +extern ULong __gdtoa_NanDflt_f[1]; void ULtof(ULong *L, ULong *bits, Long exp, int k) { switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: *L = 0; break; - - case STRTOG_Normal: - case STRTOG_NaNbits: + case STRTOG_Normal: + case STRTOG_NaNbits: L[0] = (bits[0] & 0x7fffff) | ((exp + 0x7f + 23) << 23); break; - - case STRTOG_Denormal: + case STRTOG_Denormal: L[0] = bits[0]; break; - - case STRTOG_Infinite: + case STRTOG_Infinite: L[0] = 0x7f800000; break; - - case STRTOG_NaN: + case STRTOG_NaN: L[0] = __gdtoa_NanDflt_f[0]; - } + } if (k & STRTOG_Neg) L[0] |= 0x80000000L; - } +} int -strtorf(CONST char *s, char **sp, int rounding, float *f) +strtorf(const char *s, char **sp, int rounding, float *f) { static const FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI, 0 /*unused*/ }; FPI *fpi, fpi1; ULong bits[1]; Long exp; int k; - fpi = &fpi0; if (rounding != FPI_Round_near) { fpi1 = fpi0; fpi1.rounding = rounding; fpi = &fpi1; - } + } k = strtodg(s, sp, fpi, &exp, bits); ULtof((ULong*)f, bits, exp, k); return k; - } +} diff --git a/third_party/gdtoa/strtorx.c b/third_party/gdtoa/strtorx.c index 6a5e3835c..cb5459bfb 100644 --- a/third_party/gdtoa/strtorx.c +++ b/third_party/gdtoa/strtorx.c @@ -1,56 +1,36 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998, 2000 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ - -#undef _0 -#undef _1 - -/* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ - -#ifdef IEEE_MC68k -#define _0 0 -#define _1 1 -#define _2 2 -#define _3 3 -#define _4 4 -#endif -#ifdef IEEE_8087 -#define _0 4 -#define _1 3 -#define _2 2 -#define _3 1 -#define _4 0 -#endif extern UShort __gdtoa_NanDflt_ldus[5]; @@ -58,58 +38,53 @@ void ULtox(UShort *L, ULong *bits, Long exp, int k) { switch(k & STRTOG_Retmask) { - case STRTOG_NoNumber: - case STRTOG_Zero: + case STRTOG_NoNumber: + case STRTOG_Zero: L[0] = L[1] = L[2] = L[3] = L[4] = 0; break; - - case STRTOG_Denormal: - L[_0] = 0; + case STRTOG_Denormal: + L[4] = 0; goto normal_bits; - - case STRTOG_Normal: - case STRTOG_NaNbits: - L[_0] = exp + 0x3fff + 63; - normal_bits: - L[_4] = (UShort)bits[0]; - L[_3] = (UShort)(bits[0] >> 16); - L[_2] = (UShort)bits[1]; - L[_1] = (UShort)(bits[1] >> 16); + case STRTOG_Normal: + case STRTOG_NaNbits: + L[4] = exp + 0x3fff + 63; + normal_bits: + L[0] = (UShort)bits[0]; + L[1] = (UShort)(bits[0] >> 16); + L[2] = (UShort)bits[1]; + L[3] = (UShort)(bits[1] >> 16); break; - - case STRTOG_Infinite: - L[_0] = 0x7fff; - L[_1] = 0x8000; - L[_2] = L[_3] = L[_4] = 0; + case STRTOG_Infinite: + L[4] = 0x7fff; + L[3] = 0x8000; + L[2] = L[1] = L[0] = 0; break; - - case STRTOG_NaN: - L[_4] = __gdtoa_NanDflt_ldus[0]; - L[_3] = __gdtoa_NanDflt_ldus[1]; - L[_2] = __gdtoa_NanDflt_ldus[2]; - L[_1] = __gdtoa_NanDflt_ldus[3]; - L[_0] = __gdtoa_NanDflt_ldus[4]; - } - if (k & STRTOG_Neg) - L[_0] |= 0x8000; + case STRTOG_NaN: + L[0] = __gdtoa_NanDflt_ldus[0]; + L[1] = __gdtoa_NanDflt_ldus[1]; + L[2] = __gdtoa_NanDflt_ldus[2]; + L[3] = __gdtoa_NanDflt_ldus[3]; + L[4] = __gdtoa_NanDflt_ldus[4]; } + if (k & STRTOG_Neg) + L[4] |= 0x8000; +} int -strtorx(CONST char *s, char **sp, int rounding, void *L) +strtorx(const char *s, char **sp, int rounding, void *L) { static const FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI, 0 /*unused*/ }; FPI *fpi, fpi1; ULong bits[2]; Long exp; int k; - fpi = &fpi0; if (rounding != FPI_Round_near) { fpi1 = fpi0; fpi1.rounding = rounding; fpi = &fpi1; - } + } k = strtodg(s, sp, fpi, &exp, bits); ULtox((UShort*)L, bits, exp, k); return k; - } +} diff --git a/third_party/gdtoa/sum.c b/third_party/gdtoa/sum.c index 73519fbff..ded6f7e32 100644 --- a/third_party/gdtoa/sum.c +++ b/third_party/gdtoa/sum.c @@ -1,65 +1,61 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -Copyright (C) 1998 by Lucent Technologies -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of Lucent or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ Bigint * -sum(Bigint *a, Bigint *b MTd) +sum(Bigint *a, Bigint *b) { Bigint *c; ULong carry, *xc, *xa, *xb, *xe, y; -#ifdef Pack_32 ULong z; -#endif - if (a->wds < b->wds) { c = b; b = a; a = c; - } - c = Balloc(a->k MTa); + } + c = Balloc(a->k); c->wds = a->wds; carry = 0; xa = a->x; xb = b->x; xc = c->x; xe = xc + b->wds; -#ifdef Pack_32 do { y = (*xa & 0xffff) + (*xb & 0xffff) + carry; carry = (y & 0x10000) >> 16; z = (*xa++ >> 16) + (*xb++ >> 16) + carry; carry = (z & 0x10000) >> 16; Storeinc(xc, z, y); - } - while(xc < xe); + } + while(xc < xe); xe += a->wds - b->wds; while(xc < xe) { y = (*xa & 0xffff) + carry; @@ -67,29 +63,15 @@ sum(Bigint *a, Bigint *b MTd) z = (*xa++ >> 16) + carry; carry = (z & 0x10000) >> 16; Storeinc(xc, z, y); - } -#else - do { - y = *xa++ + *xb++ + carry; - carry = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } - while(xc < xe); - xe += a->wds - b->wds; - while(xc < xe) { - y = *xa++ + carry; - carry = (y & 0x10000) >> 16; - *xc++ = y & 0xffff; - } -#endif + } if (carry) { if (c->wds == c->maxwds) { - b = Balloc(c->k + 1 MTa); + b = Balloc(c->k + 1); Bcopy(b, c); - Bfree(c MTa); + Bfree(c); c = b; - } - c->x[c->wds++] = 1; } - return c; + c->x[c->wds++] = 1; } + return c; +} diff --git a/third_party/gdtoa/ulp.c b/third_party/gdtoa/ulp.c index c10f8dc9a..023393666 100644 --- a/third_party/gdtoa/ulp.c +++ b/third_party/gdtoa/ulp.c @@ -1,66 +1,58 @@ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ The author of this software is David M. Gay. │ +│ Please send bug reports to David M. Gay │ +│ or Justine Tunney │ +│ │ +│ 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. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/gdtoa/gdtoa.internal.h" - /* clang-format off */ -/**************************************************************** - -The author of this software is David M. Gay. - -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. - -****************************************************************/ - -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ double ulp(U *x) { Long L; U a; - L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; -#ifndef Sudden_Underflow if (L > 0) { -#endif -#ifdef IBM - L |= Exp_msk1 >> 4; -#endif word0(&a) = L; word1(&a) = 0; -#ifndef Sudden_Underflow - } + } else { L = -L >> Exp_shift; if (L < Exp_shift) { word0(&a) = 0x80000 >> L; word1(&a) = 0; - } + } else { word0(&a) = 0; L -= Exp_shift; word1(&a) = L >= 31 ? 1 : 1 << (31 - L); - } } -#endif - return dval(&a); } + return dval(&a); +} diff --git a/third_party/lua/lua.main.c b/third_party/lua/lua.main.c index 0c01bb93e..1a32a0600 100644 --- a/third_party/lua/lua.main.c +++ b/third_party/lua/lua.main.c @@ -9,6 +9,8 @@ #include "libc/calls/calls.h" #include "libc/calls/sigbits.h" #include "libc/calls/struct/sigaction.h" +#include "libc/dce.h" +#include "libc/log/log.h" #include "libc/runtime/gc.internal.h" #include "libc/sysv/consts/exit.h" #include "libc/x/x.h" @@ -644,6 +646,7 @@ static int pmain (lua_State *L) { int main (int argc, char **argv) { int status, result; lua_State *L; + if (IsModeDbg()) ShowCrashReports(); L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); diff --git a/third_party/quickjs/quickjs.mk b/third_party/quickjs/quickjs.mk index 2063502b2..8e8396a5f 100644 --- a/third_party/quickjs/quickjs.mk +++ b/third_party/quickjs/quickjs.mk @@ -186,7 +186,7 @@ o/$(MODE)/third_party/quickjs/quickjs.o: \ -DSTACK_FRAME_UNLIMITED o/$(MODE)/third_party/quickjs/call.o: QUOTA = -M1024m -C16 -o/$(MODE)/third_party/quickjs/quickjs.o: QUOTA = -M512m +o/$(MODE)/third_party/quickjs/quickjs.o: QUOTA = -M512m -C16 .PHONY: o/$(MODE)/third_party/quickjs o/$(MODE)/third_party/quickjs: \ diff --git a/third_party/sqlite3/sqlite3.mk b/third_party/sqlite3/sqlite3.mk index 642d75d57..d7d4d5f47 100644 --- a/third_party/sqlite3/sqlite3.mk +++ b/third_party/sqlite3/sqlite3.mk @@ -165,7 +165,7 @@ $(THIRD_PARTY_SQLITE3_SHELL_OBJS): \ o/$(MODE)/%.shell.o: %.c @$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) $(OUTPUT_OPTION) $< -o/$(MODE)/third_party/sqlite3/shell.shell.o: QUOTA = -M512m +o/$(MODE)/third_party/sqlite3/shell.shell.o: QUOTA = -M512m -C16 o/$(MODE)/third_party/sqlite3/vdbe.o: QUOTA = -M512m o/$(MODE)/third_party/sqlite3/vdbe.shell.o: QUOTA = -M512m diff --git a/tool/emacs/cosmo-c-constants.el b/tool/emacs/cosmo-c-constants.el index 81c9f4c3e..68ebfecaf 100644 --- a/tool/emacs/cosmo-c-constants.el +++ b/tool/emacs/cosmo-c-constants.el @@ -4,7 +4,8 @@ "WEOF" "NDEBUG" "HUGE_VAL" - "CLK_TCK")) + "CLK_TCK" + "FLT_ROUNDS")) (defconst cosmo-c-constants-c11 '("__func__" diff --git a/tool/emacs/cosmo-cpp-constants.el b/tool/emacs/cosmo-cpp-constants.el index 4213de907..7c20fa489 100644 --- a/tool/emacs/cosmo-cpp-constants.el +++ b/tool/emacs/cosmo-cpp-constants.el @@ -54,7 +54,8 @@ "__SSE4_2__" "__XSAVE__" "__CLFLUSHOPT__" - "__RDPID__")) + "__RDPID__" + "FLT_ROUNDS")) (defconst cosmo-cpp-constants-gcc-92 '("__x86_64__"