~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/decimal.cc

Merge Gustaf-  Replaced macros with functions/templates

Show diffs side-by-side

added added

removed removed

Lines of Context:
363
363
#define DIG_MASK     100000000
364
364
#define DIG_BASE     1000000000
365
365
#define DIG_MAX      (DIG_BASE-1)
366
 
#define ROUND_UP(X)  (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
 
366
 
 
367
template<typename T> 
 
368
inline static T round_up(const T &x)
 
369
{
 
370
  return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1;
 
371
}
 
372
 
367
373
static const dec1 powers10[DIG_PER_DEC1+1]={
368
374
  1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
369
375
static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
379
385
                              (d)->buf[(d)->len-1] | 1))
380
386
#endif
381
387
 
382
 
#define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error)                   \
383
 
        do                                                              \
384
 
        {                                                               \
385
 
          if (unlikely(intg1+frac1 > (len)))                            \
386
 
          {                                                             \
387
 
            if (unlikely(intg1 > (len)))                                \
388
 
            {                                                           \
389
 
              intg1=(len);                                              \
390
 
              frac1=0;                                                  \
391
 
              error=E_DEC_OVERFLOW;                                     \
392
 
            }                                                           \
393
 
            else                                                        \
394
 
            {                                                           \
395
 
              frac1=(len)-intg1;                                        \
396
 
              error=E_DEC_TRUNCATED;                                    \
397
 
            }                                                           \
398
 
          }                                                             \
399
 
          else                                                          \
400
 
            error=E_DEC_OK;                                             \
401
 
        } while(0)
402
 
 
403
 
#define ADD(to, from1, from2, carry)  /* assume carry <= 1 */           \
404
 
        do                                                              \
405
 
        {                                                               \
406
 
          dec1 a=(from1)+(from2)+(carry);                               \
407
 
          assert((carry) <= 1);                                    \
408
 
          if (((carry)= a >= DIG_BASE)) /* no division here! */         \
409
 
            a-=DIG_BASE;                                                \
410
 
          (to)=a;                                                       \
411
 
        } while(0)
412
 
 
413
 
#define ADD2(to, from1, from2, carry)                                   \
414
 
        do                                                              \
415
 
        {                                                               \
416
 
          dec2 a=((dec2)(from1))+(from2)+(carry);                       \
417
 
          if (((carry)= a >= DIG_BASE))                                 \
418
 
            a-=DIG_BASE;                                                \
419
 
          if (unlikely(a >= DIG_BASE))                                  \
420
 
          {                                                             \
421
 
            a-=DIG_BASE;                                                \
422
 
            carry++;                                                    \
423
 
          }                                                             \
424
 
          (to)=(dec1) a;                                                \
425
 
        } while(0)
426
 
 
427
 
#define SUB(to, from1, from2, carry) /* to=from1-from2 */               \
428
 
        do                                                              \
429
 
        {                                                               \
430
 
          dec1 a=(from1)-(from2)-(carry);                               \
431
 
          if (((carry)= a < 0))                                         \
432
 
            a+=DIG_BASE;                                                \
433
 
          (to)=a;                                                       \
434
 
        } while(0)
435
 
 
436
 
#define SUB2(to, from1, from2, carry) /* to=from1-from2 */              \
437
 
        do                                                              \
438
 
        {                                                               \
439
 
          dec1 a=(from1)-(from2)-(carry);                               \
440
 
          if (((carry)= a < 0))                                         \
441
 
            a+=DIG_BASE;                                                \
442
 
          if (unlikely(a < 0))                                          \
443
 
          {                                                             \
444
 
            a+=DIG_BASE;                                                \
445
 
            carry++;                                                    \
446
 
          }                                                             \
447
 
          (to)=a;                                                       \
448
 
        } while(0)
449
 
 
450
 
/**
451
 
  Swap the contents of two variables.
452
 
 */
453
 
#define swap_variables(TYPE, a, b) \
454
 
  do {                             \
455
 
    TYPE dummy;                    \
456
 
    dummy= a;                      \
457
 
    a= b;                          \
458
 
    b= dummy;                      \
459
 
  } while (0)
460
 
 
461
 
 
 
