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

ruby-changes:12359

From: tadf <ko1@a...>
Date: Sun, 12 Jul 2009 23:57:58 +0900 (JST)
Subject: [ruby-changes:12359] Ruby:r24055 (trunk): * complex.c: added some shortcuts.

tadf	2009-07-12 23:57:42 +0900 (Sun, 12 Jul 2009)

  New Revision: 24055

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

  Log:
    * complex.c: added some shortcuts.
    * rational.c: ditto.

  Modified files:
    trunk/ChangeLog
    trunk/complex.c
    trunk/rational.c

Index: complex.c
===================================================================
--- complex.c	(revision 24054)
+++ complex.c	(revision 24055)
@@ -195,8 +195,18 @@
 inline static VALUE
 f_zero_p(VALUE x)
 {
-    if (FIXNUM_P(x))
+    switch (TYPE(x)) {
+      case T_FIXNUM:
 	return f_boolcast(FIX2LONG(x) == 0);
+      case T_BIGNUM:
+	return Qfalse;
+      case T_RATIONAL:
+      {
+	  VALUE num = RRATIONAL(x)->num;
+
+	  return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 0);
+      }
+    }
     return rb_funcall(x, id_eqeq_p, 1, ZERO);
 }
 
@@ -205,8 +215,20 @@
 inline static VALUE
 f_one_p(VALUE x)
 {
-    if (FIXNUM_P(x))
+    switch (TYPE(x)) {
+      case T_FIXNUM:
 	return f_boolcast(FIX2LONG(x) == 1);
+      case T_BIGNUM:
+	return Qfalse;
+      case T_RATIONAL:
+      {
+	  VALUE num = RRATIONAL(x)->num;
+	  VALUE den = RRATIONAL(x)->den;
+
+	  return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 &&
+			    FIXNUM_P(den) && FIX2LONG(den) == 1);
+      }
+    }
     return rb_funcall(x, id_eqeq_p, 1, ONE);
 }
 
@@ -860,27 +882,33 @@
     }
     if (k_fixnum_p(other)) {
 	if (f_gt_p(other, ZERO)) {
-	    VALUE x, z, n;
+	    VALUE x, z;
+	    long n;
 
 	    x = self;
 	    z = x;
-	    n = f_sub(other, ONE);
+	    n = FIX2LONG(other) - 1;
 
-	    while (f_nonzero_p(n)) {
-		VALUE a;
+	    while (n) {
+		long q, r;
 
-		while (a = f_divmod(n, TWO),
-		       f_zero_p(RARRAY_PTR(a)[1])) {
+		while (1) {
 		    get_dat1(x);
 
+		    q = n / 2;
+		    r = n % 2;
+
+		    if (r)
+			break;
+
 		    x = f_complex_new2(CLASS_OF(self),
 				       f_sub(f_mul(dat->real, dat->real),
 					     f_mul(dat->imag, dat->imag)),
 				       f_mul(f_mul(TWO, dat->real), dat->imag));
-		    n = RARRAY_PTR(a)[0];
+		    n = q;
 		}
 		z = f_mul(z, x);
-		n = f_sub(n, ONE);
+		n--;
 	    }
 	    return z;
 	}
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24054)
+++ ChangeLog	(revision 24055)
@@ -1,3 +1,9 @@
+Sun Jul 12 23:56:40 2009  Tadayoshi Funaba  <tadf@d...>
+
+	* complex.c: added some shortcuts.
+
+	* rational.c: ditto.
+
 Sun Jul 12 23:30:26 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* object.c (rb_to_integer, rb_check_to_integer): return Bignum
Index: rational.c
===================================================================
--- rational.c	(revision 24054)
+++ rational.c	(revision 24055)
@@ -166,8 +166,18 @@
 inline static VALUE
 f_zero_p(VALUE x)
 {
-    if (FIXNUM_P(x))
+    switch (TYPE(x)) {
+      case T_FIXNUM:
 	return f_boolcast(FIX2LONG(x) == 0);
+      case T_BIGNUM:
+	return Qfalse;
+      case T_RATIONAL:
+      {
+	  VALUE num = RRATIONAL(x)->num;
+
+	  return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 0);
+      }
+    }
     return rb_funcall(x, id_eqeq_p, 1, ZERO);
 }
 
@@ -176,8 +186,20 @@
 inline static VALUE
 f_one_p(VALUE x)
 {
-    if (FIXNUM_P(x))
+    switch (TYPE(x)) {
+      case T_FIXNUM:
 	return f_boolcast(FIX2LONG(x) == 1);
+      case T_BIGNUM:
+	return Qfalse;
+      case T_RATIONAL:
+      {
+	  VALUE num = RRATIONAL(x)->num;
+	  VALUE den = RRATIONAL(x)->den;
+
+	  return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 &&
+			    FIXNUM_P(den) && FIX2LONG(den) == 1);
+      }
+    }
     return rb_funcall(x, id_eqeq_p, 1, ONE);
 }
 
@@ -846,6 +868,10 @@
 	{
 	    get_dat2(self, other);
 
+	    if (f_one_p(self))
+		return f_rational_new_no_reduce2(CLASS_OF(self),
+						 bdat->den, bdat->num);
+
 	    return f_muldiv(self,
 			    adat->num, adat->den,
 			    bdat->num, bdat->den, '/');

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

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