3 <<strtod>>, <<strtof>>, <<strtold>>, <<strtod_l>>, <<strtof_l>>, <<strtold_l>>---string to double or float
28 double strtod(const char *restrict <[str]>, char **restrict <[tail]>);
29 float strtof(const char *restrict <[str]>, char **restrict <[tail]>);
30 long double strtold(const char *restrict <[str]>,
31 char **restrict <[tail]>);
34 double strtod_l(const char *restrict <[str]>, char **restrict <[tail]>,
36 float strtof_l(const char *restrict <[str]>, char **restrict <[tail]>,
38 long double strtold_l(const char *restrict <[str]>,
39 char **restrict <[tail]>,
42 double _strtod_r(void *<[reent]>,
43 const char *restrict <[str]>, char **restrict <[tail]>);
46 <<strtod>>, <<strtof>>, <<strtold>> parse the character string
47 <[str]>, producing a substring which can be converted to a double,
48 float, or long double value, respectively. The substring converted
49 is the longest initial subsequence of <[str]>, beginning with the
50 first non-whitespace character, that has one of these formats:
51 .[+|-]<[digits]>[.[<[digits]>]][(e|E)[+|-]<[digits]>]
52 .[+|-].<[digits]>[(e|E)[+|-]<[digits]>]
53 .[+|-](i|I)(n|N)(f|F)[(i|I)(n|N)(i|I)(t|T)(y|Y)]
54 .[+|-](n|N)(a|A)(n|N)[<(>[<[hexdigits]>]<)>]
55 .[+|-]0(x|X)<[hexdigits]>[.[<[hexdigits]>]][(p|P)[+|-]<[digits]>]
56 .[+|-]0(x|X).<[hexdigits]>[(p|P)[+|-]<[digits]>]
57 The substring contains no characters if <[str]> is empty, consists
58 entirely of whitespace, or if the first non-whitespace
59 character is something other than <<+>>, <<->>, <<.>>, or a
60 digit, and cannot be parsed as infinity or NaN. If the platform
61 does not support NaN, then NaN is treated as an empty substring.
62 If the substring is empty, no conversion is done, and
63 the value of <[str]> is stored in <<*<[tail]>>>. Otherwise,
64 the substring is converted, and a pointer to the final string
65 (which will contain at least the terminating null character of
66 <[str]>) is stored in <<*<[tail]>>>. If you want no
67 assignment to <<*<[tail]>>>, pass a null pointer as <[tail]>.
69 This implementation returns the nearest machine number to the
70 input decimal string. Ties are broken by using the IEEE
71 round-even rule. However, <<strtof>> is currently subject to
72 double rounding errors.
74 <<strtod_l>>, <<strtof_l>>, <<strtold_l>> are like <<strtod>>,
75 <<strtof>>, <<strtold>> but perform the conversion based on the
76 locale specified by the locale object locale. If <[locale]> is
77 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is
80 The alternate function <<_strtod_r>> is a reentrant version.
81 The extra argument <[reent]> is a pointer to a reentrancy structure.
84 These functions return the converted substring value, if any. If
85 no conversion could be performed, 0 is returned. If the correct
86 value is out of the range of representable values, plus or minus
87 <<HUGE_VAL>> (<<HUGE_VALF>>, <<HUGE_VALL>>) is returned, and
88 <<ERANGE>> is stored in errno. If the correct value would cause
89 underflow, 0 is returned and <<ERANGE>> is stored in errno.
93 <<strtof>>, <<strtold>> are C99.
94 <<strtod_l>>, <<strtof_l>>, <<strtold_l>> are GNU extensions.
96 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
97 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
100 /****************************************************************
102 The author of this software is David M. Gay.
104 Copyright (C) 1998-2001 by Lucent Technologies
107 Permission to use, copy, modify, and distribute this software and
108 its documentation for any purpose and without fee is hereby
109 granted, provided that the above copyright notice appear in all
110 copies and that both that the copyright notice and this
111 permission notice and warranty disclaimer appear in supporting
112 documentation, and that the name of Lucent or any of its entities
113 not be used in advertising or publicity pertaining to
114 distribution of the software without specific, written prior
117 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
118 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
119 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
120 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
121 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
122 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
123 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
126 ****************************************************************/
128 /* Please send bug reports to David M. Gay (dmg at acm dot org,
129 * with " at " changed at "@" and " dot " changed to "."). */
131 /* Original file gdtoa-strtod.c Modified 06-21-2006 by Jeff Johnston to work within newlib. */
140 #include "../locale/setlocale.h"
142 /* #ifndef NO_FENV_H */
143 /* #include <fenv.h> */
149 #ifndef NO_IEEE_Scale
150 #define Avoid_Underflow
152 /* The factor of 2^106 in tinytens[4] helps us avoid setting the underflow */
153 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
154 static const double tinytens
[] = { 1e-16, 1e-32,
155 #ifdef _DOUBLE_IS_32BITS
159 9007199254740992. * 9007199254740992.e
-256
166 #ifdef Honor_FLT_ROUNDS
167 #define Rounding rounding
168 #undef Check_FLT_ROUNDS
169 #define Check_FLT_ROUNDS
171 #define Rounding Flt_Rounds
182 #ifdef Avoid_Underflow /*{*/
192 if (!scale
|| (i
= 2*P
+ 1 - ((dword0(x
) & Exp_mask
) >> Exp_shift
)) <= 0)
193 return rv
; /* Is there an example where i <= 0 ? */
194 dword0(u
) = Exp_1
+ ((__int32_t
)i
<< Exp_shift
);
195 #ifndef _DOUBLE_IS_32BITS
211 switch(k
& STRTOG_Retmask
) {
212 case STRTOG_NoNumber
:
217 case STRTOG_Denormal
:
225 L
[_0
] = (bits
[1] & ~0x100000) | ((exp
+ 0x3ff + 52) << 20);
228 case STRTOG_Infinite
:
238 L
[_0
] |= 0x80000000L
;
240 #endif /* !NO_HEX_FP */
243 _strtod_l (struct _reent
*ptr
, const char *__restrict s00
, char **__restrict se
,
246 #ifdef Avoid_Underflow
249 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, decpt
, dsign
,
250 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
251 const char *s
, *s0
, *s1
;
256 _Bigint
*bb
= NULL
, *bb1
, *bd
= NULL
, *bd0
, *bs
= NULL
, *delta
= NULL
;
257 #ifdef Avoid_Underflow
261 int inexact
, oldinexact
;
263 #ifdef Honor_FLT_ROUNDS
266 #ifdef __HAVE_LOCALE_INFO__
267 const char *decimal_point
= __get_numeric_locale(loc
)->decimal_point
;
268 const int dec_len
= strlen (decimal_point
);
270 const char *decimal_point
= ".";
271 const int dec_len
= 1;
274 delta
= bs
= bd
= NULL
;
275 sign
= nz0
= nz
= decpt
= 0;
277 for(s
= s00
;;s
++) switch(*s
) {
301 static const FPI fpi
= { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
307 /* If the number is not hex, then the parse of
311 #if defined(FE_DOWNWARD) && defined(FE_TONEAREST) && defined(FE_TOWARDZERO) && defined(FE_UPWARD)
313 switch(fegetround()) {
314 case FE_TOWARDZERO
: fpi1
.rounding
= 0; break;
315 case FE_UPWARD
: fpi1
.rounding
= 2; break;
316 case FE_DOWNWARD
: fpi1
.rounding
= 3;
321 switch((i
= gethex(ptr
, &s
, &fpi1
, &exp
, &bb
, sign
, loc
)) & STRTOG_Retmask
) {
322 case STRTOG_NoNumber
:
330 copybits(bits
, fpi
.nbits
, bb
);
333 ULtod(rv
.i
, bits
, exp
, i
);
335 /* try to avoid the bug of testing an 8087 register value */
336 if ((dword0(rv
)&Exp_mask
) == 0)
351 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
357 if (strncmp (s
, decimal_point
, dec_len
) == 0)
362 for(; c
== '0'; c
= *++s
)
364 if (c
> '0' && c
<= '9') {
372 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
377 for(i
= 1; i
< nz
; i
++)
380 else if (nd
<= DBL_DIG
+ 1)
384 else if (nd
<= DBL_DIG
+ 1)
392 if (c
== 'e' || c
== 'E') {
393 if (!nd
&& !nz
&& !nz0
) {
404 if (c
>= '0' && c
<= '9') {
407 if (c
> '0' && c
<= '9') {
410 while((c
= *++s
) >= '0' && c
<= '9')
412 if (s
- s1
> 8 || L
> 19999)
413 /* Avoid confusion from exponents
414 * so large that e might overflow.
416 e
= 19999; /* safe for 16 bit ints */
431 /* Check for Nan and Infinity */
433 static const FPI fpinan
= /* only 52 explicit bits */
434 { 52, 1-1023-53+1, 2046-1023-53+1, 1, SI
};
439 if (match(&s
,"nf")) {
441 if (!match(&s
,"inity"))
443 dword0(rv
) = 0x7ff00000;
444 #ifndef _DOUBLE_IS_32BITS
446 #endif /*!_DOUBLE_IS_32BITS*/
452 if (match(&s
, "an")) {
455 && hexnan(&s
, &fpinan
, bits
)
457 dword0(rv
) = 0x7ff00000 | bits
[1];
458 #ifndef _DOUBLE_IS_32BITS
459 dword1(rv
) = bits
[0];
460 #endif /*!_DOUBLE_IS_32BITS*/
471 #endif /* INFNAN_CHECK */
480 /* Now we have nd0 digits, starting at s0, followed by a
481 * decimal point, followed by nd-nd0 digits. The number we're
482 * after is the integer represented by those digits times
487 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
492 oldinexact
= get_inexact();
494 dval(rv
) = tens
[k
- 9] * dval(rv
) + z
;
499 #ifndef Honor_FLT_ROUNDS
511 #ifdef Honor_FLT_ROUNDS
512 /* round correctly FLT_ROUNDS = 2 or 3 */
514 dval(rv
) = -dval(rv
);
518 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
523 if (e
<= Ten_pmax
+ i
) {
524 /* A fancier test would sometimes let us do
525 * this for larger i values.
527 #ifdef Honor_FLT_ROUNDS
528 /* round correctly FLT_ROUNDS = 2 or 3 */
530 dval(rv
) = -dval(rv
);
537 /* VAX exponent range is so narrow we must
538 * worry about overflow here...
541 dword0(rv
) -= P
*Exp_msk1
;
542 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
543 if ((dword0(rv
) & Exp_mask
)
544 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
546 dword0(rv
) += P
*Exp_msk1
;
548 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
553 #ifndef Inaccurate_Divide
554 else if (e
>= -Ten_pmax
) {
555 #ifdef Honor_FLT_ROUNDS
556 /* round correctly FLT_ROUNDS = 2 or 3 */
558 dval(rv
) = -dval(rv
);
562 /* rv = */ rounded_quotient(dval(rv
), tens
[-e
]);
573 oldinexact
= get_inexact();
575 #ifdef Avoid_Underflow
578 #ifdef Honor_FLT_ROUNDS
579 if ((rounding
= Flt_Rounds
) >= 2) {
581 rounding
= rounding
== 2 ? 0 : 2;
587 #endif /*IEEE_Arith*/
589 /* Get starting approximation = rv * 10**e1 */
592 if ( (i
= e1
& 15) !=0)
595 if (e1
> DBL_MAX_10_EXP
) {
598 _REENT_ERRNO(ptr
) = ERANGE
;
600 /* Can't trust HUGE_VAL */
602 #ifdef Honor_FLT_ROUNDS
604 case 0: /* toward 0 */
605 case 3: /* toward -infinity */
607 #ifndef _DOUBLE_IS_32BITS
609 #endif /*!_DOUBLE_IS_32BITS*/
612 dword0(rv
) = Exp_mask
;
613 #ifndef _DOUBLE_IS_32BITS
615 #endif /*!_DOUBLE_IS_32BITS*/
617 #else /*Honor_FLT_ROUNDS*/
618 dword0(rv
) = Exp_mask
;
619 #ifndef _DOUBLE_IS_32BITS
621 #endif /*!_DOUBLE_IS_32BITS*/
622 #endif /*Honor_FLT_ROUNDS*/
624 /* set overflow bit */
626 dval(rv0
) *= dval(rv0
);
630 #ifndef _DOUBLE_IS_32BITS
632 #endif /*!_DOUBLE_IS_32BITS*/
633 #endif /*IEEE_Arith*/
639 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
641 dval(rv
) *= bigtens
[j
];
642 /* The last multiplication could overflow. */
643 dword0(rv
) -= P
*Exp_msk1
;
644 dval(rv
) *= bigtens
[j
];
645 if ((z
= dword0(rv
) & Exp_mask
)
646 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
648 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
649 /* set to largest number */
650 /* (Can't trust DBL_MAX) */
652 #ifndef _DOUBLE_IS_32BITS
654 #endif /*!_DOUBLE_IS_32BITS*/
657 dword0(rv
) += P
*Exp_msk1
;
662 if ( (i
= e1
& 15) !=0)
665 if (e1
>= 1 << n_bigtens
)
667 #ifdef Avoid_Underflow
670 for(j
= 0; e1
> 0; j
++, e1
>>= 1)
672 dval(rv
) *= tinytens
[j
];
673 if (scale
&& (j
= 2*P
+ 1 - ((dword0(rv
) & Exp_mask
)
674 >> Exp_shift
)) > 0) {
675 /* scaled rv is denormal; zap j low bits */
677 #ifndef _DOUBLE_IS_32BITS
679 #endif /*!_DOUBLE_IS_32BITS*/
681 dword0(rv
) = (P
+2)*Exp_msk1
;
683 dword0(rv
) &= 0xffffffff << (j
-32);
685 #ifndef _DOUBLE_IS_32BITS
687 dword1(rv
) &= 0xffffffff << j
;
688 #endif /*!_DOUBLE_IS_32BITS*/
691 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
693 dval(rv
) *= tinytens
[j
];
694 /* The last multiplication could underflow. */
695 dval(rv0
) = dval(rv
);
696 dval(rv
) *= tinytens
[j
];
698 dval(rv
) = 2.*dval(rv0
);
699 dval(rv
) *= tinytens
[j
];
705 _REENT_ERRNO(ptr
) = ERANGE
;
711 #ifndef Avoid_Underflow
712 #ifndef _DOUBLE_IS_32BITS
717 #endif /*_DOUBLE_IS_32BITS*/
718 /* The refinement below will clean
719 * this approximation up.
726 /* Now the hard part -- adjusting rv to the correct value.*/
728 /* Put digits into bd: true value = bd * 10^e */
730 bd0
= s2b(ptr
, s0
, nd0
, nd
, y
);
735 bd
= Balloc(ptr
,bd0
->_k
);
739 bb
= d2b(ptr
,dval(rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
759 #ifdef Honor_FLT_ROUNDS
763 #ifdef Avoid_Underflow
767 i
= j
+ bbbits
- 1; /* logb(rv) */
769 if (i
< Emin
) { /* denormal */
775 Lsb1
= Lsb
<< (i
-32);
777 #else /*Avoid_Underflow*/
778 #ifdef Sudden_Underflow
780 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
784 #else /*Sudden_Underflow*/
786 i
= j
+ bbbits
- 1; /* logb(rv) */
787 if (i
< Emin
) /* denormal */
791 #endif /*Sudden_Underflow*/
792 #endif /*Avoid_Underflow*/
795 #ifdef Avoid_Underflow
798 i
= bb2
< bd2
? bb2
: bd2
;
807 bs
= pow5mult(ptr
, bs
, bb5
);
810 bb1
= mult(ptr
, bs
, bb
);
817 bb
= lshift(ptr
, bb
, bb2
);
822 bd
= pow5mult(ptr
, bd
, bd5
);
827 bd
= lshift(ptr
, bd
, bd2
);
832 bs
= lshift(ptr
, bs
, bs2
);
836 delta
= diff(ptr
, bb
, bd
);
839 dsign
= delta
->_sign
;
842 #ifdef Honor_FLT_ROUNDS
845 /* Error is less than an ulp */
846 if (!delta
->_x
[0] && delta
->_wds
<= 1) {
862 && !(dword0(rv
) & Frac_mask
)) {
863 y
= dword0(rv
) & Exp_mask
;
864 #ifdef Avoid_Underflow
865 if (!scale
|| y
> 2*P
*Exp_msk1
)
870 delta
= lshift(ptr
, delta
,Log2P
);
871 if (cmp(delta
, bs
) <= 0)
876 #ifdef Avoid_Underflow
877 if (scale
&& (y
= dword0(rv
) & Exp_mask
)
879 dword0(adj
) += (2*P
+1)*Exp_msk1
- y
;
881 #ifdef Sudden_Underflow
882 if ((dword0(rv
) & Exp_mask
) <=
884 dword0(rv
) += P
*Exp_msk1
;
885 dval(rv
) += adj
*ulp(dval(rv
));
886 dword0(rv
) -= P
*Exp_msk1
;
889 #endif /*Sudden_Underflow*/
890 #endif /*Avoid_Underflow*/
891 dval(rv
) += adj
*ulp(dval(rv
));
895 adj
= ratio(delta
, bs
);
898 if (adj
<= 0x7ffffffe) {
899 /* adj = rounding ? ceil(adj) : floor(adj); */
902 if (!((rounding
>>1) ^ dsign
))
907 #ifdef Avoid_Underflow
908 if (scale
&& (y
= dword0(rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
909 dword0(adj
) += (2*P
+1)*Exp_msk1
- y
;
911 #ifdef Sudden_Underflow
912 if ((dword0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
913 dword0(rv
) += P
*Exp_msk1
;
914 adj
*= ulp(dval(rv
));
919 dword0(rv
) -= P
*Exp_msk1
;
922 #endif /*Sudden_Underflow*/
923 #endif /*Avoid_Underflow*/
924 adj
*= ulp(dval(rv
));
926 if (dword0(rv
) == Big0
&& dword1(rv
) == Big1
)
933 #endif /*Honor_FLT_ROUNDS*/
936 /* Error is less than half an ulp -- check for
937 * special case of mantissa a power of two.
939 if (dsign
|| dword1(rv
) || dword0(rv
) & Bndry_mask
941 #ifdef Avoid_Underflow
942 || (dword0(rv
) & Exp_mask
) <= (2*P
+1)*Exp_msk1
944 || (dword0(rv
) & Exp_mask
) <= Exp_msk1
949 if (!delta
->x
[0] && delta
->wds
<= 1)
954 if (!delta
->_x
[0] && delta
->_wds
<= 1) {
961 delta
= lshift(ptr
,delta
,Log2P
);
962 if (cmp(delta
, bs
) > 0)
967 /* exactly half-way between */
969 if ((dword0(rv
) & Bndry_mask1
) == Bndry_mask1
971 #ifdef Avoid_Underflow
972 (scale
&& (y
= dword0(rv
) & Exp_mask
) <= 2*P
*Exp_msk1
)
973 ? (0xffffffff & (0xffffffff << (2*P
+1-(y
>>Exp_shift
)))) :
976 /*boundary case -- increment exponent*/
977 if (dword0(rv
) == Big0
&& dword1(rv
) == Big1
)
979 dword0(rv
) = (dword0(rv
) & Exp_mask
)
985 #ifndef _DOUBLE_IS_32BITS
987 #endif /*!_DOUBLE_IS_32BITS*/
988 #ifdef Avoid_Underflow
994 else if (!(dword0(rv
) & Bndry_mask
) && !dword1(rv
)) {
996 /* boundary case -- decrement exponent */
997 #ifdef Sudden_Underflow /*{{*/
998 L
= dword0(rv
) & Exp_mask
;
1002 #ifdef Avoid_Underflow
1003 if (L
<= (scale
? (2*P
+1)*Exp_msk1
: Exp_msk1
))
1006 #endif /*Avoid_Underflow*/
1010 #else /*Sudden_Underflow}{*/
1011 #ifdef Avoid_Underflow
1013 L
= dword0(rv
) & Exp_mask
;
1014 if (L
<= (2*P
+1)*Exp_msk1
) {
1015 if (L
> (P
+2)*Exp_msk1
)
1016 /* round even ==> */
1019 /* rv = smallest denormal */
1023 #endif /*Avoid_Underflow*/
1024 L
= (dword0(rv
) & Exp_mask
) - Exp_msk1
;
1025 #endif /*Sudden_Underflow}*/
1026 dword0(rv
) = L
| Bndry_mask1
;
1027 #ifndef _DOUBLE_IS_32BITS
1028 dword1(rv
) = 0xffffffff;
1029 #endif /*!_DOUBLE_IS_32BITS*/
1036 #ifndef ROUND_BIASED
1037 #ifdef Avoid_Underflow
1039 if (!(dword0(rv
) & Lsb1
))
1042 else if (!(dword1(rv
) & Lsb
))
1045 if (!(dword1(rv
) & LSB
))
1050 #ifdef Avoid_Underflow
1051 dval(rv
) += sulp(rv
, scale
);
1053 dval(rv
) += ulp(dval(rv
));
1055 #ifndef ROUND_BIASED
1057 #ifdef Avoid_Underflow
1058 dval(rv
) -= sulp(rv
, scale
);
1060 dval(rv
) -= ulp(dval(rv
));
1062 #ifndef Sudden_Underflow
1067 #ifdef Avoid_Underflow
1073 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1075 aadj
= dval(aadj1
) = 1.;
1076 else if (dword1(rv
) || dword0(rv
) & Bndry_mask
) {
1077 #ifndef Sudden_Underflow
1078 if (dword1(rv
) == Tiny1
&& !dword0(rv
))
1085 /* special case -- power of FLT_RADIX to be */
1086 /* rounded down... */
1088 if (aadj
< 2./FLT_RADIX
)
1089 aadj
= 1./FLT_RADIX
;
1092 dval(aadj1
) = -aadj
;
1097 dval(aadj1
) = dsign
? aadj
: -aadj
;
1098 #ifdef Check_FLT_ROUNDS
1100 case 2: /* towards +infinity */
1103 case 0: /* towards 0 */
1104 case 3: /* towards -infinity */
1108 if (Flt_Rounds
== 0)
1110 #endif /*Check_FLT_ROUNDS*/
1112 y
= dword0(rv
) & Exp_mask
;
1114 /* Check for overflow */
1116 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
1117 dval(rv0
) = dval(rv
);
1118 dword0(rv
) -= P
*Exp_msk1
;
1119 adj
= dval(aadj1
) * ulp(dval(rv
));
1121 if ((dword0(rv
) & Exp_mask
) >=
1122 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
1123 if (dword0(rv0
) == Big0
&& dword1(rv0
) == Big1
)
1126 #ifndef _DOUBLE_IS_32BITS
1128 #endif /*!_DOUBLE_IS_32BITS*/
1132 dword0(rv
) += P
*Exp_msk1
;
1135 #ifdef Avoid_Underflow
1136 if (scale
&& y
<= 2*P
*Exp_msk1
) {
1137 if (aadj
<= 0x7fffffff) {
1138 if ((z
= aadj
) == 0)
1141 dval(aadj1
) = dsign
? aadj
: -aadj
;
1143 dword0(aadj1
) += (2*P
+1)*Exp_msk1
- y
;
1145 adj
= dval(aadj1
) * ulp(dval(rv
));
1148 #ifdef Sudden_Underflow
1149 if ((dword0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
1150 dval(rv0
) = dval(rv
);
1151 dword0(rv
) += P
*Exp_msk1
;
1152 adj
= dval(aadj1
) * ulp(dval(rv
));
1155 if ((dword0(rv
) & Exp_mask
) < P
*Exp_msk1
)
1157 if ((dword0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
1160 if (dword0(rv0
) == Tiny0
1161 && dword1(rv0
) == Tiny1
)
1163 #ifndef _DOUBLE_IS_32BITS
1168 #endif /*_DOUBLE_IS_32BITS*/
1172 dword0(rv
) -= P
*Exp_msk1
;
1175 adj
= dval(aadj1
) * ulp(dval(rv
));
1178 #else /*Sudden_Underflow*/
1179 /* Compute adj so that the IEEE rounding rules will
1180 * correctly round rv + adj in some half-way cases.
1181 * If rv * ulp(rv) is denormalized (i.e.,
1182 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1183 * trouble from bits lost to denormalization;
1184 * example: 1.2e-307 .
1186 if (y
<= (P
-1)*Exp_msk1
&& aadj
> 1.) {
1187 dval(aadj1
) = (double)(int)(aadj
+ 0.5);
1189 dval(aadj1
) = -dval(aadj1
);
1191 adj
= dval(aadj1
) * ulp(dval(rv
));
1193 #endif /*Sudden_Underflow*/
1194 #endif /*Avoid_Underflow*/
1196 z
= dword0(rv
) & Exp_mask
;
1198 #ifdef Avoid_Underflow
1202 /* Can we stop now? */
1203 #ifndef _DOUBLE_IS_32BITS
1204 /* If FE_INVALID floating point exceptions are
1205 enabled, a conversion to a 32 bit value is
1206 dangerous. A positive double value can result
1207 in a negative 32 bit int, thus raising SIGFPE.
1208 To avoid this, always convert into 64 bit here. */
1209 __int64_t L
= (__int64_t
)aadj
;
1214 /* The tolerances below are conservative. */
1215 if (dsign
|| dword1(rv
) || dword0(rv
) & Bndry_mask
) {
1216 if (aadj
< .4999999 || aadj
> .5000001)
1219 else if (aadj
< .4999999/FLT_RADIX
)
1232 dword0(rv0
) = Exp_1
+ (70 << Exp_shift
);
1233 #ifndef _DOUBLE_IS_32BITS
1235 #endif /*!_DOUBLE_IS_32BITS*/
1239 else if (!oldinexact
)
1242 #ifdef Avoid_Underflow
1244 dword0(rv0
) = Exp_1
- 2*P
*Exp_msk1
;
1245 #ifndef _DOUBLE_IS_32BITS
1247 #endif /*!_DOUBLE_IS_32BITS*/
1248 dval(rv
) *= dval(rv0
);
1250 /* try to avoid the bug of testing an 8087 register value */
1251 if ((dword0(rv
) & Exp_mask
) == 0)
1252 _REENT_ERRNO(ptr
) = ERANGE
;
1255 #endif /* Avoid_Underflow */
1257 if (inexact
&& !(dword0(rv
) & Exp_mask
)) {
1258 /* set underflow bit */
1260 dval(rv0
) *= dval(rv0
);
1272 return sign
? -dval(rv
) : dval(rv
);
1276 _strtod_r (struct _reent
*ptr
,
1277 const char *__restrict s00
,
1278 char **__restrict se
)
1280 return _strtod_l (ptr
, s00
, se
, __get_current_locale ());
1286 strtod_l (const char *__restrict s00
, char **__restrict se
, locale_t loc
)
1288 return _strtod_l (_REENT
, s00
, se
, loc
);
1292 strtod (const char *__restrict s00
, char **__restrict se
)
1294 return _strtod_l (_REENT
, s00
, se
, __get_current_locale ());
1298 strtof_l (const char *__restrict s00
, char **__restrict se
, locale_t loc
)
1300 double val
= _strtod_l (_REENT
, s00
, se
, loc
);
1302 return signbit (val
) ? -nanf ("") : nanf ("");
1303 float retval
= (float) val
;
1305 if (isinf (retval
) && !isinf (val
))
1306 _REENT_ERRNO(_REENT
) = ERANGE
;
1312 * These two functions are not quite correct as they return true for
1313 * zero, however they are 'good enough' for the test in strtof below
1314 * as we only need to know whether the double test is false when
1315 * the float test is true.
1322 return (dword0(u
) & Exp_mask
) == 0;
1328 union { float f
; __uint32_t i
; } u
;
1330 return (u
.i
& 0x7f800000) == 0;
1334 strtof (const char *__restrict s00
,
1335 char **__restrict se
)
1337 double val
= _strtod_l (_REENT
, s00
, se
, __get_current_locale ());
1339 return signbit (val
) ? -nanf ("") : nanf ("");
1340 float retval
= (float) val
;
1342 if ((isinf (retval
) && !isinf (val
)) || (isdenormf(retval
) && !isdenorm(val
)))
1343 _REENT_ERRNO(_REENT
) = ERANGE
;