1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
mrkn 2016-11-11 14:39:16 +00:00
parent dae15fa0d4
commit 8ee8c5eeb6

View file

@ -27,6 +27,9 @@
#define GMP_GCD_DIGITS 1 #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; VALUE rb_cRational;
static ID id_abs, id_cmp, id_convert, id_eqeq_p, id_expt, id_fdiv, 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)) if (FIXNUM_P(x) && FIXNUM_P(y))
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y))); return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
if (f_negative_p(x)) if (INT_NEGATIVE_P(x))
x = f_negate(x); x = rb_int_uminus(x);
if (f_negative_p(y)) if (INT_NEGATIVE_P(y))
y = f_negate(y); y = rb_int_uminus(y);
if (f_zero_p(x)) if (INT_ZERO_P(x))
return y; return y;
if (f_zero_p(y)) if (INT_ZERO_P(y))
return x; return x;
for (;;) { for (;;) {
@ -336,7 +339,7 @@ f_gcd_normal(VALUE x, VALUE y)
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y))); return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
} }
z = x; z = x;
x = f_mod(y, x); x = rb_int_modulo(y, x);
y = z; y = z;
} }
/* NOTREACHED */ /* NOTREACHED */