aboutsummaryrefslogtreecommitdiffstats
path: root/rational.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-12 15:43:26 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-12 15:43:26 +0000
commit14826021e222b5a55435d1e16951f43f656989cb (patch)
treee3ca93b0ce2a1dbd69ca5a93abee9ec6debf0de3 /rational.c
parent69a446c875c63e451d604f3546e2207322c70567 (diff)
downloadruby-14826021e222b5a55435d1e16951f43f656989cb.tar.gz
rational.c: optimize Integer#lcm
* rational.c (f_div, f_mul, f_abs): optimize Integer#lcm Author: Tadashi Saito <tad.a.digger@gmail.com> * 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.c16
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));
}