From: Sergey Kaplun <sergey_v_kaplun@mail.ru>
To: gdb-patches@sourceware.org
Cc: Sergey Kaplun <sergey_v_kaplun@mail.ru>
Subject: [PATCH v1 2/2] Fix the right shift of negative numbers
Date: Tue, 2 Apr 2024 23:17:58 +0300 [thread overview]
Message-ID: <1ec0c7b71c5dcc85d194aad7acf0e4e3b4c21527.1712086444.git.sergey_v_kaplun@mail.ru> (raw)
In-Reply-To: <cover.1712086444.git.sergey_v_kaplun@mail.ru>
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
next prev parent reply other threads:[~2024-04-02 20:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-02 20:17 [PATCH v1 0/2] " Sergey Kaplun
2024-04-02 20:17 ` [PATCH v1 1/2] gdb/testsuite: enable back gdb.base/bitshift.exp Sergey Kaplun
2024-04-02 20:17 ` Sergey Kaplun [this message]
2024-04-13 7:41 ` [PING] [PATCH v1 0/2] Fix the right shift of negative numbers Sergey Kaplun
2024-04-23 8:45 ` [PING*2][PATCH " Sergey Kaplun
2024-04-23 14:46 ` Tom Tromey
2024-04-25 13:23 ` Sergey Kaplun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1ec0c7b71c5dcc85d194aad7acf0e4e3b4c21527.1712086444.git.sergey_v_kaplun@mail.ru \
--to=sergey_v_kaplun@mail.ru \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).