summaryrefslogtreecommitdiff
path: root/strings/decimal.c
diff options
context:
space:
mode:
Diffstat (limited to 'strings/decimal.c')
-rw-r--r--strings/decimal.c91
1 files changed, 46 insertions, 45 deletions
diff --git a/strings/decimal.c b/strings/decimal.c
index 07ccc537e47..979f1b179f9 100644
--- a/strings/decimal.c
+++ b/strings/decimal.c
@@ -357,7 +357,7 @@ int decimal2string(const decimal_t *from, char *to, int *to_len,
if (!(intg_len= fixed_precision ? fixed_intg : intg))
intg_len= 1;
frac_len= fixed_precision ? fixed_decimals : frac;
- len= from->sign + intg_len + test(frac) + frac_len;
+ len= from->sign + intg_len + MY_TEST(frac) + frac_len;
if (fixed_precision)
{
if (frac > fixed_decimals)
@@ -383,7 +383,7 @@ int decimal2string(const decimal_t *from, char *to, int *to_len,
}
else
frac-=j;
- len= from->sign + intg_len + test(frac) + frac_len;
+ len= from->sign + intg_len + MY_TEST(frac) + frac_len;
}
*to_len=len;
s[len]=0;
@@ -400,7 +400,7 @@ int decimal2string(const decimal_t *from, char *to, int *to_len,
for (; frac>0; frac-=DIG_PER_DEC1)
{
dec1 x=*buf++;
- for (i=min(frac, DIG_PER_DEC1); i; i--)
+ for (i=MY_MIN(frac, DIG_PER_DEC1); i; i--)
{
dec1 y=x/DIG_MASK;
*s1++='0'+(uchar)y;
@@ -423,7 +423,7 @@ int decimal2string(const decimal_t *from, char *to, int *to_len,
for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
{
dec1 x=*--buf;
- for (i=min(intg, DIG_PER_DEC1); i; i--)
+ for (i=MY_MIN(intg, DIG_PER_DEC1); i; i--)
{
dec1 y=x/10;
*--s='0'+(uchar)(x-y*10);
@@ -1036,7 +1036,7 @@ int decimal2ulonglong(const decimal_t *from, ulonglong *to)
if (from->sign)
{
- *to=ULL(0);
+ *to= 0;
return E_DEC_OVERFLOW;
}
@@ -1517,8 +1517,8 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
if (to != from)
{
- dec1 *p0= buf0+intg0+max(frac1, frac0);
- dec1 *p1= buf1+intg0+max(frac1, frac0);
+ dec1 *p0= buf0+intg0+MY_MAX(frac1, frac0);
+ dec1 *p1= buf1+intg0+MY_MAX(frac1, frac0);
DBUG_ASSERT(p0 - buf0 <= len);
DBUG_ASSERT(p1 - buf1 <= len);
@@ -1529,7 +1529,7 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
buf0=to->buf;
buf1=to->buf;
to->sign=from->sign;
- to->intg=min(intg0, len)*DIG_PER_DEC1;
+ to->intg=MY_MIN(intg0, len)*DIG_PER_DEC1;
}
if (frac0 > frac1)
@@ -1598,24 +1598,6 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
x+=10;
*buf1=powers10[pos]*(x-y);
}
- /*
- In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
- the buffer are as follows.
-
- Before <1, 5e8>
- After <2, 5e8>
-
- Hence we need to set the 2nd field to 0.
- The same holds if we round 1.5e-9 to 2e-9.
- */
- if (frac0 < frac1)
- {
- dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
- dec1 *end= to->buf + len;
-
- while (buf < end)
- *buf++=0;
- }
if (*buf1 >= DIG_BASE)
{
carry=1;
@@ -1631,12 +1613,13 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
scale=frac0*DIG_PER_DEC1;
error=E_DEC_TRUNCATED; /* XXX */
}
- for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
+ for (buf1=to->buf+intg0+MY_MAX(frac0,0); buf1 > to->buf; buf1--)
{
buf1[0]=buf1[-1];
}
*buf1=1;
to->intg++;
+ intg0++;
}
}
else
@@ -1650,7 +1633,7 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
/* making 'zero' with the proper scale */
dec1 *p0= to->buf + frac0 + 1;
to->intg=1;
- to->frac= max(scale, 0);
+ to->frac= MY_MAX(scale, 0);
to->sign= 0;
for (buf1= to->buf; buf1<p0; buf1++)
*buf1= 0;
@@ -1658,6 +1641,24 @@ decimal_round(const decimal_t *from, decimal_t *to, int scale,
}
}
}
+ /*
+ In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
+ the buffer are as follows.
+
+ Before <1, 5e8>
+ After <2, 5e8>
+
+ Hence we need to set the 2nd field to 0.
+ The same holds if we round 1.5e-9 to 2e-9.
+ */
+ if (frac0 < frac1)
+ {
+ dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
+ dec1 *end= to->buf + len;
+
+ while (buf < end)
+ *buf++=0;
+ }
/* Here we check 999.9 -> 1000 case when we need to increase intg */
first_dig= to->intg % DIG_PER_DEC1;
@@ -1699,11 +1700,11 @@ int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param)
{
switch (op) {
case '-':
- return ROUND_UP(max(from1->intg, from2->intg)) +
- ROUND_UP(max(from1->frac, from2->frac));
+ return ROUND_UP(MY_MAX(from1->intg, from2->intg)) +
+ ROUND_UP(MY_MAX(from1->frac, from2->frac));
case '+':
- return ROUND_UP(max(from1->intg, from2->intg)+1) +
- ROUND_UP(max(from1->frac, from2->frac));
+ return ROUND_UP(MY_MAX(from1->intg, from2->intg)+1) +
+ ROUND_UP(MY_MAX(from1->frac, from2->frac));
case '*':
return ROUND_UP(from1->intg+from2->intg)+
ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
@@ -1718,7 +1719,7 @@ static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
{
int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
- frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
+ frac0=MY_MAX(frac1, frac2), intg0=MY_MAX(intg1, intg2), error;
dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
sanity(to);
@@ -1743,7 +1744,7 @@ static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
buf0=to->buf+intg0+frac0;
to->sign=from1->sign;
- to->frac=max(from1->frac, from2->frac);
+ to->frac=MY_MAX(from1->frac, from2->frac);
to->intg=intg0*DIG_PER_DEC1;
if (unlikely(error))
{
@@ -1754,7 +1755,7 @@ static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
set_if_smaller(intg2, intg0);
}
- /* part 1 - max(frac) ... min (frac) */
+ /* part 1 - MY_MAX(frac) ... min (frac) */
if (frac1 > frac2)
{
buf1=from1->buf+intg1+frac1;
@@ -1772,14 +1773,14 @@ static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
while (buf1 > stop)
*--buf0=*--buf1;
- /* part 2 - min(frac) ... min(intg) */
+ /* part 2 - MY_MIN(frac) ... MY_MIN(intg) */
carry=0;
while (buf1 > stop2)
{
ADD(*--buf0, *--buf1, *--buf2, carry);
}
- /* part 3 - min(intg) ... max(intg) */
+ /* part 3 - MY_MIN(intg) ... MY_MAX(intg) */
buf1= intg1 > intg2 ? ((stop=from1->buf)+intg1-intg2) :
((stop=from2->buf)+intg2-intg1) ;
while (buf1 > stop)
@@ -1800,7 +1801,7 @@ static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
{
int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
- int frac0=max(frac1, frac2), error;
+ int frac0=MY_MAX(frac1, frac2), error;
dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2;
my_bool carry=0;
@@ -1876,7 +1877,7 @@ static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
buf0=to->buf+intg1+frac0;
- to->frac=max(from1->frac, from2->frac);
+ to->frac=MY_MAX(from1->frac, from2->frac);
to->intg=intg1*DIG_PER_DEC1;
if (unlikely(error))
{
@@ -1887,7 +1888,7 @@ static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
}
carry=0;
- /* part 1 - max(frac) ... min (frac) */
+ /* part 1 - MY_MAX(frac) ... min (frac) */
if (frac1 > frac2)
{
buf1=start1+intg1+frac1;
@@ -1911,7 +1912,7 @@ static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
}
}
- /* part 2 - min(frac) ... intg2 */
+ /* part 2 - MY_MIN(frac) ... intg2 */
while (buf2 > start2)
{
SUB(*--buf0, *--buf1, *--buf2, carry);
@@ -2174,11 +2175,11 @@ static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
{
/* we're calculating N1 % N2.
The result will have
- frac=max(frac1, frac2), as for subtraction
+ frac=MY_MAX(frac1, frac2), as for subtraction
intg=intg2
*/
to->sign=from1->sign;
- to->frac=max(from1->frac, from2->frac);
+ to->frac=MY_MAX(from1->frac, from2->frac);
frac0=0;
}
else
@@ -2311,7 +2312,7 @@ static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
/*
now the result is in tmp1, it has
intg=prec1-frac1
- frac=max(frac1, frac2)=to->frac
+ frac=MY_MAX(frac1, frac2)=to->frac
*/
if (dcarry)
*--start1=dcarry;
@@ -2349,7 +2350,7 @@ static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
}
DBUG_ASSERT(intg0 <= ROUND_UP(from2->intg));
stop1=start1+frac0+intg0;
- to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
+ to->intg=MY_MIN(intg0*DIG_PER_DEC1, from2->intg);
}
if (unlikely(intg0+frac0 > to->len))
{