summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-11 14:39:16 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-11 14:39:16 +0000
commit8ee8c5eeb6fb3c0038f4f5e9a41e6cd4627364f7 (patch)
treeb26f076a8e9cf9cc4d2a9cc2dc8dd6df35c62a3e
parentdae15fa0d4943a636b3678935cad95e5a0fb8637 (diff)
rational.c: optimize Integer#gcd.
* rational.c (f_gcd_normal): optimize Integer#gcd. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--rational.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/rational.c b/rational.c
index 0e06eedfd0..e612ab225c 100644
--- a/rational.c
+++ b/rational.c
@@ -27,6 +27,9 @@
#define GMP_GCD_DIGITS 1
+#define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? ((SIGNED_VALUE)(x) < 0) : BIGNUM_NEGATIVE_P(x))
+#define INT_ZERO_P(x) (FIXNUM_P(x) ? (FIX2LONG(x) == 0) : rb_bigzero_p(x))
+
VALUE rb_cRational;
static ID id_abs, id_cmp, id_convert, id_eqeq_p, id_expt, id_fdiv,
@@ -318,14 +321,14 @@ f_gcd_normal(VALUE x, VALUE y)
if (FIXNUM_P(x) && FIXNUM_P(y))
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
- if (f_negative_p(x))
- x = f_negate(x);
- if (f_negative_p(y))
- y = f_negate(y);
+ if (INT_NEGATIVE_P(x))
+ x = rb_int_uminus(x);
+ if (INT_NEGATIVE_P(y))
+ y = rb_int_uminus(y);
- if (f_zero_p(x))
+ if (INT_ZERO_P(x))
return y;
- if (f_zero_p(y))
+ if (INT_ZERO_P(y))
return x;
for (;;) {
@@ -336,7 +339,7 @@ f_gcd_normal(VALUE x, VALUE y)
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
}
z = x;
- x = f_mod(y, x);
+ x = rb_int_modulo(y, x);
y = z;
}
/* NOTREACHED */