36
36
***************************************************************/
40
#include "drizzled/internal/m_string.h" /* for memcpy and NOT_FIXED_DEC */
38
#include <my_base.h> /* for EOVERFLOW on Windows */
39
#include <my_global.h>
40
#include <m_string.h> /* for memcpy and NOT_FIXED_DEC */
43
Appears to suffice to not call malloc() in most cases.
45
see if it is possible to get rid of malloc().
46
this constant is sufficient to avoid malloc() on all inputs I have tried.
48
#define DTOA_BUFF_SIZE (420 * sizeof(void *))
50
50
/* Magic value returned by dtoa() to indicate overflow */
51
51
#define DTOA_OVERFLOW 9999
53
static double my_strtod_int(const char *, char **, int *);
54
static char *dtoa(double, int, int, int *, int *, char **);
53
static double my_strtod_int(const char *, char **, int *, char *, size_t);
54
static char *dtoa(double, int, int, int *, int *, char **, char *, size_t);
55
static void dtoa_free(char *, char *, size_t);
78
79
(including the terminating '\0').
79
80
@param error if not NULL, points to a location where the status of
80
81
conversion is stored upon return.
81
false successful conversion
82
true the input number is [-,+]infinity or nan.
82
FALSE successful conversion
83
TRUE the input number is [-,+]infinity or nan.
83
84
The output string in this case is always '0'.
84
85
@return number of written characters (excluding terminating '\0')
87
size_t my_fcvt(double x, int precision, char *to, bool *error)
88
size_t my_fcvt(double x, int precision, char *to, my_bool *error)
89
90
int decpt, sign, len, i;
90
91
char *res, *src, *end, *dst= to;
91
assert(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
93
res= dtoa(x, 5, precision, &decpt, &sign, &end);
92
char buf[DTOA_BUFF_SIZE];
93
DBUG_ASSERT(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
95
res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
95
97
if (decpt == DTOA_OVERFLOW)
99
dtoa_free(res, buf, sizeof(buf));
100
102
if (error != NULL)
179
181
'width + 1' bytes.
180
182
@param error if not NULL, points to a location where the status of
181
183
conversion is stored upon return.
182
false successful conversion
183
true the input number is [-,+]infinity or nan.
184
FALSE successful conversion
185
TRUE the input number is [-,+]infinity or nan.
184
186
The output string in this case is always '0'.
185
187
@return number of written characters (excluding terminating '\0')
196
198
my_gcvt(55, ..., 1, ...);
198
200
We do our best to minimize such cases by:
200
202
- passing to dtoa() the field width as the number of significant digits
202
204
- removing the sign of the number early (and decreasing the width before
203
205
passing it to dtoa())
205
207
- choosing the proper format to preserve the most number of significant
209
211
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
212
214
int decpt, sign, len, exp_len;
213
215
char *res, *src, *end, *dst= to, *dend= dst + width;
214
bool have_space, force_e_format;
215
assert(width > 0 && to != NULL);
216
char buf[DTOA_BUFF_SIZE];
217
my_bool have_space, force_e_format;
218
DBUG_ASSERT(width > 0 && to != NULL);
217
220
/* We want to remove '-' from equations early */
221
res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? min(width, DBL_DIG) :
222
min(width, FLT_DIG), &decpt, &sign, &end);
224
res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : min(width, FLT_DIG),
225
&decpt, &sign, &end, buf, sizeof(buf));
224
226
if (decpt == DTOA_OVERFLOW)
228
dtoa_free(res, buf, sizeof(buf));
229
231
if (error != NULL)
234
236
if (error != NULL)
296
298
((decpt <= width && (decpt >= -1 || (decpt == -2 &&
297
299
(len > 1 || !force_e_format)))) &&
298
300
!force_e_format)) &&
301
303
Use the 'e' format in some cases even if we have enough space for the
302
304
'f' one. See comment for MAX_DECPT_FOR_F_FORMAT.
315
317
if (width < decpt)
317
319
if (error != NULL)
323
325
We want to truncate (len - width) least significant digits after the
324
326
decimal point. For this we are calling dtoa with mode=5, passing the
325
327
number of significant digits = (len-decpt) - (len-width) = width-decpt
328
res= dtoa(x, 5, width - decpt, &decpt, &sign, &end);
329
dtoa_free(res, buf, sizeof(buf));
330
res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
384
386
if (error != NULL)
389
391
/* Do we have to truncate any digits? */
392
394
/* Yes, re-convert with a smaller width */
394
res= dtoa(x, 4, width, &decpt, &sign, &end);
395
dtoa_free(res, buf, sizeof(buf));
396
res= dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
450
452
rejected character.
451
453
@param error Upon return is set to EOVERFLOW in case of underflow or
454
456
@return The resulting double value. In case of underflow, 0.0 is
455
457
returned. In case overflow, signed DBL_MAX is returned.
458
460
double my_strtod(const char *str, char **end, int *error)
462
char buf[DTOA_BUFF_SIZE];
461
assert(str != NULL && end != NULL && *end != NULL && error != NULL);
464
DBUG_ASSERT(str != NULL && end != NULL && *end != NULL && error != NULL);
463
res= my_strtod_int(str, end, error);
466
res= my_strtod_int(str, end, error, buf, sizeof(buf));
464
467
return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
530
typedef int32_t Long;
531
typedef uint32_t ULong;
532
typedef int64_t LLong;
533
typedef uint64_t ULLong;
534
#define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
535
and dtoa should round accordingly.
536
#define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
537
and Honor_FLT_ROUNDS is not #defined.
539
TODO: check if we can get rid of the above two
543
typedef uint32 ULong;
545
typedef uint64 ULLong;
535
547
typedef union { double d; ULong L[2]; } U;
583
595
#endif /*Flt_Rounds*/
597
#ifdef Honor_FLT_ROUNDS
598
#define Rounding rounding
599
#undef Check_FLT_ROUNDS
600
#define Check_FLT_ROUNDS
602
#define Rounding Flt_Rounds
585
605
#define rounded_product(a,b) a*= b
586
606
#define rounded_quotient(a,b) a/= b
589
609
#define Big1 0xffffffff
590
610
#define FFFFFFFF 0xffffffffUL
612
/* This is tested to be enough for dtoa */
616
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
617
2*sizeof(int) + y->wds*sizeof(ULong))
592
619
/* Arbitrary-length integer */
594
621
typedef struct Bigint
603
630
int wds; /* current length in 32-bit words */
606
static Bigint *Bcopy(Bigint* dst, Bigint* src)
634
/* A simple stack-memory based allocator for Bigints */
636
typedef struct Stack_alloc
608
dst->sign= src->sign;
611
assert(dst->maxwds >= src->wds);
613
memcpy(dst->p.x, src->p.x, src->wds*sizeof(ULong));
618
static Bigint *Balloc(int k)
642
Having list of free blocks lets us reduce maximum required amount
643
of memory from ~4000 bytes to < 1680 (tested on x86).
645
Bigint *freelist[Kmax+1];
650
Try to allocate object on stack, and resort to malloc if all
651
stack memory is used.
654
static Bigint *Balloc(int k, Stack_alloc *alloc)
622
/* TODO: some malloc failure checking */
625
rv= (Bigint*) malloc(sizeof(Bigint));
627
rv->p.x= (ULong*)malloc(x * sizeof(ULong));
657
if (k <= Kmax && alloc->freelist[k])
659
rv= alloc->freelist[k];
660
alloc->freelist[k]= rv->p.next;
667
len= sizeof(Bigint) + x * sizeof(ULong);
669
if (alloc->free + len <= alloc->end)
671
rv= (Bigint*) alloc->free;
675
rv= (Bigint*) malloc(len);
632
680
rv->sign= rv->wds= 0;
681
rv->p.x= (ULong*) (rv + 1);
640
688
list. Otherwise call free().
643
static void Bfree(Bigint *v)
691
static void Bfree(Bigint *v, Stack_alloc *alloc)
693
char *gptr= (char*) v; /* generic pointer */
694
if (gptr < alloc->begin || gptr >= alloc->end)
696
else if (v->k <= Kmax)
699
Maintain free lists only for stack objects: this way we don't
700
have to bother with freeing lists in the end of dtoa;
701
heap should not be used normally anyway.
703
v->p.next= alloc->freelist[v->k];
704
alloc->freelist[v->k]= v;
710
This is to place return value of dtoa in: tries to use stack
711
as well, but passes by free lists management and just aligns len by
715
static char *dtoa_alloc(int i, Stack_alloc *alloc)
718
int aligned_size= (i + sizeof(ULong) - 1) / sizeof(ULong) * sizeof(ULong);
719
if (alloc->free + aligned_size <= alloc->end)
722
alloc->free+= aligned_size;
731
dtoa_free() must be used to free values s returned by dtoa()
732
This is the counterpart of dtoa_alloc()
735
static void dtoa_free(char *gptr, char *buf, size_t buf_size)
737
if (gptr < buf || gptr >= buf + buf_size)
651
742
/* Bigint arithmetic functions */
653
744
/* Multiply by m and add a */
655
static Bigint *multadd(Bigint *b, int m, int a)
746
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc)
689
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9)
780
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9, Stack_alloc *alloc)
696
787
for (k= 0, y= 1; x > y; y <<= 1, k++) ;
706
b= multadd(b, 10, *s++ - '0');
797
b= multadd(b, 10, *s++ - '0', alloc);
707
798
while (++i < nd0);
712
803
for(; i < nd; i++)
713
b= multadd(b, 10, *s++ - '0');
804
b= multadd(b, 10, *s++ - '0', alloc);
815
906
/* Multiply two Bigint numbers */
817
static Bigint *mult(Bigint *a, Bigint *b)
908
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
820
911
int k, wa, wb, wc;
907
998
#define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
909
static Bigint *pow5mult(Bigint *b, int k)
1000
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc)
911
1002
Bigint *b1, *p5, *p51;
913
1004
static int p05[3]= { 5, 25, 125 };
916
b= multadd(b, p05[i-1], 0);
1007
b= multadd(b, p05[i-1], 0, alloc);
1116
static Bigint *d2b(double d, int *e, int *bits)
1207
static Bigint *d2b(double d, int *e, int *bits, Stack_alloc *alloc)
1194
1285
9007199254740992.*9007199254740992.e-256 /* = 2^106 * 1e-53 */
1197
The factor of 2^53 in tinytens[4] helps us avoid setting the underflow
1288
The factor of 2^53 in tinytens[4] helps us avoid setting the underflow
1198
1289
flag unnecessarily. It leads to a song and dance at the end of strtod.
1200
1291
#define Scale_Bit 0x10
1204
1295
strtod for IEEE--arithmetic machines.
1206
1297
This strtod returns a nearest machine number to the input decimal
1207
1298
string (or sets errno to EOVERFLOW). Ties are broken by the IEEE round-even
1210
1301
Inspired loosely by William D. Clinger's paper "How to Read Floating
1211
1302
Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
1215
1306
1. We only require IEEE (not IEEE double-extended).
1216
1307
2. We get by with floating-point arithmetic in a case that
1217
1308
Clinger missed -- when we're computing d * 10^n
1229
1320
for 0 <= k <= 22).
1232
static double my_strtod_int(const char *s00, char **se, int *error)
1323
static double my_strtod_int(const char *s00, char **se, int *error, char *buf, size_t buf_size)
1235
1326
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1238
1329
double aadj, aadj1, adj, rv, rv0;
1241
Bigint *bb= NULL, *bb1= NULL, *bd= NULL, *bd0= NULL, *bs= NULL, *delta= NULL;
1332
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1242
1333
#ifdef SET_INEXACT
1243
1334
int inexact, oldinexact;
1336
#ifdef Honor_FLT_ROUNDS
1343
alloc.begin= alloc.free= buf;
1344
alloc.end= buf + buf_size;
1345
memset(alloc.freelist, 0, sizeof(alloc.freelist));
1249
1347
sign= nz0= nz= 0;
1251
1349
for (s= s00; s < end; s++)
1412
1521
A fancier test would sometimes let us do
1413
1522
this for larger i values.
1524
#ifdef Honor_FLT_ROUNDS
1525
/* round correctly FLT_ROUNDS = 2 or 3 */
1416
1533
dval(rv)*= tens[i];
1417
1534
/* rv = */ rounded_product(dval(rv), tens[e]);
1522
1671
/* Put digits into bd: true value = bd * 10^e */
1524
bd0= s2b(s0, nd0, nd, y);
1673
bd0= s2b(s0, nd0, nd, y, &alloc);
1677
bd= Balloc(bd0->k, &alloc);
1529
1678
Bcopy(bd, bd0);
1530
bb= d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1679
bb= d2b(dval(rv), &bbe, &bbbits, &alloc); /* rv = bb * 2^bbe */
1568
bs= pow5mult(bs, bb5);
1721
bs= pow5mult(bs, bb5, &alloc);
1722
bb1= mult(bs, bb, &alloc);
1574
bb= lshift(bb, bb2);
1727
bb= lshift(bb, bb2, &alloc);
1576
bd= pow5mult(bd, bd5);
1729
bd= pow5mult(bd, bd5, &alloc);
1578
bd= lshift(bd, bd2);
1731
bd= lshift(bd, bd2, &alloc);
1580
bs= lshift(bs, bs2);
1581
delta= diff(bb, bd);
1733
bs= lshift(bs, bs2, &alloc);
1734
delta= diff(bb, bd, &alloc);
1582
1735
dsign= delta->sign;
1583
1736
delta->sign= 0;
1584
1737
i= cmp(delta, bs);
1738
#ifdef Honor_FLT_ROUNDS
1743
/* Error is less than an ulp */
1744
if (!delta->x[0] && delta->wds <= 1)
1763
if (!word1(rv) && !(word0(rv) & Frac_mask))
1765
y= word0(rv) & Exp_mask;
1766
if (!scale || y > 2*P*Exp_msk1)
1768
delta= lshift(delta,Log2P);
1769
if (cmp(delta, bs) <= 0)
1774
if (scale && (y= word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)
1775
word0(adj)+= (2 * P + 1) * Exp_msk1 - y;
1776
dval(rv)+= adj * ulp(dval(rv));
1780
adj= ratio(delta, bs);
1783
if (adj <= 0x7ffffffe)
1785
/* adj = rounding ? ceil(adj) : floor(adj); */
1789
if (!((rounding >> 1) ^ dsign))
1794
if (scale && (y= word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)
1795
word0(adj)+= (2 * P + 1) * Exp_msk1 - y;
1796
adj*= ulp(dval(rv));
1803
#endif /*Honor_FLT_ROUNDS*/
1893
2124
static char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
2125
char **rve, char *buf, size_t buf_size)
1897
2128
Arguments ndigits, decpt, sign are similar to those
1904
2135
0 ==> shortest string that yields d when read in
1905
2136
and rounded to nearest.
1907
2137
1 ==> like 0, but with Steele & White stopping rule;
1908
2138
e.g. with IEEE P754 arithmetic , mode 0 gives
1909
2139
1e23 whereas mode 1 gives 9.999999999999999e22.
1910
2 ==> cmax(1,ndigits) significant digits. This gives a
2140
2 ==> max(1,ndigits) significant digits. This gives a
1911
2141
return value similar to that of ecvt, except
1912
2142
that trailing zeros are suppressed.
1913
2143
3 ==> through ndigits past the decimal point. This
1917
2147
4,5 ==> similar to 2 and 3, respectively, but (in
1918
2148
round-nearest mode) with the tests of mode 0 to
1919
2149
possibly return a shorter string that rounds to d.
2150
With IEEE arithmetic and compilation with
2151
-DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2152
as modes 2 and 3 when FLT_ROUNDS != 1.
1920
2153
6-9 ==> Debugging modes similar to mode - 4: don't try
1921
2154
fast floating-point estimate (if applicable).
1926
2159
to hold the suppressed trailing zeros.
1929
int bbits, b2, b5, be, dig, i, ieps, ilim=0, ilim0, ilim1= 0,
2162
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1= 0,
1930
2163
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1931
2164
spec_case, try_quick;
1935
Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
2168
Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1936
2169
double d2, ds, eps;
2171
#ifdef Honor_FLT_ROUNDS
2176
alloc.begin= alloc.free= buf;
2177
alloc.end= buf + buf_size;
2178
memset(alloc.freelist, 0, sizeof(alloc.freelist));
1939
2180
if (word0(d) & Sign_bit)
1950
2191
(!dval(d) && (*decpt= 1)))
1952
2193
/* Infinity, NaN, 0 */
1953
char *res= (char*) malloc(2);
2194
char *res= (char*) dtoa_alloc(2, &alloc);
1962
b= d2b(dval(d), &be, &bbits);
2202
#ifdef Honor_FLT_ROUNDS
2203
if ((rounding= Flt_Rounds) >= 2)
2206
rounding= rounding == 2 ? 0 : 2;
2213
b= d2b(dval(d), &be, &bbits, &alloc);
1963
2214
if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
1965
2216
dval(d2)= dval(d);
1971
2222
log10(x) = log(x) / log(10)
1972
2223
~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1973
2224
log10(d)= (i-Bias)*log(2)/log(10) + log10(d2)
1975
2226
This suggests computing an approximation k to log10(d) by
1977
2228
k= (i - Bias)*0.301029995663981
1978
2229
+ ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1980
2231
We want k to be too large rather than too small.
1981
2232
The error in the first-order Taylor series approximation
1982
2233
is in our favor, so we just round up the constant enough
2077
s= s0= (char*)malloc(i+1); /* +1 for trailing '\0' appended
2078
at end of function */
2332
s= s0= dtoa_alloc(i, &alloc);
2334
#ifdef Honor_FLT_ROUNDS
2335
if (mode > 1 && rounding != 1)
2080
2339
if (ilim >= 0 && ilim <= Quick_max && try_quick)
2258
mhi= pow5mult(mhi, m5);
2534
mhi= pow5mult(mhi, m5, &alloc);
2535
b1= mult(mhi, b, &alloc);
2263
2539
if ((j= b5 - m5))
2540
b= pow5mult(b, j, &alloc);
2543
b= pow5mult(b, b5, &alloc);
2547
S= pow5mult(S, s5, &alloc);
2273
2549
/* Check for special case that d is a normalized power of 2. */
2276
2552
if ((mode < 2 || leftright)
2553
#ifdef Honor_FLT_ROUNDS
2279
2558
if (!word1(d) && !(word0(d) & Bndry_mask) &&
2291
2570
Arrange for convenient computation of quotients:
2292
2571
shift left if necessary so divisor has 4 leading 0 bits.
2294
2573
Perhaps we should just compute leading 28 bits of S once
2295
2574
a nd for all and pass them and a shift to quorem, so it
2296
2575
can do shifts and ors to compute the numerator for q.
2594
b= lshift(b, b2, &alloc);
2596
S= lshift(S, s2, &alloc);
2320
2599
if (cmp(b,S) < 0)
2323
2602
/* we botched the k estimate */
2324
b= multadd(b, 10, 0);
2603
b= multadd(b, 10, 0, &alloc);
2326
mhi= multadd(mhi, 10, 0);
2605
mhi= multadd(mhi, 10, 0, &alloc);
2330
2609
if (ilim <= 0 && (mode == 3 || mode == 5))
2332
if (ilim < 0 || cmp(b,S= multadd(S,5,0)) <= 0)
2611
if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2334
2613
/* no digits, fcvt style */
2363
2642
dig= quorem(b,S) + '0';
2364
2643
/* Do we yet have the shortest decimal string that will round to d? */
2365
2644
j= cmp(b, mlo);
2366
delta= diff(S, mhi);
2645
delta= diff(S, mhi, &alloc);
2367
2646
j1= delta->sign ? 1 : cmp(b, delta);
2647
Bfree(delta, &alloc);
2369
2648
if (j1 == 0 && mode != 1 && !(word1(d) & 1)
2649
#ifdef Honor_FLT_ROUNDS
2372
2654
if (dig == '9')
2701
#ifdef Honor_FLT_ROUNDS
2411
b= multadd(b, 10, 0);
2707
b= multadd(b, 10, 0, &alloc);
2412
2708
if (mlo == mhi)
2413
mlo= mhi= multadd(mhi, 10, 0);
2709
mlo= mhi= multadd(mhi, 10, 0, &alloc);
2416
mlo= multadd(mlo, 10, 0);
2417
mhi= multadd(mhi, 10, 0);
2712
mlo= multadd(mlo, 10, 0, &alloc);
2713
mhi= multadd(mhi, 10, 0, &alloc);