~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/dtoa.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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
  char buf[DTOA_BUFF_SIZE];
96
92
  assert(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
97
93
 
98
 
  res= dtoa(x, 5, precision, &decpt, &sign, &end);
 
94
  res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
99
95
 
100
96
  if (decpt == DTOA_OVERFLOW)
101
97
  {
102
 
    free(res);
 
98
    dtoa_free(res, buf, sizeof(buf));
103
99
    *to++= '0';
104
100
    *to= '\0';
105
101
    if (error != NULL)
135
131
    if (len <= decpt)
136
132
      *dst++= '.';
137
133
 
138
 
    for (i= precision - max(0, (len - decpt)); i > 0; i--)
 
134
    for (i= precision - cmax(0, (len - decpt)); i > 0; i--)
139
135
      *dst++= '0';
140
136
  }
141
137
 
143
139
  if (error != NULL)
144
140
    *error= false;
145
141
 
146
 
  free(res);
 
142
  dtoa_free(res, buf, sizeof(buf));
147
143
 
148
144
  return dst - to;
149
145
}
216
212
{
217
213
  int decpt, sign, len, exp_len;
218
214
  char *res, *src, *end, *dst= to, *dend= dst + width;
 
215
  char buf[DTOA_BUFF_SIZE];
219
216
  bool have_space, force_e_format;
220
217
  assert(width > 0 && to != NULL);
221
218
 
223
220
  if (x < 0.)
224
221
    width--;
225
222
 
226
 
  res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? min(width, DBL_DIG) : 
227
 
            min(width, FLT_DIG), &decpt, &sign, &end);
228
 
 
 
223
  res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : cmin(width, FLT_DIG),
 
224
            &decpt, &sign, &end, buf, sizeof(buf));
229
225
  if (decpt == DTOA_OVERFLOW)
230
226
  {
231
 
    free(res);
 
227
    dtoa_free(res, buf, sizeof(buf));
232
228
    *to++= '0';
233
229
    *to= '\0';
234
230
    if (error != NULL)
329
325
        decimal point. For this we are calling dtoa with mode=5, passing the
330
326
        number of significant digits = (len-decpt) - (len-width) = width-decpt
331
327
      */
332
 
      free(res);
333
 
      res= dtoa(x, 5, width - decpt, &decpt, &sign, &end);
 
328
      dtoa_free(res, buf, sizeof(buf));
 
329
      res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
334
330
      src= res;
335
331
      len= end - res;
336
332
    }
395
391
    if (width < len)
396
392
    {
397
393
      /* Yes, re-convert with a smaller width */
398
 
      free(res);
399
 
      res= dtoa(x, 4, width, &decpt, &sign, &end);
 
394
      dtoa_free(res, buf, sizeof(buf));
 
395
      res= dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
400
396
      src= res;
401
397
      len= end - res;
402
398
      if (--decpt < 0)
436
432
  }
437
433
 
438
434
end:
439
 
  free(res);
 
435
  dtoa_free(res, buf, sizeof(buf));
440
436
  *dst= '\0';
441
437
 
442
438
  return dst - to;
462
458
 
463
459
double my_strtod(const char *str, char **end, int *error)
464
460
{
 
461
  char buf[DTOA_BUFF_SIZE];
465
462
  double res;
466
463
  assert(str != NULL && end != NULL && *end != NULL && error != NULL);
467
464
 
468
 
  res= my_strtod_int(str, end, error);
 
465
  res= my_strtod_int(str, end, error, buf, sizeof(buf));
469
466
  return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
470
467
}
471
468
 
594
591
#define Big1 0xffffffff
595
592
#define FFFFFFFF 0xffffffffUL
596
593
 
 
594
/* This is tested to be enough for dtoa */
 
595
 
 
596
#define Kmax 15
 
597
 
 
598
#define Bcopy(x,y) memcpy(&x->sign, &y->sign, \
 
599
                          2*sizeof(int) + y->wds*sizeof(ULong))
 
600
 
597
601
/* Arbitrary-length integer */
598
602
 
599
603
typedef struct Bigint
608
612
  int wds;                 /* current length in 32-bit words */
