2021-08-19 13:07:37 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │
|
|
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
|
|
|
│ │
|
|
|
|
│ The author of this software is David M. Gay. │
|
|
|
|
│ Please send bug reports to David M. Gay <dmg@acm.org> │
|
|
|
|
│ or Justine Tunney <jtunney@gmail.com> │
|
|
|
|
│ │
|
|
|
|
│ Copyright (C) 1998, 1999 by Lucent Technologies │
|
|
|
|
│ All Rights Reserved │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and distribute this software and │
|
|
|
|
│ its documentation for any purpose and without fee is hereby │
|
|
|
|
│ granted, provided that the above copyright notice appear in all │
|
|
|
|
│ copies and that both that the copyright notice and this │
|
|
|
|
│ permission notice and warranty disclaimer appear in supporting │
|
|
|
|
│ documentation, and that the name of Lucent or any of its entities │
|
|
|
|
│ not be used in advertising or publicity pertaining to │
|
|
|
|
│ distribution of the software without specific, written prior │
|
|
|
|
│ permission. │
|
|
|
|
│ │
|
|
|
|
│ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │
|
|
|
|
│ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │
|
|
|
|
│ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │
|
|
|
|
│ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │
|
|
|
|
│ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │
|
|
|
|
│ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │
|
|
|
|
│ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │
|
|
|
|
│ THIS SOFTWARE. │
|
|
|
|
│ │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/errno.h"
|
2020-12-05 20:20:41 +00:00
|
|
|
#include "third_party/gdtoa/gdtoa.internal.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
|
2020-12-27 15:02:35 +00:00
|
|
|
int
|
2021-10-14 00:27:13 +00:00
|
|
|
__gdtoa_gethex(const char **sp, const FPI *fpi,
|
2022-06-19 08:13:03 +00:00
|
|
|
Long *exp, Bigint **bp, int sign, ThInfo **PTI)
|
2020-12-01 11:43:40 +00:00
|
|
|
{
|
|
|
|
Bigint *b;
|
2021-08-19 13:07:37 +00:00
|
|
|
const unsigned char *decpt, *s0, *s, *s1;
|
2020-12-01 11:43:40 +00:00
|
|
|
int big, esign, havedig, irv, j, k, n, n0, nbits, up, zret;
|
|
|
|
ULong L, lostbits, *x;
|
|
|
|
Long e, e1;
|
2021-10-14 00:27:13 +00:00
|
|
|
/**** if (!__gdtoa_hexdig['0']) __gdtoa_hexdig_init(); ****/
|
2020-12-01 11:43:40 +00:00
|
|
|
*bp = 0;
|
|
|
|
havedig = 0;
|
2021-08-19 13:07:37 +00:00
|
|
|
s0 = *(const unsigned char **)sp + 2;
|
2020-12-01 11:43:40 +00:00
|
|
|
while(s0[havedig] == '0')
|
|
|
|
havedig++;
|
|
|
|
s0 += havedig;
|
|
|
|
s = s0;
|
|
|
|
decpt = 0;
|
|
|
|
zret = 0;
|
|
|
|
e = 0;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (__gdtoa_hexdig[*s])
|
2020-12-01 11:43:40 +00:00
|
|
|
havedig++;
|
|
|
|
else {
|
|
|
|
zret = 1;
|
|
|
|
if (*s != '.')
|
|
|
|
goto pcheck;
|
|
|
|
decpt = ++s;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (!__gdtoa_hexdig[*s])
|
2020-12-01 11:43:40 +00:00
|
|
|
goto pcheck;
|
|
|
|
while(*s == '0')
|
|
|
|
s++;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (__gdtoa_hexdig[*s])
|
2020-12-01 11:43:40 +00:00
|
|
|
zret = 0;
|
|
|
|
havedig = 1;
|
|
|
|
s0 = s;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
while(__gdtoa_hexdig[*s])
|
2020-12-01 11:43:40 +00:00
|
|
|
s++;
|
|
|
|
if (*s == '.' && !decpt) {
|
|
|
|
decpt = ++s;
|
2021-10-14 00:27:13 +00:00
|
|
|
while(__gdtoa_hexdig[*s])
|
2020-12-01 11:43:40 +00:00
|
|
|
s++;
|
2021-08-19 13:07:37 +00:00
|
|
|
}/*}*/
|
2020-12-01 11:43:40 +00:00
|
|
|
if (decpt)
|
|
|
|
e = -(((Long)(s-decpt)) << 2);
|
2021-08-19 13:07:37 +00:00
|
|
|
pcheck:
|
2020-12-01 11:43:40 +00:00
|
|
|
s1 = s;
|
|
|
|
big = esign = 0;
|
|
|
|
switch(*s) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case 'p':
|
|
|
|
case 'P':
|
2020-12-01 11:43:40 +00:00
|
|
|
switch(*++s) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case '-':
|
2020-12-01 11:43:40 +00:00
|
|
|
esign = 1;
|
|
|
|
/* no break */
|
2021-08-19 13:07:37 +00:00
|
|
|
case '+':
|
2020-12-01 11:43:40 +00:00
|
|
|
s++;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
if ((n = __gdtoa_hexdig[*s]) == 0 || n > 0x19) {
|
2020-12-01 11:43:40 +00:00
|
|
|
s = s1;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
e1 = n - 0x10;
|
2021-10-14 00:27:13 +00:00
|
|
|
while((n = __gdtoa_hexdig[*++s]) !=0 && n <= 0x19) {
|
2020-12-01 11:43:40 +00:00
|
|
|
if (e1 & 0xf8000000)
|
|
|
|
big = 1;
|
|
|
|
e1 = 10*e1 + n - 0x10;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
if (esign)
|
|
|
|
e1 = -e1;
|
|
|
|
e += e1;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
*sp = (char*)s;
|
|
|
|
if (!havedig)
|
|
|
|
*sp = (char*)s0 - 1;
|
|
|
|
if (zret)
|
|
|
|
return STRTOG_Zero;
|
|
|
|
if (big) {
|
|
|
|
if (esign) {
|
|
|
|
switch(fpi->rounding) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_up:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (sign)
|
|
|
|
break;
|
|
|
|
goto ret_tiny;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_down:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (!sign)
|
|
|
|
break;
|
|
|
|
goto ret_tiny;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
goto retz;
|
2021-08-19 13:07:37 +00:00
|
|
|
ret_tiny:
|
2022-06-19 08:13:03 +00:00
|
|
|
b = __gdtoa_Balloc(0, PTI);
|
2020-12-01 11:43:40 +00:00
|
|
|
b->wds = 1;
|
|
|
|
b->x[0] = 1;
|
|
|
|
goto dret;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
switch(fpi->rounding) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_near:
|
2020-12-01 11:43:40 +00:00
|
|
|
goto ovfl1;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_up:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (!sign)
|
|
|
|
goto ovfl1;
|
|
|
|
goto ret_big;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_down:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (sign)
|
|
|
|
goto ovfl1;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
|
|
|
ret_big:
|
2020-12-01 11:43:40 +00:00
|
|
|
nbits = fpi->nbits;
|
|
|
|
n0 = n = nbits >> kshift;
|
|
|
|
if (nbits & kmask)
|
|
|
|
++n;
|
|
|
|
for(j = n, k = 0; j >>= 1; ++k);
|
2022-06-19 08:13:03 +00:00
|
|
|
*bp = b = __gdtoa_Balloc(k, PTI);
|
2020-12-01 11:43:40 +00:00
|
|
|
b->wds = n;
|
|
|
|
for(j = 0; j < n0; ++j)
|
|
|
|
b->x[j] = ALL_ON;
|
|
|
|
if (n > n0)
|
|
|
|
b->x[j] = ALL_ON >> (ULbits - (nbits & kmask));
|
|
|
|
*exp = fpi->emax;
|
|
|
|
return STRTOG_Normal | STRTOG_Inexlo;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
n = s1 - s0 - 1;
|
|
|
|
for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
|
|
|
|
k++;
|
2022-06-19 08:13:03 +00:00
|
|
|
b = __gdtoa_Balloc(k, PTI);
|
2020-12-01 11:43:40 +00:00
|
|
|
x = b->x;
|
|
|
|
n = 0;
|
|
|
|
L = 0;
|
|
|
|
while(s1 > s0) {
|
|
|
|
if (*--s1 == '.')
|
|
|
|
continue;
|
|
|
|
if (n == ULbits) {
|
|
|
|
*x++ = L;
|
|
|
|
L = 0;
|
|
|
|
n = 0;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2024-09-15 00:11:04 +00:00
|
|
|
// We can shift in a way that changes the sign bit or overflows,
|
|
|
|
// so we need to cast to unsigned to avoid undefined behavior
|
|
|
|
L |= (unsigned)(__gdtoa_hexdig[*s1] & 0x0f) << n;
|
2020-12-01 11:43:40 +00:00
|
|
|
n += 4;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
*x++ = L;
|
|
|
|
b->wds = n = x - b->x;
|
|
|
|
n = ULbits*n - hi0bits(L);
|
|
|
|
nbits = fpi->nbits;
|
|
|
|
lostbits = 0;
|
|
|
|
x = b->x;
|
|
|
|
if (n > nbits) {
|
|
|
|
n -= nbits;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (__gdtoa_any_on(b,n)) {
|
2020-12-01 11:43:40 +00:00
|
|
|
lostbits = 1;
|
|
|
|
k = n - 1;
|
|
|
|
if (x[k>>kshift] & 1 << (k & kmask)) {
|
|
|
|
lostbits = 2;
|
2021-10-14 00:27:13 +00:00
|
|
|
if (k > 0 && __gdtoa_any_on(b,k))
|
2020-12-01 11:43:40 +00:00
|
|
|
lostbits = 3;
|
|
|
|
}
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
__gdtoa_rshift(b, n);
|
2020-12-01 11:43:40 +00:00
|
|
|
e += n;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
else if (n < nbits) {
|
|
|
|
n = nbits - n;
|
2022-06-19 08:13:03 +00:00
|
|
|
b = __gdtoa_lshift(b, n, PTI);
|
2020-12-01 11:43:40 +00:00
|
|
|
e -= n;
|
|
|
|
x = b->x;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
if (e > fpi->emax) {
|
2021-08-19 13:07:37 +00:00
|
|
|
ovfl:
|
2022-06-19 08:13:03 +00:00
|
|
|
__gdtoa_Bfree(b, PTI);
|
2021-08-19 13:07:37 +00:00
|
|
|
ovfl1:
|
2020-12-01 11:43:40 +00:00
|
|
|
errno = ERANGE;
|
|
|
|
switch (fpi->rounding) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_zero:
|
2020-12-01 11:43:40 +00:00
|
|
|
goto ret_big;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_down:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (!sign)
|
|
|
|
goto ret_big;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_up:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (sign)
|
|
|
|
goto ret_big;
|
|
|
|
}
|
2021-08-19 13:07:37 +00:00
|
|
|
return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
|
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
irv = STRTOG_Normal;
|
|
|
|
if (e < fpi->emin) {
|
|
|
|
irv = STRTOG_Denormal;
|
|
|
|
n = fpi->emin - e;
|
|
|
|
if (n >= nbits) {
|
|
|
|
switch (fpi->rounding) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_near:
|
2021-10-14 00:27:13 +00:00
|
|
|
if (n == nbits && (n < 2 || lostbits || __gdtoa_any_on(b,n-1)))
|
2020-12-01 11:43:40 +00:00
|
|
|
goto one_bit;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_up:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (!sign)
|
|
|
|
goto one_bit;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_down:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (sign) {
|
2021-08-19 13:07:37 +00:00
|
|
|
one_bit:
|
2020-12-01 11:43:40 +00:00
|
|
|
x[0] = b->wds = 1;
|
2021-08-19 13:07:37 +00:00
|
|
|
dret:
|
2020-12-01 11:43:40 +00:00
|
|
|
*bp = b;
|
|
|
|
*exp = fpi->emin;
|
|
|
|
errno = ERANGE;
|
|
|
|
return STRTOG_Denormal | STRTOG_Inexhi
|
|
|
|
| STRTOG_Underflow;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-19 08:13:03 +00:00
|
|
|
__gdtoa_Bfree(b, PTI);
|
2021-08-19 13:07:37 +00:00
|
|
|
retz:
|
2020-12-01 11:43:40 +00:00
|
|
|
errno = ERANGE;
|
|
|
|
return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
k = n - 1;
|
|
|
|
if (lostbits)
|
|
|
|
lostbits = 1;
|
|
|
|
else if (k > 0)
|
2021-10-14 00:27:13 +00:00
|
|
|
lostbits = __gdtoa_any_on(b,k);
|
2020-12-01 11:43:40 +00:00
|
|
|
if (x[k>>kshift] & 1 << (k & kmask))
|
|
|
|
lostbits |= 2;
|
|
|
|
nbits -= n;
|
2021-10-14 00:27:13 +00:00
|
|
|
__gdtoa_rshift(b,n);
|
2020-12-01 11:43:40 +00:00
|
|
|
e = fpi->emin;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
if (lostbits) {
|
|
|
|
up = 0;
|
|
|
|
switch(fpi->rounding) {
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_zero:
|
2020-12-01 11:43:40 +00:00
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_near:
|
2020-12-01 11:43:40 +00:00
|
|
|
if (lostbits & 2
|
2021-08-19 13:07:37 +00:00
|
|
|
&& (lostbits | x[0]) & 1)
|
2020-12-01 11:43:40 +00:00
|
|
|
up = 1;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_up:
|
2020-12-01 11:43:40 +00:00
|
|
|
up = 1 - sign;
|
|
|
|
break;
|
2021-08-19 13:07:37 +00:00
|
|
|
case FPI_Round_down:
|
2020-12-01 11:43:40 +00:00
|
|
|
up = sign;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
if (up) {
|
|
|
|
k = b->wds;
|
2022-06-19 08:13:03 +00:00
|
|
|
b = __gdtoa_increment(b, PTI);
|
2020-12-01 11:43:40 +00:00
|
|
|
x = b->x;
|
|
|
|
if (irv == STRTOG_Denormal) {
|
|
|
|
if (nbits == fpi->nbits - 1
|
2021-08-19 13:07:37 +00:00
|
|
|
&& x[nbits >> kshift] & 1 << (nbits & kmask))
|
2020-12-01 11:43:40 +00:00
|
|
|
irv = STRTOG_Normal;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
else if (b->wds > k
|
2021-08-19 13:07:37 +00:00
|
|
|
|| ((n = nbits & kmask) !=0
|
|
|
|
&& hi0bits(x[k-1]) < 32-n)) {
|
2021-10-14 00:27:13 +00:00
|
|
|
__gdtoa_rshift(b,1);
|
2020-12-01 11:43:40 +00:00
|
|
|
if (++e > fpi->emax)
|
|
|
|
goto ovfl;
|
|
|
|
}
|
2021-08-19 13:07:37 +00:00
|
|
|
irv |= STRTOG_Inexhi;
|
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
else
|
|
|
|
irv |= STRTOG_Inexlo;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
*bp = b;
|
|
|
|
*exp = e;
|
|
|
|
return irv;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|