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-05 20:20:41 +00:00
|
|
|
#include "third_party/gdtoa/gdtoa.internal.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
|
2021-10-14 00:27:13 +00:00
|
|
|
void
|
|
|
|
freedtoa(char *s)
|
|
|
|
{
|
2022-06-19 08:13:03 +00:00
|
|
|
ThInfo *TI = 0;
|
2021-10-14 00:27:13 +00:00
|
|
|
Bigint *b = (Bigint *)((int *)s - 1);
|
2022-06-19 08:13:03 +00:00
|
|
|
b->maxwds = 1 << (b->k = *(int *)b);
|
|
|
|
__gdtoa_Bfree(b, &TI);
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 15:02:35 +00:00
|
|
|
char *
|
2022-06-19 08:13:03 +00:00
|
|
|
__gdtoa_rv_alloc(int i, ThInfo **PTI)
|
2020-12-01 11:43:40 +00:00
|
|
|
{
|
|
|
|
int j, k, *r;
|
|
|
|
j = sizeof(ULong);
|
2022-06-19 08:13:03 +00:00
|
|
|
for (k = 0; (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int)) + j <= i;
|
|
|
|
j <<= 1)
|
|
|
|
k++;
|
|
|
|
r = (int *)__gdtoa_Balloc(k, PTI);
|
2024-09-01 21:42:14 +00:00
|
|
|
if (r == NULL)
|
|
|
|
return NULL;
|
2020-12-01 11:43:40 +00:00
|
|
|
*r = k;
|
2022-06-19 08:13:03 +00:00
|
|
|
return (char *)(r + 1);
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
|
2020-12-27 15:02:35 +00:00
|
|
|
char *
|
2022-06-19 08:13:03 +00:00
|
|
|
__gdtoa_nrv_alloc(char *s, char **rve, int n, ThInfo **PTI)
|
2020-12-01 11:43:40 +00:00
|
|
|
{
|
|
|
|
char *rv, *t;
|
2022-06-19 08:13:03 +00:00
|
|
|
t = rv = __gdtoa_rv_alloc(n, PTI);
|
|
|
|
while ((*t = *s++) != 0)
|
2020-12-01 11:43:40 +00:00
|
|
|
t++;
|
|
|
|
if (rve)
|
|
|
|
*rve = t;
|
|
|
|
return rv;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
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_quorem(Bigint *b, Bigint *S)
|
2020-12-01 11:43:40 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
ULong *bx, *bxe, q, *sx, *sxe;
|
|
|
|
ULLong borrow, carry, y, ys;
|
|
|
|
n = S->wds;
|
|
|
|
#ifdef DEBUG
|
2021-08-19 13:07:37 +00:00
|
|
|
if (b->wds > n)
|
2021-10-14 00:27:13 +00:00
|
|
|
Bug("oversize b in __gdtoa_quorem");
|
2020-12-01 11:43:40 +00:00
|
|
|
#endif
|
|
|
|
if (b->wds < n)
|
|
|
|
return 0;
|
|
|
|
sx = S->x;
|
|
|
|
sxe = sx + --n;
|
|
|
|
bx = b->x;
|
|
|
|
bxe = bx + n;
|
|
|
|
q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
|
|
|
|
#ifdef DEBUG
|
2021-08-19 13:07:37 +00:00
|
|
|
if (q > 9)
|
2021-10-14 00:27:13 +00:00
|
|
|
Bug("oversized quotient in __gdtoa_quorem");
|
2020-12-01 11:43:40 +00:00
|
|
|
#endif
|
|
|
|
if (q) {
|
|
|
|
borrow = 0;
|
|
|
|
carry = 0;
|
|
|
|
do {
|
|
|
|
ys = *sx++ * (ULLong)q + carry;
|
|
|
|
carry = ys >> 32;
|
2021-10-14 00:27:13 +00:00
|
|
|
y = *bx - (ys & 0xffffffff) - borrow;
|
2020-12-01 11:43:40 +00:00
|
|
|
borrow = y >> 32 & 1UL;
|
2021-10-14 00:27:13 +00:00
|
|
|
*bx++ = y & 0xffffffff;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
|
|
|
while(sx <= sxe);
|
2020-12-01 11:43:40 +00:00
|
|
|
if (!*bxe) {
|
|
|
|
bx = b->x;
|
|
|
|
while(--bxe > bx && !*bxe)
|
|
|
|
--n;
|
|
|
|
b->wds = n;
|
|
|
|
}
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
if (__gdtoa_cmp(b, S) >= 0) {
|
2020-12-01 11:43:40 +00:00
|
|
|
q++;
|
|
|
|
borrow = 0;
|
|
|
|
carry = 0;
|
|
|
|
bx = b->x;
|
|
|
|
sx = S->x;
|
|
|
|
do {
|
|
|
|
ys = *sx++ + carry;
|
|
|
|
carry = ys >> 32;
|
2021-10-14 00:27:13 +00:00
|
|
|
y = *bx - (ys & 0xffffffff) - borrow;
|
2020-12-01 11:43:40 +00:00
|
|
|
borrow = y >> 32 & 1UL;
|
2021-10-14 00:27:13 +00:00
|
|
|
*bx++ = y & 0xffffffff;
|
2021-08-19 13:07:37 +00:00
|
|
|
}
|
|
|
|
while(sx <= sxe);
|
2020-12-01 11:43:40 +00:00
|
|
|
bx = b->x;
|
|
|
|
bxe = bx + n;
|
|
|
|
if (!*bxe) {
|
|
|
|
while(--bxe > bx && !*bxe)
|
|
|
|
--n;
|
|
|
|
b->wds = n;
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 13:07:37 +00:00
|
|
|
return q;
|
|
|
|
}
|