Commit e0ac194f authored by Damien George's avatar Damien George
Browse files

py: Fix rshift and not of zero/one edge cases in mpz.

Addresses issue #1027.
parent 816a46a4
......@@ -871,11 +871,17 @@ void mpz_not_inpl(mpz_t *dest, const mpz_t *z) {
if (dest != z) {
mpz_set(dest, z);
}
if (dest->neg) {
if (dest->len == 0) {
mpz_need_dig(dest, 1);
dest->dig[0] = 1;
dest->len = 1;
dest->neg = 1;
} else if (dest->neg) {
dest->neg = 0;
mpz_dig_t k = 1;
dest->len = mpn_sub(dest->dig, dest->dig, dest->len, &k, 1);
} else {
mpz_need_dig(dest, dest->len + 1);
mpz_dig_t k = 1;
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &k, 1);
dest->neg = 1;
......@@ -924,7 +930,14 @@ void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs) {
round_up = 1;
}
if (round_up) {
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &round_up, 1);
if (dest->len == 0) {
// dest == 0, so need to add 1 by hand (answer will be -1)
dest->dig[0] = 1;
dest->len = 1;
} else {
// dest > 0, so can use mpn_add to add 1
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &round_up, 1);
}
}
}
}
......
# test [0,-0,1,-1] edge cases of bignum
long_zero = (2**64) >> 65
long_neg_zero = -long_zero
long_one = long_zero + 1
long_neg_one = -long_one
cases = [long_zero, long_neg_zero, long_one, long_neg_one]
print(cases)
print([-c for c in cases])
print([~c for c in cases])
print([c >> 1 for c in cases])
print([c << 1 for c in cases])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment