[前][次][番号順一覧][スレッド一覧]

ruby-changes:44660

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 10:29:06 +0900 (JST)
Subject: [ruby-changes:44660] mrkn:r56733 (trunk): rational.c: optimize Rational#<=>

mrkn	2016-11-12 10:29:01 +0900 (Sat, 12 Nov 2016)

  New Revision: 56733

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56733

  Log:
    rational.c: optimize Rational#<=>
    
    * rational.c (nurat_cmp): optimize Rational#<=>.
      Author: Tadashi Saito <tad.a.digger@g...>
    
    * numeric.c (rb_int_cmp): rename from int_cmp and remove static
      to be exported.
    
    * internal.h (rb_int_cmp): exported.

  Modified files:
    trunk/internal.h
    trunk/numeric.c
    trunk/rational.c
Index: internal.h
===================================================================
--- internal.h	(revision 56732)
+++ internal.h	(revision 56733)
@@ -1173,6 +1173,7 @@ enum ruby_num_rounding_mode rb_num_get_r https://github.com/ruby/ruby/blob/trunk/internal.h#L1173
 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)))
Index: numeric.c
===================================================================
--- numeric.c	(revision 56732)
+++ numeric.c	(revision 56733)
@@ -152,7 +152,6 @@ static VALUE fix_mul(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/numeric.c#L152
 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, enu https://github.com/ruby/ruby/blob/trunk/numeric.c#L2062
     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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4021
     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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5237
     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);
Index: rational.c
===================================================================
--- rational.c	(revision 56732)
+++ rational.c	(revision 56733)
@@ -1072,15 +1072,17 @@ nurat_cmp(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1072
 	{
 	    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) https://github.com/ruby/ruby/blob/trunk/rational.c#L1094
 		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 {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]