]>
git.saurik.com Git - apple/libc.git/blob - gdtoa/FreeBSD/gdtoa-misc.c
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998, 1999 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 static Bigint
*freelist
[Kmax
+1];
41 #ifndef Omit_Private_Memory
43 #define PRIVATE_MEM 2304
45 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
46 static double private_mem
[PRIVATE_mem
], *pmem_next
= private_mem
;
59 #ifndef Omit_Private_Memory
64 if ( (rv
= freelist
[k
]) !=0) {
65 freelist
[k
] = rv
->next
;
69 #ifdef Omit_Private_Memory
70 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
-1)*sizeof(ULong
));
72 len
= (sizeof(Bigint
) + (x
-1)*sizeof(ULong
) + sizeof(double) - 1)
74 if (pmem_next
- private_mem
+ len
<= PRIVATE_mem
) {
75 rv
= (Bigint
*)pmem_next
;
79 rv
= (Bigint
*)MALLOC(len
*sizeof(double));
85 rv
->sign
= rv
->wds
= 0;
99 v
->next
= freelist
[v
->k
];
114 register ULong x
= *y
;
156 (b
, m
, a
) Bigint
*b
; int m
, a
;
158 (Bigint
*b
, int m
, int a
) /* multiply by m and add a */
179 y
= *x
* (ULLong
)m
+ carry
;
181 *x
++ = y
& 0xffffffffUL
;
185 y
= (xi
& 0xffff) * m
+ carry
;
186 z
= (xi
>> 16) * m
+ (y
>> 16);
188 *x
++ = (z
<< 16) + (y
& 0xffff);
198 if (wds
>= b
->maxwds
) {
213 (x
) register ULong x
;
220 if (!(x
& 0xffff0000)) {
224 if (!(x
& 0xff000000)) {
228 if (!(x
& 0xf0000000)) {
232 if (!(x
& 0xc0000000)) {
236 if (!(x
& 0x80000000)) {
238 if (!(x
& 0x40000000))
263 (a
, b
) Bigint
*a
, *b
;
265 (Bigint
*a
, Bigint
*b
)
270 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
281 if (a
->wds
< b
->wds
) {
293 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++)
301 for(; xb
< xbe
; xc0
++) {
302 if ( (y
= *xb
++) !=0) {
307 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
309 *xc
++ = z
& 0xffffffffUL
;
317 for(; xb
< xbe
; xb
++, xc0
++) {
318 if ( (y
= *xb
& 0xffff) !=0) {
323 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
325 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
332 if ( (y
= *xb
>> 16) !=0) {
338 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
341 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
349 for(; xb
< xbe
; xc0
++) {
350 if ( (y
= *xb
++) !=0) {
355 z
= *x
++ * y
+ *xc
+ carry
;
365 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
375 (b
, k
) Bigint
*b
; int k
;
380 Bigint
*b1
, *p5
, *p51
;
382 static int p05
[3] = { 5, 25, 125 };
384 if ( (i
= k
& 3) !=0)
385 b
= multadd(b
, p05
[i
-1], 0);
389 if ((p5
= p5s
) == 0) {
391 #ifdef MULTIPLE_THREADS
392 ACQUIRE_DTOA_LOCK(1);
411 if ((p51
= p5
->next
) == 0) {
412 #ifdef MULTIPLE_THREADS
413 ACQUIRE_DTOA_LOCK(1);
414 if (!(p51
= p5
->next
)) {
415 p51
= p5
->next
= mult(p5
,p5
);
420 p51
= p5
->next
= mult(p5
,p5
);
432 (b
, k
) Bigint
*b
; int k
;
439 ULong
*x
, *x1
, *xe
, z
;
444 for(i
= b
->maxwds
; n1
> i
; i
<<= 1)
448 for(i
= 0; i
< n
; i
++)
467 *x1
++ = *x
<< k
& 0xffff | z
;
486 (a
, b
) Bigint
*a
, *b
;
488 (Bigint
*a
, Bigint
*b
)
491 ULong
*xa
, *xa0
, *xb
, *xb0
;
497 if (i
> 1 && !a
->x
[i
-1])
498 Bug("cmp called with a->x[a->wds-1] == 0");
499 if (j
> 1 && !b
->x
[j
-1])
500 Bug("cmp called with b->x[b->wds-1] == 0");
510 return *xa
< *xb
? -1 : 1;
520 (a
, b
) Bigint
*a
, *b
;
522 (Bigint
*a
, Bigint
*b
)
527 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
564 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
565 borrow
= y
>> 32 & 1UL;
566 *xc
++ = y
& 0xffffffffUL
;
571 borrow
= y
>> 32 & 1UL;
572 *xc
++ = y
& 0xffffffffUL
;
577 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
578 borrow
= (y
& 0x10000) >> 16;
579 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
580 borrow
= (z
& 0x10000) >> 16;
585 y
= (*xa
& 0xffff) - borrow
;
586 borrow
= (y
& 0x10000) >> 16;
587 z
= (*xa
++ >> 16) - borrow
;
588 borrow
= (z
& 0x10000) >> 16;
593 y
= *xa
++ - *xb
++ - borrow
;
594 borrow
= (y
& 0x10000) >> 16;
600 borrow
= (y
& 0x10000) >> 16;
614 (a
, e
) Bigint
*a
; int *e
;
619 ULong
*xa
, *xa0
, w
, y
, z
;
633 if (!y
) Bug("zero y in b2d");
639 d0
= Exp_1
| y
>> Ebits
- k
;
640 w
= xa
> xa0
? *--xa
: 0;
641 d1
= y
<< (32-Ebits
) + k
| w
>> Ebits
- k
;
644 z
= xa
> xa0
? *--xa
: 0;
646 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
647 y
= xa
> xa0
? *--xa
: 0;
648 d1
= z
<< k
| y
>> 32 - k
;
655 if (k
< Ebits
+ 16) {
656 z
= xa
> xa0
? *--xa
: 0;
657 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
658 w
= xa
> xa0
? *--xa
: 0;
659 y
= xa
> xa0
? *--xa
: 0;
660 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
663 z
= xa
> xa0
? *--xa
: 0;
664 w
= xa
> xa0
? *--xa
: 0;
666 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
667 y
= xa
> xa0
? *--xa
: 0;
668 d1
= w
<< k
+ 16 | y
<< k
;
672 word0(d
) = d0
>> 16 | d0
<< 16;
673 word1(d
) = d1
>> 16 | d1
<< 16;
683 (d
, e
, bits
) double d
; int *e
, *bits
;
685 (double d
, int *e
, int *bits
)
693 d0
= word0(d
) >> 16 | word0(d
) << 16;
694 d1
= word1(d
) >> 16 | word1(d
) << 16;
708 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
709 #ifdef Sudden_Underflow
710 de
= (int)(d0
>> Exp_shift
);
715 if ( (de
= (int)(d0
>> Exp_shift
)) !=0)
720 if ( (k
= lo0bits(&y
)) !=0) {
721 x
[0] = y
| z
<< 32 - k
;
726 i
= b
->wds
= (x
[1] = z
) !=0 ? 2 : 1;
731 Bug("Zero passed to d2b");
740 if ( (k
= lo0bits(&y
)) !=0)
742 x
[0] = y
| z
<< 32 - k
& 0xffff;
743 x
[1] = z
>> k
- 16 & 0xffff;
749 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
750 x
[2] = z
>> k
& 0xffff;
765 Bug("Zero passed to d2b");
783 #ifndef Sudden_Underflow
787 *e
= (de
- Bias
- (P
-1) << 2) + k
;
788 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
790 *e
= de
- Bias
- (P
-1) + k
;
793 #ifndef Sudden_Underflow
796 *e
= de
- Bias
- (P
-1) + 1 + k
;
798 *bits
= 32*i
- hi0bits(x
[i
-1]);
800 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
811 bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
812 CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256
816 bigtens
[] = { 1e16
, 1e32
, 1e64
};
817 CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
819 bigtens
[] = { 1e16
, 1e32
};
820 CONST
double tinytens
[] = { 1e-16, 1e-32 };
826 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
827 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
836 strcp_D2A(a
, b
) char *a
; char *b
;
838 strcp_D2A(char *a
, CONST
char *b
)
850 memcpy_D2A(a
, b
, len
) Char
*a
; Char
*b
; size_t len
;
852 memcpy_D2A(void *a1
, void *b1
, size_t len
)
855 register char *a
= (char*)a1
, *ae
= a
+ len
;
856 register char *b
= (char*)b1
, *a0
= a
;
862 #endif /* NO_STRING_H */