3 <<strtod>>, <<strtof>>---string to double or float
14 double strtod(const char *<[str]>, char **<[tail]>);
15 float strtof(const char *<[str]>, char **<[tail]>);
17 double _strtod_r(void *<[reent]>,
18 const char *<[str]>, char **<[tail]>);
22 double strtod(<[str]>,<[tail]>)
26 float strtof(<[str]>,<[tail]>)
30 double _strtod_r(<[reent]>,<[str]>,<[tail]>)
36 The function <<strtod>> parses the character string <[str]>,
37 producing a substring which can be converted to a double
38 value. The substring converted is the longest initial
39 subsequence of <[str]>, beginning with the first
40 non-whitespace character, that has the format:
41 .[+|-]<[digits]>[.][<[digits]>][(e|E)[+|-]<[digits]>]
42 The substring contains no characters if <[str]> is empty, consists
43 entirely of whitespace, or if the first non-whitespace
44 character is something other than <<+>>, <<->>, <<.>>, or a
45 digit. If the substring is empty, no conversion is done, and
46 the value of <[str]> is stored in <<*<[tail]>>>. Otherwise,
47 the substring is converted, and a pointer to the final string
48 (which will contain at least the terminating null character of
49 <[str]>) is stored in <<*<[tail]>>>. If you want no
50 assignment to <<*<[tail]>>>, pass a null pointer as <[tail]>.
51 <<strtof>> is identical to <<strtod>> except for its return type.
53 This implementation returns the nearest machine number to the
54 input decimal string. Ties are broken by using the IEEE
57 The alternate function <<_strtod_r>> is a reentrant version.
58 The extra argument <[reent]> is a pointer to a reentrancy structure.
61 <<strtod>> returns the converted substring value, if any. If
62 no conversion could be performed, 0 is returned. If the
63 correct value is out of the range of representable values,
64 plus or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is
65 stored in errno. If the correct value would cause underflow, 0
66 is returned and <<ERANGE>> is stored in errno.
68 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
69 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
72 /****************************************************************
74 The author of this software is David M. Gay.
76 Copyright (C) 1998-2001 by Lucent Technologies
79 Permission to use, copy, modify, and distribute this software and
80 its documentation for any purpose and without fee is hereby
81 granted, provided that the above copyright notice appear in all
82 copies and that both that the copyright notice and this
83 permission notice and warranty disclaimer appear in supporting
84 documentation, and that the name of Lucent or any of its entities
85 not be used in advertising or publicity pertaining to
86 distribution of the software without specific, written prior
89 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
90 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
91 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
92 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
93 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
94 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
95 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
98 ****************************************************************/
100 /* Please send bug reports to David M. Gay (dmg at acm dot org,
101 * with " at " changed at "@" and " dot " changed to "."). */
103 /* Original file gdtoa-strtod.c Modified 06-21-2006 by Jeff Johnston to work within newlib. */
112 /* #ifndef NO_FENV_H */
113 /* #include <fenv.h> */
121 #ifndef NO_IEEE_Scale
122 #define Avoid_Underflow
124 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
125 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
126 static _CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
127 9007199254740992.e-256
132 #ifdef Honor_FLT_ROUNDS
133 #define Rounding rounding
134 #undef Check_FLT_ROUNDS
135 #define Check_FLT_ROUNDS
137 #define Rounding Flt_Rounds
141 _DEFUN (ULtod, (L, bits, exp, k),
147 switch(k & STRTOG_Retmask) {
148 case STRTOG_NoNumber:
153 case STRTOG_Denormal:
161 L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20);
164 case STRTOG_Infinite:
174 L[_0] |= 0x80000000L;
179 _DEFUN (match, (sp, t),
180 _CONST char **sp _AND
184 _CONST char *s = *sp;
186 while( (d = *t++) !=0) {
187 if ((c = *++s) >= 'A' && c <= 'Z')
195 #endif /* INFNAN_CHECK */
199 _DEFUN (_strtod_r, (ptr, s00, se),
200 struct _reent *ptr _AND
201 _CONST char *s00 _AND
204 #ifdef Avoid_Underflow
207 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, dsign,
208 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
209 _CONST char *s, *s0, *s1;
210 double aadj, aadj1, adj, rv, rv0;
213 _Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
215 int inexact, oldinexact;
217 #ifdef Honor_FLT_ROUNDS
221 delta = bs = bd = NULL;
222 sign = nz0 = nz = decpt = 0;
224 for(s = s00;;s++) switch(*s) {
248 static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI };
255 #if defined(FE_DOWNWARD) && defined(FE_TONEAREST) && defined(FE_TOWARDZERO) && defined(FE_UPWARD)
257 switch(fegetround()) {
258 case FE_TOWARDZERO: fpi1.rounding = 0; break;
259 case FE_UPWARD: fpi1.rounding = 2; break;
260 case FE_DOWNWARD: fpi1.rounding = 3;
265 switch((i = gethex(ptr, &s, &fpi1, &exp, &bb, sign)) & STRTOG_Retmask) {
266 case STRTOG_NoNumber:
273 copybits(bits, fpi.nbits, bb);
276 ULtod(((U*)&rv)->L, bits, exp, i);
289 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
296 if (c == *localeconv()->decimal_point)
304 for(; c == '0'; c = *++s)
306 if (c > '0' && c <= '9') {
314 for(; c >= '0' && c <= '9'; c = *++s) {
319 for(i = 1; i < nz; i++)
322 else if (nd <= DBL_DIG + 1)
326 else if (nd <= DBL_DIG + 1)
334 if (c == 'e' || c == 'E') {
335 if (!nd && !nz && !nz0) {
346 if (c >= '0' && c <= '9') {
349 if (c > '0' && c <= '9') {
352 while((c = *++s) >= '0' && c <= '9')
354 if (s - s1 > 8 || L > 19999)
355 /* Avoid confusion from exponents
356 * so large that e might overflow.
358 e = 19999; /* safe for 16 bit ints */
373 /* Check for Nan and Infinity */
375 static FPI fpinan = /* only 52 explicit bits */
376 { 52, 1-1023-53+1, 2046-1023-53+1, 1, SI };
381 if (match(&s,"nf")) {
383 if (!match(&s,"inity"))
385 dword0(rv) = 0x7ff00000;
392 if (match(&s, "an")) {
395 && hexnan(&s, &fpinan, bits)
397 dword0(rv) = 0x7ff00000 | bits[1];
398 dword1(rv) = bits[0];
402 dword0(rv) = NAN_WORD0;
403 dword1(rv) = NAN_WORD1;
410 #endif /* INFNAN_CHECK */
419 /* Now we have nd0 digits, starting at s0, followed by a
420 * decimal point, followed by nd-nd0 digits. The number we're
421 * after is the integer represented by those digits times
426 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
431 oldinexact = get_inexact();
433 dval(rv) = tens[k - 9] * dval(rv) + z;
438 #ifndef Honor_FLT_ROUNDS
450 #ifdef Honor_FLT_ROUNDS
451 /* round correctly FLT_ROUNDS = 2 or 3 */
457 /* rv = */ rounded_product(dval(rv), tens[e]);
462 if (e <= Ten_pmax + i) {
463 /* A fancier test would sometimes let us do
464 * this for larger i values.
466 #ifdef Honor_FLT_ROUNDS
467 /* round correctly FLT_ROUNDS = 2 or 3 */
476 /* VAX exponent range is so narrow we must
477 * worry about overflow here...
480 dword0(rv) -= P*Exp_msk1;
481 /* rv = */ rounded_product(dval(rv), tens[e]);
482 if ((dword0(rv) & Exp_mask)
483 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
485 dword0(rv) += P*Exp_msk1;
487 /* rv = */ rounded_product(dval(rv), tens[e]);
492 #ifndef Inaccurate_Divide
493 else if (e >= -Ten_pmax) {
494 #ifdef Honor_FLT_ROUNDS
495 /* round correctly FLT_ROUNDS = 2 or 3 */
501 /* rv = */ rounded_quotient(dval(rv), tens[-e]);
512 oldinexact = get_inexact();
514 #ifdef Avoid_Underflow
517 #ifdef Honor_FLT_ROUNDS
518 if ((rounding = Flt_Rounds) >= 2) {
520 rounding = rounding == 2 ? 0 : 2;
526 #endif /*IEEE_Arith*/
528 /* Get starting approximation = rv * 10**e1 */
531 if ( (i = e1 & 15) !=0)
534 if (e1 > DBL_MAX_10_EXP) {
537 ptr->_errno = ERANGE;
539 /* Can't trust HUGE_VAL */
541 #ifdef Honor_FLT_ROUNDS
543 case 0: /* toward 0 */
544 case 3: /* toward -infinity */
549 dword0(rv) = Exp_mask;
552 #else /*Honor_FLT_ROUNDS*/
553 dword0(rv) = Exp_mask;
555 #endif /*Honor_FLT_ROUNDS*/
557 /* set overflow bit */
559 dval(rv0) *= dval(rv0);
564 #endif /*IEEE_Arith*/
570 for(j = 0; e1 > 1; j++, e1 >>= 1)
572 dval(rv) *= bigtens[j];
573 /* The last multiplication could overflow. */
574 dword0(rv) -= P*Exp_msk1;
575 dval(rv) *= bigtens[j];
576 if ((z = dword0(rv) & Exp_mask)
577 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
579 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
580 /* set to largest number */
581 /* (Can't trust DBL_MAX) */
586 dword0(rv) += P*Exp_msk1;
591 if ( (i = e1 & 15) !=0)
594 if (e1 >= 1 << n_bigtens)
596 #ifdef Avoid_Underflow
599 for(j = 0; e1 > 0; j++, e1 >>= 1)
601 dval(rv) *= tinytens[j];
602 if (scale && (j = 2*P + 1 - ((dword0(rv) & Exp_mask)
603 >> Exp_shift)) > 0) {
604 /* scaled rv is denormal; zap j low bits */
608 dword0(rv) = (P+2)*Exp_msk1;
610 dword0(rv) &= 0xffffffff << (j-32);
613 dword1(rv) &= 0xffffffff << j;
616 for(j = 0; e1 > 1; j++, e1 >>= 1)
618 dval(rv) *= tinytens[j];
619 /* The last multiplication could underflow. */
620 dval(rv0) = dval(rv);
621 dval(rv) *= tinytens[j];
623 dval(rv) = 2.*dval(rv0);
624 dval(rv) *= tinytens[j];
630 ptr->_errno = ERANGE;
636 #ifndef Avoid_Underflow
639 /* The refinement below will clean
640 * this approximation up.
647 /* Now the hard part -- adjusting rv to the correct value.*/
649 /* Put digits into bd: true value = bd * 10^e */
651 bd0 = s2b(ptr, s0, nd0, nd, y);
654 bd = Balloc(ptr,bd0->_k);
656 bb = d2b(ptr,dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
672 #ifdef Honor_FLT_ROUNDS
676 #ifdef Avoid_Underflow
678 i = j + bbbits - 1; /* logb(rv) */
679 if (i < Emin) /* denormal */
683 #else /*Avoid_Underflow*/
684 #ifdef Sudden_Underflow
686 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
690 #else /*Sudden_Underflow*/
692 i = j + bbbits - 1; /* logb(rv) */
693 if (i < Emin) /* denormal */
697 #endif /*Sudden_Underflow*/
698 #endif /*Avoid_Underflow*/
701 #ifdef Avoid_Underflow
704 i = bb2 < bd2 ? bb2 : bd2;
713 bs = pow5mult(ptr, bs, bb5);
714 bb1 = mult(ptr, bs, bb);
719 bb = lshift(ptr, bb, bb2);
721 bd = pow5mult(ptr, bd, bd5);
723 bd = lshift(ptr, bd, bd2);
725 bs = lshift(ptr, bs, bs2);
726 delta = diff(ptr, bb, bd);
727 dsign = delta->_sign;
730 #ifdef Honor_FLT_ROUNDS
733 /* Error is less than an ulp */
734 if (!delta->_x[0] && delta->_wds <= 1) {
750 && !(dword0(rv) & Frac_mask)) {
751 y = dword0(rv) & Exp_mask;
752 #ifdef Avoid_Underflow
753 if (!scale || y > 2*P*Exp_msk1)
758 delta = lshift(ptr, delta,Log2P);
759 if (cmp(delta, bs) <= 0)
764 #ifdef Avoid_Underflow
765 if (scale && (y = dword0(rv) & Exp_mask)
767 dword0(adj) += (2*P+1)*Exp_msk1 - y;
769 #ifdef Sudden_Underflow
770 if ((dword0(rv) & Exp_mask) <=
772 dword0(rv) += P*Exp_msk1;
773 dval(rv) += adj*ulp(dval(rv));
774 dword0(rv) -= P*Exp_msk1;
777 #endif /*Sudden_Underflow*/
778 #endif /*Avoid_Underflow*/
779 dval(rv) += adj*ulp(dval(rv));
783 adj = ratio(delta, bs);
786 if (adj <= 0x7ffffffe) {
787 /* adj = rounding ? ceil(adj) : floor(adj); */
790 if (!((rounding>>1) ^ dsign))
795 #ifdef Avoid_Underflow
796 if (scale && (y = dword0(rv) & Exp_mask) <= 2*P*Exp_msk1)
797 dword0(adj) += (2*P+1)*Exp_msk1 - y;
799 #ifdef Sudden_Underflow
800 if ((dword0(rv) & Exp_mask) <= P*Exp_msk1) {
801 dword0(rv) += P*Exp_msk1;
802 adj *= ulp(dval(rv));
807 dword0(rv) -= P*Exp_msk1;
810 #endif /*Sudden_Underflow*/
811 #endif /*Avoid_Underflow*/
812 adj *= ulp(dval(rv));
819 #endif /*Honor_FLT_ROUNDS*/
822 /* Error is less than half an ulp -- check for
823 * special case of mantissa a power of two.
825 if (dsign || dword1(rv) || dword0(rv) & Bndry_mask
827 #ifdef Avoid_Underflow
828 || (dword0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
830 || (dword0(rv) & Exp_mask) <= Exp_msk1
835 if (!delta->x[0] && delta->wds <= 1)
840 if (!delta->_x[0] && delta->_wds <= 1) {
847 delta = lshift(ptr,delta,Log2P);
848 if (cmp(delta, bs) > 0)
853 /* exactly half-way between */
855 if ((dword0(rv) & Bndry_mask1) == Bndry_mask1
857 #ifdef Avoid_Underflow
858 (scale && (y = dword0(rv) & Exp_mask) <= 2*P*Exp_msk1)
859 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
862 /*boundary case -- increment exponent*/
863 dword0(rv) = (dword0(rv) & Exp_mask)
870 #ifdef Avoid_Underflow
876 else if (!(dword0(rv) & Bndry_mask) && !dword1(rv)) {
878 /* boundary case -- decrement exponent */
879 #ifdef Sudden_Underflow /*{{*/
880 L = dword0(rv) & Exp_mask;
884 #ifdef Avoid_Underflow
885 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
888 #endif /*Avoid_Underflow*/
892 #else /*Sudden_Underflow}{*/
893 #ifdef Avoid_Underflow
895 L = dword0(rv) & Exp_mask;
896 if (L <= (2*P+1)*Exp_msk1) {
897 if (L > (P+2)*Exp_msk1)
901 /* rv = smallest denormal */
905 #endif /*Avoid_Underflow*/
906 L = (dword0(rv) & Exp_mask) - Exp_msk1;
907 #endif /*Sudden_Underflow}*/
908 dword0(rv) = L | Bndry_mask1;
909 dword1(rv) = 0xffffffff;
917 if (!(dword1(rv) & LSB))
921 dval(rv) += ulp(dval(rv));
924 dval(rv) -= ulp(dval(rv));
925 #ifndef Sudden_Underflow
930 #ifdef Avoid_Underflow
936 if ((aadj = ratio(delta, bs)) <= 2.) {
939 else if (dword1(rv) || dword0(rv) & Bndry_mask) {
940 #ifndef Sudden_Underflow
941 if (dword1(rv) == Tiny1 && !dword0(rv))
948 /* special case -- power of FLT_RADIX to be */
949 /* rounded down... */
951 if (aadj < 2./FLT_RADIX)
960 aadj1 = dsign ? aadj : -aadj;
961 #ifdef Check_FLT_ROUNDS
963 case 2: /* towards +infinity */
966 case 0: /* towards 0 */
967 case 3: /* towards -infinity */
973 #endif /*Check_FLT_ROUNDS*/
975 y = dword0(rv) & Exp_mask;
977 /* Check for overflow */
979 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
980 dval(rv0) = dval(rv);
981 dword0(rv) -= P*Exp_msk1;
982 adj = aadj1 * ulp(dval(rv));
984 if ((dword0(rv) & Exp_mask) >=
985 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
986 if (dword0(rv0) == Big0 && dword1(rv0) == Big1)
993 dword0(rv) += P*Exp_msk1;
996 #ifdef Avoid_Underflow
997 if (scale && y <= 2*P*Exp_msk1) {
998 if (aadj <= 0x7fffffff) {
1002 aadj1 = dsign ? aadj : -aadj;
1004 dword0(aadj1) += (2*P+1)*Exp_msk1 - y;
1006 adj = aadj1 * ulp(dval(rv));
1009 #ifdef Sudden_Underflow
1010 if ((dword0(rv) & Exp_mask) <= P*Exp_msk1) {
1011 dval(rv0) = dval(rv);
1012 dword0(rv) += P*Exp_msk1;
1013 adj = aadj1 * ulp(dval(rv));
1016 if ((dword0(rv) & Exp_mask) < P*Exp_msk1)
1018 if ((dword0(rv) & Exp_mask) <= P*Exp_msk1)
1021 if (dword0(rv0) == Tiny0
1022 && dword1(rv0) == Tiny1)
1029 dword0(rv) -= P*Exp_msk1;
1032 adj = aadj1 * ulp(dval(rv));
1035 #else /*Sudden_Underflow*/
1036 /* Compute adj so that the IEEE rounding rules will
1037 * correctly round rv + adj in some half-way cases.
1038 * If rv * ulp(rv) is denormalized (i.e.,
1039 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1040 * trouble from bits lost to denormalization;
1041 * example: 1.2e-307 .
1043 if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
1044 aadj1 = (double)(int)(aadj + 0.5);
1048 adj = aadj1 * ulp(dval(rv));
1050 #endif /*Sudden_Underflow*/
1051 #endif /*Avoid_Underflow*/
1053 z = dword0(rv) & Exp_mask;
1055 #ifdef Avoid_Underflow
1059 /* Can we stop now? */
1062 /* The tolerances below are conservative. */
1063 if (dsign || dword1(rv) || dword0(rv) & Bndry_mask) {
1064 if (aadj < .4999999 || aadj > .5000001)
1067 else if (aadj < .4999999/FLT_RADIX)
1080 dword0(rv0) = Exp_1 + (70 << Exp_shift);
1085 else if (!oldinexact)
1088 #ifdef Avoid_Underflow
1090 dword0(rv0) = Exp_1 - 2*P*Exp_msk1;
1092 dval(rv) *= dval(rv0);
1094 /* try to avoid the bug of testing an 8087 register value */
1095 if (dword0(rv) == 0 && dword1(rv) == 0)
1096 ptr->_errno = ERANGE;
1099 #endif /* Avoid_Underflow */
1101 if (inexact && !(dword0(rv) & Exp_mask)) {
1102 /* set underflow bit */
1104 dval(rv0) *= dval(rv0);
1116 return sign ? -dval(rv) : dval(rv);
1122 _DEFUN (strtod, (s00, se),
1123 _CONST char *s00 _AND char **se)
1125 return _strtod_r (_REENT, s00, se);
1129 _DEFUN (strtof, (s00, se),
1130 _CONST char *s00 _AND
1133 double retval = _strtod_r (_REENT, s00, se);
1136 return (float)retval;