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

ruby-changes:44663

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 15:07:26 +0900 (JST)
Subject: [ruby-changes:44663] mrkn:r56736 (trunk): rational.c: purge f_cmp

mrkn	2016-11-12 15:07:24 +0900 (Sat, 12 Nov 2016)

  New Revision: 56736

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

  Log:
    rational.c: purge f_cmp
    
    * rational.c (f_cmp, nurat_expt): purge f_cmp.
      Author: Tadashi Saito <tad.a.digger@g...>
    
    * rational.c (INT_POSITIVE_P): added.
    
    * numeric.c (FIXNUM_POSITIVE_P): move the definition into internal.h.
    
    * internal.h (FIXNUM_POSITIVE_P): ditto.

  Modified files:
    trunk/internal.h
    trunk/numeric.c
    trunk/rational.c
Index: internal.h
===================================================================
--- internal.h	(revision 56735)
+++ internal.h	(revision 56736)
@@ -1137,6 +1137,7 @@ void Init_newline(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L1137
 
 /* numeric.c */
 
+#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0))
 #define FIXNUM_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0)
 #define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0))
 
Index: numeric.c
===================================================================
--- numeric.c	(revision 56735)
+++ numeric.c	(revision 56736)
@@ -260,8 +260,6 @@ compare_with_zero(VALUE num, ID mid) https://github.com/ruby/ruby/blob/trunk/numeric.c#L260
     return r;
 }
 
-#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0))
-
 static inline int
 int_pos_p(VALUE num)
 {
Index: rational.c
===================================================================
--- rational.c	(revision 56735)
+++ rational.c	(revision 56736)
@@ -27,6 +27,7 @@ https://github.com/ruby/ruby/blob/trunk/rational.c#L27
 
 #define GMP_GCD_DIGITS 1
 
+#define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x))
 #define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? FIXNUM_NEGATIVE_P(x) : BIGNUM_NEGATIVE_P(x))
 #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
 
@@ -72,20 +73,6 @@ f_add(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L73
 }
 
 inline static VALUE
-f_cmp(VALUE x, VALUE y)
-{
-    if (FIXNUM_P(x) && FIXNUM_P(y)) {
-	long c = FIX2LONG(x) - FIX2LONG(y);
-	if (c > 0)
-	    c = 1;
-	else if (c < 0)
-	    c = -1;
-	return INT2FIX(c);
-    }
-    return rb_funcall(x, id_cmp, 1, y);
-}
-
-inline static VALUE
 f_div(VALUE x, VALUE y)
 {
     if (FIXNUM_P(y) && FIX2LONG(y) == 1)
@@ -437,15 +424,13 @@ nurat_s_new_bang(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/rational.c#L424
 	if (!k_integer_p(den))
 	    den = f_to_i(den);
 
-	switch (FIX2INT(f_cmp(den, ZERO))) {
-	  case -1:
+        if (INT_NEGATIVE_P(den)) {
 	    num = f_negate(num);
 	    den = f_negate(den);
-	    break;
-	  case 0:
+        }
+        else if (INT_ZERO_P(den)) {
 	    rb_raise_zerodiv();
-	    break;
-	}
+        }
 	break;
     }
 
@@ -1014,11 +999,11 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L999
 	    if (f_one_p(dat->num)) {
 		return f_rational_new_bang1(CLASS_OF(self), ONE);
 	    }
-	    else if (f_minus_one_p(dat->num) && k_integer_p(other)) {
+	    else if (f_minus_one_p(dat->num) && RB_INTEGER_TYPE_P(other)) {
 		return f_rational_new_bang1(CLASS_OF(self), INT2FIX(f_odd_p(other) ? -1 : 1));
 	    }
-	    else if (f_zero_p(dat->num)) {
-		if (FIX2INT(f_cmp(other, ZERO)) == -1) {
+	    else if (INT_ZERO_P(dat->num)) {
+		if (f_negative_p(other)) {
 		    rb_raise_zerodiv();
 		}
 		else {
@@ -1029,25 +1014,23 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1014
     }
 
     /* General case */
-    if (RB_TYPE_P(other, T_FIXNUM)) {
+    if (FIXNUM_P(other)) {
 	{
 	    VALUE num, den;
 
 	    get_dat1(self);
 
-	    switch (FIX2INT(f_cmp(other, ZERO))) {
-	      case 1:
+            if (INT_POSITIVE_P(other)) {
 		num = rb_int_pow(dat->num, other);
 		den = rb_int_pow(dat->den, other);
-		break;
-	      case -1:
+            }
+            else if (INT_NEGATIVE_P(other)) {
 		num = rb_int_pow(dat->den, rb_int_uminus(other));
 		den = rb_int_pow(dat->num, rb_int_uminus(other));
-		break;
-	      default:
+            }
+            else {
 		num = ONE;
 		den = ONE;
-		break;
 	    }
 	    return f_rational_new2(CLASS_OF(self), num, den);
 	}

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

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