From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2111) id EA8C0385DDC0; Tue, 11 Jun 2024 18:37:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA8C0385DDC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1718131036; bh=RBecGBPr3O+P2Ic3gpBQ777rNuzzewIIDSqjBUJ65cs=; h=From:To:Subject:Date:From; b=svLwSkKll6lkmDlFRe6klTb6qbbJ5CNEW8BWRCbsxzaQSdckwVZiVRYVk2FEEnuax xz2CfX9uGINHPw6lUpYqoXtbdNevOuNohVGsXd4O3fdQVIvxvz4e0RmkSZefEi/s5v yiMbm8EKEUmmrf2e7KJjdhG4vp7QeQeoNo4FqI+U= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Hannes Domani To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix right shift of negative numbers X-Act-Checkin: binutils-gdb X-Git-Author: Hannes Domani X-Git-Refname: refs/heads/master X-Git-Oldrev: 4bafd5b7f377bac19a6dad748f6a162556696c01 X-Git-Newrev: d17731525424349d7e63b517acf9f45114979fbb Message-Id: <20240611183716.EA8C0385DDC0@sourceware.org> Date: Tue, 11 Jun 2024 18:37:16 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd17731525424= 349d7e63b517acf9f45114979fbb commit d17731525424349d7e63b517acf9f45114979fbb Author: Hannes Domani Date: Tue Jun 11 20:32:27 2024 +0200 Fix right shift of negative numbers =20 PR31590 shows that right shift of negative numbers doesn't work correctly since GDB 14: =20 (gdb) p (-3) >> 1 $1 =3D -1 =20 GDB 13 and earlier returned the correct value -2. And there actually is one test that shows the failure: =20 print -1 >> 1 $84 =3D 0 (gdb) FAIL: gdb.base/bitshift.exp: lang=3Dasm: rsh neg lhs: print -1 >>= 1 =20 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: =20 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. =20 So this changes mpz_tdiv_q_2exp to mpz_fdiv_q_2exp, since it does right shifts for both positive and negative numbers. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D31590 Approved-By: Tom Tromey Diff: --- 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; } =20 gdb_mpz &operator>>=3D (unsigned long nbits) { - mpz_tdiv_q_2exp (m_val, m_val, nbits); + mpz_fdiv_q_2exp (m_val, m_val, nbits); return *this; } =20 diff --git a/gdb/testsuite/gdb.base/bitshift.exp b/gdb/testsuite/gdb.base/b= itshift.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" " =3D -1" test_shift $lang "print -1 >> 1" " =3D -1" + test_shift $lang "print -2 >> 1" " =3D -1" + test_shift $lang "print -3 >> 1" " =3D -2" test_shift $lang "print -8 >> 1" " =3D -4" test_shift $lang "print [make_int64 $lang -8] >> 1" " =3D -4" }