* bignum.c (bary_neg): Extracted from bary_2comp.
(bary_plus_one): Extracted from bary_2comp. (bary_2comp): Use bary_neg and bary_plus_one. (big_extend_carry): Extracted from get2comp. (get2comp): Use big_extend_carry. (rb_integer_unpack): Use big_extend_carry. (rb_big_neg): Use bary_neg. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
69f1a3351f
commit
1522375b99
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
|||||||
|
Sat Jun 29 23:26:41 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* bignum.c (bary_neg): Extracted from bary_2comp.
|
||||||
|
(bary_plus_one): Extracted from bary_2comp.
|
||||||
|
(bary_2comp): Use bary_neg and bary_plus_one.
|
||||||
|
(big_extend_carry): Extracted from get2comp.
|
||||||
|
(get2comp): Use big_extend_carry.
|
||||||
|
(rb_integer_unpack): Use big_extend_carry.
|
||||||
|
(rb_big_neg): Use bary_neg.
|
||||||
|
|
||||||
Sat Jun 29 22:31:59 2013 Tanaka Akira <akr@fsij.org>
|
Sat Jun 29 22:31:59 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* bignum.c (bary_2comp): Simplified.
|
* bignum.c (bary_2comp): Simplified.
|
||||||
|
42
bignum.c
42
bignum.c
@ -267,13 +267,17 @@ bytes_2comp(unsigned char *buf, size_t len)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static void
|
||||||
bary_2comp(BDIGIT *ds, size_t n)
|
bary_neg(BDIGIT *ds, size_t n)
|
||||||
{
|
{
|
||||||
size_t i = n;
|
while (n--)
|
||||||
if (!n) return 1;
|
ds[n] = BIGLO(~ds[n]);
|
||||||
while (i--) ds[i] = BIGLO(~ds[i]);
|
}
|
||||||
i = 0;
|
|
||||||
|
static int
|
||||||
|
bary_plus_one(BDIGIT *ds, size_t n)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
ds[i] = BIGLO(ds[i]+1);
|
ds[i] = BIGLO(ds[i]+1);
|
||||||
if (ds[i] != 0)
|
if (ds[i] != 0)
|
||||||
@ -282,6 +286,21 @@ bary_2comp(BDIGIT *ds, size_t n)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
bary_2comp(BDIGIT *ds, size_t n)
|
||||||
|
{
|
||||||
|
if (!n) return 1;
|
||||||
|
bary_neg(ds, n);
|
||||||
|
return bary_plus_one(ds, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
big_extend_carry(VALUE x)
|
||||||
|
{
|
||||||
|
rb_big_resize(x, RBIGNUM_LEN(x)+1);
|
||||||
|
BDIGITS(x)[RBIGNUM_LEN(x)-1] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* modify a bignum by 2's complement */
|
/* modify a bignum by 2's complement */
|
||||||
static void
|
static void
|
||||||
get2comp(VALUE x)
|
get2comp(VALUE x)
|
||||||
@ -290,9 +309,7 @@ get2comp(VALUE x)
|
|||||||
BDIGIT *ds = BDIGITS(x);
|
BDIGIT *ds = BDIGITS(x);
|
||||||
|
|
||||||
if (bary_2comp(ds, i)) {
|
if (bary_2comp(ds, i)) {
|
||||||
rb_big_resize(x, RBIGNUM_LEN(x)+1);
|
big_extend_carry(x);
|
||||||
ds = BDIGITS(x);
|
|
||||||
ds[RBIGNUM_LEN(x)-1] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1696,8 +1713,7 @@ rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t na
|
|||||||
|
|
||||||
if (sign == -2) {
|
if (sign == -2) {
|
||||||
if (val) {
|
if (val) {
|
||||||
rb_big_resize(val, (long)num_bdigits+1);
|
big_extend_carry(val);
|
||||||
BDIGITS(val)[num_bdigits] = 1;
|
|
||||||
}
|
}
|
||||||
else if (num_bdigits == numberof(fixbuf)) {
|
else if (num_bdigits == numberof(fixbuf)) {
|
||||||
val = bignew((long)num_bdigits+1, 0);
|
val = bignew((long)num_bdigits+1, 0);
|
||||||
@ -3026,9 +3042,7 @@ rb_big_neg(VALUE x)
|
|||||||
ds = BDIGITS(z);
|
ds = BDIGITS(z);
|
||||||
i = RBIGNUM_LEN(x);
|
i = RBIGNUM_LEN(x);
|
||||||
if (!i) return INT2FIX(~(SIGNED_VALUE)0);
|
if (!i) return INT2FIX(~(SIGNED_VALUE)0);
|
||||||
while (i--) {
|
bary_neg(ds, i);
|
||||||
ds[i] = BIGLO(~ds[i]);
|
|
||||||
}
|
|
||||||
RBIGNUM_SET_SIGN(z, !RBIGNUM_SIGN(z));
|
RBIGNUM_SET_SIGN(z, !RBIGNUM_SIGN(z));
|
||||||
if (RBIGNUM_SIGN(x)) get2comp(z);
|
if (RBIGNUM_SIGN(x)) get2comp(z);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user