609
613
} Bigint;
610
614
 
611
 
static Bigint *Bcopy(Bigint* dst, Bigint* src)
 
615
 
 
616
/* A simple stack-memory based allocator for Bigints */
 
617
 
 
618
typedef struct Stack_alloc
612
619
{
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)
 
620
  char *begin;
 
621
  char *free;
 
622
  char *end;
 
623
  /*
 
624
    Having list of free blocks lets us reduce maximum required amount
 
625
    of memory from ~4000 bytes to < 1680 (tested on x86).
 
626
  */
 
627
  Bigint *freelist[Kmax+1];
 
628
} Stack_alloc;
 
629
 
 
630
 
 
631
/*
 
632
  Try to allocate object on stack, and resort to malloc if all
 
633
  stack memory is used.
 
634
*/
 
635
 
 
636
static Bigint *Balloc(int k, Stack_alloc *alloc)
624
637
{
625
638
  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
 
 
 
639
  if (k <= Kmax &&  alloc->freelist[k])
 
640
  {
 
641
    rv= alloc->freelist[k];
 
642
    alloc->freelist[k]= rv->p.next;
 
643
  }
 
644
  else
 
645
  {
 
646
    int x, len;
 
647
 
 
648
    x= 1 << k;
 
649
    len= sizeof(Bigint) + x * sizeof(ULong);
 
650
 
 
651
    if (alloc->free + len <= alloc->end)
 
652
    {
 
653
      rv= (Bigint*) alloc->free;
 
654
      alloc->free+= len;
 
655
    }
 
656
    else
 
657
      rv= (Bigint*) malloc(len);
 
658
 
 
659
    rv->k= k;
 
660
    rv->maxwds= x;
 
661
  }
637
662
  rv->sign= rv->wds= 0;
638
 
 
 
663
  rv->p.x= (ULong*) (rv + 1);
639
664
  return rv;
640
665
}
641
666
 
645
670
  list. Otherwise call free().
646
671
*/
647
672
 
648
 
static void Bfree(Bigint *v)
649
 
{
650
 
  if(!v)
651
 
    return;
652
 
  free(v->p.x);
653
 
  free(v);
654
 
}
 
673
static void Bfree(Bigint *v, Stack_alloc *alloc)
 
674
{
 
675
  char *gptr= (char*) v;                       /* generic pointer */
 
676
  if (gptr < alloc->begin || gptr >= alloc->end)
 
677
    free(gptr);
 
678
  else if (v->k <= Kmax)
 
679
  {
 
680
    /*
 
681
      Maintain free lists only for stack objects: this way we don't
 
682
      have to bother with freeing lists in the end of dtoa;
 
683
      heap should not be used normally anyway.
 
684
    */
 
685
    v->p.next= alloc->freelist[v->k];
 
686
    alloc->freelist[v->k]= v;
 
687
  }
 
688
}
 
689
 
 
690
 
 
691
/*
 
692
  This is to place return value of dtoa in: tries to use stack
 
693
  as well, but passes by free lists management and just aligns len by
 
694
  sizeof(ULong).
 
695
*/
 
696
 
 
697
static char *dtoa_alloc(int i, Stack_alloc *alloc)
 
698
{
 
699
  char *rv;
 
700
  int aligned_size= (i + sizeof(ULong) - 1) / sizeof(ULong) * sizeof(ULong);
 
701
  if (alloc->free + aligned_size <= alloc->end)
 
702
  {
 
703
    rv= alloc->free;
 
704
    alloc->free+= aligned_size;
 
705
  }
 
706
  else
 
707
    rv= (char *)malloc(i);
 
708
  return rv;
 
709
}
 
710
 
 
711
 
 
712
/*
 
713
  dtoa_free() must be used to free values s returned by dtoa()
 
714
  This is the counterpart of dtoa_alloc()
 
715
*/
 
716
 
 
717
static void dtoa_free(char *gptr, char *buf, size_t buf_size)
 
718
{
 
719
  if (gptr < buf || gptr >= buf + buf_size)
 
720
    free(gptr);
 
721
}
 
722
 
655
723
 
656
724
/* Bigint arithmetic functions */
657
725
 