388
inline static void fix_intg_frac_error(const int len, int &intg1, int &frac1, int &error)
 
389
{
 
390
  if (unlikely(intg1+frac1 > len))
 
391
  {
 
392
    if (unlikely(intg1 > len))
 
393
    {
 
394
      intg1=(len);
 
395
      frac1=0;
 
396
      error=E_DEC_OVERFLOW;
 
397
    }
 
398
    else
 
399
    {
 
400
      frac1=(len)-intg1;
 
401
      error=E_DEC_TRUNCATED;
 
402
    }
 
403
  }
 
404
  else
 
405
    error=E_DEC_OK;
 
406
}
 
407
 
 
408
/* assume carry <= 1 */
 
409
inline static void add(dec1 &to, const dec1 &from1, const dec1& from2, dec1 &carry)
 
410
{
 
411
  dec1 a=from1+from2+carry;
 
412
  assert(carry <= 1);
 
413
  if ((carry= (a >= DIG_BASE))) /* no division here! */
 
414
    a-=DIG_BASE;
 
415
  to=a;
 
416
}
 
417
 
 
418
inline static void add2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
 
419
{
 
420
  dec2 a=dec2(from1)+from2+carry;
 
421
  if ((carry= (a >= DIG_BASE)))
 
422
    a-=DIG_BASE;
 
423
  if (unlikely(a >= DIG_BASE))
 
424
  {
 
425
    a-=DIG_BASE;
 
426
    carry++;
 
427
  }
 
428
  to=dec1(a);
 
429
}
 
430
 
 
431
/* to=from1-from2 */
 
