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 "."). */
33 #include <sys/config.h>
38 #ifndef MULTIPLE_THREADS
44 rv_alloc(ptr
, i
) struct _reent
*ptr
, int i
;
46 rv_alloc(struct _reent
*ptr
, int i
)
52 /* Allocate buffer in a compatible way with legacy _ldtoa_r(). */
54 for (_REENT_MP_RESULT_K (ptr
) = 0;
55 sizeof (Bigint
) - sizeof (ULong
) + j
<= i
; j
<<= 1)
56 _REENT_MP_RESULT_K (ptr
)++;
57 _REENT_MP_RESULT (ptr
) = eBalloc (ptr
, _REENT_MP_RESULT_K (ptr
));
58 r
= (char *) _REENT_MP_RESULT (ptr
);
62 #ifndef MULTIPLE_THREADS
67 #ifndef MULTIPLE_THREADS
75 nrv_alloc(ptr
, s
, rve
, n
) struct _reent
*ptr
, char *s
, **rve
; int n
;
77 nrv_alloc(struct _reent
*ptr
, char *s
, char **rve
, int n
)
82 t
= rv
= rv_alloc(ptr
, n
);
85 while((*t
= *s
++) !=0)
92 /* freedtoa(s) must be used to free values s returned by dtoa
93 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
94 * but for consistency with earlier versions of dtoa, it is optional
95 * when MULTIPLE_THREADS is not defined.
100 freedtoa(ptr
, s
) struct _reent
*ptr
, char *s
;
102 freedtoa(struct _reent
*ptr
, char *s
)
105 /* Free buffer allocated in a compatible way with legacy _ldtoa_r(). */
106 Bigint
*b
= (Bigint
*)s
;
107 b
->_maxwds
= 1 << (b
->_k
= _REENT_MP_RESULT_K (ptr
));
109 #ifndef MULTIPLE_THREADS
110 if (s
== dtoa_result
)
114 DEF_STRONG(freedtoa
);
119 (b
, S
) Bigint
*b
, *S
;
121 (Bigint
*b
, Bigint
*S
)
125 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
127 ULLong borrow
, carry
, y
, ys
;
129 ULong borrow
, carry
, y
, ys
;
137 /*debug*/ if (b
->_wds
> n
)
138 /*debug*/ Bug("oversize b in quorem");
146 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
149 /*debug*/ Bug("oversized quotient in quorem");
156 ys
= *sx
++ * (ULLong
)q
+ carry
;
158 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
159 borrow
= y
>> 32 & 1UL;
160 *bx
++ = y
& 0xffffffffUL
;
164 ys
= (si
& 0xffff) * q
+ carry
;
165 zs
= (si
>> 16) * q
+ (ys
>> 16);
167 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
168 borrow
= (y
& 0x10000) >> 16;
169 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
170 borrow
= (z
& 0x10000) >> 16;
173 ys
= *sx
++ * q
+ carry
;
175 y
= *bx
- (ys
& 0xffff) - borrow
;
176 borrow
= (y
& 0x10000) >> 16;
184 while(--bxe
> bx
&& !*bxe
)
189 if (cmp(b
, S
) >= 0) {
199 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
200 borrow
= y
>> 32 & 1UL;
201 *bx
++ = y
& 0xffffffffUL
;
205 ys
= (si
& 0xffff) + carry
;
206 zs
= (si
>> 16) + (ys
>> 16);
208 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
209 borrow
= (y
& 0x10000) >> 16;
210 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
211 borrow
= (z
& 0x10000) >> 16;
216 y
= *bx
- (ys
& 0xffff) - borrow
;
217 borrow
= (y
& 0x10000) >> 16;
226 while(--bxe
> bx
&& !*bxe
)
233 #endif /* _USE_GDTOA */