Skip to content

Commit

Permalink
lightweight digits-width check at numeric_div()
Browse files Browse the repository at this point in the history
  • Loading branch information
kaigai committed Mar 16, 2024
1 parent 59b03a7 commit 01e685a
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/xpu_numeric.cu
Original file line number Diff line number Diff line change
Expand Up @@ -837,29 +837,6 @@ pgfn_numeric_mul(XPU_PGFUNCTION_ARGS)
return true;
}

INLINE_FUNCTION(int)
select_div_scale(int128_t ival)
{
int n_digs = 0;

assert(ival >= 0);
while (ival >= 10000)
{
ival /= 10000;
n_digs += 4;
}
if (ival >= 1000)
n_digs += 4;
else if (ival >= 100)
n_digs += 3;
else if (ival >= 10)
n_digs += 2;
else if (ival >= 1)
n_digs += 1;

return Max(16 - n_digs, 0);
}

PUBLIC_FUNCTION(bool)
pgfn_numeric_div(XPU_PGFUNCTION_ARGS)
{
Expand Down Expand Up @@ -932,7 +909,6 @@ pgfn_numeric_div(XPU_PGFUNCTION_ARGS)
int128_t div = datum_b.u.value;
int128_t x, ival = 0;
int16_t weight = datum_a.weight - datum_b.weight;
int max_loops = -1;
bool negative = false;

if (rem < 0)
Expand All @@ -949,16 +925,15 @@ pgfn_numeric_div(XPU_PGFUNCTION_ARGS)
div = -div;
}
assert(rem >= 0 && div >= 0);

ival = rem / div;
rem -= ival * div;
max_loops = select_div_scale(ival);
for (int loop=0; loop < max_loops; loop++)
for (;;)
{
x = rem / div;
ival = 10 * ival + x;
rem -= x * div;
if (rem == 0)
/*
* 999,999,999,999,999 = 0x03 8D7E A4C6 7FFF
*/
if (rem == 0 || (ival >> 50) != 0)
break;
rem *= 10;
weight++;
Expand Down

0 comments on commit 01e685a

Please sign in to comment.