~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/dtoa.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 20:29:54 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425202954-slopmkjj7vxal5lk
Migrated archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */
15
15
 
16
16
/****************************************************************
17
17
 
35
35
 
36
36
 ***************************************************************/
37
37
 
38
 
#include "config.h"
39
 
 
40
 
#include "drizzled/internal/m_string.h"  /* for memcpy and NOT_FIXED_DEC */
41
 
 
42
 
#include <float.h>
43
 
 
44
 
#include <cstdlib>
45
 
#include <cerrno>
46
 
#include <algorithm>
47
 
 
48
 
using namespace std;
49
 
 
50
 
namespace drizzled
51
 
{
52
 
namespace internal
53
 
{
 
38
#include <mystrings/m_string.h>  /* for memcpy and NOT_FIXED_DEC */
 
39
#include <stdlib.h>
 
40
 
 
41
/**
 
42
   Appears to suffice to not call malloc() in most cases.
 
43
   @todo
 
44
     see if it is possible to get rid of malloc().
 
45
     this constant is sufficient to avoid malloc() on all inputs I have tried.
 
46
*/
 
47
#define DTOA_BUFF_SIZE (420 * sizeof(void *))
54
48
 
55
49
/* Magic value returned by dtoa() to indicate overflow */
56
50
#define DTOA_OVERFLOW 9999
57
51
 
58
 
static double my_strtod_int(const char *, char **, int *);
59
 
static char *dtoa(double, int, int, int *, int *, char **);
 
52
static double my_strtod_int(const char *, char **, int *, char *, size_t);
 
53
static char *dtoa(double, int, int, int *, int *, char **, char *, size_t);
 
54
static void dtoa_free(char *, char *, size_t);
60
55
 
61
56
/**
62
57
   @brief
93
88
{
94
89
  int decpt, sign, len, i;
95
90
  char *res, *src, *end, *dst= to;
 
91
  double alignedbuf[(DTOA_BUFF_SIZE+1)/sizeof(double)];
 
92
  char *buf= (char*)alignedbuf;
96
93
  assert(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
97
94
 
98
 
  res= dtoa(x, 5, precision, &decpt, &sign, &end);
 
95
  res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
99
96
 
100
97
  if (decpt == DTOA_OVERFLOW)
101
98
  {
102
 
    free(res);
 
99
    dtoa_free(res, buf, sizeof(buf));
103
100
    *to++= '0';
104
101
    *to= '\0';
105
102
    if (error != NULL)
135
132
    if (len <= decpt)
136
133
      *dst++= '.';
137
134
 
138
 
    for (i= precision - max(0, (len - decpt)); i > 0; i--)
 
135
    for (i= precision - cmax(0, (len - decpt)); i > 0; i--)
139
136
      *dst++= '0';
140
137
  }
141
138
 
143
140
  if (error != NULL)
144
141
    *error= false;
145
142
 
146
 
  free(res);
 
143
  dtoa_free(res, buf, sizeof(buf));
147
144
 
148
145
  return dst - to;
149
146
}
216
213
{
217
214
  int decpt, sign, len, exp_len;
218
215
  char *res, *src, *end, *dst= to, *dend= dst + width;
 
216
  double alignedbuf[(DTOA_BUFF_SIZE+1)/sizeof(double)];
 
217
  char *buf= (char*)alignedbuf;
219
218
  bool have_space, force_e_format;
220
219
  assert(width > 0 && to != NULL);
221
220
 
223
222
  if (x < 0.)
224
223
    width--;
225
224
 
226
 
  res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? min(width, DBL_DIG) : 
227
 
            min(width, FLT_DIG), &decpt, &sign, &end);
228
 
 
 
225
  res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : cmin(width, FLT_DIG),
 
226
            &decpt, &sign, &end, buf, sizeof(buf));
229
227
  if (decpt == DTOA_OVERFLOW)
230
228
  {
231
 
    free(res);
 
229
    dtoa_free(res, buf, sizeof(buf));
232
230
    *to++= '0';
233
231
    *to= '\0';
234
232
    if (error != NULL)
329
327
        decimal point. For this we are calling dtoa with mode=5, passing the
330
328
        number of significant digits = (len-decpt) - (len-width) = width-decpt
331
329
      */
332
 
      free(res);
333
 
      res= dtoa(x, 5, width - decpt, &decpt, &sign, &end);
 
330
      dtoa_free(res, buf, sizeof(buf));
 
331
      res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
334
332
      src= res;
335
333
      len= end - res;
336
334
    }
395
393
    if (width < len)
396
394
    {
397
395
      /* Yes, re-convert with a smaller width */
398
 
      free(res);
399
 
      res= dtoa(x, 4, width, &decpt, &sign, &end);
 
396
      dtoa_free(res, buf, sizeof(buf));
 
397
      res= dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
400
398
      src= res;
401
399
      len= end - res;
402
400
      if (--decpt < 0)
436
434
  }
437
435
 
438
436
end:
439
 
  free(res);
 
437
  dtoa_free(res, buf, sizeof(buf));
440
438
  *dst= '\0';
441
439
 
442
440
  return dst - to;
462
460
 
463
461
double my_strtod(const char *str, char **end, int *error)
464
462
{
 
463
  double alignedbuf[(DTOA_BUFF_SIZE+1)/sizeof(double)];
 
464
  char *buf= (char*)alignedbuf;
465
465
  double res;
466
466
  assert(str != NULL && end != NULL && *end != NULL && error != NULL);
467
467
 
468
 
  res= my_strtod_int(str, end, error);
 
468
  res= my_strtod_int(str, end, error, buf, sizeof(buf));
469
469
  return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
470
470
}
471
471
 
594
594
#define Big1 0xffffffff
595
595
#define FFFFFFFF 0xffffffffUL
596
596
 
 
597
/* This is tested to be enough for dtoa */
 
598
 
 
599
#define Kmax 15
 
600
 
 
601
#define Bcopy(x,y) memcpy(&x->sign, &y->sign, \
 
602
                          2*sizeof(int) + y->wds*sizeof(ULong))
 
603
 
597
604
/* Arbitrary-length integer */
598
605
 
599
606
typedef struct Bigint
608
615
  int wds;                 /* current length in 32-bit words */
609
616
} Bigint;
610
617
 
611
 
static Bigint *Bcopy(Bigint* dst, Bigint* src)
 
618
 
 
619
/* A simple stack-memory based allocator for Bigints */
 
620
 
 
621
typedef struct Stack_alloc
612
622
{
613
 
  dst->sign= src->sign;
614
 
  dst->wds= src->wds;
615
 
 
616
 
  assert(dst->maxwds >= src->wds);
617
 
 
618
 
  memcpy(dst->p.x, src->p.x, src->wds*sizeof(ULong));
619
 
 
620
 
  return dst;
621
 
}
622
 
 
623
 
static Bigint *Balloc(int k)
 
623
  char *begin;
 
624
  char *free;
 
625
  char *end;
 
626
  /*
 
627
    Having list of free blocks lets us reduce maximum required amount
 
628
    of memory from ~4000 bytes to < 1680 (tested on x86).
 
629
  */
 
630
  Bigint *freelist[Kmax+1];
 
631
} Stack_alloc;
 
632
 
 
633
 
 
634
/*
 
635
  Try to allocate object on stack, and resort to malloc if all
 
636
  stack memory is used.
 
637
*/
 
