catching more bitwise-xor cases

This commit is contained in:
Alex Shinn 2017-05-07 16:45:18 +09:00
parent cb7eaa7fe6
commit 9cf8a3ddf3
2 changed files with 11 additions and 7 deletions

View file

@ -164,17 +164,17 @@ sexp sexp_bit_xor (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp y) {
#if SEXP_USE_BIGNUMS
} else if (sexp_bignump(x)) {
sexp_gc_preserve2(ctx, res, tmp);
if (sexp_fixnump(y)) {
if (sexp_fixnump(y) && sexp_unbox_fixnum(y) >= 0) {
res = sexp_copy_bignum(ctx, NULL, x, 0);
if (sexp_bignum_sign(res) < 0)
sexp_set_twos_complement(res);
sexp_bignum_data(res)[0] ^= sexp_unbox_fixnum(y);
if (sexp_bignum_sign(res) < 0)
sexp_set_twos_complement(res);
} else if (sexp_bignump(y)) {
if (sexp_bignum_length(x) >= sexp_bignum_length(y)) {
} else if (sexp_bignump(y) || sexp_fixnump(y)) {
if (sexp_fixnump(y) || sexp_bignum_length(x) >= sexp_bignum_length(y)) {
res = sexp_copy_bignum(ctx, NULL, x, 0);
tmp = sexp_twos_complement(ctx, y);
tmp = sexp_fixnump(y) ? sexp_fixnum_to_twos_complement(ctx, y, sexp_bignum_length(x)) : sexp_twos_complement(ctx, y);
len = sexp_bignum_length(tmp);
} else {
res = sexp_copy_bignum(ctx, NULL, y, 0);
@ -186,10 +186,9 @@ sexp sexp_bit_xor (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp y) {
for (i=0; i<len; i++)
sexp_bignum_data(res)[i]
= sexp_bignum_data(res)[i] ^ sexp_bignum_data(tmp)[i];
if (sexp_bignum_sign(res) < 0)
sexp_set_twos_complement(res);
if (!((sexp_bignum_sign(x) < 0) ^ (sexp_bignum_sign(y) < 0))) {
if ((sexp_bignum_sign(x) < 0) ^ (sexp_fixnump(y) || sexp_bignum_sign(y) < 0))
sexp_set_twos_complement(res);
if (sexp_fixnump(y) || sexp_bignum_sign(y) < 0) {
sexp_negate_exact(res);
}
} else {

View file

@ -28,6 +28,8 @@
1689392892000000000000193073517000000000000))
(test -2604237975 (bitwise-xor 1694076839 -4290775858))
(test -1865418641 (bitwise-xor -193073517 1689392892))
(test -1689392892000142479725
(bitwise-xor -193073517 1689392892000000000000))
(test -1510500507664429879296
(bitwise-xor -193073517000000000000 1689392892000000000000))
(test -1510500507664429879296
@ -35,6 +37,9 @@
(test -461856550205267621490541042387407516495872
(bitwise-xor -1930735170000000000001689392892000000000000
1689392892000000000000193073517000000000000))
(test -461856550205267621490541042387407516495872
(bitwise-xor 1689392892000000000000193073517000000000000
-1930735170000000000001689392892000000000000))
(test 461856550205267621490541042387407516495872
(bitwise-xor -1930735170000000000001689392892000000000000
-1689392892000000000000193073517000000000000))