aboutsummaryrefslogtreecommitdiff
path: root/dtoa.c
diff options
context:
space:
mode:
Diffstat (limited to 'dtoa.c')
-rw-r--r--dtoa.c111
1 files changed, 83 insertions, 28 deletions
diff --git a/dtoa.c b/dtoa.c
index 8dac7b8..33fd6f0 100644
--- a/dtoa.c
+++ b/dtoa.c
@@ -1533,12 +1533,18 @@ ThInfo {
1533set_max_dtoa_threads(unsigned int n) 1533set_max_dtoa_threads(unsigned int n)
1534{ 1534{
1535 size_t L; 1535 size_t L;
1536 ThInfo *newTI1;
1536 1537
1537 if (n > maxthreads) { 1538 if (n > maxthreads) {
1538 L = n*sizeof(ThInfo); 1539 L = n*sizeof(ThInfo);
1539 if (TI1) { 1540 if (TI1) {
1540 TI1 = (ThInfo*)REALLOC(TI1, L); 1541 newTI1 = (ThInfo*)REALLOC(TI1, L);
1541 memset(TI1 + maxthreads, 0, (n-maxthreads)*sizeof(ThInfo)); 1542 if (newTI1) {
1543 TI1 = newTI1;
1544 memset(TI1 + maxthreads, 0, (n-maxthreads)*sizeof(ThInfo));
1545 }
1546 else
1547 return;
1542 } 1548 }
1543 else { 1549 else {
1544 TI1 = (ThInfo*)MALLOC(L); 1550 TI1 = (ThInfo*)MALLOC(L);
@@ -1871,7 +1877,7 @@ mult(Bigint *a, Bigint *b MTd)
1871#else 1877#else
1872#ifdef Pack_32 1878#ifdef Pack_32
1873 for(; xb < xbe; xb++, xc0++) { 1879 for(; xb < xbe; xb++, xc0++) {
1874 if (y = *xb & 0xffff) { 1880 if ((y = *xb & 0xffff)) {
1875 x = xa; 1881 x = xa;
1876 xc = xc0; 1882 xc = xc0;
1877 carry = 0; 1883 carry = 0;
@@ -1885,7 +1891,7 @@ mult(Bigint *a, Bigint *b MTd)
1885 while(x < xae); 1891 while(x < xae);
1886 *xc = carry; 1892 *xc = carry;
1887 } 1893 }
1888 if (y = *xb >> 16) { 1894 if ((y = *xb >> 16)) {
1889 x = xa; 1895 x = xa;
1890 xc = xc0; 1896 xc = xc0;
1891 carry = 0; 1897 carry = 0;
@@ -2718,13 +2724,14 @@ enum { /* rounding values: same as FLT_ROUNDS */
2718 }; 2724 };
2719 2725
2720 void 2726 void
2721gethex( const char **sp, U *rvp, int rounding, int sign MTd) 2727gethex(const char **sp, U *rvp, int rounding, int sign MTd)
2722{ 2728{
2723 Bigint *b; 2729 Bigint *b;
2730 char d;
2724 const unsigned char *decpt, *s0, *s, *s1; 2731 const unsigned char *decpt, *s0, *s, *s1;
2725 Long e, e1; 2732 Long e, e1;
2726 ULong L, lostbits, *x; 2733 ULong L, lostbits, *x;
2727 int big, denorm, esign, havedig, k, n, nbits, up, zret; 2734 int big, denorm, esign, havedig, k, n, nb, nbits, nz, up, zret;
2728#ifdef IBM 2735#ifdef IBM
2729 int j; 2736 int j;
2730#endif 2737#endif
@@ -2742,6 +2749,9 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2742#endif 2749#endif
2743#endif /*}}*/ 2750#endif /*}}*/
2744 }; 2751 };
2752#ifdef IEEE_Arith
2753 int check_denorm = 0;
2754#endif
2745#ifdef USE_LOCALE 2755#ifdef USE_LOCALE
2746 int i; 2756 int i;
2747#ifdef NO_LOCALE_CACHE 2757#ifdef NO_LOCALE_CACHE
@@ -2893,7 +2903,7 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2893 k++; 2903 k++;
2894 b = Balloc(k MTa); 2904 b = Balloc(k MTa);
2895 x = b->x; 2905 x = b->x;
2896 n = 0; 2906 havedig = n = nz = 0;
2897 L = 0; 2907 L = 0;
2898#ifdef USE_LOCALE 2908#ifdef USE_LOCALE
2899 for(i = 0; decimalpoint[i+1]; ++i); 2909 for(i = 0; decimalpoint[i+1]; ++i);
@@ -2908,22 +2918,28 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2908 if (*--s1 == '.') 2918 if (*--s1 == '.')
2909 continue; 2919 continue;
2910#endif 2920#endif
2921 if ((d = hexdig[*s1]))
2922 havedig = 1;
2923 else if (!havedig) {
2924 e += 4;
2925 continue;
2926 }
2911 if (n == ULbits) { 2927 if (n == ULbits) {
2912 *x++ = L; 2928 *x++ = L;
2913 L = 0; 2929 L = 0;
2914 n = 0; 2930 n = 0;
2915 } 2931 }
2916 L |= (hexdig[*s1] & 0x0f) << n; 2932 L |= (d & 0x0f) << n;
2917 n += 4; 2933 n += 4;
2918 } 2934 }
2919 *x++ = L; 2935 *x++ = L;
2920 b->wds = n = x - b->x; 2936 b->wds = n = x - b->x;
2921 n = ULbits*n - hi0bits(L); 2937 nb = ULbits*n - hi0bits(L);
2922 nbits = Nbits; 2938 nbits = Nbits;
2923 lostbits = 0; 2939 lostbits = 0;
2924 x = b->x; 2940 x = b->x;
2925 if (n > nbits) { 2941 if (nb > nbits) {
2926 n -= nbits; 2942 n = nb - nbits;
2927 if (any_on(b,n)) { 2943 if (any_on(b,n)) {
2928 lostbits = 1; 2944 lostbits = 1;
2929 k = n - 1; 2945 k = n - 1;
@@ -2936,8 +2952,8 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2936 rshift(b, n); 2952 rshift(b, n);
2937 e += n; 2953 e += n;
2938 } 2954 }
2939 else if (n < nbits) { 2955 else if (nb < nbits) {
2940 n = nbits - n; 2956 n = nbits - nb;
2941 b = lshift(b, n MTa); 2957 b = lshift(b, n MTa);
2942 e -= n; 2958 e -= n;
2943 x = b->x; 2959 x = b->x;
@@ -2992,12 +3008,49 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2992 return; 3008 return;
2993 } 3009 }
2994 k = n - 1; 3010 k = n - 1;
3011#ifdef IEEE_Arith
3012 if (!k) {
3013 switch(rounding) {
3014 case Round_near:
3015 if (((b->x[0] & 3) == 3) || (lostbits && (b->x[0] & 1))) {
3016 multadd(b, 1, 1 MTa);
3017 emin_check:
3018 if (b->x[1] == (1 << (Exp_shift + 1))) {
3019 rshift(b,1);
3020 e = emin;
3021 goto normal;
3022 }
3023 }
3024 break;
3025 case Round_up:
3026 if (!sign && (lostbits || (b->x[0] & 1))) {
3027 incr_denorm:
3028 multadd(b, 1, 2 MTa);
3029 check_denorm = 1;
3030 lostbits = 0;
3031 goto emin_check;
3032 }
3033 break;
3034 case Round_down:
3035 if (sign && (lostbits || (b->x[0] & 1)))
3036 goto incr_denorm;
3037 break;
3038 }
3039 }
3040#endif
2995 if (lostbits) 3041 if (lostbits)
2996 lostbits = 1; 3042 lostbits = 1;
2997 else if (k > 0) 3043 else if (k > 0)
2998 lostbits = any_on(b,k); 3044 lostbits = any_on(b,k);
3045#ifdef IEEE_Arith
3046 else if (check_denorm)
3047 goto no_lostbits;
3048#endif
2999 if (x[k>>kshift] & 1 << (k & kmask)) 3049 if (x[k>>kshift] & 1 << (k & kmask))
3000 lostbits |= 2; 3050 lostbits |= 2;
3051#ifdef IEEE_Arith
3052 no_lostbits:
3053#endif
3001 nbits -= n; 3054 nbits -= n;
3002 rshift(b,n); 3055 rshift(b,n);
3003 e = emin; 3056 e = emin;
@@ -3022,16 +3075,9 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
3022 k = b->wds; 3075 k = b->wds;
3023 b = increment(b MTa); 3076 b = increment(b MTa);
3024 x = b->x; 3077 x = b->x;
3025 if (denorm) { 3078 if (!denorm && (b->wds > k
3026#if 0
3027 if (nbits == Nbits - 1
3028 && x[nbits >> kshift] & 1 << (nbits & kmask))
3029 denorm = 0; /* not currently used */
3030#endif
3031 }
3032 else if (b->wds > k
3033 || ((n = nbits & kmask) !=0 3079 || ((n = nbits & kmask) !=0
3034 && hi0bits(x[k-1]) < 32-n)) { 3080 && hi0bits(x[k-1]) < 32-n))) {
3035 rshift(b,1); 3081 rshift(b,1);
3036 if (++e > Emax) 3082 if (++e > Emax)
3037 goto ovfl; 3083 goto ovfl;
@@ -3041,8 +3087,10 @@ gethex( const char **sp, U *rvp, int rounding, int sign MTd)
3041#ifdef IEEE_Arith 3087#ifdef IEEE_Arith
3042 if (denorm) 3088 if (denorm)
3043 word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0; 3089 word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
3044 else 3090 else {
3091 normal:
3045 word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20); 3092 word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
3093 }
3046 word1(rvp) = b->x[0]; 3094 word1(rvp) = b->x[0];
3047#endif 3095#endif
3048#ifdef IBM 3096#ifdef IBM
@@ -3409,6 +3457,7 @@ retlow1:
3409 if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) { 3457 if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
3410 i = 1 - j; 3458 i = 1 - j;
3411 if (i <= 31) { 3459 if (i <= 31) {
3460 /* cppcheck-suppress integerOverflowCond */
3412 if (word1(rv) & (0x1 << i)) 3461 if (word1(rv) & (0x1 << i))
3413 goto odd; 3462 goto odd;
3414 } 3463 }
@@ -3619,10 +3668,11 @@ fpconv_strtod(const char *s00, char **se)
3619 c = *++s; 3668 c = *++s;
3620 if (c > '0' && c <= '9') { 3669 if (c > '0' && c <= '9') {
3621 L = c - '0'; 3670 L = c - '0';
3622 s1 = s; 3671 while((c = *++s) >= '0' && c <= '9') {
3623 while((c = *++s) >= '0' && c <= '9') 3672 if (L <= 19999)
3624 L = 10*L + c - '0'; 3673 L = 10*L + c - '0';
3625 if (s - s1 > 8 || L > 19999) 3674 }
3675 if (L > 19999)
3626 /* Avoid confusion from exponents 3676 /* Avoid confusion from exponents
3627 * so large that e might overflow. 3677 * so large that e might overflow.
3628 */ 3678 */
@@ -4884,6 +4934,7 @@ nrv_alloc(const char *s, char *s0, size_t s0len, char **rve, int n MTd)
4884 s0 = rv_alloc(n MTa); 4934 s0 = rv_alloc(n MTa);
4885 else if (s0len <= n) { 4935 else if (s0len <= n) {
4886 rv = 0; 4936 rv = 0;
4937 /* cppcheck-suppress nullPointerArithmetic */
4887 t = rv + n; 4938 t = rv + n;
4888 goto rve_chk; 4939 goto rve_chk;
4889 } 4940 }
@@ -5237,9 +5288,11 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
5237#ifndef SET_INEXACT 5288#ifndef SET_INEXACT
5238#ifdef Check_FLT_ROUNDS 5289#ifdef Check_FLT_ROUNDS
5239 try_quick = Rounding == 1; 5290 try_quick = Rounding == 1;
5291#else
5292 try_quick = 1;
5240#endif 5293#endif
5241#endif /*SET_INEXACT*/ 5294#endif /*SET_INEXACT*/
5242#endif 5295#endif /*USE_BF96*/
5243 5296
5244 if (mode > 5) { 5297 if (mode > 5) {
5245 mode -= 4; 5298 mode -= 4;
@@ -5281,6 +5334,7 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
5281 else if (blen <= i) { 5334 else if (blen <= i) {
5282 buf = 0; 5335 buf = 0;
5283 if (rve) 5336 if (rve)
5337 /* cppcheck-suppress nullPointerArithmetic */
5284 *rve = buf + i; 5338 *rve = buf + i;
5285 return buf; 5339 return buf;
5286 } 5340 }
@@ -5469,6 +5523,7 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
5469 res3 = p10->b1 * dbhi + (tv3 & 0xffffffffull); 5523 res3 = p10->b1 * dbhi + (tv3 & 0xffffffffull);
5470 res = p10->b0 * dbhi + (tv3>>32) + (res3>>32); 5524 res = p10->b0 * dbhi + (tv3>>32) + (res3>>32);
5471 be += p10->e - 0x3fe; 5525 be += p10->e - 0x3fe;
5526 /* cppcheck-suppress integerOverflowCond */
5472 eulp = j1 = be - 54 + ulpadj; 5527 eulp = j1 = be - 54 + ulpadj;
5473 if (!(res & 0x8000000000000000ull)) { 5528 if (!(res & 0x8000000000000000ull)) {
5474 --be; 5529 --be;