638
 
 
639
static Bigint *Balloc(int k, Stack_alloc *alloc)
624
640
{
625
641
  Bigint *rv;
626
 
 
627
 
  /* TODO: some malloc failure checking */
628
 
 
629
 
  int x= 1 << k;
630
 
  rv= (Bigint*) malloc(sizeof(Bigint));
631
 
 
632
 
  rv->p.x= (ULong*)malloc(x * sizeof(ULong));
633
 
 
634
 
  rv->k= k;
635
 
  rv->maxwds= x;
636
 
 
 
642
  if (k <= Kmax &&  alloc->freelist[k])
 
643
  {
 
644
    rv= alloc->freelist[k];
 
645
    alloc->freelist[k]= rv->p.next;
 
646
  }
 
647
  else
 
648
  {
 
649
    int x, len;
 
650
 
 
651
    x= 1 << k;
 
652
    len= ALIGN_SIZE(sizeof(Bigint) + x * sizeof(ULong));
 
653
 
 
654
    if (alloc->free + len <= alloc->end)
 
655
    {
 
656
      rv= (Bigint*) alloc->free;
 
657
      alloc->free+= len;
 
658
    }
 
659
    else
 
660
      rv= (Bigint*) malloc(len);
 
661
 
 
662
    rv->k= k;
 
663
    rv->maxwds= x;
 
664
  }
637
665
  rv->sign= rv->wds= 0;
638
 
 
 
666
  rv->p.x= (ULong*) (rv + 1);
639
667
  return rv;
640
668
}
641
669
 
645
673
  list. Otherwise call free().
646
674
*/
647
675
 
648
 
static void Bfree(Bigint *v)
649
 
{
650
 
  if(!v)
651
 
    return;
652
 
  free(v->p.x);
653
 
  free(v);
654
 
}
 
676
static void Bfree(Bigint *v, Stack_alloc *alloc)
 
677
{
 
678
  char *gptr= (char*) v;                       /* generic pointer */
 
679
  if (gptr < alloc->begin || gptr >= alloc->end)
 
680
    free(gptr);
 
681
  else if (v->k <= Kmax)
 
682
  {
 
683
    /*
 
684
      Maintain free lists only for stack objects: this way we don't
 
685
      have to bother with freeing lists in the end of dtoa;
 
686
      heap should not be used normally anyway.
 
687
    */
 
688
    v->p.next= alloc->freelist[v->k];
 
689
    alloc->freelist[v->k]= v;
 
690
  }
 
691
}
 
692
 
 
693
 
 
694
/*
 
695
  This is to place return value of dtoa in: tries to use stack
 
696
  as well, but passes by free lists management and just aligns len by
 
697
  sizeof(ULong).
 
698
*/
 
699
 
 
700
static char *dtoa_alloc(int i, Stack_alloc *alloc)
 
701
{
 
702
  char *rv;
 
703
  int aligned_size= (i + sizeof(double) - 1) / sizeof(double) * sizeof(double);
 
704
  if (alloc->free + aligned_size <= alloc->end)
 
705
  {
 
706
    rv= alloc->free;
 
707
    alloc->free+= aligned_size;
 
708
  }
 
709
  else
 
710
    rv= (char *)malloc(i);
 
711
  return rv;
 
712
}
 
713
 
 
714
 
 
715
/*
 
716
  dtoa_free() must be used to free values s returned by dtoa()
 
717
  This is the counterpart of dtoa_alloc()
 
718
*/
 
719
 
 
720
static void dtoa_free(char *gptr, char *buf, size_t buf_size)
 
721
{
 
722
  if (gptr < buf || gptr >= buf + buf_size)
 
723
    free(gptr);
 
724
}
 
725
 
655
726
 
656
727
/* Bigint arithmetic functions */
657
728
 
658
729
/* Multiply by m and add a */
659
730
 
660
 
static Bigint *multadd(Bigint *b, int m, int a)
 
731
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc)
661
732
{
662
733
  int i, wds;
663
734
  ULong *x;
679
750
  {
680
751
    if (wds >= b->maxwds)
681
752
    {
682
 
      b1= Balloc(b->k+1);
 
753
      b1= Balloc(b->k+1, alloc);
683
754
      Bcopy(b1, b);
684
 
      Bfree(b);
 
755
      Bfree(b, alloc);
685
756
      b= b1;
686
757
    }
687
758
    b->p.x[wds++]= (ULong) carry;
691
762
}
692
763
 
693
764
 
694
 
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9)
 
