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

ruby-changes:42127

From: nobu <ko1@a...>
Date: Sun, 20 Mar 2016 18:53:55 +0900 (JST)
Subject: [ruby-changes:42127] nobu:r54201 (trunk): numeric.c: optimize Fixnum<->Bignum comparisons

nobu	2016-03-20 18:53:50 +0900 (Sun, 20 Mar 2016)

  New Revision: 54201

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

  Log:
    numeric.c: optimize Fixnum<->Bignum comparisons
    
    * numeric.c (fix_gt, fix_ge, fix_lt, fix_le): optimize comparisons
      Fixnum against Bignum by rb_big_cmp in inversed order without
      new Bignum instance.

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 54200)
+++ numeric.c	(revision 54201)
@@ -3482,7 +3482,7 @@ fix_gt(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3482
 	return Qfalse;
     }
     else if (RB_TYPE_P(y, T_BIGNUM)) {
-	return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse;
+	return rb_big_cmp(y, x) == INT2FIX(-1) ? Qtrue : Qfalse;
     }
     else if (RB_TYPE_P(y, T_FLOAT)) {
         return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
@@ -3508,7 +3508,7 @@ fix_ge(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3508
 	return Qfalse;
     }
     else if (RB_TYPE_P(y, T_BIGNUM)) {
-	return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse;
+	return rb_big_cmp(y, x) != INT2FIX(+1) ? Qtrue : Qfalse;
     }
     else if (RB_TYPE_P(y, T_FLOAT)) {
 	VALUE rel = rb_integer_float_cmp(x, y);
@@ -3534,7 +3534,7 @@ fix_lt(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3534
 	return Qfalse;
     }
     else if (RB_TYPE_P(y, T_BIGNUM)) {
-	return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse;
+	return rb_big_cmp(y, x) == INT2FIX(+1) ? Qtrue : Qfalse;
     }
     else if (RB_TYPE_P(y, T_FLOAT)) {
         return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
@@ -3560,7 +3560,7 @@ fix_le(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3560
 	return Qfalse;
     }
     else if (RB_TYPE_P(y, T_BIGNUM)) {
-	return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse;
+	return rb_big_cmp(y, x) != INT2FIX(-1) ? Qtrue : Qfalse;
     }
     else if (RB_TYPE_P(y, T_FLOAT)) {
 	VALUE rel = rb_integer_float_cmp(x, y);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54200)
+++ ChangeLog	(revision 54201)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Mar 20 18:53:49 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* numeric.c (fix_gt, fix_ge, fix_lt, fix_le): optimize comparisons
+	  Fixnum against Bignum by rb_big_cmp in inversed order without
+	  new Bignum instance.
+
 Sun Mar 20 18:44:52 2016  NARUSE, Yui  <naruse@r...>
 
 	* time.c (add): remove FIXABLE() which is in LONG2NUM().

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

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