[PATCH v1 2/2] Fix the right shift of negative numbers

Sergey Kaplun
2024-04-02T20:18:43Z

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(-)

diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 51e06abc050..878ce1da43a 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -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;
}

diff --git a/gdb/testsuite/gdb.base/bitshift.exp b/gdb/testsuite/gdb.base/bitshift.exp
index 806886cc1bf..0bc99cc438f 100644
--- a/gdb/testsuite/gdb.base/bitshift.exp
+++ b/gdb/testsuite/gdb.base/bitshift.exp
@@ -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
--
2.43.2