From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] Fix right shift of negative numbers
Date: Fri, 31 May 2024 13:47:58 +0200 [thread overview]
Message-ID: <20240531114801.2745-2-ssbssa@yahoo.de> (raw)
In-Reply-To: <20240531114801.2745-1-ssbssa@yahoo.de>
PR31590 shows that right shift of negative numbers doesn't work
correctly since GDB 14:
(gdb) p (-3) >> 1
$1 = -1
GDB 13 and earlier returned the correct value -2.
And there actually is one test that shows the failure:
print -1 >> 1
$84 = 0
(gdb) FAIL: gdb.base/bitshift.exp: lang=asm: rsh neg lhs: print -1 >> 1
The problem was introduced with the change to gmp functions in
commit 303a881f87.
It's wrong because gdb_mpz::operator>> uses mpz_tdif_q_2exp, which
always rounds toward zero, and the gmp docu says this:
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 two¿s complement the same as the bitwise logical functions
do, whereas mpz_tdiv_q_2exp effectively treats n as sign and magnitude.
So this changes mpz_tdiv_q_2exp to mpz_fdiv_q_2exp, since it
does right shifts for both positive and negative numbers.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31590
---
gdb/gmp-utils.h | 4 ++--
gdb/testsuite/gdb.base/bitshift.exp | 2 ++
2 files changed, 4 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 61c7eca2747..17f6b78fed2 100644
--- a/gdb/testsuite/gdb.base/bitshift.exp
+++ b/gdb/testsuite/gdb.base/bitshift.exp
@@ -344,6 +344,8 @@ proc test_shifts {} {
with_test_prefix "rsh neg lhs" {
test_shift $lang "print -1 >> 0" " = -1"
test_shift $lang "print -1 >> 1" " = -1"
+ test_shift $lang "print -2 >> 1" " = -1"
+ test_shift $lang "print -3 >> 1" " = -2"
test_shift $lang "print -8 >> 1" " = -4"
test_shift $lang "print [make_int64 $lang -8] >> 1" " = -4"
}
--
2.35.1
next prev parent reply other threads:[~2024-05-31 11:48 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240531114801.2745-1-ssbssa.ref@yahoo.de>
2024-05-31 11:47 ` [PATCH 1/5] Restore bitshift.exp tests Hannes Domani
2024-05-31 11:47 ` Hannes Domani [this message]
2024-06-11 18:15 ` [PATCH 2/5] Fix right shift of negative numbers Tom Tromey
2024-05-31 11:47 ` [PATCH 3/5] Fix too-large or negative " Hannes Domani
2024-06-11 18:16 ` Tom Tromey
2024-05-31 11:48 ` [PATCH 4/5] Fix 64-bit shifts where long only has 32-bit size Hannes Domani
2024-06-11 18:28 ` Tom Tromey
2024-05-31 11:48 ` [PATCH 5/5] Fix cast types for opencl Hannes Domani
2024-06-11 18:29 ` Tom Tromey
2024-06-11 19:31 ` Tom Tromey
2024-06-11 19:34 ` Hannes Domani
2024-06-11 18:11 ` [PATCH 1/5] Restore bitshift.exp tests Tom Tromey
2024-06-11 18:38 ` Hannes Domani
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=20240531114801.2745-2-ssbssa@yahoo.de \
--to=ssbssa@yahoo.de \
--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).