1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 ****************************************************************/
29 /* Please send bug reports to
31 Bell Laboratories, Room 2C-463
33 Murray Hill, NJ 07974-0636
40 #ifndef MULTIPLE_THREADS
55 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
61 #ifndef MULTIPLE_THREADS
69 nrv_alloc(s, rve, n) char *s, **rve; int n;
71 nrv_alloc(char *s, char **rve, int n)
77 while((*t = *s++) !=0)
84 /* freedtoa(s) must be used to free values s returned by dtoa
85 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
86 * but for consistency with earlier versions of dtoa, it is optional
87 * when MULTIPLE_THREADS is not defined.
97 Bigint *b = (Bigint *)((int *)s - 1);
98 b->maxwds = 1 << (b->k = *(int*)b);
100 #ifndef MULTIPLE_THREADS
101 if (s == dtoa_result)
109 (b, S) Bigint *b, *S;
111 (Bigint *b, Bigint *S)
115 ULong *bx, *bxe, q, *sx, *sxe;
117 ULLong borrow, carry, y, ys;
119 ULong borrow, carry, y, ys;
127 /*debug*/ if (b->wds > n)
128 /*debug*/ Bug("oversize b in quorem");
136 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
139 /*debug*/ Bug("oversized quotient in quorem");
146 ys = *sx++ * (ULLong)q + carry;
148 y = *bx - (ys & 0xffffffffUL) - borrow;
149 borrow = y >> 32 & 1UL;
150 *bx++ = y & 0xffffffffUL;
154 ys = (si & 0xffff) * q + carry;
155 zs = (si >> 16) * q + (ys >> 16);
157 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
158 borrow = (y & 0x10000) >> 16;
159 z = (*bx >> 16) - (zs & 0xffff) - borrow;
160 borrow = (z & 0x10000) >> 16;
163 ys = *sx++ * q + carry;
165 y = *bx - (ys & 0xffff) - borrow;
166 borrow = (y & 0x10000) >> 16;
174 while(--bxe > bx && !*bxe)
179 if (cmp(b, S) >= 0) {
189 y = *bx - (ys & 0xffffffffUL) - borrow;
190 borrow = y >> 32 & 1UL;
191 *bx++ = y & 0xffffffffUL;
195 ys = (si & 0xffff) + carry;
196 zs = (si >> 16) + (ys >> 16);
198 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
199 borrow = (y & 0x10000) >> 16;
200 z = (*bx >> 16) - (zs & 0xffff) - borrow;
201 borrow = (z & 0x10000) >> 16;
206 y = *bx - (ys & 0xffff) - borrow;
207 borrow = (y & 0x10000) >> 16;
216 while(--bxe > bx && !*bxe)