From fb40c3776a1d339ce78f24fc913e425e780891e7 Mon Sep 17 00:00:00 2001 From: mrkn Date: Sat, 12 Nov 2016 01:29:01 +0000 Subject: rational.c: optimize Rational#<=> * rational.c (nurat_cmp): optimize Rational#<=>. Author: Tadashi Saito * 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 --- rational.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'rational.c') 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 { -- cgit v1.2.3