~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/decimal.cc

  • Committer: Andrew Hutchings
  • Date: 2010-09-26 08:33:47 UTC
  • mto: (1795.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1796.
  • Revision ID: andrew@linuxjedi.co.uk-20100926083347-fzsc9e7w0j4u7bj1
Disable boost:po allow_guessing which was making some wrong assumptions

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