Permalink
Browse files

use mrb_int_mul_overflow()

  • Loading branch information...
cremno committed May 10, 2016
1 parent 6369374 commit 7453a5dfea75e696122ba7895bca76e6ca013d23
Showing with 10 additions and 28 deletions.
  1. +4 −12 src/numeric.c
  2. +6 −16 src/vm.c
View
@@ -541,29 +541,21 @@ int_to_i(mrb_state *mrb, mrb_value num)
return num;
}
/*tests if N*N would overflow*/
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1-MRB_FIXNUM_SHIFT)/2))
#define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
mrb_value
mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y)
{
mrb_int a;
a = mrb_fixnum(x);
if (mrb_fixnum_p(y)) {
mrb_float c;
mrb_int b;
mrb_int b, c;
if (a == 0) return x;
b = mrb_fixnum(y);
if (FIT_SQRT_INT(a) && FIT_SQRT_INT(b))
return mrb_fixnum_value(a*b);
c = a * b;
if ((a != 0 && c/a != b) || !FIXABLE(c)) {
return mrb_float_value(mrb, (mrb_float)a*(mrb_float)b);
if (mrb_int_mul_overflow(a, b, &c)) {
return mrb_float_value(mrb, (mrb_float)a * (mrb_float)b);
}
return mrb_fixnum_value((mrb_int)c);
return mrb_fixnum_value(c);
}
return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y));
}
View
@@ -1862,25 +1862,15 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM):
{
mrb_value z;
z = mrb_fixnum_mul(mrb, regs[a], regs[a+1]);
mrb_int x, y, z;
switch (mrb_type(z)) {
case MRB_TT_FIXNUM:
{
SET_INT_VALUE(regs[a], mrb_fixnum(z));
}
break;
case MRB_TT_FLOAT:
{
SET_FLOAT_VALUE(mrb, regs[a], mrb_float(z));
}
break;
default:
/* cannot happen */
x = mrb_fixnum(regs[a]);
y = mrb_fixnum(regs[a+1]);
if (mrb_int_mul_overflow(x, y, &z)) {
SET_FLOAT_VALUE(mrb, regs[a], (mrb_float)x * (mrb_float)y);
break;
}
SET_INT_VALUE(regs[a], z);
}
break;
case TYPES2(MRB_TT_FIXNUM,MRB_TT_FLOAT):

0 comments on commit 7453a5d

Please sign in to comment.