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 /* $FreeBSD: head/contrib/gdtoa/strtod.c 227753 2011-11-20 14:45:42Z theraven $ */
45 #define Avoid_Underflow
47 /* The factor of 2^106 in tinytens[4] helps us avoid setting the underflow */
48 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
49 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
50 9007199254740992.*9007199254740992.e
-256
55 #ifdef Honor_FLT_ROUNDS
56 #undef Check_FLT_ROUNDS
57 #define Check_FLT_ROUNDS
59 #define Rounding Flt_Rounds
62 #ifdef Avoid_Underflow /*{*/
66 (x
, scale
) U
*x
; int scale
;
76 if (!scale
|| (i
= 2*P
+ 1 - ((word0(x
) & Exp_mask
) >> Exp_shift
)) <= 0)
77 return rv
; /* Is there an example where i <= 0 ? */
78 word0(&u
) = Exp_1
+ (i
<< Exp_shift
);
87 (s00
, se
, loc
) CONST
char *s00
; char **se
; locale_t loc
89 (CONST
char *s00
, char **se
, locale_t loc
)
92 #ifdef Avoid_Underflow
95 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, decpt
, dsign
,
96 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
97 CONST
char *s
, *s0
, *s1
;
100 U adj
, aadj1
, rv
, rv0
;
102 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
103 #ifdef Avoid_Underflow
107 int inexact
, oldinexact
;
109 #ifdef USE_LOCALE /*{{*/
110 #ifdef NO_LOCALE_CACHE
111 char *decimalpoint
= localeconv_l(loc
)->decimal_point
;
112 int dplen
= strlen(decimalpoint
);
115 static char *decimalpoint_cache
;
117 if (!(s0
= decimalpoint_cache
)) {
118 s0
= localeconv_l(loc
)->decimal_point
;
119 if ((decimalpoint_cache
= (char*)MALLOC(strlen(s0
) + 1))) {
120 strcpy(decimalpoint_cache
, s0
);
121 s0
= decimalpoint_cache
;
125 decimalpoint
= (char*)s0
;
126 #endif /*NO_LOCALE_CACHE*/
127 #else /*USE_LOCALE}{*/
129 #endif /*USE_LOCALE}}*/
131 #ifdef Honor_FLT_ROUNDS /*{*/
133 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
134 Rounding
= Flt_Rounds
;
137 switch(fegetround()) {
138 case FE_TOWARDZERO
: Rounding
= 0; break;
139 case FE_UPWARD
: Rounding
= 2; break;
140 case FE_DOWNWARD
: Rounding
= 3;
145 sign
= nz0
= nz
= decpt
= 0;
147 for(s
= s00
;;s
++) switch(*s
) {
169 #ifndef NO_HEX_FP /*{*/
171 static FPI fpi
= { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
178 #ifdef Honor_FLT_ROUNDS
180 fpi1
.rounding
= Rounding
;
184 switch((i
= gethex(&s
, &fpi1
, &exp
, &bb
, sign
)) & STRTOG_Retmask
) {
185 case STRTOG_NoNumber
:
192 copybits(bits
, fpi
.nbits
, bb
);
195 ULtod(((U
*)&rv
)->L
, bits
, exp
, i
);
208 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
215 if (c
== *decimalpoint
) {
216 for(i
= 1; decimalpoint
[i
]; ++i
)
217 if (s
[i
] != decimalpoint
[i
])
227 for(; c
== '0'; c
= *++s
)
229 if (c
> '0' && c
<= '9') {
237 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
242 for(i
= 1; i
< nz
; i
++)
245 else if (nd
<= DBL_DIG
+ 1)
249 else if (nd
<= DBL_DIG
+ 1)
257 if (c
== 'e' || c
== 'E') {
258 if (!nd
&& !nz
&& !nz0
) {
269 if (c
>= '0' && c
<= '9') {
272 if (c
> '0' && c
<= '9') {
275 while((c
= *++s
) >= '0' && c
<= '9')
277 if (s
- s1
> 8 || L
> 19999)
278 /* Avoid confusion from exponents
279 * so large that e might overflow.
281 e
= 19999; /* safe for 16 bit ints */
296 /* Check for Nan and Infinity */
298 static FPI fpinan
= /* only 52 explicit bits */
299 { 52, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
304 if (match(&s
,"nf")) {
306 if (!match(&s
,"inity"))
308 word0(&rv
) = 0x7ff00000;
315 if (match(&s
, "an")) {
318 && hexnan(&s
, &fpinan
, bits
)
320 word0(&rv
) = 0x7ff80000 | bits
[1];
321 word1(&rv
) = bits
[0];
325 word0(&rv
) = NAN_WORD0
;
326 word1(&rv
) = NAN_WORD1
;
333 #endif /* INFNAN_CHECK */
342 /* Now we have nd0 digits, starting at s0, followed by a
343 * decimal point, followed by nd-nd0 digits. The number we're
344 * after is the integer represented by those digits times
349 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
354 oldinexact
= get_inexact();
356 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
361 #ifndef Honor_FLT_ROUNDS
368 #ifndef ROUND_BIASED_without_Round_Up
374 #ifdef Honor_FLT_ROUNDS
375 /* round correctly FLT_ROUNDS = 2 or 3 */
381 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
386 if (e
<= Ten_pmax
+ i
) {
387 /* A fancier test would sometimes let us do
388 * this for larger i values.
390 #ifdef Honor_FLT_ROUNDS
391 /* round correctly FLT_ROUNDS = 2 or 3 */
398 dval(&rv
) *= tens
[i
];
400 /* VAX exponent range is so narrow we must
401 * worry about overflow here...
404 word0(&rv
) -= P
*Exp_msk1
;
405 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
406 if ((word0(&rv
) & Exp_mask
)
407 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
409 word0(&rv
) += P
*Exp_msk1
;
411 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
416 #ifndef Inaccurate_Divide
417 else if (e
>= -Ten_pmax
) {
418 #ifdef Honor_FLT_ROUNDS
419 /* round correctly FLT_ROUNDS = 2 or 3 */
425 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
429 #endif /* ROUND_BIASED_without_Round_Up */
437 oldinexact
= get_inexact();
439 #ifdef Avoid_Underflow
442 #ifdef Honor_FLT_ROUNDS
445 Rounding
= Rounding
== 2 ? 0 : 2;
451 #endif /*IEEE_Arith*/
453 /* Get starting approximation = rv * 10**e1 */
456 if ( (i
= e1
& 15) !=0)
457 dval(&rv
) *= tens
[i
];
459 if (e1
> DBL_MAX_10_EXP
) {
461 /* Can't trust HUGE_VAL */
463 #ifdef Honor_FLT_ROUNDS
465 case 0: /* toward 0 */
466 case 3: /* toward -infinity */
471 word0(&rv
) = Exp_mask
;
474 #else /*Honor_FLT_ROUNDS*/
475 word0(&rv
) = Exp_mask
;
477 #endif /*Honor_FLT_ROUNDS*/
479 /* set overflow bit */
481 dval(&rv0
) *= dval(&rv0
);
486 #endif /*IEEE_Arith*/
501 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
503 dval(&rv
) *= bigtens
[j
];
504 /* The last multiplication could overflow. */
505 word0(&rv
) -= P
*Exp_msk1
;
506 dval(&rv
) *= bigtens
[j
];
507 if ((z
= word0(&rv
) & Exp_mask
)
508 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
510 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
511 /* set to largest number */
512 /* (Can't trust DBL_MAX) */
517 word0(&rv
) += P
*Exp_msk1
;
522 if ( (i
= e1
& 15) !=0)
523 dval(&rv
) /= tens
[i
];
525 if (e1
>= 1 << n_bigtens
)
527 #ifdef Avoid_Underflow
530 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
532 dval(&rv
) *= tinytens
[j
];
533 if (scale
&& (j
= 2*P
+ 1 - ((word0(&rv
) & Exp_mask
)
534 >> Exp_shift
)) > 0) {
535 /* scaled rv is denormal; zap j low bits */
539 word0(&rv
) = (P
+2)*Exp_msk1
;
541 word0(&rv
) &= 0xffffffff << (j
-32);
544 word1(&rv
) &= 0xffffffff << j
;
547 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
549 dval(&rv
) *= tinytens
[j
];
550 /* The last multiplication could underflow. */
551 dval(&rv0
) = dval(&rv
);
552 dval(&rv
) *= tinytens
[j
];
554 dval(&rv
) = 2.*dval(&rv0
);
555 dval(&rv
) *= tinytens
[j
];
562 #ifndef Avoid_Underflow
565 /* The refinement below will clean
566 * this approximation up.
573 /* Now the hard part -- adjusting rv to the correct value.*/
575 /* Put digits into bd: true value = bd * 10^e */
577 bd0
= s2b(s0
, nd0
, nd
, y
, dplen
);
582 bb
= d2b(dval(&rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
598 #ifdef Honor_FLT_ROUNDS
602 #ifdef Avoid_Underflow
606 i
= j
+ bbbits
- 1; /* logb(rv) */
608 if (i
< Emin
) { /* denormal */
614 Lsb1
= Lsb
<< (i
-32);
616 #else /*Avoid_Underflow*/
617 #ifdef Sudden_Underflow
619 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
623 #else /*Sudden_Underflow*/
625 i
= j
+ bbbits
- 1; /* logb(&rv) */
626 if (i
< Emin
) /* denormal */
630 #endif /*Sudden_Underflow*/
631 #endif /*Avoid_Underflow*/
634 #ifdef Avoid_Underflow
637 i
= bb2
< bd2
? bb2
: bd2
;
646 bs
= pow5mult(bs
, bb5
);
652 bb
= lshift(bb
, bb2
);
654 bd
= pow5mult(bd
, bd5
);
656 bd
= lshift(bd
, bd2
);
658 bs
= lshift(bs
, bs2
);
659 delta
= diff(bb
, bd
);
663 #ifdef Honor_FLT_ROUNDS
666 /* Error is less than an ulp */
667 if (!delta
->x
[0] && delta
->wds
<= 1) {
683 && !(word0(&rv
) & Frac_mask
)) {
684 y
= word0(&rv
) & Exp_mask
;
685 #ifdef Avoid_Underflow
686 if (!scale
|| y
> 2*P
*Exp_msk1
)
691 delta
= lshift(delta
,Log2P
);
692 if (cmp(delta
, bs
) <= 0)
697 #ifdef Avoid_Underflow
698 if (scale
&& (y
= word0(&rv
) & Exp_mask
)
700 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
702 #ifdef Sudden_Underflow
703 if ((word0(&rv
) & Exp_mask
) <=
705 word0(&rv
) += P
*Exp_msk1
;
706 dval(&rv
) += adj
*ulp(&rv
);
707 word0(&rv
) -= P
*Exp_msk1
;
710 #endif /*Sudden_Underflow*/
711 #endif /*Avoid_Underflow*/
712 dval(&rv
) += adj
.d
*ulp(&rv
);
716 dval(&adj
) = ratio(delta
, bs
);
719 if (adj
.d
<= 0x7ffffffe) {
720 /* dval(&adj) = Rounding ? ceil(&adj) : floor(&adj); */
723 if (!((Rounding
>>1) ^ dsign
))
728 #ifdef Avoid_Underflow
729 if (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
730 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
732 #ifdef Sudden_Underflow
733 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
734 word0(&rv
) += P
*Exp_msk1
;
735 dval(&adj
) *= ulp(&rv
);
740 word0(&rv
) -= P
*Exp_msk1
;
743 #endif /*Sudden_Underflow*/
744 #endif /*Avoid_Underflow*/
745 dval(&adj
) *= ulp(&rv
);
747 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
755 #endif /*Honor_FLT_ROUNDS*/
758 /* Error is less than half an ulp -- check for
759 * special case of mantissa a power of two.
761 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
763 #ifdef Avoid_Underflow
764 || (word0(&rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
766 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
771 if (!delta
->x
[0] && delta
->wds
<= 1)
776 if (!delta
->x
[0] && delta
->wds
<= 1) {
783 delta
= lshift(delta
,Log2P
);
784 if (cmp(delta
, bs
) > 0)
789 /* exactly half-way between */
791 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
793 #ifdef Avoid_Underflow
794 (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
795 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
798 /*boundary case -- increment exponent*/
799 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
801 word0(&rv
) = (word0(&rv
) & Exp_mask
)
808 #ifdef Avoid_Underflow
814 else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
816 /* boundary case -- decrement exponent */
817 #ifdef Sudden_Underflow /*{{*/
818 L
= word0(&rv
) & Exp_mask
;
822 #ifdef Avoid_Underflow
823 if (L
<= (scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
826 #endif /*Avoid_Underflow*/
830 #else /*Sudden_Underflow}{*/
831 #ifdef Avoid_Underflow
833 L
= word0(&rv
) & Exp_mask
;
834 if (L
<= (2*P
+1)*Exp_msk1
) {
835 if (L
> (P
+2)*Exp_msk1
)
839 /* rv = smallest denormal */
843 #endif /*Avoid_Underflow*/
844 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
845 #endif /*Sudden_Underflow}}*/
846 word0(&rv
) = L
| Bndry_mask1
;
847 word1(&rv
) = 0xffffffff;
855 #ifdef Avoid_Underflow
857 if (!(word0(&rv
) & Lsb1
))
860 else if (!(word1(&rv
) & Lsb
))
863 if (!(word1(&rv
) & LSB
))
868 #ifdef Avoid_Underflow
869 dval(&rv
) += sulp(&rv
, scale
);
871 dval(&rv
) += ulp(&rv
);
875 #ifdef Avoid_Underflow
876 dval(&rv
) -= sulp(&rv
, scale
);
878 dval(&rv
) -= ulp(&rv
);
880 #ifndef Sudden_Underflow
885 #ifdef Avoid_Underflow
891 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
893 aadj
= dval(&aadj1
) = 1.;
894 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
895 #ifndef Sudden_Underflow
896 if (word1(&rv
) == Tiny1
&& !word0(&rv
))
903 /* special case -- power of FLT_RADIX to be */
904 /* rounded down... */
906 if (aadj
< 2./FLT_RADIX
)
910 dval(&aadj1
) = -aadj
;
915 dval(&aadj1
) = dsign
? aadj
: -aadj
;
916 #ifdef Check_FLT_ROUNDS
918 case 2: /* towards +infinity */
921 case 0: /* towards 0 */
922 case 3: /* towards -infinity */
928 #endif /*Check_FLT_ROUNDS*/
930 y
= word0(&rv
) & Exp_mask
;
932 /* Check for overflow */
934 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
935 dval(&rv0
) = dval(&rv
);
936 word0(&rv
) -= P
*Exp_msk1
;
937 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
938 dval(&rv
) += dval(&adj
);
939 if ((word0(&rv
) & Exp_mask
) >=
940 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
941 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
948 word0(&rv
) += P
*Exp_msk1
;
951 #ifdef Avoid_Underflow
952 if (scale
&& y
<= 2*P
*Exp_msk1
) {
953 if (aadj
<= 0x7fffffff) {
957 dval(&aadj1
) = dsign
? aadj
: -aadj
;
959 word0(&aadj1
) += (2*P
+1)*Exp_msk1
- y
;
961 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
962 dval(&rv
) += dval(&adj
);
964 #ifdef Sudden_Underflow
965 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
966 dval(&rv0
) = dval(&rv
);
967 word0(&rv
) += P
*Exp_msk1
;
968 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
971 if ((word0(&rv
) & Exp_mask
) < P
*Exp_msk1
)
973 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
)
976 if (word0(&rv0
) == Tiny0
977 && word1(&rv0
) == Tiny1
)
984 word0(&rv
) -= P
*Exp_msk1
;
987 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
990 #else /*Sudden_Underflow*/
991 /* Compute dval(&adj) so that the IEEE rounding rules will
992 * correctly round rv + dval(&adj) in some half-way cases.
993 * If rv * ulp(&rv) is denormalized (i.e.,
994 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
995 * trouble from bits lost to denormalization;
996 * example: 1.2e-307 .
998 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
999 dval(&aadj1
) = (double)(int)(aadj
+ 0.5);
1001 dval(&aadj1
) = -dval(&aadj1
);
1003 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
1005 #endif /*Sudden_Underflow*/
1006 #endif /*Avoid_Underflow*/
1008 z
= word0(&rv
) & Exp_mask
;
1010 #ifdef Avoid_Underflow
1014 /* Can we stop now? */
1017 /* The tolerances below are conservative. */
1018 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1019 if (aadj
< .4999999 || aadj
> .5000001)
1022 else if (aadj
< .4999999/FLT_RADIX
)
1040 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
1045 else if (!oldinexact
)
1048 #ifdef Avoid_Underflow
1050 word0(&rv0
) = Exp_1
- 2*P
*Exp_msk1
;
1052 dval(&rv
) *= dval(&rv0
);
1054 /* try to avoid the bug of testing an 8087 register value */
1056 if (!(word0(&rv
) & Exp_mask
))
1058 if (word0(&rv
) == 0 && word1(&rv
) == 0)
1063 #endif /* Avoid_Underflow */
1065 if (inexact
&& !(word0(&rv
) & Exp_mask
)) {
1066 /* set underflow bit */
1067 dval(&rv0
) = 1e-300;
1068 dval(&rv0
) *= dval(&rv0
);
1074 return sign
? -dval(&rv
) : dval(&rv
);
1080 (s00
, se
, loc
) CONST
char *s00
; char **se
; locale_t
1082 (CONST
char *s00
, char **se
)
1085 return strtod_l(s00
, se
, __get_locale());