432
inline static void sub(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
 
433
{
 
434
  dec1 a=from1-from2-carry;
 
435
  if ((carry= (a < 0)))
 
436
    a+=DIG_BASE;
 
437
  to=a;
 
438
}
 
439
 
 
440
/* to=from1-from2 */
 
441
inline static void sub2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
 
442
{
 
443
  dec1 a=from1-from2-carry;
 
444
  if ((carry= (a < 0)))
 
445
    a+=DIG_BASE;
 
446
  if (unlikely(a < 0))
 
447
  {
 
448
    a+=DIG_BASE;
 
449
    carry++;
 
450
  }
 
451
  to=a;
 
452
}
462
453
 
463
454
/**
464
455
  @brief  Get maximum value for given precision and scale
527
518
int decimal_actual_fraction(decimal_t *from)
528
519
{
529
520
  int frac= from->frac, i;
530
 
  dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
 
521
  dec1 *buf0= from->buf + round_up(from->intg) + round_up(frac) - 1;
531
522
 
532
523
  if (frac == 0)
533
524
    return 0;
632
623
  {
633
624
    char *s1= s + intg_len;
634
625
    fill= frac_len - frac;
635
 
    buf=buf0+ROUND_UP(intg);
 
626
    buf=buf0+round_up(intg);
636
627
    *s1++='.';
637
628
    for (; frac>0; frac-=DIG_PER_DEC1)
638
629
    {
657
648
  if (intg)
658
649
  {
659
650
    s+=intg;
660
 
    for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
 
651
    for (buf=buf0+round_up(intg); intg>0; intg-=DIG_PER_DEC1)
661
652
    {
662
653
      dec1 x=*--buf;
663
654
      for (i=min(intg, DIG_PER_DEC1); i; i--)
687
678
{
688
679
  int start, stop, i;
689
680
  dec1 *buf_beg= from->buf;
690
 
  dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
 
681
  dec1 *end= from->buf + round_up(from->intg) + round_up(from->frac);
691
682
  dec1 *buf_end= end - 1;
692
683
 
693
684
  /* find non-zero digit from number begining */
753
744
*/
754
745
static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
755
746
{
756
 
  dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
757
 
  dec1 *end= dec->buf + ROUND_UP(last) - 1;
 
747
  dec1 *from= dec->buf + round_up(beg + 1) - 1;
 
748
  dec1 *end= dec->buf + round_up(last) - 1;
758
749
  int c_shift= DIG_PER_DEC1 - shift;
759
750
  assert(from >= dec->buf);
760
751
  assert(end < dec->buf + dec->len);
781
772
*/
782
773
static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
783
774
{
784
 
  dec1 *from= dec->buf + ROUND_UP(last) - 1;
785
 
  dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
 
775
  dec1 *from= dec->buf + round_up(last) - 1;
 
776
  dec1 *end= dec->buf + round_up(beg + 1) - 1;
786
777
  int c_shift= DIG_PER_DEC1 - shift;
787
778
  assert(from < dec->buf + dec->len);
788
779
  assert(end >= dec->buf);
818
809
  /* index of position after last decimal digit */
819
810
  int end;
820
811
  /* index of digit position just after point */
821
 
  int point= ROUND_UP(dec->intg) * DIG_PER_DEC1;
 
812
  int point= round_up(dec->intg) * DIG_PER_DEC1;
822
813
  /* new point position */
823
814
  int new_point= point + shift;
824
815
  /* number of digits in result */
845
836
  digits_frac= end - new_point;
846
837
  set_if_bigger(digits_frac, 0);
847
838
 
848
 
  if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) >
 
839
  if ((new_len= round_up(digits_int) + (new_frac_len= round_up(digits_frac))) >
849
840
      dec->len)
850
841
  {
851
842
    int lack= new_len - dec->len;
938
929
    {
939
930
      /* move left */
940
931
      d_shift= new_front / DIG_PER_DEC1;
941
 
      to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
942
 
      barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
 
932
      to= dec->buf + (round_up(beg + 1) - 1 - d_shift);
 
933
      barier= dec->buf + (round_up(end) - 1 - d_shift);
943
934
      assert(to >= dec->buf);
944
935
      assert(barier + d_shift < dec->buf + dec->len);
945
936
      for(; to <= barier; to++)
952
943
    {
953
944
      /* move right */
954
945
      d_shift= (1 - new_front) / DIG_PER_DEC1;
955
 
      to= dec->buf + ROUND_UP(end) - 1 + d_shift;
956
 
      barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
 
946
      to= dec->buf + round_up(end) - 1 + d_shift;
 
947
      barier= dec->buf + round_up(beg + 1) - 1 + d_shift;
957
948
      assert(to < dec->buf + dec->len);
958
949
      assert(barier - d_shift >= dec->buf);
959
950
      for(; to >= barier; to--)
972
963
 
973
964
    Only one of following 'for' loops will work becouse beg <= end
974
965
  */
975
 
  beg= ROUND_UP(beg + 1) - 1;
976
 
  end= ROUND_UP(end) - 1;
 
966
  beg= round_up(beg + 1) - 1;
 
967
  end= round_up(end) - 1;
977
968
  assert(new_point >= 0);
978
969
 
979
970
  /* We don't want negative new_point below */
980
971
  if (new_point != 0)
981
 
    new_point= ROUND_UP(new_point) - 1;
 
972
    new_point= round_up(new_point) - 1;
982
973
 
983
974
  if (new_point > end)
984
975
  {
1073
1064
      error=E_DEC_OVERFLOW;
1074
1065
      intg=to->intg;
1075
1066
    }
1076
 
    intg1=ROUND_UP(intg);
1077
 
    frac1=ROUND_UP(frac);
 
1067
    intg1=round_up(intg);
 
1068
    frac1=round_up(frac);
1078
1069
    if (intg1+frac1 > to->len)
1079
1070
    {
1080
1071
      error= E_DEC_OOM;
1083
1074
  }
1084
1075
  else
1085
1076
  {
1086
 
    intg1=ROUND_UP(intg);
1087
 
    frac1=ROUND_UP(frac);
1088
 
    FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
 
1077
    intg1=round_up(intg);
 
1078
    frac1=round_up(frac);
 
1079
    fix_intg_frac_error(to->len, intg1, frac1, error);
1089
1080
    if (unlikely(error))
1090
1081
    {
1091
1082
      frac=frac1*DIG_PER_DEC1;
1549
1540
  d_copy[0]^= 0x80;
1550
1541
  from= d_copy;
1551
1542
 
1552
 
  FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
 
1543
  fix_intg_frac_error(to->len, intg1, frac1, error);
1553
1544
  if (unlikely(error))
1554
1545
  {
1555
1546
    if (intg1 < intg0+(intg0x>0))
1669
1660
decimal_round(const decimal_t *from, decimal_t *to, int scale,
1670
1661
              decimal_round_mode mode)
1671
1662
{
1672
 
  int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1,
1673
 
      frac1=ROUND_UP(from->frac), round_digit= 0,
1674
 
      intg0=ROUND_UP(from->intg), error=E_DEC_OK, len=to->len,
1675
 
      intg1=ROUND_UP(from->intg +
 
1663
  int frac0=scale>0 ? round_up(scale) : scale/DIG_PER_DEC1,
 
1664
      frac1=round_up(from->frac), round_digit= 0,
 
1665
      intg0=round_up(from->intg), error=E_DEC_OK, len=to->len,
 
1666
      intg1=round_up(from->intg +
1676
1667
                     (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX)));
1677
1668
  dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
1678
1669
  int first_dig;
1807
1798
    carry=1;
1808
1799
    *buf1-=DIG_BASE;
1809
1800
    while (carry && --buf1 >= to->buf)
1810
 
      ADD(*buf1, *buf1, 0, carry);
 
1801
      add(*buf1, *buf1, 0, carry);
1811
1802
    if (unlikely(carry))
1812
1803
    {
1813
1804
      /* shifting the number to create space for new digit */
1860
1851
 
1861
1852
static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1862
1853
{
1863
 
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1864
 
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
 
1854
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
 
1855
      frac1=round_up(from1->frac), frac2=round_up(from2->frac),
1865
1856
      frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
1866
1857
  dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
1867
1858
 
1877
1868
    to->buf[0]=0; /* safety */
1878
1869
  }
1879
1870
 
1880
 
  FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
 
1871
  fix_intg_frac_error(to->len, intg0, frac0, error);
1881
1872
  if (unlikely(error == E_DEC_OVERFLOW))
1882
1873
  {
1883
1874
    max_decimal(to->len * DIG_PER_DEC1, 0, to);
1920
1911
  carry=0;
1921
1912
  while (buf1 > stop2)
1922
1913
  {
1923
 
    ADD(*--buf0, *--buf1, *--buf2, carry);
 
1914
    add(*--buf0, *--buf1, *--buf2, carry);
1924
1915
  }
1925
1916
 
1926
1917
  /* part 3 - cmin(intg) ... cmax(intg) */
1928
1919
                        ((stop=from2->buf)+intg2-intg1) ;
1929
1920
  while (buf1 > stop)
1930
1921
  {
1931
 
    ADD(*--buf0, *--buf1, 0, carry);
 
1922
    add(*--buf0, *--buf1, 0, carry);
1932
1923
  }
1933
1924
 
1934
1925
  if (unlikely(carry))
1942
1933
   if to==0, return -1/0/+1 - the result of the comparison */
1943
1934
static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1944
1935
{
1945
 
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1946
 
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
 
1936
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
 
1937
      frac1=round_up(from1->frac), frac2=round_up(from2->frac);
1947
1938
  int frac0=max(frac1, frac2), error;
1948
1939
  dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
1949
1940
 
2009
2000
  /* ensure that always from1 > from2 (and intg1 >= intg2) */
2010
2001
  if (carry)
2011
2002
  {
2012
 
    swap_variables(const decimal_t *,from1, from2);
2013
 
    swap_variables(dec1 *,start1, start2);
2014
 
    swap_variables(int,intg1,intg2);
2015
 
    swap_variables(int,frac1,frac2);
 
2003
    swap(from1, from2);
 
2004
    swap(start1, start2);
 
2005
    swap(intg1, intg2);
 
2006
    swap(frac1, frac2);
2016
2007
    to->sign= 1 - to->sign;
2017
2008
  }
2018
2009
 
2019
 
  FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
 
2010
  fix_intg_frac_error(to->len, intg1, frac0, error);
2020
2011
  buf0=to->buf+intg1+frac0;
2021
2012
 
2022
2013
  to->frac=max(from1->frac, from2->frac);
2050
2041
      *--buf0=0;
2051
2042
    while (buf2 > stop2)
2052
2043
    {
2053
 
      SUB(*--buf0, 0, *--buf2, carry);
 
2044
      sub(*--buf0, 0, *--buf2, carry);
2054
2045
    }
2055
2046
  }
2056
2047
 
2057
2048
  /* part 2 - cmin(frac) ... intg2 */
2058
2049
  while (buf2 > start2)
2059
2050
  {
2060
 
    SUB(*--buf0, *--buf1, *--buf2, carry);
 
2051
    sub(*--buf0, *--buf1, *--buf2, carry);
2061
2052
  }
2062
2053
 
2063
2054
  /* part 3 - intg2 ... intg1 */
2064
2055
  while (carry && buf1 > start1)
2065
2056
  {
2066
 
    SUB(*--buf0, *--buf1, 0, carry);
 
2057
    sub(*--buf0, *--buf1, 0, carry);
2067
2058
  }
2068
2059
 
2069
2060
  while (buf1 > start1)
2107
2098
int decimal_is_zero(const decimal_t *from)
2108
2099
{
2109
2100
  dec1 *buf1=from->buf,
2110
 
       *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac);
 
2101
       *end=buf1+round_up(from->intg)+round_up(from->frac);
2111
2102
  while (buf1 < end)
2112
2103
    if (*buf1++)
2113
2104
      return 0;
2136
2127
*/
2137
2128
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2138
2129
{
2139
 
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
2140
 
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
2141
 
      intg0=ROUND_UP(from1->intg+from2->intg),
 
2130
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
 
2131
      frac1=round_up(from1->frac), frac2=round_up(from2->frac),
 
2132
      intg0=round_up(from1->intg+from2->intg),
2142
2133
      frac0=frac1+frac2, error, i, j, d_to_move;
2143
2134
  dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
2144
2135
       *start2, *stop2, *stop1, *start0, carry;
2147
2138
 
2148
2139
  i=intg0;
2149
2140
  j=frac0;
2150
 
  FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
 
2141
  fix_intg_frac_error(to->len, intg0, frac0, error);
2151
2142
  to->sign=from1->sign != from2->sign;
2152
2143
  to->frac=from1->frac+from2->frac;
2153
2144
  to->intg=intg0*DIG_PER_DEC1;
2188
2179
      dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
2189
2180
      hi=(dec1)(p/DIG_BASE);
2190
2181
      lo=(dec1)(p-((dec2)hi)*DIG_BASE);
2191
 
      ADD2(*buf0, *buf0, lo, carry);
 
2182
      add2(*buf0, *buf0, lo, carry);
2192
2183
      carry+=hi;
2193
2184
    }
2194
2185
    if (carry)
2195
2186
    {
2196
2187
      if (buf0 < to->buf)
2197
2188
        return E_DEC_OVERFLOW;
2198
 
      ADD2(*buf0, *buf0, 0, carry);
 
2189
      add2(*buf0, *buf0, 0, carry);
2199
2190
    }
2200
2191
    for (buf0--; carry; buf0--)
2201
2192
    {
2202
2193
      if (buf0 < to->buf)
2203
2194
        return E_DEC_OVERFLOW;
2204
 
      ADD(*buf0, *buf0, 0, carry);
 
2195
      add(*buf0, *buf0, 0, carry);
2205
2196
    }
2206
2197
  }
2207
2198
 
2224
2215
    }
2225
2216
  }
2226
2217
  buf1= to->buf;
2227
 
  d_to_move= intg0 + ROUND_UP(to->frac);
 
2218
  d_to_move= intg0 + round_up(to->frac);
2228
2219
  while (!*buf1 && (to->intg > DIG_PER_DEC1))
2229
2220
  {
2230
2221
    buf1++;
2254
2245
static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
2255
2246
                       decimal_t *to, decimal_t *mod, int scale_incr)
2256
2247
{
2257
 
  int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
2258
 
      frac2=ROUND_UP(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
 
2248
  int frac1=round_up(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
 
2249
      frac2=round_up(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
2259
2250
      error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
2260
2251
  dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
2261
2252
       *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
2305
2296
    intg0=0;
2306
2297
  }
2307
2298
  else
2308
 
    intg0=ROUND_UP(dintg);
 
2299
    intg0=round_up(dintg);
2309
2300
  if (mod)
2310
2301
  {
2311
2302
    /* we're calculating N1 % N2.
2324
2315
      N2 is in the buf2, has prec2 digits. Scales are frac1 and
2325
2316
      frac2 accordingly.
2326
2317
      Thus, the result will have
2327
 
         frac = ROUND_UP(frac1+frac2+scale_incr)
 
2318
         frac = round_up(frac1+frac2+scale_incr)
2328
2319
      and
2329
2320
         intg = (prec1-frac1) - (prec2-frac2) + 1
2330
2321
         prec = intg+frac
2331
2322
    */
2332
 
    frac0=ROUND_UP(frac1+frac2+scale_incr);
2333
 
    FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
 
2323
    frac0=round_up(frac1+frac2+scale_incr);
 
2324
    fix_intg_frac_error(to->len, intg0, frac0, error);
2334
2325
    to->sign=from1->sign != from2->sign;
2335
2326
    to->intg=intg0*DIG_PER_DEC1;
2336
2327
    to->frac=frac0*DIG_PER_DEC1;
2341
2332
    while (dintg++ < 0)
2342
2333
      *buf0++=0;
2343
2334
 
2344
 
  len1=(i=ROUND_UP(prec1))+ROUND_UP(2*frac2+scale_incr+1) + 1;
 
2335
  len1=(i=round_up(prec1))+round_up(2*frac2+scale_incr+1) + 1;
2345
2336
  set_if_bigger(len1, 3);
2346
2337
  if (!(tmp1=(dec1 *)alloca(len1*sizeof(dec1))))
2347
2338
    return E_DEC_OOM;
2351
2342
  start1=tmp1;
2352
2343
  stop1=start1+len1;
2353
2344
  start2=buf2;
2354
 
  stop2=buf2+ROUND_UP(prec2)-1;
 
2345
  stop2=buf2+round_up(prec2)-1;
2355
2346
 
2356
2347
  /* removing end zeroes */
2357
2348
  while (*stop2 == 0 && stop2 >= start2)
2410
2401
        x=guess * (*--buf2);
2411
2402
        hi=(dec1)(x/DIG_BASE);
2412
2403
        lo=(dec1)(x-((dec2)hi)*DIG_BASE);
2413
 
        SUB2(*buf1, *buf1, lo, carry);
 
2404
        sub2(*buf1, *buf1, lo, carry);
2414
2405
        carry+=hi;
2415
2406
      }
2416
2407
      carry= dcarry < carry;
2424
2415
        buf1=start1+len2;
2425
2416
        for (carry=0; buf2 > start2; buf1--)
2426
2417
        {
2427
 
          ADD(*buf1, *buf1, *--buf2, carry);
 
2418
          add(*buf1, *buf1, *--buf2, carry);
2428
2419
        }
2429
2420
      }
2430
2421
    }
2443
2434
    if (dcarry)
2444
2435
      *--start1=dcarry;
2445
2436
    buf0=to->buf;
2446
 
    intg0=(int) (ROUND_UP(prec1-frac1)-(start1-tmp1));
2447
 
    frac0=ROUND_UP(to->frac);
 
2437
    intg0=(int) (round_up(prec1-frac1)-(start1-tmp1));
 
2438
    frac0=round_up(to->frac);
2448
2439
    error=E_DEC_OK;
2449
2440
    if (unlikely(frac0==0 && intg0==0))
2450
2441
    {
2474
2465
        error=E_DEC_OVERFLOW;
2475
2466
        goto done;
2476
2467
      }
2477
 
      assert(intg0 <= ROUND_UP(from2->intg));
 
2468
      assert(intg0 <= round_up(from2->intg));
2478
2469
      stop1=start1+frac0+intg0;
2479
2470
      to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
2480
2471
    }
2554
2545
{
2555
2546
  int i;
2556
2547
  printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
2557
 
  for (i=0; i < ROUND_UP(d->frac)+ROUND_UP(d->intg)-1; i++)
 
2548
  for (i=0; i < round_up(d->frac)+round_up(d->intg)-1; i++)
2558
2549
    printf("%09d, ", d->buf[i]);
2559
2550
  printf("%09d} */ ", d->buf[i]);
2560
2551
}