658
726
/* Multiply by m and add a */
659
727
 
660
 
static Bigint *multadd(Bigint *b, int m, int a)
 
728
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc)
661
729
{
662
730
  int i, wds;
663
731
  ULong *x;
679
747
  {
680
748
    if (wds >= b->maxwds)
681
749
    {
682
 
      b1= Balloc(b->k+1);
 
750
      b1= Balloc(b->k+1, alloc);
683
751
      Bcopy(b1, b);
684
 
      Bfree(b);
 
752
      Bfree(b, alloc);
685
753
      b= b1;
686
754
    }
687
755
    b->p.x[wds++]= (ULong) carry;
691
759
}
692
760
 
693
761
 
694
 
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9)
 
762
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9, Stack_alloc *alloc)
695
763
{
696
764
  Bigint *b;
697
765
  int i, k;
699
767
 
700
768
  x= (nd + 8) / 9;
701
769
  for (k= 0, y= 1; x > y; y <<= 1, k++) ;
702
 
  b= Balloc(k);
 
770
  b= Balloc(k, alloc);
703
771
  b->p.x[0]= y9;
704
772
  b->wds= 1;
705
773
 
708
776
  {
709
777
    s+= 9;
710
778
    do
711
 
      b= multadd(b, 10, *s++ - '0');
 
779
      b= multadd(b, 10, *s++ - '0', alloc);
712
780
    while (++i < nd0);
713
781
    s++;
714
782
  }
715
783
  else
716
784
    s+= 10;
717
785
  for(; i < nd; i++)
718
 
    b= multadd(b, 10, *s++ - '0');
 
786
    b= multadd(b, 10, *s++ - '0', alloc);
719
787
  return b;
720
788
}
721
789
 
806
874
 
807
875
/* Convert integer to Bigint number */
808
876
 
809
 
static Bigint *i2b(int i)
 
877
static Bigint *i2b(int i, Stack_alloc *alloc)
810
878
{
811
879
  Bigint *b;
812
880
 
813
 
  b= Balloc(1);
 
881
  b= Balloc(1, alloc);
814
882
  b->p.x[0]= i;
815
883
  b->wds= 1;
816
884
  return b;
819
887
 
820
888
/* Multiply two Bigint numbers */
821
889
 
822
 
static Bigint *mult(Bigint *a, Bigint *b)
 
890
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
823
891
{
824
892
  Bigint *c;
825
893
  int k, wa, wb, wc;
839
907
  wc= wa + wb;
840
908
  if (wc > a->maxwds)
841
909
    k++;
842
 
  c= Balloc(k);
 
910
  c= Balloc(k, alloc);
843
911
  for (x= c->p.x, xa= x + wc; x < xa; x++)
844
912
    *x= 0;
845
913
  xa= a->p.x;
911
979
 
912
980
#define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
913
981
 
914
 
static Bigint *pow5mult(Bigint *b, int k)
 
982
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc)
915
983
{
916
984
  Bigint *b1, *p5, *p51;
917
985
  int i;
918
986
  static int p05[3]= { 5, 25, 125 };
919
987
 
920
988
  if ((i= k & 3))
921
 
    b= multadd(b, p05[i-1], 0);
 
989
    b= multadd(b, p05[i-1], 0, alloc);
922
990
 
923
991
  if (!(k>>= 2))
924
992
    return b;
927
995
  {
928
996
    if (k & 1)
929
997
    {
930
 
      b1= mult(b, p5);
931
 
      Bfree(b);
 
998
      b1= mult(b, p5, alloc);
 
999
      Bfree(b, alloc);
932
1000
      b= b1;
933
1001
    }
934
1002
    if (!(k>>= 1))
937
1005
    if (p5 < p5_a + P5A_MAX)
938
1006
      ++p5;
939
1007
    else if (p5 == p5_a + P5A_MAX)
940
 
      p5= mult(p5, p5);
 
1008
      p5= mult(p5, p5, alloc);
941
1009
    else
942
1010
    {
943
 
      p51= mult(p5, p5);
944
 
      Bfree(p5);
 
1011
      p51= mult(p5, p5, alloc);
 
1012
      Bfree(p5, alloc);
945
1013
      p5= p51;
946
1014
    }
947
1015
  }
949
1017
}
950
1018
 
951
1019
 
952
 
static Bigint *lshift(Bigint *b, int k)
 
1020
static Bigint *lshift(Bigint *b, int k, Stack_alloc *alloc)
953
1021
{
954
1022
  int i, k1, n, n1;
955
1023
  Bigint *b1;
960
1028
  n1= n + b->wds + 1;
961
1029
  for (i= b->maxwds; n1 > i; i<<= 1)
962
1030
    k1++;
963
 
  b1= Balloc(k1);
 
1031
  b1= Balloc(k1, alloc);
964
1032
  x1= b1->p.x;
965
1033
  for (i= 0; i < n; i++)
966
1034
    *x1++= 0;
984
1052
      *x1++= *x++;
985
1053
    while (x < xe);
986
1054
  b1->wds= n1 - 1;
987
 
  Bfree(b);
 
1055
  Bfree(b, alloc);
988
1056
  return b1;
989
1057
}
990
1058
 
1013
1081
}
1014
1082
 
