~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to strings/decimal.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 19:11:14 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717191114-gzmhr3vgsr01fslw
Removal of DBUG from strings/ library

Show diffs side-by-side

added added

removed removed

Lines of Context:
139
139
  999999900, 999999990 };
140
140
 
141
141
#ifdef HAVE_purify
142
 
#define sanity(d) DBUG_ASSERT((d)->len > 0)
 
142
#define sanity(d) assert((d)->len > 0)
143
143
#else
144
 
#define sanity(d) DBUG_ASSERT((d)->len >0 && ((d)->buf[0] | \
 
144
#define sanity(d) assert((d)->len >0 && ((d)->buf[0] | \
145
145
                              (d)->buf[(d)->len-1] | 1))
146
146
#endif
147
147
 
170
170
        do                                                              \
171
171
        {                                                               \
172
172
          dec1 a=(from1)+(from2)+(carry);                               \
173
 
          DBUG_ASSERT((carry) <= 1);                                    \
 
173
          assert((carry) <= 1);                                    \
174
174
          if (((carry)= a >= DIG_BASE)) /* no division here! */         \
175
175
            a-=DIG_BASE;                                                \
176
176
          (to)=a;                                                       \
227
227
{
228
228
  int intpart;
229
229
  dec1 *buf= to->buf;
230
 
  DBUG_ASSERT(precision && precision >= frac);
 
230
  assert(precision && precision >= frac);
231
231
 
232
232
  to->sign= 0;
233
233
  if ((intpart= to->intg= (precision - frac)))
264
264
  if (intg > 0)
265
265
  {
266
266
    for (i= (intg - 1) % DIG_PER_DEC1; *buf0 < powers10[i--]; intg--) ;
267
 
    DBUG_ASSERT(intg > 0);
 
267
    assert(intg > 0);
268
268
  }
269
269
  else
270
270
    intg=0;
338
338
  char *s=to;
339
339
  dec1 *buf, *buf0=from->buf, tmp;
340
340
 
341
 
  DBUG_ASSERT(*to_len >= 2+from->sign);
 
341
  assert(*to_len >= 2+from->sign);
342
342
 
343
343
  /* removing leading zeroes */
344
344
  buf0= remove_leading_zeroes(from, &intg);
516
516
  dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
517
517
  dec1 *end= dec->buf + ROUND_UP(last) - 1;
518
518
  int c_shift= DIG_PER_DEC1 - shift;
519
 
  DBUG_ASSERT(from >= dec->buf);
520
 
  DBUG_ASSERT(end < dec->buf + dec->len);
 
519
  assert(from >= dec->buf);
 
520
  assert(end < dec->buf + dec->len);
521
521
  if (beg % DIG_PER_DEC1 < shift)
522
522
    *(from - 1)= (*from) / powers10[c_shift];
523
523
  for(; from < end; from++)
546
546
  dec1 *from= dec->buf + ROUND_UP(last) - 1;
547
547
  dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
548
548
  int c_shift= DIG_PER_DEC1 - shift;
549
 
  DBUG_ASSERT(from < dec->buf + dec->len);
550
 
  DBUG_ASSERT(end >= dec->buf);
 
549
  assert(from < dec->buf + dec->len);
 
550
  assert(end >= dec->buf);
551
551
  if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
552
552
    *(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
553
553
  for(; from > end; from--)
655
655
        result
656
656
      */
657
657
      do_left= l_mini_shift <= beg;
658
 
      DBUG_ASSERT(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
 
658
      assert(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
659
659
    }
660
660
    else
661
661
    {
663
663
      l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
664
664
      /* see comment above */
665
665
      do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
666
 
      DBUG_ASSERT(!do_left || l_mini_shift <= beg);
 
666
      assert(!do_left || l_mini_shift <= beg);
667
667
    }
668
668
    if (do_left)
669
669
    {
703
703
      d_shift= new_front / DIG_PER_DEC1;
704
704
      to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
705
705
      barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
706
 
      DBUG_ASSERT(to >= dec->buf);
707
 
      DBUG_ASSERT(barier + d_shift < dec->buf + dec->len);
 
706
      assert(to >= dec->buf);
 
707
      assert(barier + d_shift < dec->buf + dec->len);
708
708
      for(; to <= barier; to++)
709
709
        *to= *(to + d_shift);
710
710
      for(barier+= d_shift; to <= barier; to++)
717
717
      d_shift= (1 - new_front) / DIG_PER_DEC1;
718
718
      to= dec->buf + ROUND_UP(end) - 1 + d_shift;
719
719
      barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
720
 
      DBUG_ASSERT(to < dec->buf + dec->len);
721
 
      DBUG_ASSERT(barier - d_shift >= dec->buf);
 
720
      assert(to < dec->buf + dec->len);
 
721
      assert(barier - d_shift >= dec->buf);
722
722
      for(; to >= barier; to--)
723
723
        *to= *(to - d_shift);
724
724
      for(barier-= d_shift; to >= barier; to--)
737
737
  */
738
738
  beg= ROUND_UP(beg + 1) - 1;
739
739
  end= ROUND_UP(end) - 1;
740
 
  DBUG_ASSERT(new_point >= 0);
 
740
  assert(new_point >= 0);
741
741
  
742
742
  /* We don't want negative new_point below */
743
743
  if (new_point != 0)
951
951
  rc = decimal2string(from, strbuf, &len, 0, 0, 0);
952
952
  end= strbuf + len;
953
953
  
954
 
  DBUG_PRINT("info", ("interm.: %s", strbuf));
955
 
 
956
954
  *to= my_strtod(strbuf, &end, &error);
957
955
             
958
 
  DBUG_PRINT("info", ("result: %f (%lx)", *to, *(ulong *)to));
959
 
 
960
956
  return (rc != E_DEC_OK) ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
961
957
}
962
958
 
976
972
{
977
973
  char buff[FLOATING_POINT_BUFFER], *end;
978
974
  int res;
979
 
  DBUG_ENTER("double2decimal");
980
975
  end= buff + my_gcvt(from, MY_GCVT_ARG_DOUBLE, sizeof(buff) - 1, buff, NULL);
981
976
  res= string2decimal(buff, to, &end);
982
 
  DBUG_PRINT("exit", ("res: %d", res));
983
 
  DBUG_RETURN(res);
 
977
  return(res);
984
978
}
985
979
 
986
980
 
1243
1237
      case 2: mi_int2store(to, x); break;
1244
1238
      case 3: mi_int3store(to, x); break;
1245
1239
      case 4: mi_int4store(to, x); break;
1246
 
      default: DBUG_ASSERT(0);
 
1240
      default: assert(0);
1247
1241
    }
1248
1242
    to+=i;
1249
1243
  }
1252
1246
  for (stop1=buf1+intg1+frac1; buf1 < stop1; to+=sizeof(dec1))
1253
1247
  {
1254
1248
    dec1 x=*buf1++ ^ mask;
1255
 
    DBUG_ASSERT(sizeof(dec1) == 4);
 
1249
    assert(sizeof(dec1) == 4);
1256
1250
    mi_int4store(to, x);
1257
1251
  }
1258
1252
 
1271
1265
      case 2: mi_int2store(to, x); break;
1272
1266
      case 3: mi_int3store(to, x); break;
1273
1267
      case 4: mi_int4store(to, x); break;
1274
 
      default: DBUG_ASSERT(0);
 
1268
      default: assert(0);
1275
1269
    }
1276
1270
    to+=i;
1277
1271
  }