765
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9, Stack_alloc *alloc)
695
766
{
696
767
  Bigint *b;
697
768
  int i, k;
699
770
 
700
771
  x= (nd + 8) / 9;
701
772
  for (k= 0, y= 1; x > y; y <<= 1, k++) ;
702
 
  b= Balloc(k);
 
773
  b= Balloc(k, alloc);
703
774
  b->p.x[0]= y9;
704
775
  b->wds= 1;
705
776
 
708
779
  {
709
780
    s+= 9;
710
781
    do
711
 
      b= multadd(b, 10, *s++ - '0');
 
782
      b= multadd(b, 10, *s++ - '0', alloc);
712
783
    while (++i < nd0);
713
784
    s++;
714
785
  }
715
786
  else
716
787
    s+= 10;
717
788
  for(; i < nd; i++)
718
 
    b= multadd(b, 10, *s++ - '0');
 
789
    b= multadd(b, 10, *s++ - '0', alloc);
719
790
  return b;
720
791
}
721
792
 
806
877
 
807
878
/* Convert integer to Bigint number */
808
879
 
809
 
static Bigint *i2b(int i)
 
880
static Bigint *i2b(int i, Stack_alloc *alloc)
810
881
{
811
882
  Bigint *b;
812
883
 
813
 
  b= Balloc(1);
 
884
  b= Balloc(1, alloc);
814
885
  b->p.x[0]= i;
815
886
  b->wds= 1;
816
887
  return b;
819
890
 
820
891
/* Multiply two Bigint numbers */
821
892
 
822
 
static Bigint *mult(Bigint *a, Bigint *b)
 
893
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
823
894
{
824
895
  Bigint *c;
825
896
  int k, wa, wb, wc;
839
910
  wc= wa + wb;
840
911
  if (wc > a->maxwds)
841
912
    k++;
842
 
  c= Balloc(k);
 
913
  c= Balloc(k, alloc);
843
914
  for (x= c->p.x, xa= x + wc; x < xa; x++)
844
915
    *x= 0;
845
916
  xa= a->p.x;
911
982
 
912
983
#define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
913
984
 
914
 
static Bigint *pow5mult(Bigint *b, int k)
 
985
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc)
915
986
{
916
987
  Bigint *b1, *p5, *p51;
917
988
  int i;
918
989
  static int p05[3]= { 5, 25, 125 };
919
990
 
920
991
  if ((i= k & 3))
921
 
    b= multadd(b, p05[i-1], 0);
 
992
    b= multadd(b, p05[i-1], 0, alloc);
922
993
 
923
994
  if (!(k>>= 2))
924
995
    return b;
927
998
  {
928
999
    if (k & 1)
929
1000
    {
930
 
      b1= mult(b, p5);
931
 
      Bfree(b);
 
1001
      b1= mult(b, p5, alloc);
 
1002
      Bfree(b, alloc);
932
1003
      b= b1;
933
1004
    }
934
1005
    if (!(k>>= 1))
937
1008
    if (p5 < p5_a + P5A_MAX)
938
1009
      ++p5;
939
1010
    else if (p5 == p5_a + P5A_MAX)
940
 
      p5= mult(p5, p5);
 
1011
      p5= mult(p5, p5, alloc);
941
1012
    else
942
1013
    {
943
 
      p51= mult(p5, p5);
944
 
      Bfree(p5);
 
1014
      p51= mult(p5, p5, alloc);
 
1015
      Bfree(p5, alloc);
945
1016
      p5= p51;
946
1017
    }
947
1018
  }
949
1020
}
950
1021
 
951
1022
 
952
 
static Bigint *lshift(Bigint *b, int k)
 
1023
static Bigint *lshift(Bigint *b, int k, Stack_alloc *alloc)
953
1024
{
954
1025
  int i, k1, n, n1;
955
1026
  Bigint *b1;
960
1031
  n1= n + b->wds + 1;
961
1032
  for (i= b->maxwds; n1 > i; i<<= 1)
962
1033
    k1++;
963
 
  b1= Balloc(k1);
 
1034
  b1= Balloc(k1, alloc);
964
1035
  x1= b1->p.x;
965
1036
  for (i= 0; i < n; i++)
966
1037
    *x1++= 0;
984
1055
      *x1++= *x++;
985
1056
    while (x < xe);
986
1057
  b1->wds= n1 - 1;
987
 
  Bfree(b);
 
1058
  Bfree(b, alloc);
988
1059
  return b1;
989
1060
}
990
1061
 
1013
1084
}
1014
1085
 
