mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 11:48:30 +00:00
Clean up gdtoa code
This commit is contained in:
parent
7341336b1a
commit
da45c7c80b
48 changed files with 2808 additions and 4721 deletions
251
third_party/gdtoa/gethex.c
vendored
251
third_party/gdtoa/gethex.c
vendored
|
@ -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 <dmg@acm.org> │
|
||||
│ or Justine Tunney <jtunney@gmail.com> │
|
||||
│ │
|
||||
│ Copyright (C) 1998, 1999 by Lucent Technologies │
|
||||
│ All Rights Reserved │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and distribute this software and │
|
||||
│ its documentation for any purpose and without fee is hereby │
|
||||
│ granted, provided that the above copyright notice appear in all │
|
||||
│ copies and that both that the copyright notice and this │
|
||||
│ permission notice and warranty disclaimer appear in supporting │
|
||||
│ documentation, and that the name of Lucent or any of its entities │
|
||||
│ not be used in advertising or publicity pertaining to │
|
||||
│ distribution of the software without specific, written prior │
|
||||
│ permission. │
|
||||
│ │
|
||||
│ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │
|
||||
│ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │
|
||||
│ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │
|
||||
│ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │
|
||||
│ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │
|
||||
│ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │
|
||||
│ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │
|
||||
│ THIS SOFTWARE. │
|
||||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue