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:
parent
dae15fa0d4
commit
8ee8c5eeb6
1 changed files with 10 additions and 7 deletions
17
rational.c
17
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 */
|
||||
|
|
Loading…
Reference in a new issue