1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
mrkn 2016-11-12 01:29:01 +00:00
parent d8769bffe5
commit fb40c3776a
3 changed files with 15 additions and 13 deletions

View file

@ -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)))

View file

@ -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);

View file

@ -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 {