1285
1279
  orig_to[0]^= 0x80;
1286
1280
 
1287
1281
  /* Check that we have written the whole decimal and nothing more */
1288
 
  DBUG_ASSERT(to == orig_to + orig_fsize0 + orig_isize0);
 
1282
  assert(to == orig_to + orig_fsize0 + orig_isize0);
1289
1283
  return error;
1290
1284
}
1291
1285
 
1353
1347
      case 2: x=mi_sint2korr(from); break;
1354
1348
      case 3: x=mi_sint3korr(from); break;
1355
1349
      case 4: x=mi_sint4korr(from); break;
1356
 
      default: DBUG_ASSERT(0);
 
1350
      default: assert(0);
1357
1351
    }
1358
1352
    from+=i;
1359
1353
    *buf=x ^ mask;
1366
1360
  }
1367
1361
  for (stop=from+intg0*sizeof(dec1); from < stop; from+=sizeof(dec1))
1368
1362
  {
1369
 
    DBUG_ASSERT(sizeof(dec1) == 4);
 
1363
    assert(sizeof(dec1) == 4);
1370
1364
    *buf=mi_sint4korr(from) ^ mask;
1371
1365
    if (((uint32)*buf) > DIG_MAX)
1372
1366
      goto err;
1375
1369
    else
1376
1370
      to->intg-=DIG_PER_DEC1;
1377
1371
  }
1378
 
  DBUG_ASSERT(to->intg >=0);
 
1372
  assert(to->intg >=0);
1379
1373
  for (stop=from+frac0*sizeof(dec1); from < stop; from+=sizeof(dec1))
1380
1374
  {
1381
 
    DBUG_ASSERT(sizeof(dec1) == 4);
 
1375
    assert(sizeof(dec1) == 4);
1382
1376
    *buf=mi_sint4korr(from) ^ mask;
1383
1377
    if (((uint32)*buf) > DIG_MAX)
1384
1378
      goto err;
1394
1388
      case 2: x=mi_sint2korr(from); break;
1395
1389
      case 3: x=mi_sint3korr(from); break;
1396
1390
      case 4: x=mi_sint4korr(from); break;
1397
 
      default: DBUG_ASSERT(0);
 
1391
      default: assert(0);
1398
1392
    }
