mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
edfa67c248
commit
bdd18a2c31
3 changed files with 20 additions and 6 deletions
|
@ -1179,6 +1179,8 @@ VALUE rb_int_equal(VALUE x, VALUE y);
|
|||
VALUE rb_int_divmod(VALUE x, VALUE y);
|
||||
VALUE rb_int_and(VALUE x, VALUE y);
|
||||
VALUE rb_int_lshift(VALUE x, VALUE y);
|
||||
VALUE rb_int_div(VALUE x, VALUE y);
|
||||
VALUE rb_int_abs(VALUE num);
|
||||
|
||||
#if USE_FLONUM
|
||||
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
|
||||
|
|
|
@ -4561,8 +4561,8 @@ fix_abs(VALUE fix)
|
|||
return LONG2NUM(i);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
int_abs(VALUE num)
|
||||
VALUE
|
||||
rb_int_abs(VALUE num)
|
||||
{
|
||||
if (FIXNUM_P(num)) {
|
||||
return fix_abs(num);
|
||||
|
@ -5250,8 +5250,8 @@ Init_Numeric(void)
|
|||
rb_define_method(rb_cInteger, "fdiv", rb_int_fdiv, 1);
|
||||
rb_define_method(rb_cInteger, "**", rb_int_pow, 1);
|
||||
|
||||
rb_define_method(rb_cInteger, "abs", int_abs, 0);
|
||||
rb_define_method(rb_cInteger, "magnitude", int_abs, 0);
|
||||
rb_define_method(rb_cInteger, "abs", rb_int_abs, 0);
|
||||
rb_define_method(rb_cInteger, "magnitude", rb_int_abs, 0);
|
||||
|
||||
rb_define_method(rb_cInteger, "===", rb_int_equal, 1);
|
||||
rb_define_method(rb_cInteger, "==", rb_int_equal, 1);
|
||||
|
|
16
rational.c
16
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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue