1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
akr 2013-06-29 15:57:07 +00:00
parent 1522375b99
commit 7251c93cb3
3 changed files with 36 additions and 9 deletions

View file

@ -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.

View file

@ -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);
}

View file

@ -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);
}