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

ruby-changes:44755

From: mrkn <ko1@a...>
Date: Sat, 19 Nov 2016 00:04:43 +0900 (JST)
Subject: [ruby-changes:44755] mrkn:r56828 (trunk): rational.c: optimization and refactoring

mrkn	2016-11-19 00:04:36 +0900 (Sat, 19 Nov 2016)

  New Revision: 56828

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

  Log:
    rational.c: optimization and refactoring
    
    * rational.c (nurat_s_new_bang, nurat_canonicalize): small optimization
      by using rb_int_uminus instead of f_negate.
    
    * rational.c (nurat_canonicalize): add assertions for parameters.
    
    * rational.c (f_negate, id_negate): removed.

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56827)
+++ rational.c	(revision 56828)
@@ -32,7 +32,7 @@ https://github.com/ruby/ruby/blob/trunk/rational.c#L32
 
 VALUE rb_cRational;
 
-static ID id_abs, id_idiv, id_integer_p, id_negate, id_to_i,
+static ID id_abs, id_idiv, id_integer_p, id_to_i,
     id_i_num, id_i_den;
 
 #define f_boolcast(x) ((x) ? Qtrue : Qfalse)
@@ -137,7 +137,6 @@ f_abs(VALUE x) https://github.com/ruby/ruby/blob/trunk/rational.c#L137
 }
 
 fun1(integer_p)
-fun1(negate)
 
 inline static VALUE
 f_to_i(VALUE x)
@@ -415,8 +414,8 @@ nurat_s_new_bang(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/rational.c#L414
 	    den = f_to_i(den);
 
         if (INT_NEGATIVE_P(den)) {
-	    num = f_negate(num);
-	    den = f_negate(den);
+	    num = rb_int_uminus(num);
+	    den = rb_int_uminus(den);
         }
         else if (INT_ZERO_P(den)) {
             rb_num_zerodiv();
@@ -469,9 +468,11 @@ nurat_int_value(VALUE num) https://github.com/ruby/ruby/blob/trunk/rational.c#L468
 static void
 nurat_canonicalize(VALUE *num, VALUE *den)
 {
+    assert(num != NULL && RB_INTEGER_TYPE_P(*num));
+    assert(den != NULL && RB_INTEGER_TYPE_P(*den));
     if (INT_NEGATIVE_P(*den)) {
-	*num = f_negate(*num);
-	*den = f_negate(*den);
+        *num = rb_int_uminus(*num);
+        *den = rb_int_uminus(*den);
     }
     else if (INT_ZERO_P(*den)) {
         rb_num_zerodiv();
@@ -2615,7 +2616,6 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2616
     id_abs = rb_intern("abs");
     id_idiv = rb_intern("div");
     id_integer_p = rb_intern("integer?");
-    id_negate = rb_intern("-@");
     id_to_i = rb_intern("to_i");
     id_i_num = rb_intern("@numerator");
     id_i_den = rb_intern("@denominator");

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

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