fixing bitwise-xor for negative bignum cases

This commit is contained in:
Alex Shinn 2017-05-07 16:36:12 +09:00
parent bddb28ace7
commit cb7eaa7fe6
2 changed files with 9 additions and 2 deletions

View file

@ -131,9 +131,9 @@ sexp sexp_bit_ior (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp y) {
sexp_set_twos_complement(res); sexp_set_twos_complement(res);
for (i=0; i<len; i++) for (i=0; i<len; i++)
sexp_bignum_data(res)[i] |= sexp_bignum_data(tmp)[i]; sexp_bignum_data(res)[i] |= sexp_bignum_data(tmp)[i];
if ((sexp_bignum_sign(res) < 0) ^ sexp_fixnump(y)) if ((sexp_bignum_sign(x) < 0) ^ (sexp_fixnump(y) || sexp_bignum_sign(y) < 0))
sexp_set_twos_complement(res); sexp_set_twos_complement(res);
if (sexp_fixnump(y)) { if (sexp_fixnump(y) || sexp_bignum_sign(y) < 0) {
sexp_negate_exact(res); sexp_negate_exact(res);
} }
} else { } else {
@ -188,6 +188,10 @@ sexp sexp_bit_xor (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp y) {
= sexp_bignum_data(res)[i] ^ sexp_bignum_data(tmp)[i]; = sexp_bignum_data(res)[i] ^ sexp_bignum_data(tmp)[i];
if (sexp_bignum_sign(res) < 0) if (sexp_bignum_sign(res) < 0)
sexp_set_twos_complement(res); sexp_set_twos_complement(res);
if (!((sexp_bignum_sign(x) < 0) ^ (sexp_bignum_sign(y) < 0))) {
sexp_set_twos_complement(res);
sexp_negate_exact(res);
}
} else { } else {
res = sexp_type_exception(ctx, self, SEXP_FIXNUM, y); res = sexp_type_exception(ctx, self, SEXP_FIXNUM, y);
} }

View file

@ -35,6 +35,9 @@
(test -461856550205267621490541042387407516495872 (test -461856550205267621490541042387407516495872
(bitwise-xor -1930735170000000000001689392892000000000000 (bitwise-xor -1930735170000000000001689392892000000000000
1689392892000000000000193073517000000000000)) 1689392892000000000000193073517000000000000))
(test 461856550205267621490541042387407516495872
(bitwise-xor -1930735170000000000001689392892000000000000
-1689392892000000000000193073517000000000000))
(test 3769478 (bitwise-and 1694076839 -4290775858)) (test 3769478 (bitwise-and 1694076839 -4290775858))
(test 1680869008 (bitwise-and -193073517 1689392892)) (test 1680869008 (bitwise-and -193073517 1689392892))