116 #include "small_mprec.h"
119 _DEFUN (_strtod_r, (ptr, s00, se),
120 struct _reent *ptr _AND
121 _CONST
char *s00 _AND
124 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, e1, esign, i, j,
125 k, nd, nd0, nf, nz, nz0, sign;
127 _CONST
char *s, *s0, *s1;
128 double aadj, aadj1, adj;
132 union double_union rv, rv0;
134 _Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
151 #define BUF_LSHIFT_SIZE 40
153 _Bigint tab_bb[BUF_LSHIFT_SIZE],tab_bb1[BUF_SIZE],tab_bd[BUF_SIZE],tab_bd0[BUF_SIZE],tab_bs[BUF_LSHIFT_SIZE], tab_delta[BUF_LSHIFT_SIZE];
154 _Bigint tab_bblshift[BUF_LSHIFT_SIZE],tab_bslshift[BUF_LSHIFT_SIZE], tab_deltalshift[BUF_LSHIFT_SIZE],tab_bdlshift[BUF_LSHIFT_SIZE];
192 for (nd = nf = 0; (c = *s) >=
'0' && c <=
'9'; nd++, s++)
194 y = 10 * y + c -
'0';
196 z = 10 * z + c -
'0';
203 for (; c ==
'0'; c = *++s)
205 if (c >
'0' && c <=
'9')
214 for (; c >=
'0' && c <=
'9'; c = *++s)
221 for (i = 1; i < nz; i++)
224 else if (nd <= DBL_DIG + 1)
228 else if (nd <= DBL_DIG + 1)
236 if (c ==
'e' || c ==
'E')
238 if (!nd && !nz && !nz0)
252 if (c >=
'0' && c <=
'9')
256 if (c >
'0' && c <=
'9')
260 while ((c = *++s) >=
'0' && c <=
'9')
261 e = 10 * e + c -
'0';
291 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
295 rv.d = tens[k - 9] * rv.d + z;
297 rv.d = small_tens[k - 9] * rv.d + z;
316 rounded_product (rv.d, tens[e]);
318 rounded_product (rv.d, small_tens[e]);
324 if (e <= Ten_pmax + i)
333 rv.d *= small_tens[i];
340 word0 (rv) -= P * Exp_msk1;
342 rounded_product (rv.d, tens[e]);
344 rounded_product (rv.d, small_tens[e]);
346 if ((word0 (rv) & Exp_mask)
347 > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
349 word0 (rv) += P * Exp_msk1;
352 rounded_product (rv.d, tens[e]);
354 rounded_product (rv.d, small_tens[e]);
360 #ifndef Inaccurate_Divide
361 else if (e >= -Ten_pmax)
364 rounded_quotient (rv.d, tens[-e]);
366 rounded_quotient (rv.d, small_tens[-e]);
378 if ((i = e1 & 15) != 0)
382 rv.d *= small_tens[i];
386 if (e1 > DBL_MAX_10_EXP)
389 ptr->_errno = ERANGE;
395 word0 (rv) = Exp_mask;
396 #ifndef _DOUBLE_IS_32BITS
401 #ifndef _DOUBLE_IS_32BITS
412 for (j = 0; e1 > 1; j++, e1 >>= 1)
417 rv.d *= small_bigtens[j];
421 word0 (rv) -= P * Exp_msk1;
425 rv.d *= small_bigtens[j];
428 if ((z = word0 (rv) & Exp_mask)
429 > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
431 if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
436 #ifndef _DOUBLE_IS_32BITS
441 word0 (rv) += P * Exp_msk1;
449 if ((i = e1 & 15) != 0)
453 rv.d /= small_tens[i];
458 if (e1 >= 1 << n_bigtens)
460 for (j = 0; e1 > 1; j++, e1 >>= 1)
468 rv.d *= small_tinytens[j];
471 rv.d *= small_tinytens[j];
479 rv.d *= small_tinytens[j];
485 ptr->_errno = ERANGE;
490 #ifndef _DOUBLE_IS_32BITS
507 bd0 = s2b (ptr, s0, nd0, nd, y);
509 bd0 = small_s2b(ptr,s0, nd0, nd, y, &tab_bd0[0]);
515 bd = Balloc (ptr, bd0->_k);
519 bd->_maxwds = 1 << (bd0->_k);
520 bd->_sign = bd->_wds =0;
525 bb = d2b (ptr, rv.d, &bbe, &bbbits);
528 bb = small_d2b (ptr, rv.d, &bbe, &bbbits, &tab_bb[0]);
529 bs = small_i2b (ptr, 1, &tab_bs[0]);
546 #ifdef Sudden_Underflow
548 j = 1 + 4 * P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
553 i = bbe + bbbits - 1;
555 j = bbe + (P - Emin);
561 i = bb2 < bd2 ? bb2 : bd2;
573 bs = pow5mult (ptr, bs, bb5);
574 bb1 = mult (ptr, bs, bb);
578 if (bs == &tab_bs[0]){
579 bs = small_pow5mult (ptr, bs, bb5,&tab_bslshift[0]);
582 bs = small_pow5mult (ptr, bs, bb5,&tab_bs[0]);
584 bb1 = small_mult (ptr, bs, bb,&tab_bb1[0]);
592 bb = lshift (ptr, bb, bb2);
594 bd = pow5mult (ptr, bd, bd5);
596 bd = lshift (ptr, bd, bd2);
598 bs = lshift (ptr, bs, bs2);
599 delta = diff (ptr, bb, bd);
600 dsign = delta->_sign;
605 if (bb == &tab_bb[0] ){
606 bb = small_lshift (ptr, bb, bb2,&tab_bblshift[0]);
609 bb = small_lshift (ptr, bb, bb2,&tab_bblshift[0]);
613 if (bd == &tab_bd[0]){
614 bd = small_pow5mult (ptr, bd, bd5, &tab_bdlshift[0]);
617 bd = small_pow5mult (ptr, bd, bd5, &tab_bd[0]);
621 if (bd == &tab_bd[0] ){
622 bd = small_lshift (ptr, bb, bd2,&tab_bdlshift[0]);
625 bd = small_lshift (ptr, bd, bd2,&tab_bd[0]);
629 if ( bs == &tab_bs[0] ){
630 bs = small_lshift (ptr, bs, bs2,&tab_bslshift[0]);
633 bs = small_lshift (ptr, bs, bs2,&tab_bs[0]);
637 delta = small_diff (ptr, bb, bd,&tab_delta[0]);
638 dsign = delta->_sign;
640 i = small_cmp (delta, bs);
648 if (dsign || word1 (rv) || word0 (rv) & Bndry_mask)
652 delta = lshift (ptr, delta, Log2P);
653 if (cmp (delta, bs) > 0)
656 if (delta == &tab_delta[0]){
657 delta = small_lshift (ptr, delta, Log2P,&tab_deltalshift[0]);
660 delta = small_lshift (ptr, delta, Log2P,&tab_delta[0]);
662 if (small_cmp (delta, bs) > 0)
672 if ((word0 (rv) & Bndry_mask1) == Bndry_mask1
673 && word1 (rv) == 0xffffffff)
676 word0 (rv) = (word0 (rv) & Exp_mask)
682 #ifndef _DOUBLE_IS_32BITS
688 else if (!(word0 (rv) & Bndry_mask) && !word1 (rv))
692 #ifdef Sudden_Underflow
693 L = word0 (rv) & Exp_mask;
702 L = (word0 (rv) & Exp_mask) - Exp_msk1;
704 word0 (rv) = L | Bndry_mask1;
705 #ifndef _DOUBLE_IS_32BITS
706 word1 (rv) = 0xffffffff;
715 if (!(word1 (rv) &
LSB))
722 rv.d += small_ulp (rv.d);
730 rv.d -= small_ulp (rv.d);
732 #ifndef Sudden_Underflow
742 if ((aadj = ratio (delta, bs)) <= 2.)
745 if ((aadj = small_ratio (delta, bs)) <= 2.)
750 else if (word1 (rv) || word0 (rv) & Bndry_mask)
752 #ifndef Sudden_Underflow
753 if (word1 (rv) == Tiny1 && !word0 (rv))
764 if (aadj < 2. / FLT_RADIX)
765 aadj = 1. / FLT_RADIX;
774 aadj1 = dsign ? aadj : -aadj;
775 #ifdef Check_FLT_ROUNDS
790 y = word0 (rv) & Exp_mask;
794 if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
797 word0 (rv) -= P * Exp_msk1;
799 adj = aadj1 * ulp (rv.d);
801 adj = aadj1 * small_ulp (rv.d);
804 if ((word0 (rv) & Exp_mask) >=
805 Exp_msk1 * (DBL_MAX_EXP + Bias - P))
807 if (word0 (rv0) == Big0 && word1 (rv0) == Big1)
809 #ifdef _DOUBLE_IS_32BITS
818 word0 (rv) += P * Exp_msk1;
822 #ifdef Sudden_Underflow
823 if ((word0 (rv) & Exp_mask) <= P * Exp_msk1)
826 word0 (rv) += P * Exp_msk1;
828 adj = aadj1 * ulp (rv.d);
830 adj = aadj1 * small_ulp (rv.d);
834 if ((word0 (rv) & Exp_mask) < P * Exp_msk1)
836 if ((word0 (rv) & Exp_mask) <= P * Exp_msk1)
839 if (word0 (rv0) == Tiny0
840 && word1 (rv0) == Tiny1)
847 word0 (rv) -= P * Exp_msk1;
852 adj = aadj1 * ulp (rv.d);
854 adj = aadj1 * small_ulp (rv.d);
866 if (y <= (P - 1) * Exp_msk1 && aadj >= 1.)
868 aadj1 = (double) (
int) (aadj + 0.5);
873 adj = aadj1 * ulp (rv.d);
875 adj = aadj1 * small_ulp (rv.d);
880 z = word0 (rv) & Exp_mask;
887 if (dsign || word1 (rv) || word0 (rv) & Bndry_mask)
889 if (aadj < .4999999 || aadj > .5000001)
892 else if (aadj < .4999999 / FLT_RADIX)
916 return sign ? -rv.d : rv.d;
922 _DEFUN (strtod, (s00, se),
923 _CONST
char *s00 _AND
char **se)
925 return _strtod_r (_REENT, s00, se);
929 _DEFUN (strtof, (s00, se),
930 _CONST
char *s00 _AND
933 return (
float)_strtod_r (_REENT, s00, se);