ruby-changes:44686
From: mrkn <ko1@a...>
Date: Sun, 13 Nov 2016 00:43:30 +0900 (JST)
Subject: [ruby-changes:44686] mrkn:r56759 (trunk): rational.c: optimize Integer#lcm
mrkn 2016-11-13 00:43:26 +0900 (Sun, 13 Nov 2016) New Revision: 56759 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56759 Log: rational.c: optimize Integer#lcm * rational.c (f_div, f_mul, f_abs): optimize Integer#lcm Author: Tadashi Saito <tad.a.digger@g...> * numeric.c (rb_int_abs): rename from int_abs to be exported. * internal.h (rb_int_div, rb_int_abs): exported. Modified files: trunk/internal.h trunk/numeric.c trunk/rational.c Index: numeric.c =================================================================== --- numeric.c (revision 56758) +++ numeric.c (revision 56759) @@ -4561,8 +4561,8 @@ fix_abs(VALUE fix) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4561 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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5250 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); Index: internal.h =================================================================== --- internal.h (revision 56758) +++ internal.h (revision 56759) @@ -1179,6 +1179,8 @@ VALUE rb_int_equal(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L1179 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))) Index: rational.c =================================================================== --- rational.c (revision 56758) +++ rational.c (revision 56759) @@ -76,6 +76,8 @@ f_div(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L76 { 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) https://github.com/ruby/ruby/blob/trunk/rational.c#L114 } 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) https://github.com/ruby/ruby/blob/trunk/rational.c#L129 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) https://github.com/ruby/ruby/blob/trunk/rational.c#L373 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)); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/