1399
1393
    *buf=(x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
1400
1394
    if (((uint32)*buf) > DIG_MAX)
1420
1414
 
1421
1415
int decimal_size(int precision, int scale)
1422
1416
{
1423
 
  DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
 
1417
  assert(scale >= 0 && precision > 0 && scale <= precision);
1424
1418
  return ROUND_UP(precision-scale)+ROUND_UP(scale);
1425
1419
}
1426
1420
 
1437
1431
      intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
1438
1432
      intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1;
1439
1433
 
1440
 
  DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
 
1434
  assert(scale >= 0 && precision > 0 && scale <= precision);
1441
1435
  return intg0*sizeof(dec1)+dig2bytes[intg0x]+
1442
1436
         frac0*sizeof(dec1)+dig2bytes[frac0x];
1443
1437
}
1480
1474
  case CEILING:         round_digit= from->sign ? 10 : 0; break;
1481
1475
  case FLOOR:           round_digit= from->sign ? 0 : 10; break;
1482
1476
  case TRUNCATE:        round_digit=10; break;
1483
 
  default: DBUG_ASSERT(0);
 
1477
  default: assert(0);
1484
1478
  }
1485
1479
 
1486
1480
  if (unlikely(frac0+intg0 > len))
1529
1523
  if (scale == frac0*DIG_PER_DEC1)
1530
1524
  {
1531
1525
    int do_inc= false;
1532
 
    DBUG_ASSERT(frac0+intg0 >= 0);
 
1526
    assert(frac0+intg0 >= 0);
1533
1527
    switch (round_digit) {
1534
1528
    case 0:
1535
1529
    {
1571
1565
  {
1572
1566
    /* TODO - fix this code as it won't work for CEILING mode */
1573
1567
    int pos=frac0*DIG_PER_DEC1-scale-1;
1574
 
    DBUG_ASSERT(frac0+intg0 > 0);
 
1568
    assert(frac0+intg0 > 0);
1575
1569
    x=*buf1 / powers10[pos];
1576
1570
    y=x % 10;
1577
1571
    if (y > round_digit ||
1690
1684
           ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
1691
1685
  case '/':
1692
1686
    return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param);
1693
 
  default: DBUG_ASSERT(0);
 
1687
  default: assert(0);
1694
1688
  }
1695
1689
  return -1; /* shut up the warning */
1696
1690
}
1770
1764
 
1771
1765
  if (unlikely(carry))
1772
1766
    *--buf0=1;
1773
 
  DBUG_ASSERT(buf0 == to->buf || buf0 == to->buf+1);
 
1767
  assert(buf0 == to->buf || buf0 == to->buf+1);
1774
1768
 
1775
1769
  return error;
1776
1770
}
2048
2042
  {
2049
2043
    dec1 *buf= to->buf;
2050
2044
    dec1 *end= to->buf + intg0 + frac0;
2051
 
    DBUG_ASSERT(buf != end);
 
2045
    assert(buf != end);
2052
2046
    for (;;)
2053
2047
    {
2054
2048
      if (*buf)
2114
2108
  if (prec2 <= 0) /* short-circuit everything: from2 == 0 */
2115
2109
    return E_DEC_DIV_ZERO;
2116
2110
  for (i= (prec2 - 1) % DIG_PER_DEC1; *buf2 < powers10[i--]; prec2--) ;
2117
 
  DBUG_ASSERT(prec2 > 0);
 
2111
  assert(prec2 > 0);
2118
2112
 
2119
2113
  i=((prec1-1) % DIG_PER_DEC1)+1;
2120
2114
  while (prec1 > 0 && *buf1 == 0)
2129
2123
    return E_DEC_OK;
2130
2124
  }
2131
2125
  for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
2132
 
  DBUG_ASSERT(prec1 > 0);
 
2126
  assert(prec1 > 0);
2133
2127
 
2134
2128
  /* let's fix scale_incr, taking into account frac1,frac2 increase */
2135
2129
  if ((scale_incr-= frac1 - from1->frac + frac2 - from2->frac) < 0)
2234
2228
          guess--;
2235
2229
        if (unlikely(start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y))
2236
2230
          guess--;
2237
 
        DBUG_ASSERT(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
 
2231
        assert(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
2238
2232
      }
2239
2233
 
2240
2234
      /* D4: multiply and subtract */
2241
2235
      buf2=stop2;
2242
2236
      buf1=start1+len2;
2243
 
      DBUG_ASSERT(buf1 < stop1);
 
2237
      assert(buf1 < stop1);
2244
2238
      for (carry=0; buf2 > start2; buf1--)
2245
2239
      {
2246
2240
        dec1 hi, lo;
2311
2305
        error=E_DEC_OVERFLOW;
2312
2306
        goto done;
2313
2307
      }
2314
 
      DBUG_ASSERT(intg0 <= ROUND_UP(from2->intg));
 
2308
      assert(intg0 <= ROUND_UP(from2->intg));
2315
2309
      stop1=start1+frac0+intg0;
2316
2310
      to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
2317
2311
    }
2322
2316
      to->frac=frac0*DIG_PER_DEC1;
2323
2317
        error=E_DEC_TRUNCATED;
2324
2318
    }
2325
 
    DBUG_ASSERT(buf0 + (stop1 - start1) <= to->buf + to->len);
 
2319
    assert(buf0 + (stop1 - start1) <= to->buf + to->len);
2326
2320
    while (start1 < stop1)
2327
2321
        *buf0++=*start1++;
2328
2322
  }