mruby-bigint: add fast path to mrb_bint_mul() & mrb_bint_div()

This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-08-14 22:36:47 +09:00
parent 16f5000794
commit e9c4486e27
+8 -3
View File
@@ -1327,6 +1327,10 @@ mrb_bint_sub(mrb_state *mrb, mrb_value x, mrb_value y)
mrb_value
mrb_bint_mul(mrb_state *mrb, mrb_value x, mrb_value y)
{
if (mrb_integer_p(y)) {
if (mrb_integer(y) == 0) return mrb_fixnum_value(0);
if (mrb_integer(y) == 1) return bint_norm(mrb, RBIGINT(x));
}
#ifndef MRB_NO_FLOAT
if (mrb_float_p(y)) {
mrb_float v1 = mrb_bint_as_float(mrb, x);
@@ -1345,6 +1349,10 @@ mrb_bint_mul(mrb_state *mrb, mrb_value x, mrb_value y)
mrb_value
mrb_bint_div(mrb_state *mrb, mrb_value x, mrb_value y)
{
if (mrb_integer_p(y)) {
if (mrb_integer(y) == 0) mrb_int_zerodiv(mrb);
if (mrb_integer(y) == 1) return bint_norm(mrb, RBIGINT(x));
}
#ifndef MRB_NO_FLOAT
if (mrb_float_p(y)) {
mrb_float v1 = mrb_bint_as_float(mrb, x);
@@ -1352,9 +1360,6 @@ mrb_bint_div(mrb_state *mrb, mrb_value x, mrb_value y)
return mrb_float_value(mrb,v1*v2);
}
#endif
if (mrb_integer_p(y) && mrb_integer(y) == 0) {
mrb_int_zerodiv(mrb);
}
y = mrb_as_bint(mrb, y);
struct RBigint *b = RBIGINT(x);
struct RBigint *b2 = RBIGINT(y);