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

ruby-changes:29644

From: akr <ko1@a...>
Date: Sun, 30 Jun 2013 00:57:18 +0900 (JST)
Subject: [ruby-changes:29644] akr:r41696 (trunk): * bignum.c (RBIGNUM_SET_NEGATIVE_SIGN): New macro.

akr	2013-06-30 00:57:07 +0900 (Sun, 30 Jun 2013)

  New Revision: 41696

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

  Log:
    * bignum.c (RBIGNUM_SET_NEGATIVE_SIGN): New macro.
      (RBIGNUM_SET_POSITIVE_SIGN): Ditto.
      (rb_big_neg): Inline get2comp to avoid double negation.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/ext/-test-/bignum/bigzero.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41695)
+++ ChangeLog	(revision 41696)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jun 30 00:14:20 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (RBIGNUM_SET_NEGATIVE_SIGN): New macro.
+	  (RBIGNUM_SET_POSITIVE_SIGN): Ditto.
+	  (rb_big_neg): Inline get2comp to avoid double negation.
+
 Sat Jun 29 23:26:41 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (bary_neg): Extracted from bary_2comp.
Index: ext/-test-/bignum/bigzero.c
===================================================================
--- ext/-test-/bignum/bigzero.c	(revision 41695)
+++ ext/-test-/bignum/bigzero.c	(revision 41696)
@@ -9,8 +9,18 @@ bug_big_zero(VALUE self, VALUE length) https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/bigzero.c#L9
     return z;
 }
 
+static VALUE
+bug_big_negzero(VALUE self, VALUE length)
+{
+    long len = NUM2ULONG(length);
+    VALUE z = rb_big_new(len, 0);
+    MEMZERO(RBIGNUM_DIGITS(z), BDIGIT, len);
+    return z;
+}
+
 void
 Init_bigzero(VALUE klass)
 {
     rb_define_singleton_method(klass, "zero", bug_big_zero, 1);
+    rb_define_singleton_method(klass, "negzero", bug_big_negzero, 1);
 }
Index: bignum.c
===================================================================
--- bignum.c	(revision 41695)
+++ bignum.c	(revision 41696)
@@ -89,6 +89,9 @@ static VALUE big_three = Qnil; https://github.com/ruby/ruby/blob/trunk/bignum.c#L89
 #define BARY_DIVMOD(q, r, x, y) bary_divmod(BARY_ARGS(q), BARY_ARGS(r), BARY_ARGS(x), BARY_ARGS(y))
 #define BARY_ZERO_P(x) bary_zero_p(BARY_ARGS(x))
 
+#define RBIGNUM_SET_NEGATIVE_SIGN(b) RBIGNUM_SET_SIGN(b, 0)
+#define RBIGNUM_SET_POSITIVE_SIGN(b) RBIGNUM_SET_SIGN(b, 1)
+
 static int nlz(BDIGIT x);
 static BDIGIT bary_small_lshift(BDIGIT *zds, BDIGIT *xds, long n, int shift);
 static void bary_small_rshift(BDIGIT *zds, BDIGIT *xds, long n, int shift, int sign_bit);
@@ -3035,16 +3038,24 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/bignum.c#L3038
 rb_big_neg(VALUE x)
 {
     VALUE z = rb_big_clone(x);
-    BDIGIT *ds;
-    long i;
+    BDIGIT *ds = BDIGITS(z);
+    long n = RBIGNUM_LEN(z);
 
-    if (!RBIGNUM_SIGN(x)) get2comp(z);
-    ds = BDIGITS(z);
-    i = RBIGNUM_LEN(x);
-    if (!i) return INT2FIX(~(SIGNED_VALUE)0);
-    bary_neg(ds, i);
-    RBIGNUM_SET_SIGN(z, !RBIGNUM_SIGN(z));
-    if (RBIGNUM_SIGN(x)) get2comp(z);
+    if (!n) return INT2FIX(-1);
+
+    if (RBIGNUM_POSITIVE_P(z)) {
+        if (bary_plus_one(ds, n)) {
+            big_extend_carry(z);
+        }
+        RBIGNUM_SET_NEGATIVE_SIGN(z);
+    }
+    else {
+        bary_neg(ds, n);
+        if (bary_plus_one(ds, n))
+            return INT2FIX(-1);
+        bary_neg(ds, n);
+        RBIGNUM_SET_POSITIVE_SIGN(z);
+    }
 
     return bignorm(z);
 }

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

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