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
50 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= i
;
56 #ifndef MULTIPLE_THREADS
64 nrv_alloc(s
, rve
, n
) char *s
, **rve
; int n
;
66 nrv_alloc(char *s
, char **rve
, int n
)
72 while((*t
= *s
++) !=0)
79 /* freedtoa(s) must be used to free values s returned by dtoa
80 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
81 * but for consistency with earlier versions of dtoa, it is optional
82 * when MULTIPLE_THREADS is not defined.
92 Bigint
*b
= (Bigint
*)((int *)s
- 1);
93 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
95 #ifndef MULTIPLE_THREADS
104 (b
, S
) Bigint
*b
, *S
;
106 (Bigint
*b
, Bigint
*S
)
110 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
112 ULLong borrow
, carry
, y
, ys
;
114 ULong borrow
, carry
, y
, ys
;
122 /*debug*/ if (b
->wds
> n
)
123 /*debug*/ Bug("oversize b in quorem");
131 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
134 /*debug*/ Bug("oversized quotient in quorem");
141 ys
= *sx
++ * (ULLong
)q
+ carry
;
143 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
144 borrow
= y
>> 32 & 1UL;
145 *bx
++ = y
& 0xffffffffUL
;
149 ys
= (si
& 0xffff) * q
+ carry
;
150 zs
= (si
>> 16) * q
+ (ys
>> 16);
152 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
153 borrow
= (y
& 0x10000) >> 16;
154 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
155 borrow
= (z
& 0x10000) >> 16;
158 ys
= *sx
++ * q
+ carry
;
160 y
= *bx
- (ys
& 0xffff) - borrow
;
161 borrow
= (y
& 0x10000) >> 16;
169 while(--bxe
> bx
&& !*bxe
)
174 if (cmp(b
, S
) >= 0) {
184 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
185 borrow
= y
>> 32 & 1UL;
186 *bx
++ = y
& 0xffffffffUL
;
190 ys
= (si
& 0xffff) + carry
;
191 zs
= (si
>> 16) + (ys
>> 16);
193 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
194 borrow
= (y
& 0x10000) >> 16;
195 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
196 borrow
= (z
& 0x10000) >> 16;
201 y
= *bx
- (ys
& 0xffff) - borrow
;
202 borrow
= (y
& 0x10000) >> 16;
211 while(--bxe
> bx
&& !*bxe
)