diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 01:29:01 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 01:29:01 +0000 |
commit | c93faa14f5d157d8e15349c0fe7b9c481140af22 (patch) | |
tree | 36e2a3494e07bd185429b0f523c3bfd36a69fb7d | |
parent | f7d258ba14a58e774a967bb8b0c095b47302c2f5 (diff) | |
download | ruby-c93faa14f5d157d8e15349c0fe7b9c481140af22.tar.gz |
rational.c: optimize Rational#<=>
* rational.c (nurat_cmp): optimize Rational#<=>.
Author: Tadashi Saito <tad.a.digger@gmail.com>
* numeric.c (rb_int_cmp): rename from int_cmp and remove static
to be exported.
* internal.h (rb_int_cmp): exported.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56733 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 9 | ||||
-rw-r--r-- | rational.c | 18 |
3 files changed, 15 insertions, 13 deletions
diff --git a/internal.h b/internal.h index 067d35885c..4a1c181c9a 100644 --- a/internal.h +++ b/internal.h @@ -1173,6 +1173,7 @@ enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts); double rb_int_fdiv_double(VALUE x, VALUE y); VALUE rb_int_pow(VALUE x, VALUE y); VALUE rb_float_pow(VALUE x, VALUE y); +VALUE rb_int_cmp(VALUE x, VALUE y); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) @@ -152,7 +152,6 @@ static VALUE fix_mul(VALUE x, VALUE y); static VALUE fix_lshift(long, unsigned long); static VALUE fix_rshift(long, unsigned long); static VALUE int_pow(long x, unsigned long y); -static VALUE int_cmp(VALUE x, VALUE y); static VALUE int_odd_p(VALUE x); static VALUE int_even_p(VALUE x); static int int_round_zero_p(VALUE num, int ndigits); @@ -2063,7 +2062,7 @@ rb_int_round(VALUE num, int ndigits, enum ruby_num_rounding_mode mode) h = rb_int_idiv(f, INT2FIX(2)); r = rb_int_modulo(num, f); n = rb_int_minus(num, r); - r = int_cmp(r, h); + r = rb_int_cmp(r, h); if (FIXNUM_POSITIVE_P(r) || (FIXNUM_ZERO_P(r) && ROUND_TO(mode, @@ -4022,8 +4021,8 @@ fix_cmp(VALUE x, VALUE y) return rb_num_coerce_cmp(x, y, id_cmp); } -static VALUE -int_cmp(VALUE x, VALUE y) +VALUE +rb_int_cmp(VALUE x, VALUE y) { if (FIXNUM_P(x)) { return fix_cmp(x, y); @@ -5238,7 +5237,7 @@ Init_Numeric(void) rb_define_method(rb_cInteger, "ceil", int_ceil, -1); rb_define_method(rb_cInteger, "truncate", int_truncate, -1); rb_define_method(rb_cInteger, "round", int_round, -1); - rb_define_method(rb_cInteger, "<=>", int_cmp, 1); + rb_define_method(rb_cInteger, "<=>", rb_int_cmp, 1); rb_define_method(rb_cInteger, "-@", rb_int_uminus, 0); rb_define_method(rb_cInteger, "+", rb_int_plus, 1); diff --git a/rational.c b/rational.c index 4592178b7f..f7f508771b 100644 --- a/rational.c +++ b/rational.c @@ -1072,15 +1072,17 @@ nurat_cmp(VALUE self, VALUE other) { get_dat1(self); - if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1) - return f_cmp(dat->num, other); /* c14n */ - return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other)); + if (dat->den == LONG2FIX(1)) + return rb_int_cmp(dat->num, other); /* c14n */ + other = f_rational_new_bang1(CLASS_OF(self), other); + goto other_is_rational; } } - else if (RB_TYPE_P(other, T_FLOAT)) { - return f_cmp(f_to_f(self), other); + else if (RB_FLOAT_TYPE_P(other)) { + return rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other)); } else if (RB_TYPE_P(other, T_RATIONAL)) { + other_is_rational: { VALUE num1, num2; @@ -1092,10 +1094,10 @@ nurat_cmp(VALUE self, VALUE other) num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den)); } else { - num1 = f_mul(adat->num, bdat->den); - num2 = f_mul(bdat->num, adat->den); + num1 = rb_int_mul(adat->num, bdat->den); + num2 = rb_int_mul(bdat->num, adat->den); } - return f_cmp(f_sub(num1, num2), ZERO); + return rb_int_cmp(rb_int_minus(num1, num2), ZERO); } } else { |