1015
1083
 
1016
 
static Bigint *diff(Bigint *a, Bigint *b)
 
1084
static Bigint *diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
1017
1085
{
1018
1086
  Bigint *c;
1019
1087
  int i, wa, wb;
1023
1091
  i= cmp(a,b);
1024
1092
  if (!i)
1025
1093
  {
1026
 
    c= Balloc(0);
 
1094
    c= Balloc(0, alloc);
1027
1095
    c->wds= 1;
1028
1096
    c->p.x[0]= 0;
1029
1097
    return c;
1037
1105
  }
1038
1106
  else
1039
1107
    i= 0;
1040
 
  c= Balloc(a->k);
 
1108
  c= Balloc(a->k, alloc);
1041
1109
  c->sign= i;
1042
1110
  wa= a->wds;
1043
1111
  xa= a->p.x;
1118
1186
}
1119
1187
 
1120
1188
 
1121
 
static Bigint *d2b(double d, int *e, int *bits)
 
1189
static Bigint *d2b(double d, int *e, int *bits, Stack_alloc *alloc)
1122
1190
{
1123
1191
  Bigint *b;
1124
1192
  int de, k;
1127
1195
#define d0 word0(d)
1128
1196
#define d1 word1(d)
1129
1197
 
1130
 
  b= Balloc(1);
 
1198
  b= Balloc(1, alloc);
1131
1199
  x= b->p.x;
1132
1200
 
1133
1201
  z= d0 & Frac_mask;
1234
1302
     for 0 <= k <= 22).
1235
1303
*/
1236
1304
 
1237
 
static double my_strtod_int(const char *s00, char **se, int *error)
 
