2495{
2496 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
2497 int esign, i,
j, k, nd, nd0, nf, nz, nz0, nz1, sign;
2499 double aadj, aadj1;
2501 U aadj2, adj,
rv, rv0;
2504 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
2505#ifdef Avoid_Underflow
2507#endif
2508#ifdef SET_INEXACT
2509 int oldinexact;
2510#endif
2511#ifndef NO_STRTOD_BIGCOMP
2512 int req_bigcomp = 0;
2513#endif
2514#ifdef Honor_FLT_ROUNDS
2515#ifdef Trust_FLT_ROUNDS
2517#else
2519 switch(fegetround()) {
2520 case FE_TOWARDZERO: bc.
rounding = 0;
break;
2521 case FE_UPWARD: bc.
rounding = 2;
break;
2523 }
2524#endif
2525#endif
2526#ifdef USE_LOCALE
2528#endif
2529
2532 for(
s = s00;;
s++)
switch(*
s) {
2533 case '-':
2534 sign = 1;
2535
2536 case '+':
2538 goto break2;
2539
2540 case 0:
2541 goto ret0;
2542 case '\t':
2543 case '\n':
2544 case '\v':
2545 case '\f':
2546 case '\r':
2547 case ' ':
2548 continue;
2549 default:
2550 goto break2;
2551 }
2552 break2:
2554#ifndef NO_HEX_FP
2556 case 'x':
2557 case 'X':
2558#ifdef Honor_FLT_ROUNDS
2560#else
2562#endif
2564 }
2565#endif
2566 nz0 = 1;
2567 while(*++
s ==
'0') ;
2570 }
2573 for(nd = nf = 0; (c = *
s) >=
'0' && c <=
'9'; nd++,
s++)
2574 if (nd < 9)
2576 else if (nd < DBL_DIG + 2)
2577 z = 10*z + c - '0';
2578 nd0 = nd;
2580 for(s1 =
s; s1 > s0 && *--s1 ==
'0'; )
2581 ++nz1;
2582#ifdef USE_LOCALE
2583 s1 = localeconv()->decimal_point;
2584 if (c == *s1) {
2585 c = '.';
2586 if (*++s1) {
2588 for(;;) {
2589 if (*++s2 != *s1) {
2590 c = 0;
2591 break;
2592 }
2593 if (!*++s1) {
2595 break;
2596 }
2597 }
2598 }
2599 }
2600#endif
2601 if (c == '.') {
2605 if (!nd) {
2606 for(; c ==
'0'; c = *++
s)
2607 nz++;
2608 if (c > '0' && c <= '9') {
2612 nf += nz;
2613 nz = 0;
2614 goto have_dig;
2615 }
2616 goto dig_done;
2617 }
2618 for(; c >=
'0' && c <=
'9'; c = *++
s) {
2619 have_dig:
2620 nz++;
2621 if (c -= '0') {
2622 nf += nz;
2623 for(i = 1; i < nz; i++)
2624 if (nd++ < 9)
2626 else if (nd <= DBL_DIG + 2)
2627 z *= 10;
2628 if (nd++ < 9)
2630 else if (nd <= DBL_DIG + 2)
2631 z = 10*z + c;
2632 nz = nz1 = 0;
2633 }
2634 }
2635 }
2636 dig_done:
2637 e = 0;
2638 if (c == 'e' || c == 'E') {
2639 if (!nd && !nz && !nz0) {
2640 goto ret0;
2641 }
2643 esign = 0;
2645 case '-':
2646 esign = 1;
2647 case '+':
2649 }
2650 if (c >= '0' && c <= '9') {
2651 while(c == '0')
2653 if (c > '0' && c <= '9') {
2654 L = c - '0';
2656 while((c = *++
s) >=
'0' && c <=
'9')
2657 L = 10*L + c - '0';
2658 if (
s - s1 > 8 || L > 19999)
2659
2660
2661
2662 e = 19999;
2663 else
2664 e = (int)L;
2665 if (esign)
2666 e = -e;
2667 }
2668 else
2669 e = 0;
2670 }
2671 else
2673 }
2674 if (!nd) {
2675 if (!nz && !nz0) {
2676#ifdef INFNAN_CHECK
2677
2679 switch(c) {
2680 case 'i':
2681 case 'I':
2682 if (match(&
s,
"nf")) {
2684 if (!match(&
s,
"inity"))
2689 }
2690 break;
2691 case 'n':
2692 case 'N':
2693 if (match(&
s,
"an")) {
2696#ifndef No_Hex_NaN
2699#endif
2701 }
2702 }
2703#endif
2704 ret0:
2706 sign = 0;
2707 }
2709 }
2710 bc.
e0 = e1 = e -= nf;
2711
2712
2713
2714
2715
2716
2717 if (!nd0)
2718 nd0 = nd;
2719 k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
2721 if (k > 9) {
2722#ifdef SET_INEXACT
2723 if (k > DBL_DIG)
2724 oldinexact = get_inexact();
2725#endif
2727 }
2728 bd0 = 0;
2729 if (nd <= DBL_DIG
2730#ifndef RND_PRODQUOT
2731#ifndef Honor_FLT_ROUNDS
2733#endif
2734#endif
2735 ) {
2736 if (!e)
2738#ifndef ROUND_BIASED_without_Round_Up
2739 if (e > 0) {
2741#ifdef VAX
2742 goto vax_ovfl_check;
2743#else
2744#ifdef Honor_FLT_ROUNDS
2745
2746 if (sign) {
2748 sign = 0;
2749 }
2750#endif
2753#endif
2754 }
2755 i = DBL_DIG - nd;
2757
2758
2759
2760#ifdef Honor_FLT_ROUNDS
2761
2762 if (sign) {
2764 sign = 0;
2765 }
2766#endif
2767 e -= i;
2769#ifdef VAX
2770
2771
2772
2773 vax_ovfl_check:
2778 goto ovfl;
2780#else
2782#endif
2784 }
2785 }
2786#ifndef Inaccurate_Divide
2788#ifdef Honor_FLT_ROUNDS
2789
2790 if (sign) {
2792 sign = 0;
2793 }
2794#endif
2797 }
2798#endif
2799#endif
2800 }
2801 e1 += nd - k;
2802
2803#ifdef IEEE_Arith
2804#ifdef SET_INEXACT
2806 if (k <= DBL_DIG)
2807 oldinexact = get_inexact();
2808#endif
2809#ifdef Avoid_Underflow
2811#endif
2812#ifdef Honor_FLT_ROUNDS
2814 if (sign)
2816 else
2819 }
2820#endif
2821#endif
2822
2823
2824
2825 if (e1 > 0) {
2826 if ((i = e1 & 15))
2828 if (e1 &= ~15) {
2829 if (e1 > DBL_MAX_10_EXP) {
2830 ovfl:
2831
2832#ifdef IEEE_Arith
2833#ifdef Honor_FLT_ROUNDS
2835 case 0:
2836 case 3:
2839 break;
2840 default:
2843 }
2844#else
2847#endif
2848#ifdef SET_INEXACT
2849
2852#endif
2853#else
2856#endif
2857 range_err:
2858 if (bd0) {
2859 Bfree(bb);
2860 Bfree(bd);
2861 Bfree(bs);
2862 Bfree(bd0);
2863 Bfree(delta);
2864 }
2865#ifndef NO_ERRNO
2866 errno = ERANGE;
2867#endif
2869 }
2870 e1 >>= 4;
2871 for(
j = 0; e1 > 1;
j++, e1 >>= 1)
2872 if (e1 & 1)
2874
2879 goto ovfl;
2881
2882
2885 }
2886 else
2888 }
2889 }
2890 else if (e1 < 0) {
2891 e1 = -e1;
2892 if ((i = e1 & 15))
2894 if (e1 >>= 4) {
2896 goto undfl;
2897#ifdef Avoid_Underflow
2898 if (e1 & Scale_Bit)
2900 for(
j = 0; e1 > 0;
j++, e1 >>= 1)
2901 if (e1 & 1)
2905
2908 goto undfl;
2912 else
2913 word0(&
rv) &= 0xffffffff << (
j-32);
2914 }
2915 else
2917 }
2918#else
2919 for(
j = 0; e1 > 1;
j++, e1 >>= 1)
2920 if (e1 & 1)
2922
2928#endif
2930 undfl:
2932 goto range_err;
2933 }
2934#ifndef Avoid_Underflow
2937
2938
2939
2940 }
2941#endif
2942 }
2943 }
2944
2945
2946
2947
2948
2950#ifndef NO_STRTOD_BIGCOMP
2952
2953
2955
2956
2957
2959 if (i > nd0)
2961 for(;;) {
2965 break;
2966 --i;
2967 }
2968 e += nd - i;
2969 nd = i;
2970 if (nd0 > nd)
2971 nd0 = nd;
2972 if (nd < 9) {
2974 for(i = 0; i < nd0; ++i)
2975 y = 10*y + s0[i] - '0';
2976 for(
j = bc.
dp1; i < nd; ++i)
2977 y = 10*
y + s0[
j++] -
'0';
2978 }
2979 }
2980#endif
2981 bd0 = s2b(s0, nd0, nd, y, bc.
dplen);
2982
2983 for(;;) {
2984 bd = Balloc(bd0->
k);
2986 bb = d2b(&
rv, &bbe, &bbbits);
2987 bs = i2b(1);
2988
2989 if (e >= 0) {
2990 bb2 = bb5 = 0;
2991 bd2 = bd5 = e;
2992 }
2993 else {
2994 bb2 = bb5 = -e;
2995 bd2 = bd5 = 0;
2996 }
2997 if (bbe >= 0)
2998 bb2 += bbe;
2999 else
3000 bd2 -= bbe;
3001 bs2 = bb2;
3002#ifdef Honor_FLT_ROUNDS
3004 bs2++;
3005#endif
3006#ifdef Avoid_Underflow
3008 Lsb1 = 0;
3015 if (i < 32)
3016 Lsb <<= i;
3017 else if (i < 52)
3018 Lsb1 = Lsb << (i-32);
3019 else
3021 }
3022#else
3023#ifdef Sudden_Underflow
3024#ifdef IBM
3025 j = 1 + 4*
P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
3026#else
3028#endif
3029#else
3034 else
3036#endif
3037#endif
3040#ifdef Avoid_Underflow
3042#endif
3043 i = bb2 < bd2 ? bb2 : bd2;
3044 if (i > bs2)
3045 i = bs2;
3046 if (i > 0) {
3047 bb2 -= i;
3048 bd2 -= i;
3049 bs2 -= i;
3050 }
3051 if (bb5 > 0) {
3052 bs = pow5mult(bs, bb5);
3053 bb1 = mult(bs, bb);
3054 Bfree(bb);
3055 bb = bb1;
3056 }
3057 if (bb2 > 0)
3058 bb = lshift(bb, bb2);
3059 if (bd5 > 0)
3060 bd = pow5mult(bd, bd5);
3061 if (bd2 > 0)
3062 bd = lshift(bd, bd2);
3063 if (bs2 > 0)
3064 bs = lshift(bs, bs2);
3065 delta =
diff(bb, bd);
3069#ifndef NO_STRTOD_BIGCOMP
3070 if (bc.
nd > nd && i <= 0) {
3072
3073 req_bigcomp = 1;
3074 break;
3075 }
3076#ifdef Honor_FLT_ROUNDS
3078 if (i < 0) {
3079 req_bigcomp = 1;
3080 break;
3081 }
3082 }
3083 else
3084#endif
3085 i = -1;
3086 }
3087#endif
3088#ifdef Honor_FLT_ROUNDS
3090 if (i < 0) {
3091
3092 if (!delta->
x[0] && delta->
wds <= 1) {
3093
3094#ifdef SET_INEXACT
3096#endif
3097 break;
3098 }
3102 goto apply_adj;
3103 }
3104 }
3105 else if (!bc.
dsign) {
3110#ifdef Avoid_Underflow
3112#else
3113 if (y)
3114#endif
3115 {
3116 delta = lshift(delta,
Log2P);
3117 if (
cmp(delta, bs) <= 0)
3119 }
3120 }
3121 apply_adj:
3122#ifdef Avoid_Underflow
3126#else
3127#ifdef Sudden_Underflow
3133 }
3134 else
3135#endif
3136#endif
3138 }
3139 break;
3140 }
3141 adj.
d =
ratio(delta, bs);
3144 if (adj.
d <= 0x7ffffffe) {
3145
3151 }
3152 }
3153#ifdef Avoid_Underflow
3156#else
3157#ifdef Sudden_Underflow
3163 else
3166 goto cont;
3167 }
3168#endif
3169#endif
3173 goto ovfl;
3175 }
3176 else
3178 goto cont;
3179 }
3180#endif
3181
3182 if (i < 0) {
3183
3184
3185
3187#ifdef IEEE_Arith
3188#ifdef Avoid_Underflow
3190#else
3192#endif
3193#endif
3194 ) {
3195#ifdef SET_INEXACT
3196 if (!delta->
x[0] && delta->
wds <= 1)
3198#endif
3199 break;
3200 }
3201 if (!delta->
x[0] && delta->
wds <= 1) {
3202
3203#ifdef SET_INEXACT
3205#endif
3206 break;
3207 }
3208 delta = lshift(delta,
Log2P);
3209 if (
cmp(delta, bs) > 0)
3210 goto drop_down;
3211 break;
3212 }
3213 if (i == 0) {
3214
3218#ifdef Avoid_Underflow
3220 ? (0xffffffff & (0xffffffff << (2*
P+1-(y>>
Exp_shift)))) :
3221#endif
3222 0xffffffff)) {
3223
3225 goto ovfl;
3228#ifdef IBM
3230#endif
3231 ;
3233#ifdef Avoid_Underflow
3235#endif
3236 break;
3237 }
3238 }
3240 drop_down:
3241
3242#ifdef Sudden_Underflow
3244#ifdef IBM
3246#else
3247#ifdef Avoid_Underflow
3249#else
3251#endif
3252#endif
3253 {
3256 break;
3257 }
3258 goto undfl;
3259 }
3261#else
3262#ifdef Avoid_Underflow
3267
3268
3269 break;
3270
3273 break;
3274 }
3275 goto undfl;
3276 }
3277 }
3278#endif
3280#endif
3283#ifdef IBM
3284 goto cont;
3285#else
3286#ifndef NO_STRTOD_BIGCOMP
3288 goto cont;
3289#endif
3290 break;
3291#endif
3292 }
3293#ifndef ROUND_BIASED
3294#ifdef Avoid_Underflow
3295 if (Lsb1) {
3297 break;
3298 }
3300 break;
3301#else
3303 break;
3304#endif
3305#endif
3307#ifdef Avoid_Underflow
3309#else
3311#endif
3312#ifndef ROUND_BIASED
3313 else {
3314#ifdef Avoid_Underflow
3316#else
3318#endif
3319#ifndef Sudden_Underflow
3323 break;
3324 }
3325 goto undfl;
3326 }
3327#endif
3328 }
3329#ifdef Avoid_Underflow
3331#endif
3332#endif
3333 break;
3334 }
3335 if ((aadj =
ratio(delta, bs)) <= 2.) {
3337 aadj = aadj1 = 1.;
3339#ifndef Sudden_Underflow
3343 break;
3344 }
3345 goto undfl;
3346 }
3347#endif
3348 aadj = 1.;
3349 aadj1 = -1.;
3350 }
3351 else {
3352
3353
3354
3355 if (aadj < 2./FLT_RADIX)
3356 aadj = 1./FLT_RADIX;
3357 else
3358 aadj *= 0.5;
3359 aadj1 = -aadj;
3360 }
3361 }
3362 else {
3363 aadj *= 0.5;
3364 aadj1 = bc.
dsign ? aadj : -aadj;
3365#ifdef Check_FLT_ROUNDS
3367 case 2:
3368 aadj1 -= 0.5;
3369 break;
3370 case 0:
3371 case 3:
3372 aadj1 += 0.5;
3373 }
3374#else
3376 aadj1 += 0.5;
3377#endif
3378 }
3380
3381
3382
3386 adj.
d = aadj1 * ulp(&
rv);
3391 goto ovfl;
3394 goto cont;
3395 }
3396 else
3398 }
3399 else {
3400#ifdef Avoid_Underflow
3402 if (aadj <= 0x7fffffff) {
3403 if ((z = aadj) <= 0)
3404 z = 1;
3405 aadj = z;
3406 aadj1 = bc.
dsign ? aadj : -aadj;
3407 }
3408 dval(&aadj2) = aadj1;
3410 aadj1 =
dval(&aadj2);
3411 adj.
d = aadj1 * ulp(&
rv);
3414#ifdef NO_STRTOD_BIGCOMP
3415 goto undfl;
3416#else
3417 {
3418 req_bigcomp = 1;
3419 break;
3420 }
3421#endif
3422 }
3423 else {
3424 adj.
d = aadj1 * ulp(&
rv);
3426 }
3427#else
3428#ifdef Sudden_Underflow
3432 adj.
d = aadj1 * ulp(&
rv);
3434#ifdef IBM
3436#else
3438#endif
3439 {
3444 break;
3445 }
3446 goto undfl;
3447 }
3450 goto cont;
3451 }
3452 else
3454 }
3455 else {
3456 adj.
d = aadj1 * ulp(&
rv);
3458 }
3459#else
3460
3461
3462
3463
3464
3465
3466
3468 aadj1 = (double)(int)(aadj + 0.5);
3470 aadj1 = -aadj1;
3471 }
3472 adj.
d = aadj1 * ulp(&
rv);
3474#endif
3475#endif
3476 }
3478#ifndef SET_INEXACT
3480#ifdef Avoid_Underflow
3482#endif
3483 if (y == z) {
3484
3486 aadj -= L;
3487
3489 if (aadj < .4999999 || aadj > .5000001)
3490 break;
3491 }
3492 else if (aadj < .4999999/FLT_RADIX)
3493 break;
3494 }
3495 }
3496#endif
3497 cont:
3498 Bfree(bb);
3499 Bfree(bd);
3500 Bfree(bs);
3501 Bfree(delta);
3502 }
3503 Bfree(bb);
3504 Bfree(bd);
3505 Bfree(bs);
3506 Bfree(bd0);
3507 Bfree(delta);
3508#ifndef NO_STRTOD_BIGCOMP
3509 if (req_bigcomp) {
3510 bd0 = 0;
3512 bigcomp(&
rv, s0, &bc);
3515 goto ovfl;
3516 if (y == 0 &&
rv.d == 0.)
3517 goto undfl;
3518 }
3519#endif
3520#ifdef SET_INEXACT
3522 if (!oldinexact) {
3526 }
3527 }
3528 else if (!oldinexact)
3529 clear_inexact();
3530#endif
3531#ifdef Avoid_Underflow
3536#ifndef NO_ERRNO
3537
3538#ifdef IEEE_Arith
3540#else
3542#endif
3543 errno = ERANGE;
3544#endif
3545 }
3546#endif
3547#ifdef SET_INEXACT
3549
3550 dval(&rv0) = 1e-300;
3552 }
3553#endif
3555 if (se)
3558 }
#define rounded_product(a, b)
#define rounded_quotient(a, b)
void gethex(CONST char **sp, U *rvp, int rounding, int sign)
impl::ratio< uint64_t > ratio