aboutsummaryrefslogtreecommitdiffstats
path: root/bignum.c
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 /bignum.c
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
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c29
1 files changed, 20 insertions, 9 deletions
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);
}