From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 1BA773858D37; Fri, 28 Apr 2023 22:31:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BA773858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682721070; bh=K22GYBOemO5Nhi/tQvNbbM9kITecBDno/dPZ7dJwocA=; h=From:To:Subject:Date:From; b=FWCKxPS0QEnQ89X7B9/q2YobxEcCYbCkqz72NNDlZhKKTiaWsAnNvIivdbVriIJvm MbKcsajkWzCsr+VRkTpT01byGdIZejec5BSsOZT8D6xa8JY2NQEHsIOvtH/dIOHAVe WoLb03j5cMQ2eoYOpoLoraJcv2aO46R3bX2sSJFg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7270] libstdc++: Fix __max_diff_type::operator>>= for negative values X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 2f0e2f97d0129be4c2d17097c4a596c0dae9277d X-Git-Newrev: 2dcd5d5ae3679833b56d0d5f4078aab84ebf71ec Message-Id: <20230428223110.1BA773858D37@sourceware.org> Date: Fri, 28 Apr 2023 22:31:10 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2dcd5d5ae3679833b56d0d5f4078aab84ebf71ec commit r13-7270-g2dcd5d5ae3679833b56d0d5f4078aab84ebf71ec Author: Patrick Palka Date: Mon Apr 24 13:39:54 2023 -0400 libstdc++: Fix __max_diff_type::operator>>= for negative values This patch fixes sign bit propagation when right-shifting a negative __max_diff_type value by more than one, a bug that our existing test coverage didn't expose until r14-159-g03cebd304955a6 fixed the front end's 'signed typedef-name' handling that the test relies on (which is a non-standard extension to the language grammar). libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (__max_diff_type::operator>>=): Fix propagation of sign bit. * testsuite/std/ranges/iota/max_size_type.cc: Avoid using the non-standard 'signed typedef-name'. Add some compile-time tests for right-shifting a negative __max_diff_type value by more than one. (cherry picked from commit 83470a5cd4c3d233e1d55b5e5553e1b9c553bf28) Diff: --- libstdc++-v3/include/bits/max_size_type.h | 3 ++- libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/bits/max_size_type.h b/libstdc++-v3/include/bits/max_size_type.h index 92b8168d02f..4796135d073 100644 --- a/libstdc++-v3/include/bits/max_size_type.h +++ b/libstdc++-v3/include/bits/max_size_type.h @@ -560,7 +560,8 @@ namespace ranges // Arithmetic right shift. const auto __msb = _M_rep._M_msb; _M_rep >>= __r._M_rep; - _M_rep._M_msb |= __msb; + if (__msb) + _M_rep |= ~(__max_size_type(-1) >> __r._M_rep); return *this; } diff --git a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc index 06114c22cae..54c26ba2b4b 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc @@ -26,8 +26,14 @@ using max_size_t = std::ranges::__detail::__max_size_type; using max_diff_t = std::ranges::__detail::__max_diff_type; using rep_t = max_size_t::__rep; +#if __SIZEOF_INT128__ +using signed_rep_t = __int128; +#else +using signed_rep_t = long long; +#endif static_assert(sizeof(max_size_t) == sizeof(max_diff_t)); +static_assert(sizeof(rep_t) == sizeof(signed_rep_t)); static_assert(std::regular); static_assert(std::totally_ordered); @@ -54,6 +60,8 @@ test01() static_assert(max_diff_t(3) % -2 == 1); static_assert(max_diff_t(-3) << 1 == -6); static_assert(max_diff_t(-3) >> 1 == -2); + static_assert(max_diff_t(-3) >> 2 == -1); + static_assert(max_diff_t(-3) >> 10 == -1); static_assert(max_diff_t(3) >> 1 == 1); static_assert(max_diff_t(3) >> 2 == 0); @@ -188,7 +196,7 @@ template void test02() { - using hw_type = std::conditional_t; + using hw_type = std::conditional_t; using max_type = std::conditional_t; using shorten_type = std::conditional_t; const int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__; @@ -246,7 +254,7 @@ template void test03() { - using hw_type = std::conditional_t; + using hw_type = std::conditional_t; using max_type = std::conditional_t; using base_type = std::conditional_t; constexpr int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__;