1 /* $NetBSD: strtod.c,v 1.14 2013/05/17 12:55:57 joerg Exp $ */
3 /****************************************************************
5 The author of this software is David M. Gay.
7 Copyright (C) 1998-2001 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 "."). */
34 #include "namespace.h"
42 #include "setlocale_local.h"
47 #define Avoid_Underflow
49 /* The factor of 2^106 in tinytens[4] helps us avoid setting the underflow */
50 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
51 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
52 9007199254740992.*9007199254740992.e
-256
57 #ifdef Honor_FLT_ROUNDS
58 #undef Check_FLT_ROUNDS
59 #define Check_FLT_ROUNDS
61 #define Rounding Flt_Rounds
64 #ifndef __HAVE_LONG_DOUBLE
65 __strong_alias(_strtold
, strtod
)
66 __weak_alias(strtold
, _strtold
)
67 __strong_alias(_strtold_l
, strtod_l
)
68 __weak_alias(strtold_l
, _strtold_l
)
71 #ifdef Avoid_Underflow /*{*/
75 (x
, scale
) U
*x
; int scale
;
85 if (!scale
|| (i
= 2*P
+ 1 - ((word0(x
) & Exp_mask
) >> Exp_shift
)) <= 0)
86 return rv
; /* Is there an example where i <= 0 ? */
87 word0(&u
) = Exp_1
+ (i
<< Exp_shift
);
94 _int_strtod_l(CONST
char *s00
, char **se
, locale_t loc
)
96 #ifdef Avoid_Underflow
102 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
103 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
104 CONST
char *s
, *s0
, *s1
;
107 U adj
, aadj1
, rv
, rv0
;
109 Bigint
*bb
= NULL
, *bb1
, *bd0
;
110 Bigint
*bd
= NULL
, *bs
= NULL
, *delta
= NULL
; /* pacify gcc */
111 #ifdef Avoid_Underflow
115 int inexact
, oldinexact
;
117 #ifdef USE_LOCALE /*{{*/
118 char *decimalpoint
= localeconv_l(loc
)->decimal_point
;
119 size_t dplen
= strlen(decimalpoint
);
120 #endif /*USE_LOCALE}}*/
122 #ifdef Honor_FLT_ROUNDS /*{*/
124 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
125 Rounding
= Flt_Rounds
;
128 switch(fegetround()) {
129 case FE_TOWARDZERO
: Rounding
= 0; break;
130 case FE_UPWARD
: Rounding
= 2; break;
131 case FE_DOWNWARD
: Rounding
= 3;
141 for(s
= s00
;;s
++) switch(*s
) {
163 #ifndef NO_HEX_FP /*{*/
165 static FPI fpi
= { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
172 #ifdef Honor_FLT_ROUNDS
174 fpi1
.rounding
= Rounding
;
178 switch((i
= gethex(&s
, &fpi1
, &expt
, &bb
, sign
, loc
)) & STRTOG_Retmask
) {
179 case STRTOG_NoNumber
:
187 copybits(bits
, fpi
.nbits
, bb
);
190 ULtod((/* LINTED */(U
*)&rv
)->L
, bits
, expt
, i
);
203 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
210 if (c
== *decimalpoint
) {
211 for(i
= 1; decimalpoint
[i
]; ++i
)
212 if (s
[i
] != decimalpoint
[i
])
224 for(; c
== '0'; c
= *++s
)
226 if (c
> '0' && c
<= '9') {
234 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
239 for(i
= 1; i
< nz
; i
++)
242 else if (nd
<= DBL_DIG
+ 1)
246 else if (nd
<= DBL_DIG
+ 1)
254 if (c
== 'e' || c
== 'E') {
255 if (!nd
&& !nz
&& !nz0
) {
267 if (c
>= '0' && c
<= '9') {
270 if (c
> '0' && c
<= '9') {
273 while((c
= *++s
) >= '0' && c
<= '9')
275 if (s
- s1
> 8 || L
> 19999)
276 /* Avoid confusion from exponents
277 * so large that e might overflow.
279 e
= 19999; /* safe for 16 bit ints */
294 /* Check for Nan and Infinity */
296 static FPI fpinan
= /* only 52 explicit bits */
297 { 52, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
302 if (match(&s
,"nf")) {
304 if (!match(&s
,"inity"))
306 word0(&rv
) = 0x7ff00000;
313 if (match(&s
, "an")) {
316 && hexnan(&s
, &fpinan
, bits
)
318 word0(&rv
) = 0x7ff00000 | bits
[1];
319 word1(&rv
) = bits
[0];
323 word0(&rv
) = NAN_WORD0
;
324 word1(&rv
) = NAN_WORD1
;
331 #endif /* INFNAN_CHECK */
340 /* Now we have nd0 digits, starting at s0, followed by a
341 * decimal point, followed by nd-nd0 digits. The number we're
342 * after is the integer represented by those digits times
347 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
352 oldinexact
= get_inexact();
354 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
359 #ifndef Honor_FLT_ROUNDS
366 #ifndef ROUND_BIASED_without_Round_Up
372 #ifdef Honor_FLT_ROUNDS
373 /* round correctly FLT_ROUNDS = 2 or 3 */
379 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
384 if (e
<= Ten_pmax
+ i
) {
385 /* A fancier test would sometimes let us do
386 * this for larger i values.
388 #ifdef Honor_FLT_ROUNDS
389 /* round correctly FLT_ROUNDS = 2 or 3 */
396 dval(&rv
) *= tens
[i
];
398 /* VAX exponent range is so narrow we must
399 * worry about overflow here...
402 word0(&rv
) -= P
*Exp_msk1
;
403 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
404 if ((word0(&rv
) & Exp_mask
)
405 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
407 word0(&rv
) += P
*Exp_msk1
;
409 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
414 #ifndef Inaccurate_Divide
415 else if (e
>= -Ten_pmax
) {
416 #ifdef Honor_FLT_ROUNDS
417 /* round correctly FLT_ROUNDS = 2 or 3 */
423 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
427 #endif /* ROUND_BIASED_without_Round_Up */
435 oldinexact
= get_inexact();
437 #ifdef Avoid_Underflow
440 #ifdef Honor_FLT_ROUNDS
443 Rounding
= Rounding
== 2 ? 0 : 2;
449 #endif /*IEEE_Arith*/
451 /* Get starting approximation = rv * 10**e1 */
454 if ( (i
= e1
& 15) !=0)
455 dval(&rv
) *= tens
[i
];
457 if (e1
> DBL_MAX_10_EXP
) {
459 /* Can't trust HUGE_VAL */
461 #ifdef Honor_FLT_ROUNDS
463 case 0: /* toward 0 */
464 case 3: /* toward -infinity */
469 word0(&rv
) = Exp_mask
;
472 #else /*Honor_FLT_ROUNDS*/
473 word0(&rv
) = Exp_mask
;
475 #endif /*Honor_FLT_ROUNDS*/
477 /* set overflow bit */
479 dval(&rv0
) *= dval(&rv0
);
484 #endif /*IEEE_Arith*/
498 e1
= (unsigned int)e1
>> 4;
499 for(j
= 0; e1
> 1; j
++, e1
= (unsigned int)e1
>> 1)
501 dval(&rv
) *= bigtens
[j
];
502 /* The last multiplication could overflow. */
503 word0(&rv
) -= P
*Exp_msk1
;
504 dval(&rv
) *= bigtens
[j
];
505 if ((z
= word0(&rv
) & Exp_mask
)
506 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
508 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
509 /* set to largest number */
510 /* (Can't trust DBL_MAX) */
515 word0(&rv
) += P
*Exp_msk1
;
520 if ( (i
= e1
& 15) !=0)
521 dval(&rv
) /= tens
[i
];
523 if (e1
>= 1 << n_bigtens
)
525 #ifdef Avoid_Underflow
528 for(j
= 0; e1
> 0; j
++, e1
= (unsigned int)e1
>> 1)
530 dval(&rv
) *= tinytens
[j
];
531 if (scale
&& (j
= 2*P
+ 1 - ((word0(&rv
) & Exp_mask
)
532 >> Exp_shift
)) > 0) {
533 /* scaled rv is denormal; zap j low bits */
537 word0(&rv
) = (P
+2)*Exp_msk1
;
539 word0(&rv
) &= 0xffffffffU
<< (j
-32);
542 word1(&rv
) &= 0xffffffffU
<< j
;
545 for(j
= 0; e1
> 1; j
++, e1
= (unsigned int)e1
>> 1)
547 dval(&rv
) *= tinytens
[j
];
548 /* The last multiplication could underflow. */
549 dval(&rv0
) = dval(&rv
);
550 dval(&rv
) *= tinytens
[j
];
552 dval(&rv
) = 2.*dval(&rv0
);
553 dval(&rv
) *= tinytens
[j
];
560 #ifndef Avoid_Underflow
563 /* The refinement below will clean
564 * this approximation up.
571 /* Now the hard part -- adjusting rv to the correct value.*/
573 /* Put digits into bd: true value = bd * 10^e */
575 bd0
= s2b(s0
, nd0
, nd
, y
, dplen
);
584 bb
= d2b(dval(&rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
604 #ifdef Honor_FLT_ROUNDS
608 #ifdef Avoid_Underflow
612 i
= j
+ bbbits
- 1; /* logb(rv) */
614 if (i
< Emin
) { /* denormal */
620 Lsb1
= Lsb
<< (i
-32);
622 #else /*Avoid_Underflow*/
623 #ifdef Sudden_Underflow
625 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
629 #else /*Sudden_Underflow*/
631 i
= j
+ bbbits
- 1; /* logb(&rv) */
632 if (i
< Emin
) /* denormal */
636 #endif /*Sudden_Underflow*/
637 #endif /*Avoid_Underflow*/
640 #ifdef Avoid_Underflow
643 i
= bb2
< bd2
? bb2
: bd2
;
652 bs
= pow5mult(bs
, bb5
);
662 bb
= lshift(bb
, bb2
);
667 bd
= pow5mult(bd
, bd5
);
672 bd
= lshift(bd
, bd2
);
677 bs
= lshift(bs
, bs2
);
681 delta
= diff(bb
, bd
);
687 #ifdef Honor_FLT_ROUNDS
690 /* Error is less than an ulp */
691 if (!delta
->x
[0] && delta
->wds
<= 1) {
707 && !(word0(&rv
) & Frac_mask
)) {
708 y
= word0(&rv
) & Exp_mask
;
709 #ifdef Avoid_Underflow
710 if (!scale
|| y
> 2*P
*Exp_msk1
)
715 delta
= lshift(delta
,Log2P
);
716 if (cmp(delta
, bs
) <= 0)
721 #ifdef Avoid_Underflow
722 if (scale
&& (y
= word0(&rv
) & Exp_mask
)
724 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
726 #ifdef Sudden_Underflow
727 if ((word0(&rv
) & Exp_mask
) <=
729 word0(&rv
) += P
*Exp_msk1
;
730 dval(&rv
) += adj
*ulp(&rv
);
731 word0(&rv
) -= P
*Exp_msk1
;
734 #endif /*Sudden_Underflow*/
735 #endif /*Avoid_Underflow*/
736 dval(&rv
) += adj
.d
*ulp(&rv
);
740 dval(&adj
) = ratio(delta
, bs
);
743 if (adj
.d
<= 0x7ffffffe) {
744 /* dval(&adj) = Rounding ? ceil(&adj) : floor(&adj); */
747 if (!((Rounding
>>1) ^ dsign
))
752 #ifdef Avoid_Underflow
753 if (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
754 word0(&adj
) += (2*P
+1)*Exp_msk1
- y
;
756 #ifdef Sudden_Underflow
757 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
758 word0(&rv
) += P
*Exp_msk1
;
759 dval(&adj
) *= ulp(&rv
);
764 word0(&rv
) -= P
*Exp_msk1
;
767 #endif /*Sudden_Underflow*/
768 #endif /*Avoid_Underflow*/
769 dval(&adj
) *= ulp(&rv
);
771 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
779 #endif /*Honor_FLT_ROUNDS*/
782 /* Error is less than half an ulp -- check for
783 * special case of mantissa a power of two.
785 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
787 #ifdef Avoid_Underflow
788 || (word0(&rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
790 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
795 if (!delta
->x
[0] && delta
->wds
<= 1)
800 if (!delta
->x
[0] && delta
->wds
<= 1) {
807 delta
= lshift(delta
,Log2P
);
808 if (cmp(delta
, bs
) > 0)
813 /* exactly half-way between */
815 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
817 #ifdef Avoid_Underflow
818 (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
819 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
822 /*boundary case -- increment exponent*/
823 if (word0(&rv
) == Big0
&& word1(&rv
) == Big1
)
825 word0(&rv
) = (word0(&rv
) & Exp_mask
)
832 #ifdef Avoid_Underflow
838 else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
840 /* boundary case -- decrement exponent */
841 #ifdef Sudden_Underflow /*{{*/
842 L
= word0(&rv
) & Exp_mask
;
846 #ifdef Avoid_Underflow
847 if (L
<= (scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
850 #endif /*Avoid_Underflow*/
854 #else /*Sudden_Underflow}{*/
855 #ifdef Avoid_Underflow
857 L
= word0(&rv
) & Exp_mask
;
858 if (L
<= (2*P
+1)*Exp_msk1
) {
859 if (L
> (P
+2)*Exp_msk1
)
863 /* rv = smallest denormal */
867 #endif /*Avoid_Underflow*/
868 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
869 #endif /*Sudden_Underflow}}*/
870 word0(&rv
) = L
| Bndry_mask1
;
871 word1(&rv
) = 0xffffffff;
879 #ifdef Avoid_Underflow
881 if (!(word0(&rv
) & Lsb1
))
884 else if (!(word1(&rv
) & Lsb
))
887 if (!(word1(&rv
) & LSB
))
892 #ifdef Avoid_Underflow
893 dval(&rv
) += sulp(&rv
, scale
);
895 dval(&rv
) += ulp(&rv
);
899 #ifdef Avoid_Underflow
900 dval(&rv
) -= sulp(&rv
, scale
);
902 dval(&rv
) -= ulp(&rv
);
904 #ifndef Sudden_Underflow
909 #ifdef Avoid_Underflow
915 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
917 aadj
= dval(&aadj1
) = 1.;
918 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
919 #ifndef Sudden_Underflow
920 if (word1(&rv
) == Tiny1
&& !word0(&rv
))
927 /* special case -- power of FLT_RADIX to be */
928 /* rounded down... */
930 if (aadj
< 2./FLT_RADIX
)
934 dval(&aadj1
) = -aadj
;
939 dval(&aadj1
) = dsign
? aadj
: -aadj
;
940 #ifdef Check_FLT_ROUNDS
943 case 2: /* towards +infinity */
946 case 0: /* towards 0 */
947 case 3: /* towards -infinity */
954 #endif /*Check_FLT_ROUNDS*/
956 y
= word0(&rv
) & Exp_mask
;
958 /* Check for overflow */
960 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
961 dval(&rv0
) = dval(&rv
);
962 word0(&rv
) -= P
*Exp_msk1
;
963 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
964 dval(&rv
) += dval(&adj
);
965 if ((word0(&rv
) & Exp_mask
) >=
966 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
967 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
974 word0(&rv
) += P
*Exp_msk1
;
977 #ifdef Avoid_Underflow
978 if (scale
&& y
<= 2*P
*Exp_msk1
) {
979 if (aadj
<= 0x7fffffff) {
983 dval(&aadj1
) = dsign
? aadj
: -aadj
;
985 word0(&aadj1
) += (2*P
+1)*Exp_msk1
- y
;
987 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
988 dval(&rv
) += dval(&adj
);
990 #ifdef Sudden_Underflow
991 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
) {
992 dval(&rv0
) = dval(&rv
);
993 word0(&rv
) += P
*Exp_msk1
;
994 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
995 dval(&rv
) += dval(&adj
);
997 if ((word0(&rv
) & Exp_mask
) < P
*Exp_msk1
)
999 if ((word0(&rv
) & Exp_mask
) <= P
*Exp_msk1
)
1002 if (word0(&rv0
) == Tiny0
1003 && word1(&rv0
) == Tiny1
)
1010 word0(&rv
) -= P
*Exp_msk1
;
1013 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
1014 dval(&rv
) += dval(&adj
);
1016 #else /*Sudden_Underflow*/
1017 /* Compute dval(&adj) so that the IEEE rounding rules will
1018 * correctly round rv + dval(&adj) in some half-way cases.
1019 * If rv * ulp(&rv) is denormalized (i.e.,
1020 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1021 * trouble from bits lost to denormalization;
1022 * example: 1.2e-307 .
1024 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
1025 dval(&aadj1
) = (double)(int)(aadj
+ 0.5);
1027 dval(&aadj1
) = -dval(&aadj1
);
1029 dval(&adj
) = dval(&aadj1
) * ulp(&rv
);
1031 #endif /*Sudden_Underflow*/
1032 #endif /*Avoid_Underflow*/
1034 z
= word0(&rv
) & Exp_mask
;
1036 #ifdef Avoid_Underflow
1040 /* Can we stop now? */
1043 /* The tolerances below are conservative. */
1044 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1045 if (aadj
< .4999999 || aadj
> .5000001)
1048 else if (aadj
< .4999999/FLT_RADIX
)
1066 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
1071 else if (!oldinexact
)
1074 #ifdef Avoid_Underflow
1076 word0(&rv0
) = Exp_1
- 2*P
*Exp_msk1
;
1078 dval(&rv
) *= dval(&rv0
);
1080 /* try to avoid the bug of testing an 8087 register value */
1082 if (!(word0(&rv
) & Exp_mask
))
1084 if (word0(&rv
) == 0 && word1(&rv
) == 0)
1089 #endif /* Avoid_Underflow */
1091 if (inexact
&& !(word0(&rv
) & Exp_mask
)) {
1092 /* set underflow bit */
1093 dval(&rv0
) = 1e-300;
1094 dval(&rv0
) *= dval(&rv0
);
1100 return sign
? -dval(&rv
) : dval(&rv
);
1104 strtod(CONST
char *s
, char **sp
)
1106 return _int_strtod_l(s
, sp
, _current_locale());
1110 __weak_alias(strtod_l
, _strtod_l
)
1114 strtod_l(CONST
char *s
, char **sp
, locale_t loc
)
1116 return _int_strtod_l(s
, sp
, loc
);