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

ruby-changes:44799

From: mrkn <ko1@a...>
Date: Tue, 22 Nov 2016 14:21:17 +0900 (JST)
Subject: [ruby-changes:44799] mrkn:r56872 (trunk): complex.c: optimize f_gt_p some cases

mrkn	2016-11-22 14:21:12 +0900 (Tue, 22 Nov 2016)

  New Revision: 56872

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

  Log:
    complex.c: optimize f_gt_p some cases
    
    * complex.c (f_gt_p): optimize f_gt_p for specific types of arguments.
    
    * internal.h (rb_int_gt, rb_float_gt, rb_rational_cmp): exported.
    
    * numeric.c (rb_float_gt): rename from flo_gt and be exported.
    
    * numeric.c (rb_int_gt): rename from int_gt and be exported.
    
    * rational.c (rb_rational_cmp): rename from nurat_cmp and be exported.

  Modified files:
    trunk/complex.c
    trunk/internal.h
    trunk/numeric.c
    trunk/rational.c
Index: internal.h
===================================================================
--- internal.h	(revision 56871)
+++ internal.h	(revision 56872)
@@ -1178,6 +1178,8 @@ VALUE rb_int_round(VALUE num, int ndigit https://github.com/ruby/ruby/blob/trunk/internal.h#L1178
 VALUE rb_int2str(VALUE num, int base);
 VALUE rb_dbl_hash(double d);
 VALUE rb_fix_plus(VALUE x, VALUE y);
+VALUE rb_int_gt(VALUE x, VALUE y);
+VALUE rb_float_gt(VALUE x, VALUE y);
 VALUE rb_int_ge(VALUE x, VALUE y);
 enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts);
 double rb_int_fdiv_double(VALUE x, VALUE y);
@@ -1385,6 +1387,7 @@ VALUE rb_lcm(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L1387
 VALUE rb_rational_reciprocal(VALUE x);
 VALUE rb_cstr_to_rat(const char *, int);
 VALUE rb_rational_abs(VALUE self);
+VALUE rb_rational_cmp(VALUE self, VALUE other);
 
 /* re.c */
 VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline);
Index: complex.c
===================================================================
--- complex.c	(revision 56871)
+++ complex.c	(revision 56872)
@@ -97,12 +97,21 @@ f_div(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/complex.c#L97
     return rb_funcall(x, '/', 1, y);
 }
 
-inline static VALUE
+inline static int
 f_gt_p(VALUE x, VALUE y)
 {
-    if (FIXNUM_P(x) && FIXNUM_P(y))
-	return f_boolcast(FIX2LONG(x) > FIX2LONG(y));
-    return rb_funcall(x, '>', 1, y);
+    if (RB_INTEGER_TYPE_P(x)) {
+        if (FIXNUM_P(x) && FIXNUM_P(y))
+            return (SIGNED_VALUE)x > (SIGNED_VALUE)y;
+        return RTEST(rb_int_gt(x, y));
+    }
+    else if (RB_FLOAT_TYPE_P(x))
+        return RTEST(rb_float_gt(x, y));
+    else if (RB_TYPE_P(x, T_RATIONAL)) {
+        int const cmp = rb_cmpint(rb_rational_cmp(x, y), x, y);
+        return cmp > 0;
+    }
+    return RTEST(rb_funcall(x, '>', 1, y));
 }
 
 inline static VALUE
Index: rational.c
===================================================================
--- rational.c	(revision 56871)
+++ rational.c	(revision 56872)
@@ -1063,8 +1063,8 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1063
  *    Rational(1,3)   <=> 1               #=> -1
  *    Rational(1,3)   <=> 0.3             #=> 1
  */
-static VALUE
-nurat_cmp(VALUE self, VALUE other)
+VALUE
+rb_rational_cmp(VALUE self, VALUE other)
 {
     if (RB_INTEGER_TYPE_P(other)) {
 	{
@@ -2648,7 +2648,7 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2648
     rb_define_method(rb_cRational, "fdiv", nurat_fdiv, 1);
     rb_define_method(rb_cRational, "**", nurat_expt, 1);
 
-    rb_define_method(rb_cRational, "<=>", nurat_cmp, 1);
+    rb_define_method(rb_cRational, "<=>", rb_rational_cmp, 1);
     rb_define_method(rb_cRational, "==", nurat_eqeq_p, 1);
     rb_define_method(rb_cRational, "coerce", nurat_coerce, 1);
 
Index: numeric.c
===================================================================
--- numeric.c	(revision 56871)
+++ numeric.c	(revision 56872)
@@ -1494,8 +1494,8 @@ flo_cmp(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1494
  * implementation-dependent value is returned.
  */
 
-static VALUE
-flo_gt(VALUE x, VALUE y)
+VALUE
+rb_float_gt(VALUE x, VALUE y)
 {
     double a, b;
 
@@ -4074,8 +4074,8 @@ fix_gt(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4074
     }
 }
 
-static VALUE
-int_gt(VALUE x, VALUE y)
+VALUE
+rb_int_gt(VALUE x, VALUE y)
 {
     if (FIXNUM_P(x)) {
 	return fix_gt(x, y);
@@ -5270,7 +5270,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5270
 
     rb_define_method(rb_cInteger, "===", rb_int_equal, 1);
     rb_define_method(rb_cInteger, "==", rb_int_equal, 1);
-    rb_define_method(rb_cInteger, ">", int_gt, 1);
+    rb_define_method(rb_cInteger, ">", rb_int_gt, 1);
     rb_define_method(rb_cInteger, ">=", rb_int_ge, 1);
     rb_define_method(rb_cInteger, "<", int_lt, 1);
     rb_define_method(rb_cInteger, "<=", int_le, 1);
@@ -5411,7 +5411,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5411
     rb_define_method(rb_cFloat, "==", flo_eq, 1);
     rb_define_method(rb_cFloat, "===", flo_eq, 1);
     rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
-    rb_define_method(rb_cFloat, ">",  flo_gt, 1);
+    rb_define_method(rb_cFloat, ">",  rb_float_gt, 1);
     rb_define_method(rb_cFloat, ">=", flo_ge, 1);
     rb_define_method(rb_cFloat, "<",  flo_lt, 1);
     rb_define_method(rb_cFloat, "<=", flo_le, 1);

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

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