diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 15:43:26 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 15:43:26 +0000 |
commit | bdd18a2c313414981aa1f457eec49bf13a8d1048 (patch) | |
tree | e3ca93b0ce2a1dbd69ca5a93abee9ec6debf0de3 /rational.c | |
parent | edfa67c2486ec18d6fe74f0786ef0a2f08faff09 (diff) |
rational.c: optimize Integer#lcm
* rational.c (f_div, f_mul, f_abs): optimize Integer#lcm
Author: Tadashi Saito <[email protected]>
* numeric.c (rb_int_abs): rename from int_abs to be exported.
* internal.h (rb_int_div, rb_int_abs): exported.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56759 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/rational.c b/rational.c index a1e06a2880..f78017e467 100644 --- a/rational.c +++ b/rational.c @@ -76,6 +76,8 @@ f_div(VALUE x, VALUE y) { if (FIXNUM_P(y) && FIX2LONG(y) == 1) return x; + if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM)) + return rb_int_div(x, y); return rb_funcall(x, '/', 1, y); } @@ -112,7 +114,10 @@ f_mul(VALUE x, VALUE y) } else if (ix == 1) return y; + return rb_int_mul(x, y); } + else if (RB_TYPE_P(x, T_BIGNUM)) + return rb_int_mul(x, y); return rb_funcall(x, '*', 1, y); } @@ -124,7 +129,14 @@ f_sub(VALUE x, VALUE y) return rb_funcall(x, '-', 1, y); } -fun1(abs) +inline static VALUE +f_abs(VALUE x) +{ + if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM)) + return rb_int_abs(x); + return rb_funcall(x, id_abs, 0); +} + fun1(integer_p) fun1(negate) @@ -361,7 +373,7 @@ f_gcd(VALUE x, VALUE y) inline static VALUE f_lcm(VALUE x, VALUE y) { - if (f_zero_p(x) || f_zero_p(y)) + if (INT_ZERO_P(x) || INT_ZERO_P(y)) return ZERO; return f_abs(f_mul(f_div(x, f_gcd(x, y)), y)); } |