1015
1086
 
1016
 
static Bigint *diff(Bigint *a, Bigint *b)
 
1087
static Bigint *diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
1017
1088
{
1018
1089
  Bigint *c;
1019
1090
  int i, wa, wb;
1023
1094
  i= cmp(a,b);
1024
1095
  if (!i)
1025
1096
  {
1026
 
    c= Balloc(0);
 
1097
    c= Balloc(0, alloc);
1027
1098
    c->wds= 1;
1028
1099
    c->p.x[0]= 0;
1029
1100
    return c;
1037
1108
  }
1038
1109
  else
1039
1110
    i= 0;
1040
 
  c= Balloc(a->k);
 
1111
  c= Balloc(a->k, alloc);
1041
1112
  c->sign= i;
1042
1113
  wa= a->wds;
1043
1114
  xa= a->p.x;
1118
1189
}
1119
1190
 
1120
1191
 
1121
 
static Bigint *d2b(double d, int *e, int *bits)
 
1192
static Bigint *d2b(double d, int *e, int *bits, Stack_alloc *alloc)
1122
1193
{
1123
1194
  Bigint *b;
1124
1195
  int de, k;
1127
1198
#define d0 word0(d)
1128
1199
#define d1 word1(d)
1129
1200
 
1130
 
  b= Balloc(1);
 
1201
  b= Balloc(1, alloc);
1131
1202
  x= b->p.x;
1132
1203
 
1133
1204
  z= d0 & Frac_mask;
1234
1305
     for 0 <= k <= 22).
1235
1306
*/
1236
1307
 
1237
 
static double my_strtod_int(const char *s00, char **se, int *error)
 
1308
static double my_strtod_int(const char *s00, char **se, int *error, char *buf, size_t buf_size)
1238
1309
{
1239
1310
  int scale;
1240
1311
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1247
1318
#ifdef SET_INEXACT
1248
1319
  int inexact, oldinexact;
1249
1320
#endif
 
1321
  Stack_alloc alloc;
1250
1322
 
1251
1323
  c= 0;
1252
1324
  *error= 0;
1253
1325
 
 
1326
  alloc.begin= alloc.free= buf;
 
1327
  alloc.end= buf + buf_size;
 
1328
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
 
1329
 
1254
1330
  sign= nz0= nz= 0;
1255
1331
  dval(rv)= 0.;
1256
1332
  for (s= s00; s < end; s++)
1526
1602
 
1527
1603
  /* Put digits into bd: true value = bd * 10^e */
1528
1604
 
1529
 
  bd0= s2b(s0, nd0, nd, y);
 
1605
  bd0= s2b(s0, nd0, nd, y, &alloc);
1530
1606
 
1531
1607
  for(;;)
1532
1608
  {
1533
 
    bd= Balloc(bd0->k);
 
1609
    bd= Balloc(bd0->k, &alloc);
1534
1610
    Bcopy(bd, bd0);
1535
 
    bb= d2b(dval(rv), &bbe, &bbbits);  /* rv = bb * 2^bbe */
1536
 
    bs= i2b(1);
 
1611
    bb= d2b(dval(rv), &bbe, &bbbits, &alloc);  /* rv = bb * 2^bbe */
 
1612
    bs= i2b(1, &alloc);
1537
1613
 
1538
1614
    if (e >= 0)
1539
1615
    {
1570
1646
    }
1571
1647
    if (bb5 > 0)
1572
1648
    {
1573
 
      bs= pow5mult(bs, bb5);
1574
 
      bb1= mult(bs, bb);
1575
 
      Bfree(bb);
 
1649
      bs= pow5mult(bs, bb5, &alloc);
 
1650
      bb1= mult(bs, bb, &alloc);
 
1651
      Bfree(bb, &alloc);
1576
1652
      bb= bb1;
1577
1653
    }
1578
1654
    if (bb2 > 0)
1579
 
      bb= lshift(bb, bb2);
 
1655
      bb= lshift(bb, bb2, &alloc);
1580
1656
    if (bd5 > 0)
1581
 
      bd= pow5mult(bd, bd5);
 
1657
      bd= pow5mult(bd, bd5, &alloc);
1582
1658
    if (bd2 > 0)
1583
 
      bd= lshift(bd, bd2);
 
1659
      bd= lshift(bd, bd2, &alloc);
1584
1660
    if (bs2 > 0)
1585
 
      bs= lshift(bs, bs2);
1586
 
    delta= diff(bb, bd);
 
1661
      bs= lshift(bs, bs2, &alloc);
 
1662
    delta= diff(bb, bd, &alloc);
1587
1663
    dsign= delta->sign;
1588
1664
    delta->sign= 0;
1589
1665
    i= cmp(delta, bs);
1611
1687
#endif
1612
1688
        break;
1613
1689
      }
1614
 
      delta= lshift(delta, Log2P);
 
1690
      delta= lshift(delta, Log2P, &alloc);
1615
1691
      if (cmp(delta, bs) > 0)
1616
1692
        goto drop_down;
1617
1693
      break;
1752
1828
      }
