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

ruby-changes:30794

From: nobu <ko1@a...>
Date: Sun, 8 Sep 2013 04:04:29 +0900 (JST)
Subject: [ruby-changes:30794] nobu:r42873 (trunk): bignum.c, math.c: type predicates

nobu	2013-09-08 04:04:18 +0900 (Sun, 08 Sep 2013)

  New Revision: 42873

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42873

  Log:
    bignum.c, math.c: type predicates
    
    * bignum.c, math.c: use type predicate macros instead of TYPE().

  Modified files:
    trunk/bignum.c
    trunk/math.c
Index: math.c
===================================================================
--- math.c	(revision 42872)
+++ math.c	(revision 42873)
@@ -444,7 +444,7 @@ math_log(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/math.c#L444
 
     rb_scan_args(argc, argv, "11", &x, &base);
 
-    if (TYPE(x) == T_BIGNUM && RBIGNUM_POSITIVE_P(x) &&
+    if (RB_TYPE_P(x, T_BIGNUM) && RBIGNUM_POSITIVE_P(x) &&
             DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
@@ -501,7 +501,7 @@ math_log2(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L501
     double d0, d;
     size_t numbits = 0;
 
-    if (TYPE(x) == T_BIGNUM && RBIGNUM_POSITIVE_P(x) &&
+    if (RB_TYPE_P(x, T_BIGNUM) && RBIGNUM_POSITIVE_P(x) &&
             DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
@@ -540,7 +540,7 @@ math_log10(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L540
     double d0, d;
     size_t numbits = 0;
 
-    if (TYPE(x) == T_BIGNUM && RBIGNUM_POSITIVE_P(x) &&
+    if (RB_TYPE_P(x, T_BIGNUM) && RBIGNUM_POSITIVE_P(x) &&
             DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
Index: bignum.c
===================================================================
--- bignum.c	(revision 42872)
+++ bignum.c	(revision 42873)
@@ -5391,18 +5391,15 @@ rb_big_cmp(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5391
 {
     int cmp;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      case T_FLOAT:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
         return rb_integer_float_cmp(x, y);
-
-      default:
+    }
+    else {
 	return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
     }
 
@@ -5429,27 +5426,21 @@ big_op(VALUE x, VALUE y, enum big_op_t o https://github.com/ruby/ruby/blob/trunk/bignum.c#L5426
     VALUE rel;
     int n;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
-      case T_BIGNUM:
+    if (FIXNUM_P(y) || RB_TYPE_P(y, T_BIGNUM)) {
 	rel = rb_big_cmp(x, y);
-	break;
-
-      case T_FLOAT:
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
         rel = rb_integer_float_cmp(x, y);
-        break;
-
-      default:
-	{
-	    ID id = 0;
-	    switch (op) {
-		case big_op_gt: id = '>'; break;
-		case big_op_ge: id = rb_intern(">="); break;
-		case big_op_lt: id = '<'; break;
-		case big_op_le: id = rb_intern("<="); break;
-	    }
-	    return rb_num_coerce_relop(x, y, id);
+    }
+    else {
+	ID id = 0;
+	switch (op) {
+	  case big_op_gt: id = '>'; break;
+	  case big_op_ge: id = rb_intern(">="); break;
+	  case big_op_lt: id = '<'; break;
+	  case big_op_le: id = rb_intern("<="); break;
 	}
+	return rb_num_coerce_relop(x, y, id);
     }
 
     if (NIL_P(rel)) return Qfalse;
@@ -5534,16 +5525,16 @@ big_le(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5525
 VALUE
 rb_big_eq(VALUE x, VALUE y)
 {
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	if (bignorm(x) == y) return Qtrue;
 	y = rb_int2big(FIX2LONG(y));
-	break;
-      case T_BIGNUM:
-	break;
-      case T_FLOAT:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
         return rb_integer_float_eq(x, y);
-      default:
+    }
+    else {
 	return rb_equal(y, x);
     }
     if (RBIGNUM_SIGN(x) != RBIGNUM_SIGN(y)) return Qfalse;
@@ -5874,8 +5865,7 @@ rb_big_plus(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5865
 {
     long n;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	n = FIX2LONG(y);
 	if ((n > 0) != RBIGNUM_SIGN(x)) {
 	    if (n < 0) {
@@ -5887,14 +5877,14 @@ rb_big_plus(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5877
 	    n = -n;
 	}
 	return bigadd_int(x, n);
-
-      case T_BIGNUM:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
 	return bignorm(bigadd(x, y, 1));
-
-      case T_FLOAT:
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
 	return DBL2NUM(rb_big2dbl(x) + RFLOAT_VALUE(y));
-
-      default:
+    }
+    else {
 	return rb_num_coerce_bin(x, y, '+');
     }
 }
@@ -5911,8 +5901,7 @@ rb_big_minus(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5901
 {
     long n;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	n = FIX2LONG(y);
 	if ((n > 0) != RBIGNUM_SIGN(x)) {
 	    if (n < 0) {
@@ -5924,14 +5913,14 @@ rb_big_minus(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5913
 	    n = -n;
 	}
 	return bigsub_int(x, n);
-
-      case T_BIGNUM:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
 	return bignorm(bigadd(x, y, 0));
-
-      case T_FLOAT:
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
 	return DBL2NUM(rb_big2dbl(x) - RFLOAT_VALUE(y));
-
-      default:
+    }
+    else {
 	return rb_num_coerce_bin(x, y, '-');
     }
 }
@@ -6004,18 +5993,15 @@ bigmul0(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5993
 VALUE
 rb_big_mul(VALUE x, VALUE y)
 {
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      case T_FLOAT:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
 	return DBL2NUM(rb_big2dbl(x) * RFLOAT_VALUE(y));
-
-      default:
+    }
+    else {
 	return rb_num_coerce_bin(x, y, '*');
     }
 
@@ -6136,27 +6122,22 @@ rb_big_divide(VALUE x, VALUE y, ID op) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6122
 {
     VALUE z;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      case T_FLOAT:
-	{
-	    if (op == '/') {
-		return DBL2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
-	    }
-	    else {
-		double dy = RFLOAT_VALUE(y);
-		if (dy == 0.0) rb_num_zerodiv();
-		return rb_dbl2big(rb_big2dbl(x) / dy);
-	    }
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
+	if (op == '/') {
+	    return DBL2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
 	}
-
-      default:
+	else {
+	    double dy = RFLOAT_VALUE(y);
+	    if (dy == 0.0) rb_num_zerodiv();
+	    return rb_dbl2big(rb_big2dbl(x) / dy);
+	}
+    }
+    else {
 	return rb_num_coerce_bin(x, y, op);
     }
     bigdivmod(x, y, &z, 0);
@@ -6206,15 +6187,10 @@ rb_big_modulo(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6187
 {
     VALUE z;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      default:
+    }
+    else if (!RB_TYPE_P(y, T_BIGNUM)) {
 	return rb_num_coerce_bin(x, y, '%');
     }
     bigdivmod(x, y, 0, &z);
@@ -6236,15 +6212,10 @@ rb_big_remainder(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6212
 {
     VALUE z;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      default:
+    }
+    else if (!RB_TYPE_P(y, T_BIGNUM)) {
 	return rb_num_coerce_bin(x, y, rb_intern("remainder"));
     }
     bigdivrem(x, y, 0, &z);
@@ -6264,15 +6235,10 @@ rb_big_divmod(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6235
 {
     VALUE div, mod;
 
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	y = rb_int2big(FIX2LONG(y));
-	break;
-
-      case T_BIGNUM:
-	break;
-
-      default:
+    }
+    else if (!RB_TYPE_P(y, T_BIGNUM)) {
 	return rb_num_coerce_bin(x, y, rb_intern("divmod"));
     }
     bigdivmod(x, y, &div, &mod);
@@ -6354,28 +6320,24 @@ rb_big_fdiv(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6320
     double dx, dy;
 
     dx = big2dbl(x);
-    switch (TYPE(y)) {
-      case T_FIXNUM:
+    if (FIXNUM_P(y)) {
 	dy = (double)FIX2LONG(y);
 	if (isinf(dx))
 	    return big_fdiv_int(x, rb_int2big(FIX2LONG(y)));
-	break;
-
-      case T_BIGNUM:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
 	dy = rb_big2dbl(y);
 	if (isinf(dx) || isinf(dy))
 	    return big_fdiv_int(x, y);
-	break;
-
-      case T_FLOAT:
+    }
+    else if (RB_FLOAT_TYPE_P(y)) {
 	dy = RFLOAT_VALUE(y);
 	if (isnan(dy))
 	    return y;
 	if (isinf(dx))
 	    return big_fdiv_float(x, y);
-	break;
-
-      default:
+    }
+    else {
 	return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
     }
     return DBL2NUM(dx / dy);
@@ -6402,22 +6364,19 @@ rb_big_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6364
 
   again:
     if (y == INT2FIX(0)) return INT2FIX(1);
-    switch (TYPE(y)) {
-      case T_FLOAT:
+    if (RB_FLOAT_TYPE_P(y)) {
 	d = RFLOAT_VALUE(y);
 	if ((!RBIGNUM_SIGN(x) && !BIGZEROP(x)) && d != round(d))
 	    return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
-	break;
-
-      case T_BIGNUM:
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
 	y = bignorm(y);
 	if (FIXNUM_P(y))
 	    goto again;
 	rb_warn("in a**b, b may be too big");
 	d = rb_big2dbl(y);
-	break;
-
-      case T_FIXNUM:
+    }
+    else if (FIXNUM_P(y)) {
 	yy = FIX2LONG(y);
 
 	if (yy < 0)
@@ -6433,20 +6392,19 @@ rb_big_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6392
                 (xbits * yy > BIGLEN_LIMIT)) {
 		rb_warn("in a**b, b may be too big");
 		d = (double)yy;
-		break;
 	    }
-	    for (mask = FIXNUM_MAX + 1; mask; mask >>= 1) {
-		if (z) z = bigsq(z);
-		if (yy & mask) {
-		    z = z ? bigtrunc(bigmul0(z, x)) : x;
+	    else {
+		for (mask = FIXNUM_MAX + 1; mask; mask >>= 1) {
+		    if (z) z = bigsq(z);
+		    if (yy & mask) {
+			z = z ? bigtrunc(bigmul0(z, x)) : x;
+		    }
 		}
+		return bignorm(z);
 	    }
-	    return bignorm(z);
 	}
-	/* NOTREACHED */
-	break;
-
-      default:
+    }
+    else {
 	return rb_num_coerce_bin(x, y, rb_intern("**"));
     }
     return DBL2NUM(pow(rb_big2dbl(x), d));

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

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