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

Message ID 1ec0c7b71c5dcc85d194aad7acf0e4e3b4c21527.1712086444.git.sergey_v_kaplun@mail.ru
State New
Headers
Series 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

Sergey Kaplun April 2, 2024, 8:17 p.m. UTC
  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(-)
  

Patch

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