1753
1829
#endif
1754
1830
 cont:
1755
 
    Bfree(bb);
1756
 
    Bfree(bd);
1757
 
    Bfree(bs);
1758
 
    Bfree(delta);
 
1831
    Bfree(bb, &alloc);
 
1832
    Bfree(bd, &alloc);
 
1833
    Bfree(bs, &alloc);
 
1834
    Bfree(delta, &alloc);
1759
1835
  }
1760
1836
#ifdef SET_INEXACT
1761
1837
  if (inexact)
1785
1861
  }
1786
1862
#endif
1787
1863
 retfree:
1788
 
  Bfree(bb);
1789
 
  Bfree(bd);
1790
 
  Bfree(bs);
1791
 
  Bfree(bd0);
1792
 
  Bfree(delta);
 
1864
  Bfree(bb, &alloc);
 
1865
  Bfree(bd, &alloc);
 
1866
  Bfree(bs, &alloc);
 
1867
  Bfree(bd0, &alloc);
 
1868
  Bfree(delta, &alloc);
1793
1869
 ret:
1794
1870
  *se= (char *)s;
1795
1871
  return sign ? -dval(rv) : dval(rv);
1896
1972
 */
1897
1973
 
1898
1974
static char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
1899
 
                  char **rve)
 
1975
                  char **rve, char *buf, size_t buf_size)
