From bdd18a2c313414981aa1f457eec49bf13a8d1048 Mon Sep 17 00:00:00 2001 From: mrkn Date: Sat, 12 Nov 2016 15:43:26 +0000 Subject: rational.c: optimize Integer#lcm * rational.c (f_div, f_mul, f_abs): optimize Integer#lcm Author: Tadashi Saito * 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 --- rational.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'rational.c') 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)); } -- cgit v1.2.3