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 David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
34 #ifndef MULTIPLE_THREADS
49 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= i
;
55 #ifndef MULTIPLE_THREADS
63 nrv_alloc(s
, rve
, n
) char *s
, **rve
; int n
;
65 nrv_alloc(char *s
, char **rve
, int n
)
71 while((*t
= *s
++) !=0)
78 /* freedtoa(s) must be used to free values s returned by dtoa
79 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
80 * but for consistency with earlier versions of dtoa, it is optional
81 * when MULTIPLE_THREADS is not defined.
91 Bigint
*b
= (Bigint
*)((int *)s
- 1);
92 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
94 #ifndef MULTIPLE_THREADS
103 (b
, S
) Bigint
*b
, *S
;
105 (Bigint
*b
, Bigint
*S
)
109 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
111 ULLong borrow
, carry
, y
, ys
;
113 ULong borrow
, carry
, y
, ys
;
121 /*debug*/ if (b
->wds
> n
)
122 /*debug*/ Bug("oversize b in quorem");
130 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
133 /*debug*/ Bug("oversized quotient in quorem");
140 ys
= *sx
++ * (ULLong
)q
+ carry
;
142 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
143 borrow
= y
>> 32 & 1UL;
144 *bx
++ = y
& 0xffffffffUL
;
148 ys
= (si
& 0xffff) * q
+ carry
;
149 zs
= (si
>> 16) * q
+ (ys
>> 16);
151 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
152 borrow
= (y
& 0x10000) >> 16;
153 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
154 borrow
= (z
& 0x10000) >> 16;
157 ys
= *sx
++ * q
+ carry
;
159 y
= *bx
- (ys
& 0xffff) - borrow
;
160 borrow
= (y
& 0x10000) >> 16;
168 while(--bxe
> bx
&& !*bxe
)
173 if (cmp(b
, S
) >= 0) {
183 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
184 borrow
= y
>> 32 & 1UL;
185 *bx
++ = y
& 0xffffffffUL
;
189 ys
= (si
& 0xffff) + carry
;
190 zs
= (si
>> 16) + (ys
>> 16);
192 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
193 borrow
= (y
& 0x10000) >> 16;
194 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
195 borrow
= (z
& 0x10000) >> 16;
200 y
= *bx
- (ys
& 0xffff) - borrow
;
201 borrow
= (y
& 0x10000) >> 16;
210 while(--bxe
> bx
&& !*bxe
)