1900
1976
{
1901
1977
  /*
1902
1978
    Arguments ndigits, decpt, sign are similar to those
1908
1984
    mode:
1909
1985
          0 ==> shortest string that yields d when read in
1910
1986
                and rounded to nearest.
1911
 
 
1912
1987
          1 ==> like 0, but with Steele & White stopping rule;
1913
1988
                e.g. with IEEE P754 arithmetic , mode 0 gives
1914
1989
                1e23 whereas mode 1 gives 9.999999999999999e22.
1940
2015
  Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
1941
2016
  double d2, ds, eps;
1942
2017
  char *s, *s0;
 
2018
  Stack_alloc alloc;
 
2019
 
 
2020
  alloc.begin= alloc.free= buf;
 
2021
  alloc.end= buf + buf_size;
 
2022
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
1943
2023
 
1944
2024
  if (word0(d) & Sign_bit)
1945
2025
  {
1951
2031
    *sign= 0;
1952
2032
 
1953
2033
  /* If infinity, set decpt to DTOA_OVERFLOW, if 0 set it to 1 */
1954
 
  if ((((word0(d) & Exp_mask) == Exp_mask) && ((*decpt= DTOA_OVERFLOW) != 0)) ||
1955
 
      (!dval(d) && ((*decpt= 1) != 0)))
 
2034
  if (((word0(d) & Exp_mask) == Exp_mask && (*decpt= DTOA_OVERFLOW)) ||
 
2035
      (!dval(d) && (*decpt= 1)))
1956
2036
  {
1957
2037
    /* Infinity, NaN, 0 */
1958
 
    char *res= (char*) malloc(2);
 
2038
    char *res= (char*) dtoa_alloc(2, &alloc);
1959
2039
    res[0]= '0';
1960
2040
    res[1]= '\0';
1961
2041
    if (rve)
1964
2044
  }
1965
2045
 
1966
2046
 
1967
 
  b= d2b(dval(d), &be, &bbits);
 
2047
  b= d2b(dval(d), &be, &bbits, &alloc);
1968
2048
  if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
1969
2049
  {
1970
2050
    dval(d2)= dval(d);
2079
2159
    if (i <= 0)
2080
2160
      i= 1;
2081
2161
  }
2082
 
  s= s0= (char*)malloc(i+1); /* +1 for trailing '\0' appended
2083
 
                                at end of function */
 
2162
  s= s0= dtoa_alloc(i, &alloc);
2084
2163
 
2085
2164
  if (ilim >= 0 && ilim <= Quick_max && try_quick)
2086
2165
  {
2245
2324
    i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
2246
2325
    b2+= i;
2247
2326
    s2+= i;
2248
 
    mhi= i2b(1);
 
2327
    mhi= i2b(1, &alloc);
2249
2328
  }
2250
2329
  if (m2 > 0 && s2 > 0)
2251
2330
  {
2260
2339
    {
2261
2340
      if (m5 > 0)
2262
2341
      {
2263
 
        mhi= pow5mult(mhi, m5);
2264
 
        b1= mult(mhi, b);
2265
 
        Bfree(b);
 
2342
        mhi= pow5mult(mhi, m5, &alloc);
 
2343
        b1= mult(mhi, b, &alloc);
 
2344
        Bfree(b, &alloc);
2266
2345
        b= b1;
2267
2346
      }
2268
2347
      if ((j= b5 - m5))
2269
 
        b= pow5mult(b, j);
 
2348
        b= pow5mult(b, j, &alloc);
2270
2349
    }
2271
2350
    else
2272
 
      b= pow5mult(b, b5);
 
2351
      b= pow5mult(b, b5, &alloc);
2273
2352
  }
2274
 
  S= i2b(1);
 
2353
  S= i2b(1, &alloc);
2275
2354
  if (s5 > 0)
2276
 
    S= pow5mult(S, s5);
 
2355
    S= pow5mult(S, s5, &alloc);
2277
2356
 
2278
2357
  /* Check for special case that d is a normalized power of 2. */
2279
2358
 
2317
2396
    s2+= i;
2318
2397
  }
2319
2398
  if (b2 > 0)
2320
 
    b= lshift(b, b2);
 
2399
    b= lshift(b, b2, &alloc);
2321
2400
  if (s2 > 0)
2322
 
    S= lshift(S, s2);
 
2401
    S= lshift(S, s2, &alloc);
2323
2402
  if (k_check)
2324
2403
  {
2325
2404
    if (cmp(b,S) < 0)
2326
2405
    {
2327
2406
      k--;
2328
2407
      /* we botched the k estimate */
2329
 
      b= multadd(b, 10, 0);
 
2408
      b= multadd(b, 10, 0, &alloc);
2330
2409
      if (leftright)
2331
 
        mhi= multadd(mhi, 10, 0);
 
2410
        mhi= multadd(mhi, 10, 0, &alloc);
2332
2411
      ilim= ilim1;
2333
2412
    }
2334
2413
  }
2335
2414
  if (ilim <= 0 && (mode == 3 || mode == 5))
