public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-205] libstdc++: Fix __max_diff_type::operator>>= for negative values
Date: Mon, 24 Apr 2023 17:41:44 +0000 (GMT)	[thread overview]
Message-ID: <20230424174144.8D5213858C39@sourceware.org> (raw)

https://gcc.gnu.org/g:83470a5cd4c3d233e1d55b5e5553e1b9c553bf28

commit r14-205-g83470a5cd4c3d233e1d55b5e5553e1b9c553bf28
Author: Patrick Palka <ppalka@redhat.com>
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.

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<max_size_t>);
 static_assert(std::totally_ordered<max_size_t>);
@@ -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<bool signed_p, bool shorten_p>
 void
 test02()
 {
-  using hw_type = std::conditional_t<signed_p, signed rep_t, rep_t>;
+  using hw_type = std::conditional_t<signed_p, signed_rep_t, rep_t>;
   using max_type = std::conditional_t<signed_p, max_diff_t, max_size_t>;
   using shorten_type = std::conditional_t<shorten_p, hw_type, max_type>;
   const int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__;
@@ -246,7 +254,7 @@ template<bool signed_p, bool toggle_base_p>
 void
 test03()
 {
-  using hw_type = std::conditional_t<signed_p, signed rep_t, rep_t>;
+  using hw_type = std::conditional_t<signed_p, signed_rep_t, rep_t>;
   using max_type = std::conditional_t<signed_p, max_diff_t, max_size_t>;
   using base_type = std::conditional_t<toggle_base_p, hw_type, max_type>;
   constexpr int hw_type_bit_size = sizeof(hw_type) * __CHAR_BIT__;

                 reply	other threads:[~2023-04-24 17:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230424174144.8D5213858C39@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.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).