1305
static double my_strtod_int(const char *s00, char **se, int *error, char *buf, size_t buf_size)
1238
1306
{
1239
1307
  int scale;
1240
1308
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1247
1315
#ifdef SET_INEXACT
1248
1316
  int inexact, oldinexact;
1249
1317
#endif
 
1318
  Stack_alloc alloc;
1250
1319
 
1251
1320
  c= 0;
1252
1321
  *error= 0;
1253
1322
 
 
1323
  alloc.begin= alloc.free= buf;
 
1324
  alloc.end= buf + buf_size;
 
1325
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
 
1326
 
1254
1327
  sign= nz0= nz= 0;
1255
1328
  dval(rv)= 0.;
1256
1329
  for (s= s00; s < end; s++)
1526
1599
 
1527
1600
  /* Put digits into bd: true value = bd * 10^e */
1528
1601
 
1529
 
  bd0= s2b(s0, nd0, nd, y);
 
1602
  bd0= s2b(s0, nd0, nd, y, &alloc);
1530
1603
 
1531
1604
  for(;;)
1532
1605
  {
1533
 
    bd= Balloc(bd0->k);
 
1606
    bd= Balloc(bd0->k, &alloc);
1534
1607
    Bcopy(bd, bd0);
1535
 
    bb= d2b(dval(rv), &bbe, &bbbits);  /* rv = bb * 2^bbe */
1536
 
    bs= i2b(1);
 
1608
    bb= d2b(dval(rv), &bbe, &bbbits, &alloc);  /* rv = bb * 2^bbe */
 
1609
    bs= i2b(1, &alloc);
1537
1610
 
1538
1611
    if (e >= 0)
1539
1612
    {
1570
1643
    }
1571
1644
    if (bb5 > 0)
1572
1645
    {
1573
 
      bs= pow5mult(bs, bb5);
1574
 
      bb1= mult(bs, bb);
1575
 
      Bfree(bb);
 
1646
      bs= pow5mult(bs, bb5, &alloc);
 
1647
      bb1= mult(bs, bb, &alloc);
 
1648
      Bfree(bb, &alloc);
1576
1649
      bb= bb1;
1577
1650
    }
1578
1651
    if (bb2 > 0)
1579
 
      bb= lshift(bb, bb2);
 
1652
      bb= lshift(bb, bb2, &alloc);
1580
1653
    if (bd5 > 0)
1581
 
      bd= pow5mult(bd, bd5);
 
1654
      bd= pow5mult(bd, bd5, &alloc);
1582
1655
    if (bd2 > 0)
1583
 
      bd= lshift(bd, bd2);
 
1656
      bd= lshift(bd, bd2, &alloc);
1584
1657
    if (bs2 > 0)
1585
 
      bs= lshift(bs, bs2);
1586
 
    delta= diff(bb, bd);
 
1658
      bs= lshift(bs, bs2, &alloc);
 
1659
    delta= diff(bb, bd, &alloc);
1587
1660
    dsign= delta->sign;
1588
1661
    delta->sign= 0;
1589
1662
    i= cmp(delta, bs);
1611
1684
#endif
1612
1685
        break;
1613
1686
      }
1614
 
      delta= lshift(delta, Log2P);
 
1687
      delta= lshift(delta, Log2P, &alloc);
1615
1688
      if (cmp(delta, bs) > 0)
1616
1689
        goto drop_down;
1617
1690
      break;
1752
1825
      }
1753
1826
#endif
1754
1827
 cont:
1755
 
    Bfree(bb);
1756
 
    Bfree(bd);
1757
 
    Bfree(bs);
1758
 
    Bfree(delta);
 
1828
    Bfree(bb, &alloc);
 
1829
    Bfree(bd, &alloc);
 
1830
    Bfree(bs, &alloc);
 
1831
    Bfree(delta, &alloc);
1759
1832
  }
1760
1833
#ifdef SET_INEXACT
1761
1834
  if (inexact)
1785
1858
  }
1786
1859
#endif
1787
1860
 retfree:
1788
 
  Bfree(bb);
1789
 
  Bfree(bd);
1790
 
  Bfree(bs);
1791
 
  Bfree(bd0);
1792
 
  Bfree(delta);
 
1861
  Bfree(bb, &alloc);
 
1862
  Bfree(bd, &alloc);
 
1863
  Bfree(bs, &alloc);
 
1864
  Bfree(bd0, &alloc);
 
1865
  Bfree(delta, &alloc);
1793
1866
 ret:
1794
1867
  *se= (char *)s;
1795
1868
  return sign ? -dval(rv) : dval(rv);
1896
1969
 */
1897
1970
 
1898
1971
static char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
1899
 
                  char **rve)
 
1972
                  char **rve, char *buf, size_t buf_size)
1900
1973
{
1901
1974
  /*
1902
1975
    Arguments ndigits, decpt, sign are similar to those
1908
1981
    mode:
1909
1982
          0 ==> shortest string that yields d when read in
1910
1983
                and rounded to nearest.
1911
 
 
1912
1984
          1 ==> like 0, but with Steele & White stopping rule;
1913
1985
                e.g. with IEEE P754 arithmetic , mode 0 gives
1914
1986
                1e23 whereas mode 1 gives 9.999999999999999e22.
1940
2012
  Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
1941
2013
  double d2, ds, eps;
1942
2014
  char *s, *s0;
 
2015
  Stack_alloc alloc;
 
2016
 
 
2017
  alloc.begin= alloc.free= buf;
 
2018
  alloc.end= buf + buf_size;
 
2019
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
1943
2020
 
1944
2021
  if (word0(d) & Sign_bit)
1945
2022
  {
1951
2028
    *sign= 0;
1952
2029
 
1953
2030
  /* 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)))
 
2031
  if (((word0(d) & Exp_mask) == Exp_mask && (*decpt= DTOA_OVERFLOW)) ||
 
2032
      (!dval(d) && (*decpt= 1)))
1956
2033
  {
1957
2034
    /* Infinity, NaN, 0 */
1958
 
    char *res= (char*) malloc(2);
 
2035
    char *res= (char*) dtoa_alloc(2, &alloc);
1959
2036
    res[0]= '0';
1960
2037
    res[1]= '\0';
1961
2038
    if (rve)
1964
2041
  }
1965
2042
 
1966
2043
 
1967
 
  b= d2b(dval(d), &be, &bbits);
 
2044
  b= d2b(dval(d), &be, &bbits, &alloc);
1968
2045
  if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
1969
2046
  {
1970
2047
    dval(d2)= dval(d);
2079
2156
    if (i <= 0)
2080
2157
      i= 1;
2081
2158
  }
2082
 
  s= s0= (char*)malloc(i+1); /* +1 for trailing '\0' appended
2083
 
                                at end of function */
 
2159
  s= s0= dtoa_alloc(i, &alloc);
2084
2160
 
2085
2161
  if (ilim >= 0 && ilim <= Quick_max && try_quick)
2086
2162
  {
2245
2321
    i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
2246
2322
    b2+= i;
2247
2323
    s2+= i;
2248
 
    mhi= i2b(1);
 
2324
    mhi= i2b(1, &alloc);
2249
2325
  }
2250
2326
  if (m2 > 0 && s2 > 0)
2251
2327
  {
2260
2336
    {
2261
2337
      if (m5 > 0)
2262
2338
      {
2263
 
        mhi= pow5mult(mhi, m5);
2264
 
        b1= mult(mhi, b);
2265
 
        Bfree(b);
 
2339
        mhi= pow5mult(mhi, m5, &alloc);
 
2340
        b1= mult(mhi, b, &alloc);
 
2341
        Bfree(b, &alloc);
2266
2342
        b= b1;
2267
2343
      }
2268
2344
      if ((j= b5 - m5))
2269
 
        b= pow5mult(b, j);
 
2345
        b= pow5mult(b, j, &alloc);
2270
2346
    }
2271
2347
    else
2272
 
      b= pow5mult(b, b5);
 
2348
      b= pow5mult(b, b5, &alloc);
2273
2349
  }
2274
 
  S= i2b(1);
 
2350
  S= i2b(1, &alloc);
2275
2351
  if (s5 > 0)
2276
 
    S= pow5mult(S, s5);
 
2352
    S= pow5mult(S, s5, &alloc);
2277
2353
 
2278
2354
  /* Check for special case that d is a normalized power of 2. */
2279
2355
 
2317
2393
    s2+= i;
2318
2394
  }
2319
2395
  if (b2 > 0)
2320
 
    b= lshift(b, b2);
 
2396
    b= lshift(b, b2, &alloc);
2321
2397
  if (s2 > 0)
2322
 
    S= lshift(S, s2);
 
2398
    S= lshift(S, s2, &alloc);
2323
2399
  if (k_check)
2324
2400
  {
2325
2401
    if (cmp(b,S) < 0)
2326
2402
    {
2327
2403
      k--;
2328
2404
      /* we botched the k estimate */
2329
 
      b= multadd(b, 10, 0);
 
2405
      b= multadd(b, 10, 0, &alloc);
2330
2406
      if (leftright)
2331
 
        mhi= multadd(mhi, 10, 0);
 
2407
        mhi= multadd(mhi, 10, 0, &alloc);
2332
2408
      ilim= ilim1;
2333
2409
    }
2334
2410
  }
2335
2411
  if (ilim <= 0 && (mode == 3 || mode == 5))
