[v1,2/2] Fix the right shift of negative numbers
Checks
Context |
Check |
Description |
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 |
success
|
Testing passed
|
linaro-tcwg-bot/tcwg_gdb_build--master-arm |
success
|
Testing passed
|
linaro-tcwg-bot/tcwg_gdb_check--master-arm |
fail
|
Testing failed
|
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 |
success
|
Testing passed
|
Commit Message
Since commit 303a881f8789 ("Use gdb_gmp for scalar arithmetic"), the
incorrect values are returned for the right shift of negative numbers,
which are truncated during the division.
```
(gdb) p /t -3
$1 = 11111111111111111111111111111101
(gdb) p /t -3 >> 1
$2 = 11111111111111111111111111111111
```
According to the documentation of gmplib [1]:
```
For positive n both mpz_fdiv_q_2exp and mpz_tdiv_q_2exp are simple
bitwise right shifts. For negative n, mpz_fdiv_q_2exp is effectively an
arithmetic right shift treating n as twos complement the same as the
bitwise logical functions do, whereas mpz_tdiv_q_2exp effectively treats
n as sign and magnitude.
```
So, use the mpz_fdiv_q_2exp variant to avoid truncation for negative
values since the behaviour for positive values is equivalent.
[1]: https://gmplib.org/gmp-man-6.2.1.pdf#Integer%20Division
---
gdb/gmp-utils.h | 4 ++--
gdb/testsuite/gdb.base/bitshift.exp | 3 +++
2 files changed, 5 insertions(+), 2 deletions(-)
@@ -280,13 +280,13 @@ struct gdb_mpz
gdb_mpz operator>> (unsigned long nbits) const
{
gdb_mpz result;
- mpz_tdiv_q_2exp (result.m_val, m_val, nbits);
+ mpz_fdiv_q_2exp (result.m_val, m_val, nbits);
return result;
}
gdb_mpz &operator>>= (unsigned long nbits)
{
- mpz_tdiv_q_2exp (m_val, m_val, nbits);
+ mpz_fdiv_q_2exp (m_val, m_val, nbits);
return *this;
}
@@ -346,6 +346,9 @@ proc test_shifts {} {
test_shift $lang "print -1 >> 1" " = -1"
test_shift $lang "print -8 >> 1" " = -4"
test_shift $lang "print [make_int64 $lang -8] >> 1" " = -4"
+ # Make sure an negative value isn't truncated by shift
+ # operator.
+ test_shift $lang "print -3 >> 1" " = -2"
}
# Make sure an unsigned 64-bit value with high bit set isn't