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 */
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));
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.
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));
216
217
if (str->alloc(length))
217
218
return check_result(mask, E_DEC_OOM);
363
364
#define DIG_MASK 100000000
364
365
#define DIG_BASE 1000000000
365
366
#define DIG_MAX (DIG_BASE-1)
368
inline static T round_up(const T &x)
370
return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1;
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 };
382
377
#define sanity(d) assert((d)->len > 0)
384
379
#define sanity(d) assert((d)->len >0 && ((d)->buf[0] | \
385
380
(d)->buf[(d)->len-1] | 1))
388
inline static void fix_intg_frac_error(const int len, int &intg1, int &frac1, int &error)
390
if (unlikely(intg1+frac1 > len))
392
if (unlikely(intg1 > len))
396
error=E_DEC_OVERFLOW;
401
error=E_DEC_TRUNCATED;
408
/* assume carry <= 1 */
409
inline static void add(dec1 &to, const dec1 &from1, const dec1& from2, dec1 &carry)
411
dec1 a=from1+from2+carry;
413
if ((carry= (a >= DIG_BASE))) /* no division here! */
418
inline static void add2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
420
dec2 a=dec2(from1)+from2+carry;
421
if ((carry= (a >= DIG_BASE)))
423
if (unlikely(a >= DIG_BASE))
432
inline static void sub(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
434
dec1 a=from1-from2-carry;
435
if ((carry= (a < 0)))
441
inline static void sub2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
443
dec1 a=from1-from2-carry;
444
if ((carry= (a < 0)))
383
#define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \
386
if (unlikely(intg1+frac1 > (len))) \
388
if (unlikely(intg1 > (len))) \
392
error=E_DEC_OVERFLOW; \
397
error=E_DEC_TRUNCATED; \
404
#define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \
407
dec1 a=(from1)+(from2)+(carry); \
408
assert((carry) <= 1); \
409
if (((carry)= a >= DIG_BASE)) /* no division here! */ \
414
#define ADD2(to, from1, from2, carry) \
417
dec2 a=((dec2)(from1))+(from2)+(carry); \
418
if (((carry)= a >= DIG_BASE)) \
420
if (unlikely(a >= DIG_BASE)) \
428
#define SUB(to, from1, from2, carry) /* to=from1-from2 */ \
431
dec1 a=(from1)-(from2)-(carry); \
432
if (((carry)= a < 0)) \
437
#define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \
440
dec1 a=(from1)-(from2)-(carry); \
441
if (((carry)= a < 0)) \
443
if (unlikely(a < 0)) \
452
Swap the contents of two variables.
454
#define swap_variables(TYPE, a, b) \
455
465
@brief Get maximum value for given precision and scale
745
755
static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
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);
773
783
static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
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);
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++)
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--)
1660
1670
decimal_round(const decimal_t *from, decimal_t *to, int scale,
1661
1671
decimal_round_mode mode)
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;
1852
1862
static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
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;
1868
1878
to->buf[0]=0; /* safety */
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))
1874
1884
max_decimal(to->len * DIG_PER_DEC1, 0, to);
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)
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;
2000
2010
/* ensure that always from1 > from2 (and intg1 >= intg2) */
2004
swap(start1, start2);
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;
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;
2013
2023
to->frac=max(from1->frac, from2->frac);
2042
2052
while (buf2 > stop2)
2044
sub(*--buf0, 0, *--buf2, carry);
2054
SUB(*--buf0, 0, *--buf2, carry);
2048
2058
/* part 2 - cmin(frac) ... intg2 */
2049
2059
while (buf2 > start2)
2051
sub(*--buf0, *--buf1, *--buf2, carry);
2061
SUB(*--buf0, *--buf1, *--buf2, carry);
2054
2064
/* part 3 - intg2 ... intg1 */
2055
2065
while (carry && buf1 > start1)
2057
sub(*--buf0, *--buf1, 0, carry);
2067
SUB(*--buf0, *--buf1, 0, carry);
2060
2070
while (buf1 > start1)
2128
2138
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
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;
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);
2187
2197
if (buf0 < to->buf)
2188
2198
return E_DEC_OVERFLOW;
2189
add2(*buf0, *buf0, 0, carry);
2199
ADD2(*buf0, *buf0, 0, carry);
2191
2201
for (buf0--; carry; buf0--)
2193
2203
if (buf0 < to->buf)
2194
2204
return E_DEC_OVERFLOW;
2195
add(*buf0, *buf0, 0, carry);
2205
ADD(*buf0, *buf0, 0, carry);
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)
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;
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)
2320
2330
intg = (prec1-frac1) - (prec2-frac2) + 1
2321
2331
prec = intg+frac
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;
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]);