1 /* $NetBSD: dmisc.c,v 1.4 2008/03/21 23:13:48 christos Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998 by Lucent Technologies
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
29 ****************************************************************/
31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
32 * with " at " changed at "@" and " dot " changed to "."). */
36 #ifndef MULTIPLE_THREADS
52 sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= i
;
55 r
= (int*)(void*)Balloc(k
);
60 #ifndef MULTIPLE_THREADS
63 (char *)(void *)(r
+1);
68 nrv_alloc(s
, rve
, n
) CONST
char *s
; char **rve
; size_t n
;
70 nrv_alloc(CONST
char *s
, char **rve
, size_t n
)
78 while((*t
= *s
++) !=0)
85 /* freedtoa(s) must be used to free values s returned by dtoa
86 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
87 * but for consistency with earlier versions of dtoa, it is optional
88 * when MULTIPLE_THREADS is not defined.
98 Bigint
*b
= (Bigint
*)(void *)((int *)(void *)s
- 1);
99 b
->maxwds
= 1 << (b
->k
= *(int*)(void*)b
);
101 #ifndef MULTIPLE_THREADS
102 if (s
== dtoa_result
)
110 (b
, S
) Bigint
*b
, *S
;
112 (Bigint
*b
, Bigint
*S
)
116 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
118 ULLong borrow
, carry
, y
, ys
;
120 ULong borrow
, carry
, y
, ys
;
128 /*debug*/ if (b
->wds
> n
)
129 /*debug*/ Bug("oversize b in quorem");
137 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
140 /*debug*/ Bug("oversized quotient in quorem");
147 ys
= *sx
++ * (ULLong
)q
+ carry
;
149 /* LINTED conversion */
150 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
151 borrow
= y
>> 32 & 1UL;
152 /* LINTED conversion */
153 *bx
++ = y
& 0xffffffffUL
;
157 ys
= (si
& 0xffff) * q
+ carry
;
158 zs
= (si
>> 16) * q
+ (ys
>> 16);
160 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
161 borrow
= (y
& 0x10000) >> 16;
162 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
163 borrow
= (z
& 0x10000) >> 16;
166 ys
= *sx
++ * q
+ carry
;
168 y
= *bx
- (ys
& 0xffff) - borrow
;
169 borrow
= (y
& 0x10000) >> 16;
177 while(--bxe
> bx
&& !*bxe
)
182 if (cmp(b
, S
) >= 0) {
192 /* LINTED conversion */
193 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
194 borrow
= y
>> 32 & 1UL;
195 /* LINTED conversion */
196 *bx
++ = y
& 0xffffffffUL
;
200 ys
= (si
& 0xffff) + carry
;
201 zs
= (si
>> 16) + (ys
>> 16);
203 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
204 borrow
= (y
& 0x10000) >> 16;
205 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
206 borrow
= (z
& 0x10000) >> 16;
211 y
= *bx
- (ys
& 0xffff) - borrow
;
212 borrow
= (y
& 0x10000) >> 16;
221 while(--bxe
> bx
&& !*bxe
)