2336
2415
  {
2337
 
    if (ilim < 0 || cmp(b,S= multadd(S,5,0)) <= 0)
 
2416
    if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2338
2417
    {
2339
2418
      /* no digits, fcvt style */
2340
2419
no_digits:
2349
2428
  if (leftright)
2350
2429
  {
2351
2430
    if (m2 > 0)
2352
 
      mhi= lshift(mhi, m2);
 
2431
      mhi= lshift(mhi, m2, &alloc);
2353
2432
 
2354
2433
    /*
2355
2434
      Compute mlo -- check for special case that d is a normalized power of 2.
2358
2437
    mlo= mhi;
2359
2438
    if (spec_case)
2360
2439
    {
2361
 
      mhi= Balloc(mhi->k);
 
2440
      mhi= Balloc(mhi->k, &alloc);
2362
2441
      Bcopy(mhi, mlo);
2363
 
      mhi= lshift(mhi, Log2P);
 
2442
      mhi= lshift(mhi, Log2P, &alloc);
2364
2443
    }
2365
2444
 
2366
2445
    for (i= 1;;i++)
2368
2447
      dig= quorem(b,S) + '0';
2369
2448
      /* Do we yet have the shortest decimal string that will round to d? */
2370
2449
      j= cmp(b, mlo);
2371
 
      delta= diff(S, mhi);
 
2450
      delta= diff(S, mhi, &alloc);
2372
2451
      j1= delta->sign ? 1 : cmp(b, delta);
2373
 
      Bfree(delta);
 
2452
      Bfree(delta, &alloc);
2374
2453
      if (j1 == 0 && mode != 1 && !(word1(d) & 1)
2375
2454
         )
2376
2455
      {
2389
2468
        }
2390
2469
        if (j1 > 0)
2391
2470
        {
2392
 
          b= lshift(b, 1);
 
2471
          b= lshift(b, 1, &alloc);
2393
2472
          j1= cmp(b, S);
2394
2473
          if ((j1 > 0 || (j1 == 0 && dig & 1))
2395
2474
              && dig++ == '9')
2413
2492
      *s++= dig;
2414
2493
      if (i == ilim)
2415
2494
        break;
2416
 
      b= multadd(b, 10, 0);
 
2495
      b= multadd(b, 10, 0, &alloc);
2417
2496
      if (mlo == mhi)
2418
 
        mlo= mhi= multadd(mhi, 10, 0);
 
2497
        mlo= mhi= multadd(mhi, 10, 0, &alloc);
2419
2498
      else
2420
2499
      {
2421
 
        mlo= multadd(mlo, 10, 0);
2422
 
        mhi= multadd(mhi, 10, 0);
 
2500
        mlo= multadd(mlo, 10, 0, &alloc);
 
2501
        mhi= multadd(mhi, 10, 0, &alloc);
2423
2502
      }
2424
2503
    }
2425
2504
  }
2433
2512
      }
2434
2513
      if (i >= ilim)
2435
2514
        break;
2436
 
      b= multadd(b, 10, 0);
 
2515
      b= multadd(b, 10, 0, &alloc);
2437
2516
    }
2438
2517
 
2439
2518
  /* Round off last digit */
2440
2519
 
2441
 
  b= lshift(b, 1);
 
2520
  b= lshift(b, 1, &alloc);
2442
2521
  j= cmp(b, S);
2443
2522
  if (j > 0 || (j == 0 && dig & 1))
2444
2523
  {
2458
2537
    s++;
2459
2538
  }
2460
2539
ret:
2461
 
  Bfree(S);
 
2540
  Bfree(S, &alloc);
2462
2541
  if (mhi)
2463
2542
  {
2464
2543
    if (mlo && mlo != mhi)
2465
 
      Bfree(mlo);
2466
 
    Bfree(mhi);
 
2544
      Bfree(mlo, &alloc);
 
2545
    Bfree(mhi, &alloc);
2467
2546
  }
2468
2547
ret1:
2469
 
  Bfree(b);
 
2548
  Bfree(b, &alloc);
2470
2549
  *s= 0;
2471
2550
  *decpt= k + 1;
2472
2551
  if (rve)
2473
2552
    *rve= s;
2474
2553
  return s0;
2475
2554
}
2476
 
 
2477
 
} /* namespace internal */
2478
 
} /* namespace drizzled */