]>
git.saurik.com Git - apple/libc.git/blob - gdtoa/FreeBSD/gdtoa-strtodg.c
c436ea9a8a96eac36e5cf899ab6bb6cc7dde3f98
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998-2001 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 "."). */
32 #include "xlocale_private.h"
40 #define fivesbits __fivesbits_D2A
41 #define all_on __all_on_D2A
42 #define set_ones __set_ones_D2A
43 #define rvOK __rvOK_D2A
44 #define mantbits __mantbits_D2A
46 #ifdef BUILDING_VARIANT
47 extern CONST
int fivesbits
[];
48 int all_on(Bigint
*b
, int n
);
49 Bigint
*set_ones(Bigint
*b
, int n
);
50 int rvOK(U
*d
, CONST FPI
*fpi
, Long
*exp
, ULong
*bits
, int exact
, int rd
, int *irv
);
52 #else /* !BUILDING_VARIANT */
54 __private_extern__ CONST
int
55 fivesbits
[] = { 0, 3, 5, 7, 10, 12, 14, 17, 19, 21,
56 24, 26, 28, 31, 33, 35, 38, 40, 42, 45,
65 increment(b
) Bigint
*b
;
80 if (*x
< (ULong
)0xffffffffL
) {
97 if (b
->wds
>= b
->maxwds
) {
110 decrement(b
) Bigint
*b
;
134 borrow
= (y
& 0x10000) >> 16;
136 } while(borrow
&& x
< xe
);
140 __private_extern__
int
142 all_on(b
, n
) Bigint
*b
; int n
;
144 all_on(Bigint
*b
, int n
)
150 xe
= x
+ (n
>> kshift
);
152 if ((*x
++ & ALL_ON
) != ALL_ON
)
155 return ((*x
| (ALL_ON
<< n
)) & ALL_ON
) == ALL_ON
;
161 set_ones(b
, n
) Bigint
*b
; int n
;
163 set_ones(Bigint
*b
, int n
)
169 k
= (n
+ ((1 << kshift
) - 1)) >> kshift
;
183 x
[-1] >>= ULbits
- n
;
187 __private_extern__
int
190 (d
, fpi
, exp
, bits
, exact
, rd
, irv
)
191 U
*d
; CONST FPI
*fpi
; Long
*exp
; ULong
*bits
; int exact
, rd
, *irv
;
193 (U
*d
, CONST FPI
*fpi
, Long
*exp
, ULong
*bits
, int exact
, int rd
, int *irv
)
197 ULong carry
, inex
, lostbits
;
198 int bdif
, e
, j
, k
, k1
, nb
, rv
;
201 b
= d2b(dval(d
), &e
, &bdif
);
202 bdif
-= nb
= fpi
->nbits
;
211 #ifndef IMPRECISE_INEXACT
224 case 1: /* round down (toward -Infinity) */
226 case 2: /* round up (toward +Infinity) */
228 default: /* round near */
239 if (b
->x
[k
>>kshift
] & ((ULong
)1 << (k
& kmask
)))
243 /* "break" cases: round up 1 bit, then truncate; bdif > 0 */
248 if ( (lostbits
= any_on(b
, bdif
)) !=0)
249 inex
= STRTOG_Inexlo
;
252 inex
= STRTOG_Inexhi
;
254 if ( (j
= nb
& kmask
) !=0)
256 if (hi0bits(b
->x
[b
->wds
- 1]) != j
) {
258 lostbits
= b
->x
[0] & 1;
265 b
= lshift(b
, -bdif
);
269 if (k
> nb
|| fpi
->sudden_underflow
) {
271 *irv
= STRTOG_Underflow
| STRTOG_Inexlo
;
275 if (k1
> 0 && !lostbits
)
276 lostbits
= any_on(b
, k1
);
277 if (!lostbits
&& !exact
)
280 carry
= b
->x
[k1
>>kshift
] & (1 << (k1
& kmask
));
282 *irv
= STRTOG_Denormal
;
285 inex
= STRTOG_Inexhi
| STRTOG_Underflow
;
288 inex
= STRTOG_Inexlo
| STRTOG_Underflow
;
291 else if (e
> fpi
->emax
) {
293 *irv
= STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
300 copybits(bits
, nb
, b
);
308 __private_extern__
int
317 L
= word1(d
) << 16 | word1(d
) >> 16;
320 if ( (L
= word1(d
)) !=0)
322 return P
- lo0bits(&L
);
324 L
= word0(d
) << 16 | word0(d
) >> 16 | Exp_msk11
;
326 L
= word0(d
) | Exp_msk1
;
328 return P
- 32 - lo0bits(&L
);
331 #endif /* BUILDING_VARIANT */
336 (s00
, se
, fpi
, exp
, bits
, loc
)
337 CONST
char *s00
; char **se
; CONST FPI
*fpi
; Long
*exp
; ULong
*bits
; locale_t loc
;
339 (CONST
char *s00
, char **se
, CONST FPI
*fpi
, Long
*exp
, ULong
*bits
, locale_t loc
)
342 int abe
, abits
, asub
;
343 int bb0
, bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, decpt
, denorm
;
344 int dsign
, e
, e1
, e2
, emin
, esign
, finished
, i
, inex
, irv
;
345 int j
, k
, nbits
, nd
, nd0
, nf
, nz
, nz0
, rd
, rvbits
, rve
, rve1
, sign
;
346 int sudden_underflow
;
347 CONST
char *s
, *s0
, *s1
;
353 Bigint
*ab
, *bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
, *rvb
, *rvb0
;
354 #ifdef USE_LOCALE /*{{*/
355 NORMALIZE_LOCALE(loc
);
356 #ifdef NO_LOCALE_CACHE
357 char *decimalpoint
= localeconv_l(loc
)->decimal_point
;
358 int dplen
= strlen(decimalpoint
);
361 static char *decimalpoint_cache
;
363 if (!(s0
= decimalpoint_cache
)) {
364 s0
= localeconv_l(loc
)->decimal_point
;
365 if ((decimalpoint_cache
= (char*)MALLOC(strlen(s0
) + 1))) {
366 strcpy(decimalpoint_cache
, s0
);
367 s0
= decimalpoint_cache
;
371 decimalpoint
= (char*)s0
;
372 #endif /*NO_LOCALE_CACHE*/
373 #else /*USE_LOCALE}{*/
375 #endif /*USE_LOCALE}}*/
378 denorm
= sign
= nz0
= nz
= 0;
382 for(s
= s00
;;s
++) switch(*s
) {
392 irv
= STRTOG_NoNumber
;
411 irv
= gethex(&s
, fpi
, exp
, &rvb
, sign
, loc
);
412 if (irv
== STRTOG_NoNumber
) {
424 sudden_underflow
= fpi
->sudden_underflow
;
427 for(decpt
= nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
434 if (c
== *decimalpoint
) {
435 for(i
= 1; decimalpoint
[i
]; ++i
)
436 if (s
[i
] != decimalpoint
[i
])
446 for(; c
== '0'; c
= *++s
)
448 if (c
> '0' && c
<= '9') {
456 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
461 for(i
= 1; i
< nz
; i
++)
464 else if (nd
<= DBL_DIG
+ 1)
468 else if (nd
<= DBL_DIG
+ 1)
476 if (c
== 'e' || c
== 'E') {
477 if (!nd
&& !nz
&& !nz0
) {
478 irv
= STRTOG_NoNumber
;
490 if (c
>= '0' && c
<= '9') {
493 if (c
> '0' && c
<= '9') {
496 while((c
= *++s
) >= '0' && c
<= '9')
498 if (s
- s1
> 8 || L
> 19999)
499 /* Avoid confusion from exponents
500 * so large that e might overflow.
502 e
= 19999; /* safe for 16 bit ints */
517 /* Check for Nan and Infinity */
522 if (match(&s
,"nf")) {
524 if (!match(&s
,"inity"))
526 irv
= STRTOG_Infinite
;
532 if (match(&s
, "an")) {
534 *exp
= fpi
->emax
+ 1;
537 irv
= hexnan(&s
, fpi
, bits
);
542 #endif /* INFNAN_CHECK */
543 irv
= STRTOG_NoNumber
;
548 TRUNCATE_DIGITS(s0
, strunc
, nd
, nd0
, nf
, fpi
->nbits
, fpi
->emin
, dplen
);
553 switch(fpi
->rounding
& 3) {
564 /* Now we have nd0 digits, starting at s0, followed by a
565 * decimal point, followed by nd-nd0 digits. The number we're
566 * after is the integer represented by those digits times
571 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
574 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
576 if (nbits
<= P
&& nd
<= DBL_DIG
) {
578 if (rvOK(&rv
, fpi
, exp
, bits
, 1, rd
, &irv
))
586 i
= fivesbits
[e
] + mantbits(&rv
) <= P
;
587 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
588 if (rvOK(&rv
, fpi
, exp
, bits
, i
, rd
, &irv
))
595 if (e
<= Ten_pmax
+ i
) {
596 /* A fancier test would sometimes let us do
597 * this for larger i values.
601 dval(&rv
) *= tens
[i
];
603 /* VAX exponent range is so narrow we must
604 * worry about overflow here...
607 dval(&adj
) = dval(&rv
);
608 word0(&adj
) -= P
*Exp_msk1
;
609 /* adj = */ rounded_product(dval(&adj
), tens
[e2
]);
610 if ((word0(&adj
) & Exp_mask
)
611 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
613 word0(&adj
) += P
*Exp_msk1
;
614 dval(&rv
) = dval(&adj
);
616 /* rv = */ rounded_product(dval(&rv
), tens
[e2
]);
618 if (rvOK(&rv
, fpi
, exp
, bits
, 0, rd
, &irv
))
623 #ifndef Inaccurate_Divide
624 else if (e
>= -Ten_pmax
) {
625 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
626 if (rvOK(&rv
, fpi
, exp
, bits
, 0, rd
, &irv
))
635 /* Get starting approximation = rv * 10**e1 */
639 if ( (i
= e1
& 15) !=0)
640 dval(&rv
) *= tens
[i
];
643 while(e1
>= (1 << (n_bigtens
-1))) {
644 e2
+= ((word0(&rv
) & Exp_mask
)
645 >> Exp_shift1
) - Bias
;
646 word0(&rv
) &= ~Exp_mask
;
647 word0(&rv
) |= Bias
<< Exp_shift1
;
648 dval(&rv
) *= bigtens
[n_bigtens
-1];
649 e1
-= 1 << (n_bigtens
-1);
651 e2
+= ((word0(&rv
) & Exp_mask
) >> Exp_shift1
) - Bias
;
652 word0(&rv
) &= ~Exp_mask
;
653 word0(&rv
) |= Bias
<< Exp_shift1
;
654 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
656 dval(&rv
) *= bigtens
[j
];
661 if ( (i
= e1
& 15) !=0)
662 dval(&rv
) /= tens
[i
];
665 while(e1
>= (1 << (n_bigtens
-1))) {
666 e2
+= ((word0(&rv
) & Exp_mask
)
667 >> Exp_shift1
) - Bias
;
668 word0(&rv
) &= ~Exp_mask
;
669 word0(&rv
) |= Bias
<< Exp_shift1
;
670 dval(&rv
) *= tinytens
[n_bigtens
-1];
671 e1
-= 1 << (n_bigtens
-1);
673 e2
+= ((word0(&rv
) & Exp_mask
) >> Exp_shift1
) - Bias
;
674 word0(&rv
) &= ~Exp_mask
;
675 word0(&rv
) |= Bias
<< Exp_shift1
;
676 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
678 dval(&rv
) *= tinytens
[j
];
682 /* e2 is a correction to the (base 2) exponent of the return
683 * value, reflecting adjustments above to avoid overflow in the
684 * native arithmetic. For native IBM (base 16) arithmetic, we
685 * must multiply e2 by 4 to change from base 16 to 2.
689 rvb
= d2b(dval(&rv
), &rve
, &rvbits
); /* rv = rvb * 2^rve */
691 if ((j
= rvbits
- nbits
) > 0) {
696 bb0
= 0; /* trailing zero bits in rvb */
697 e2
= rve
+ rvbits
- nbits
;
698 if (e2
> fpi
->emax
+ 1)
700 rve1
= rve
+ rvbits
- nbits
;
701 if (e2
< (emin
= fpi
->emin
)) {
705 rvb
= lshift(rvb
, j
);
716 irv
= STRTOG_Underflow
| STRTOG_Inexlo
;
717 /* When __DARWIN_UNIX03 is set, we don't need this (errno is set later) */
718 #if !defined(NO_ERRNO) && !__DARWIN_UNIX03
723 rvb
->x
[0] = rvb
->wds
= rvbits
= 1;
729 if (sudden_underflow
&& e2
+ 1 < emin
)
733 /* Now the hard part -- adjusting rv to the correct value.*/
735 /* Put digits into bd: true value = bd * 10^e */
737 bd0
= s2b(s0
, nd0
, nd
, y
, dplen
);
744 bbbits
= rvbits
- bb0
;
761 j
= nbits
+ 1 - bbbits
;
762 i
= bbe
+ bbbits
- nbits
;
763 if (i
< emin
) /* denormal */
767 i
= bb2
< bd2
? bb2
: bd2
;
776 bs
= pow5mult(bs
, bb5
);
783 bb
= lshift(bb
, bb2
);
787 bd
= pow5mult(bd
, bd5
);
789 bd
= lshift(bd
, bd2
);
791 bs
= lshift(bs
, bs2
);
793 inex
= STRTOG_Inexhi
;
794 delta
= diff(bb
, bd
);
795 if (delta
->wds
<= 1 && !delta
->x
[0])
798 delta
->sign
= finished
= 0;
803 if ( (finished
= dsign
^ (rd
&1)) !=0) {
805 irv
|= STRTOG_Inexhi
;
808 irv
|= STRTOG_Inexlo
;
811 for(i
= 0, j
= nbits
; j
>= ULbits
;
813 if (rvb
->x
[i
] & ALL_ON
)
816 if (j
> 1 && lo0bits(rvb
->x
+ i
) < j
- 1)
819 rvb
= set_ones(rvb
, rvbits
= nbits
);
822 irv
|= dsign
? STRTOG_Inexlo
: STRTOG_Inexhi
;
826 /* Error is less than half an ulp -- check for
827 * special case of mantissa a power of two.
830 ? STRTOG_Normal
| STRTOG_Inexlo
831 : STRTOG_Normal
| STRTOG_Inexhi
;
832 if (dsign
|| bbbits
> 1 || denorm
|| rve1
== emin
)
834 delta
= lshift(delta
,1);
835 if (cmp(delta
, bs
) > 0) {
836 irv
= STRTOG_Normal
| STRTOG_Inexlo
;
842 /* exactly half-way between */
844 if (denorm
&& all_on(rvb
, rvbits
)) {
845 /*boundary case -- increment exponent*/
848 rve
= emin
+ nbits
- (rvbits
= 1);
849 irv
= STRTOG_Normal
| STRTOG_Inexhi
;
853 irv
= STRTOG_Normal
| STRTOG_Inexlo
;
855 else if (bbbits
== 1) {
858 /* boundary case -- decrement exponent */
860 irv
= STRTOG_Normal
| STRTOG_Inexhi
;
861 if (rvb
->wds
== 1 && rvb
->x
[0] == 1)
862 sudden_underflow
= 1;
866 rvb
= set_ones(rvb
, rvbits
= nbits
);
870 irv
= STRTOG_Normal
| STRTOG_Inexhi
;
871 if ((bbbits
< nbits
&& !denorm
) || !(rvb
->x
[0] & 1))
874 rvb
= increment(rvb
);
875 j
= kmask
& (ULbits
- (rvbits
& kmask
));
876 if (hi0bits(rvb
->x
[rvb
->wds
- 1]) != j
)
878 irv
= STRTOG_Normal
| STRTOG_Inexhi
;
884 irv
= STRTOG_Normal
| STRTOG_Inexlo
;
888 if ((dval(&adj
) = ratio(delta
, bs
)) <= 2.) {
890 inex
= STRTOG_Inexlo
;
893 inex
= STRTOG_Inexhi
;
895 else if (denorm
&& bbbits
<= 1) {
899 irv
= STRTOG_Underflow
| STRTOG_Inexlo
;
902 adj0
= dval(&adj
) = 1.;
905 adj0
= dval(&adj
) *= 0.5;
908 inex
= STRTOG_Inexlo
;
910 if (dval(&adj
) < 2147483647.) {
919 if (asub
&& adj0
> 0.)
923 if (!asub
&& adj0
> 0.) {
926 inex
= STRTOG_Inexact
- inex
;
934 /* adj *= ulp(dval(&rv)); */
935 /* if (asub) rv -= adj; else rv += adj; */
937 if (!denorm
&& rvbits
< nbits
) {
938 rvb
= lshift(rvb
, j
= nbits
- rvbits
);
942 ab
= d2b(dval(&adj
), &abe
, &abits
);
946 ab
= lshift(ab
, abe
);
950 j
= hi0bits(rvb
->x
[rvb
->wds
-1]);
955 else if (rvb
->wds
<= k
956 || hi0bits( rvb
->x
[k
]) >
957 hi0bits(rvb0
->x
[k
])) {
958 /* unlikely; can only have lost 1 high bit */
964 rvb
= lshift(rvb
, 1);
975 || hi0bits(rvb
->x
[k
]) < hi0bits(rvb0
->x
[k
])) {
977 if (++rvbits
== nbits
)
995 /* Can we stop now? */
996 tol
= dval(&adj
) * 5e-16; /* > max rel error */
997 dval(&adj
) = adj0
- .5;
998 if (dval(&adj
) < -tol
) {
1004 else if (dval(&adj
) > tol
&& adj0
< 1. - tol
) {
1009 bb0
= denorm
? 0 : trailz(rvb
);
1015 if (!denorm
&& (j
= nbits
- rvbits
)) {
1017 rvb
= lshift(rvb
, j
);
1028 if (rve
> fpi
->emax
) {
1029 switch(fpi
->rounding
& 3) {
1030 case FPI_Round_near
:
1036 case FPI_Round_down
:
1040 /* Round to largest representable magnitude */
1043 irv
= STRTOG_Normal
| STRTOG_Inexlo
;
1046 be
= b
+ ((fpi
->nbits
+ 31) >> 5);
1049 if ((j
= fpi
->nbits
& 0x1f))
1054 irv
= STRTOG_Infinite
| STRTOG_Overflow
| STRTOG_Inexhi
;
1059 *exp
= fpi
->emax
+ 1;
1063 if (sudden_underflow
) {
1065 irv
= STRTOG_Underflow
| STRTOG_Inexlo
;
1066 #if !defined(NO_ERRNO) && __DARWIN_UNIX03
1071 irv
= (irv
& ~STRTOG_Retmask
) |
1072 (rvb
->wds
> 0 ? STRTOG_Denormal
: STRTOG_Zero
);
1073 if (irv
& STRTOG_Inexact
) {
1074 irv
|= STRTOG_Underflow
;
1075 #if !defined(NO_ERRNO) && __DARWIN_UNIX03
1086 copybits(bits
, nbits
, rvb
);