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

ruby-changes:29908

From: akr <ko1@a...>
Date: Sun, 14 Jul 2013 00:36:32 +0900 (JST)
Subject: [ruby-changes:29908] akr:r41960 (trunk): * bignum.c (rb_big_aref): Less scan when the number is negative.

akr	2013-07-14 00:36:18 +0900 (Sun, 14 Jul 2013)

  New Revision: 41960

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

  Log:
    * bignum.c (rb_big_aref): Less scan when the number is negative.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41959)
+++ ChangeLog	(revision 41960)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jul 14 00:32:51 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (rb_big_aref): Less scan when the number is negative.
+
 Sun Jul 14 00:17:42 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (big_shift): Avoid signed integer overflow.
Index: bignum.c
===================================================================
--- bignum.c	(revision 41959)
+++ bignum.c	(revision 41960)
@@ -5889,9 +5889,9 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/bignum.c#L5889
 rb_big_aref(VALUE x, VALUE y)
 {
     BDIGIT *xds;
-    BDIGIT_DBL num;
     unsigned long shift;
     long i, s1, s2;
+    BDIGIT bit;
 
     if (RB_TYPE_P(y, T_BIGNUM)) {
 	if (!RBIGNUM_SIGN(y))
@@ -5910,21 +5910,19 @@ rb_big_aref(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5910
     }
     s1 = shift/BITSPERDIG;
     s2 = shift%BITSPERDIG;
+    bit = (BDIGIT)1 << s2;
 
     if (s1 >= RBIGNUM_LEN(x)) goto out_of_range;
-    if (!RBIGNUM_SIGN(x)) {
-	xds = BDIGITS(x);
-	i = 0; num = 1;
-	while (num += BIGLO(~xds[i]), ++i <= s1) {
-	    num = BIGDN(num);
-	}
-    }
-    else {
-	num = BDIGITS(x)[s1];
-    }
-    if (num & ((BDIGIT_DBL)1<<s2))
-	return INT2FIX(1);
-    return INT2FIX(0);
+
+    xds = BDIGITS(x);
+    if (RBIGNUM_POSITIVE_P(x))
+        return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
+    if (xds[s1] & (bit-1))
+        return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
+    for (i = 0; i < s1; i++)
+        if (xds[i])
+            return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
+    return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
 }
 
 /*

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

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