2336
2412
  {
2337
 
    if (ilim < 0 || cmp(b,S= multadd(S,5,0)) <= 0)
 
2413
    if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2338
2414
    {
2339
2415
      /* no digits, fcvt style */
2340
2416
no_digits:
2349
2425
  if (leftright)
2350
2426
  {
2351
2427
    if (m2 > 0)
2352
 
      mhi= lshift(mhi, m2);
 
2428
      mhi= lshift(mhi, m2, &alloc);
2353
2429
 
2354
2430
    /*
2355
2431
      Compute mlo -- check for special case that d is a normalized power of 2.
2358
2434
    mlo= mhi;
2359
2435
    if (spec_case)
2360
2436
    {
2361
 
      mhi= Balloc(mhi->k);
 
2437
      mhi= Balloc(mhi->k, &alloc);
2362
2438
      Bcopy(mhi, mlo);
2363
 
      mhi= lshift(mhi, Log2P);
 
2439
      mhi= lshift(mhi, Log2P, &alloc);
2364
2440
    }
2365
2441
 
2366
2442
    for (i= 1;;i++)
2368
2444
      dig= quorem(b,S) + '0';
2369
2445
      /* Do we yet have the shortest decimal string that will round to d? */
2370
2446
      j= cmp(b, mlo);
2371
 
      delta= diff(S, mhi);
 
2447
      delta= diff(S, mhi, &alloc);
2372
2448
      j1= delta->sign ? 1 : cmp(b, delta);
2373
 
      Bfree(delta);
 
2449
      Bfree(delta, &alloc);
2374
2450
      if (j1 == 0 && mode != 1 && !(word1(d) & 1)
2375
2451
         )
2376
2452
      {
2389
2465
        }
2390
2466
        if (j1 > 0)
2391
2467
        {
2392
 
          b= lshift(b, 1);
 
2468
          b= lshift(b, 1, &alloc);
2393
2469
          j1= cmp(b, S);
2394
2470
          if ((j1 > 0 || (j1 == 0 && dig & 1))
2395
2471
              && dig++ == '9')
2413
2489
      *s++= dig;
2414
2490
      if (i == ilim)
2415
2491
        break;
2416
 
      b= multadd(b, 10, 0);
 
2492
      b= multadd(b, 10, 0, &alloc);
2417
2493
      if (mlo == mhi)
2418
 
        mlo= mhi= multadd(mhi, 10, 0);
 
2494
        mlo= mhi= multadd(mhi, 10, 0, &alloc);
2419
2495
      else
2420
2496
      {
2421
 
        mlo= multadd(mlo, 10, 0);
2422
 
        mhi= multadd(mhi, 10, 0);
 
2497
        mlo= multadd(mlo, 10, 0, &alloc);
 
2498
        mhi= multadd(mhi, 10, 0, &alloc);
2423
2499
      }
2424
2500
    }
2425
2501
  }
2433
2509
      }
2434
2510
      if (i >= ilim)
2435
2511
        break;
2436
 
      b= multadd(b, 10, 0);
 
2512
      b= multadd(b, 10, 0, &alloc);
2437
2513
    }
2438
2514
 
2439
2515
  /* Round off last digit */
2440
2516
 
2441
 
  b= lshift(b, 1);
 
2517
  b= lshift(b, 1, &alloc);
2442
2518
  j= cmp(b, S);
2443
2519
  if (j > 0 || (j == 0 && dig & 1))
2444
2520
  {
2458
2534
    s++;
2459
2535
  }
2460
2536
ret:
2461
 
  Bfree(S);
 
2537
  Bfree(S, &alloc);
2462
2538
  if (mhi)
2463
2539
  {
2464
2540
    if (mlo && mlo != mhi)
2465
 
      Bfree(mlo);
2466
 
    Bfree(mhi);
 
2541
      Bfree(mlo, &alloc);
 
2542
    Bfree(mhi, &alloc);
2467
2543
  }
2468
2544
ret1:
2469
 
  Bfree(b);
 
2545
  Bfree(b, &alloc);
2470
2546
  *s= 0;
2471
2547
  *decpt= k + 1;
2472
2548
  if (rve)
2473
2549
    *rve= s;
2474
2550
  return s0;
2475
2551
}
2476
 
 
2477
 
} /* namespace internal */
2478
 
} /* namespace drizzled */