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

ruby-changes:44633

From: mrkn <ko1@a...>
Date: Fri, 11 Nov 2016 23:39:20 +0900 (JST)
Subject: [ruby-changes:44633] mrkn:r56706 (trunk): rational.c: optimize Integer#gcd.

mrkn	2016-11-11 23:39:16 +0900 (Fri, 11 Nov 2016)

  New Revision: 56706

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

  Log:
    rational.c: optimize Integer#gcd.
    
    * rational.c (f_gcd_normal): optimize Integer#gcd.
      Author: Tadashi Saito <tad.a.digger@g...>

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56705)
+++ rational.c	(revision 56706)
@@ -27,6 +27,9 @@ https://github.com/ruby/ruby/blob/trunk/rational.c#L27
 
 #define GMP_GCD_DIGITS 1
 
+#define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? ((SIGNED_VALUE)(x) < 0) : BIGNUM_NEGATIVE_P(x))
+#define INT_ZERO_P(x) (FIXNUM_P(x) ? (FIX2LONG(x) == 0) : rb_bigzero_p(x))
+
 VALUE rb_cRational;
 
 static ID id_abs, id_cmp, id_convert, id_eqeq_p, id_expt, id_fdiv,
@@ -318,14 +321,14 @@ f_gcd_normal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L321
     if (FIXNUM_P(x) && FIXNUM_P(y))
 	return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
 
-    if (f_negative_p(x))
-	x = f_negate(x);
-    if (f_negative_p(y))
-	y = f_negate(y);
+    if (INT_NEGATIVE_P(x))
+	x = rb_int_uminus(x);
+    if (INT_NEGATIVE_P(y))
+	y = rb_int_uminus(y);
 
-    if (f_zero_p(x))
+    if (INT_ZERO_P(x))
 	return y;
-    if (f_zero_p(y))
+    if (INT_ZERO_P(y))
 	return x;
 
     for (;;) {
@@ -336,7 +339,7 @@ f_gcd_normal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L339
 		return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
 	}
 	z = x;
-	x = f_mod(y, x);
+	x = rb_int_modulo(y, x);
 	y = z;
     }
     /* NOTREACHED */

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

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