diff --git a/ChangeLog b/ChangeLog index 18f090a582..9fc27dae12 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Apr 22 22:54:00 2013 Charlie Somerville + + * insns.def (opt_mod): Use % operator if both operands are positive for + a significant performance improvement. Thanks to @samsaffron. + Mon Apr 22 17:09:37 2013 Nobuyoshi Nakada * marshal.c (r_object0): copy all instance variables not only generic diff --git a/insns.def b/insns.def index fc4db92d4f..b51e60018c 100644 --- a/insns.def +++ b/insns.def @@ -1524,13 +1524,15 @@ opt_mod { if (FIXNUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_MOD, FIXNUM_REDEFINED_OP_FLAG )) { - long x, y, mod; + long x, y; x = FIX2LONG(recv); y = FIX2LONG(obj); - { + if (x > 0 && y > 0) { + val = LONG2FIX(x % y); + } else { /* copied from numeric.c#fixdivmod */ - long div; + long div, mod; if (y == 0) rb_num_zerodiv(); @@ -1551,8 +1553,8 @@ opt_mod mod += y; div -= 1; } + val = LONG2FIX(mod); } - val = LONG2FIX(mod); } else if (FLONUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_MOD, FLOAT_REDEFINED_OP_FLAG)) {