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 *))
55
50
/* Magic value returned by dtoa() to indicate overflow */
56
51
#define DTOA_OVERFLOW 9999
58
static double my_strtod_int(const char *, char **, int *);
59
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);
83
79
(including the terminating '\0').
84
80
@param error if not NULL, points to a location where the status of
85
81
conversion is stored upon return.
86
false successful conversion
87
true the input number is [-,+]infinity or nan.
82
FALSE successful conversion
83
TRUE the input number is [-,+]infinity or nan.
88
84
The output string in this case is always '0'.
89
85
@return number of written characters (excluding terminating '\0')
92
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)
94
90
int decpt, sign, len, i;
95
91
char *res, *src, *end, *dst= to;
96
assert(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
98
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));
100
97
if (decpt == DTOA_OVERFLOW)
99
dtoa_free(res, buf, sizeof(buf));
105
102
if (error != NULL)
184
181
'width + 1' bytes.
185
182
@param error if not NULL, points to a location where the status of
186
183
conversion is stored upon return.
187
false successful conversion
188
true the input number is [-,+]infinity or nan.
184
FALSE successful conversion
185
TRUE the input number is [-,+]infinity or nan.
189
186
The output string in this case is always '0'.
190
187
@return number of written characters (excluding terminating '\0')
201
198
my_gcvt(55, ..., 1, ...);
203
200
We do our best to minimize such cases by:
205
202
- passing to dtoa() the field width as the number of significant digits
207
204
- removing the sign of the number early (and decreasing the width before
208
205
passing it to dtoa())
210
207
- choosing the proper format to preserve the most number of significant
214
211
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
217
214
int decpt, sign, len, exp_len;
218
215
char *res, *src, *end, *dst= to, *dend= dst + width;
219
bool have_space, force_e_format;
220
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);
222
220
/* We want to remove '-' from equations early */
226
res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? min(width, DBL_DIG) :
227
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));
229
226
if (decpt == DTOA_OVERFLOW)
228
dtoa_free(res, buf, sizeof(buf));
234
231
if (error != NULL)
239
236
if (error != NULL)
301
298
((decpt <= width && (decpt >= -1 || (decpt == -2 &&
302
299
(len > 1 || !force_e_format)))) &&
303
300
!force_e_format)) &&
306
303
Use the 'e' format in some cases even if we have enough space for the
307
304
'f' one. See comment for MAX_DECPT_FOR_F_FORMAT.
320
317
if (width < decpt)
322
319
if (error != NULL)
328
325
We want to truncate (len - width) least significant digits after the
329
326
decimal point. For this we are calling dtoa with mode=5, passing the
330
327
number of significant digits = (len-decpt) - (len-width) = width-decpt
333
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));
389
386
if (error != NULL)
394
391
/* Do we have to truncate any digits? */
397
394
/* Yes, re-convert with a smaller width */
399
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));
455
452
rejected character.
456
453
@param error Upon return is set to EOVERFLOW in case of underflow or
459
456
@return The resulting double value. In case of underflow, 0.0 is
460
457
returned. In case overflow, signed DBL_MAX is returned.
463
460
double my_strtod(const char *str, char **end, int *error)
462
char buf[DTOA_BUFF_SIZE];
466
assert(str != NULL && end != NULL && *end != NULL && error != NULL);
464
DBUG_ASSERT(str != NULL && end != NULL && *end != NULL && error != NULL);
468
res= my_strtod_int(str, end, error);
466
res= my_strtod_int(str, end, error, buf, sizeof(buf));
469
467
return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
535
typedef int32_t Long;
536
typedef uint32_t ULong;
537
typedef int64_t LLong;
538
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;
540
547
typedef union { double d; ULong L[2]; } U;
588
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
590
605
#define rounded_product(a,b) a*= b
591
606
#define rounded_quotient(a,b) a/= b
594
609
#define Big1 0xffffffff
595
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))
597
619
/* Arbitrary-length integer */
599
621
typedef struct Bigint
608
630
int wds; /* current length in 32-bit words */
611
static Bigint *Bcopy(Bigint* dst, Bigint* src)
634
/* A simple stack-memory based allocator for Bigints */
636
typedef struct Stack_alloc
613
dst->sign= src->sign;
616
assert(dst->maxwds >= src->wds);
618
memcpy(dst->p.x, src->p.x, src->wds*sizeof(ULong));
623
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)
627
/* TODO: some malloc failure checking */
630
rv= (Bigint*) malloc(sizeof(Bigint));
632
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);
637
680
rv->sign= rv->wds= 0;
681
rv->p.x= (ULong*) (rv + 1);
645
688
list. Otherwise call free().
648
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)
656
742
/* Bigint arithmetic functions */
658
744
/* Multiply by m and add a */
660
static Bigint *multadd(Bigint *b, int m, int a)
746
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc)
694
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)
701
787
for (k= 0, y= 1; x > y; y <<= 1, k++) ;
711
b= multadd(b, 10, *s++ - '0');
797
b= multadd(b, 10, *s++ - '0', alloc);
712
798
while (++i < nd0);
717
803
for(; i < nd; i++)
718
b= multadd(b, 10, *s++ - '0');
804
b= multadd(b, 10, *s++ - '0', alloc);
820
906
/* Multiply two Bigint numbers */
822
static Bigint *mult(Bigint *a, Bigint *b)
908
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
825
911
int k, wa, wb, wc;
912
998
#define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
914
static Bigint *pow5mult(Bigint *b, int k)
1000
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc)
916
1002
Bigint *b1, *p5, *p51;
918
1004
static int p05[3]= { 5, 25, 125 };
921
b= multadd(b, p05[i-1], 0);
1007
b= multadd(b, p05[i-1], 0, alloc);
1121
static Bigint *d2b(double d, int *e, int *bits)
1207
static Bigint *d2b(double d, int *e, int *bits, Stack_alloc *alloc)
1199
1285
9007199254740992.*9007199254740992.e-256 /* = 2^106 * 1e-53 */
1202
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
1203
1289
flag unnecessarily. It leads to a song and dance at the end of strtod.
1205
1291
#define Scale_Bit 0x10
1209
1295
strtod for IEEE--arithmetic machines.
1211
1297
This strtod returns a nearest machine number to the input decimal
1212
1298
string (or sets errno to EOVERFLOW). Ties are broken by the IEEE round-even
1215
1301
Inspired loosely by William D. Clinger's paper "How to Read Floating
1216
1302
Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
1220
1306
1. We only require IEEE (not IEEE double-extended).
1221
1307
2. We get by with floating-point arithmetic in a case that
1222
1308
Clinger missed -- when we're computing d * 10^n
1234
1320
for 0 <= k <= 22).
1237
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)
1240
1326
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1243
1329
double aadj, aadj1, adj, rv, rv0;
1246
Bigint *bb= NULL, *bb1= NULL, *bd= NULL, *bd0= NULL, *bs= NULL, *delta= NULL;
1332
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1247
1333
#ifdef SET_INEXACT
1248
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));
1254
1347
sign= nz0= nz= 0;
1256
1349
for (s= s00; s < end; s++)
1417
1521
A fancier test would sometimes let us do
1418
1522
this for larger i values.
1524
#ifdef Honor_FLT_ROUNDS
1525
/* round correctly FLT_ROUNDS = 2 or 3 */
1421
1533
dval(rv)*= tens[i];
1422
1534
/* rv = */ rounded_product(dval(rv), tens[e]);
1527
1671
/* Put digits into bd: true value = bd * 10^e */
1529
bd0= s2b(s0, nd0, nd, y);
1673
bd0= s2b(s0, nd0, nd, y, &alloc);
1677
bd= Balloc(bd0->k, &alloc);
1534
1678
Bcopy(bd, bd0);
1535
bb= d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1679
bb= d2b(dval(rv), &bbe, &bbbits, &alloc); /* rv = bb * 2^bbe */
1573
bs= pow5mult(bs, bb5);
1721
bs= pow5mult(bs, bb5, &alloc);
1722
bb1= mult(bs, bb, &alloc);
1579
bb= lshift(bb, bb2);
1727
bb= lshift(bb, bb2, &alloc);
1581
bd= pow5mult(bd, bd5);
1729
bd= pow5mult(bd, bd5, &alloc);
1583
bd= lshift(bd, bd2);
1731
bd= lshift(bd, bd2, &alloc);
1585
bs= lshift(bs, bs2);
1586
delta= diff(bb, bd);
1733
bs= lshift(bs, bs2, &alloc);
1734
delta= diff(bb, bd, &alloc);
1587
1735
dsign= delta->sign;
1588
1736
delta->sign= 0;
1589
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*/
1898
2124
static char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
2125
char **rve, char *buf, size_t buf_size)
1902
2128
Arguments ndigits, decpt, sign are similar to those
1909
2135
0 ==> shortest string that yields d when read in
1910
2136
and rounded to nearest.
1912
2137
1 ==> like 0, but with Steele & White stopping rule;
1913
2138
e.g. with IEEE P754 arithmetic , mode 0 gives
1914
2139
1e23 whereas mode 1 gives 9.999999999999999e22.
1915
2 ==> cmax(1,ndigits) significant digits. This gives a
2140
2 ==> max(1,ndigits) significant digits. This gives a
1916
2141
return value similar to that of ecvt, except
1917
2142
that trailing zeros are suppressed.
1918
2143
3 ==> through ndigits past the decimal point. This
1922
2147
4,5 ==> similar to 2 and 3, respectively, but (in
1923
2148
round-nearest mode) with the tests of mode 0 to
1924
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.
1925
2153
6-9 ==> Debugging modes similar to mode - 4: don't try
1926
2154
fast floating-point estimate (if applicable).
1931
2159
to hold the suppressed trailing zeros.
1934
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,
1935
2163
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1936
2164
spec_case, try_quick;
1940
Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
2168
Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1941
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));
1944
2180
if (word0(d) & Sign_bit)
1955
2191
(!dval(d) && (*decpt= 1)))
1957
2193
/* Infinity, NaN, 0 */
1958
char *res= (char*) malloc(2);
2194
char *res= (char*) dtoa_alloc(2, &alloc);
1967
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);
1968
2214
if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
1970
2216
dval(d2)= dval(d);
1976
2222
log10(x) = log(x) / log(10)
1977
2223
~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1978
2224
log10(d)= (i-Bias)*log(2)/log(10) + log10(d2)
1980
2226
This suggests computing an approximation k to log10(d) by
1982
2228
k= (i - Bias)*0.301029995663981
1983
2229
+ ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1985
2231
We want k to be too large rather than too small.
1986
2232
The error in the first-order Taylor series approximation
1987
2233
is in our favor, so we just round up the constant enough
2082
s= s0= (char*)malloc(i+1); /* +1 for trailing '\0' appended
2083
at end of function */
2332
s= s0= dtoa_alloc(i, &alloc);
2334
#ifdef Honor_FLT_ROUNDS
2335
if (mode > 1 && rounding != 1)
2085
2339
if (ilim >= 0 && ilim <= Quick_max && try_quick)
2263
mhi= pow5mult(mhi, m5);
2534
mhi= pow5mult(mhi, m5, &alloc);
2535
b1= mult(mhi, b, &alloc);
2268
2539
if ((j= b5 - m5))
2540
b= pow5mult(b, j, &alloc);
2543
b= pow5mult(b, b5, &alloc);
2547
S= pow5mult(S, s5, &alloc);
2278
2549
/* Check for special case that d is a normalized power of 2. */
2281
2552
if ((mode < 2 || leftright)
2553
#ifdef Honor_FLT_ROUNDS
2284
2558
if (!word1(d) && !(word0(d) & Bndry_mask) &&
2296
2570
Arrange for convenient computation of quotients:
2297
2571
shift left if necessary so divisor has 4 leading 0 bits.
2299
2573
Perhaps we should just compute leading 28 bits of S once
2300
2574
a nd for all and pass them and a shift to quorem, so it
2301
2575
can do shifts and ors to compute the numerator for q.
2594
b= lshift(b, b2, &alloc);
2596
S= lshift(S, s2, &alloc);
2325
2599
if (cmp(b,S) < 0)
2328
2602
/* we botched the k estimate */
2329
b= multadd(b, 10, 0);
2603
b= multadd(b, 10, 0, &alloc);
2331
mhi= multadd(mhi, 10, 0);
2605
mhi= multadd(mhi, 10, 0, &alloc);
2335
2609
if (ilim <= 0 && (mode == 3 || mode == 5))
2337
if (ilim < 0 || cmp(b,S= multadd(S,5,0)) <= 0)
2611
if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2339
2613
/* no digits, fcvt style */
2368
2642
dig= quorem(b,S) + '0';
2369
2643
/* Do we yet have the shortest decimal string that will round to d? */
2370
2644
j= cmp(b, mlo);
2371
delta= diff(S, mhi);
2645
delta= diff(S, mhi, &alloc);
2372
2646
j1= delta->sign ? 1 : cmp(b, delta);
2647
Bfree(delta, &alloc);
2374
2648
if (j1 == 0 && mode != 1 && !(word1(d) & 1)
2649
#ifdef Honor_FLT_ROUNDS
2377
2654
if (dig == '9')
2701
#ifdef Honor_FLT_ROUNDS
2416
b= multadd(b, 10, 0);
2707
b= multadd(b, 10, 0, &alloc);
2417
2708
if (mlo == mhi)
2418
mlo= mhi= multadd(mhi, 10, 0);
2709
mlo= mhi= multadd(mhi, 10, 0, &alloc);
2421
mlo= multadd(mlo, 10, 0);
2422
mhi= multadd(mhi, 10, 0);
2712
mlo= multadd(mlo, 10, 0, &alloc);
2713
mhi= multadd(mhi, 10, 0, &alloc);