aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-29 15:57:07 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-29 15:57:07 +0000
commit7251c93cb3e39be231ad7cdf356dcceb79522d46 (patch)
tree80e57f98ec4cc4db7b969cb780f0baad1528638e
parent1522375b99de8fa5e6e99d69724c89250d8181c2 (diff)
downloadruby-7251c93cb3e39be231ad7cdf356dcceb79522d46.tar.gz
* bignum.c (RBIGNUM_SET_NEGATIVE_SIGN): New macro.
(RBIGNUM_SET_POSITIVE_SIGN): Ditto. (rb_big_neg): Inline get2comp to avoid double negation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41696 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--bignum.c29
-rw-r--r--ext/-test-/bignum/bigzero.c10
3 files changed, 36 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index 02269952b4..500ab585d9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sun Jun 30 00:14:20 2013 Tanaka Akira <akr@fsij.org>
+
+ * 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@fsij.org>
* bignum.c (bary_neg): Extracted from bary_2comp.
diff --git a/bignum.c b/bignum.c
index 382986366f..15172503d0 100644
--- a/bignum.c
+++ b/bignum.c
@@ -89,6 +89,9 @@ static VALUE big_three = Qnil;
#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
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);
}
diff --git a/ext/-test-/bignum/bigzero.c b/ext/-test-/bignum/bigzero.c
index 5181d71aab..2f7c272744 100644
--- a/ext/-test-/bignum/bigzero.c
+++ b/ext/-test-/bignum/bigzero.c
@@ -9,8 +9,18 @@ bug_big_zero(VALUE self, VALUE length)
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);
}