* [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values
@ 2023-04-24 16:23 Patrick Palka
2023-04-24 16:28 ` Patrick Palka
2023-04-24 16:39 ` Jonathan Wakely
0 siblings, 2 replies; 3+ messages in thread
From: Patrick Palka @ 2023-04-24 16:23 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Patrick Palka
This patch fixes sign bit propagation when right-shifting a negative
__max_diff_type value by more than one, a bug which our test coverage
failed to uncover until r14-159-g03cebd304955a6 fixed the front end's
'signed typedef-name' handling (which is a non-standard extension to
the language grammar).
Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13.2/12/11?
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
'signed typedef-name'. Add compile-time tests for
right-shifting a negative __max_diff_type value by more than
one.
---
libstdc++-v3/include/bits/max_size_type.h | 3 ++-
.../testsuite/std/ranges/iota/max_size_type.cc | 11 +++++++++--
2 files changed, 11 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..985acd5a803 100644
--- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
+++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
@@ -26,6 +26,11 @@
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 = signed __int128;
+#else
+using signed_rep_t = long long;
+#endif
static_assert(sizeof(max_size_t) == sizeof(max_diff_t));
@@ -54,6 +59,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 +195,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 +253,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__;
--
2.40.0.374.g7580f92ffa
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values
2023-04-24 16:23 [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values Patrick Palka
@ 2023-04-24 16:28 ` Patrick Palka
2023-04-24 16:39 ` Jonathan Wakely
1 sibling, 0 replies; 3+ messages in thread
From: Patrick Palka @ 2023-04-24 16:28 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, libstdc++
On Mon, 24 Apr 2023, Patrick Palka wrote:
> This patch fixes sign bit propagation when right-shifting a negative
> __max_diff_type value by more than one, a bug which our test coverage
> failed to uncover until r14-159-g03cebd304955a6 fixed the front end's
> 'signed typedef-name' handling (which is a non-standard extension to
> the language grammar).
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13.2/12/11?
>
> 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
> 'signed typedef-name'. Add compile-time tests for
> right-shifting a negative __max_diff_type value by more than
> one.
> ---
> libstdc++-v3/include/bits/max_size_type.h | 3 ++-
> .../testsuite/std/ranges/iota/max_size_type.cc | 11 +++++++++--
> 2 files changed, 11 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..985acd5a803 100644
> --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
> @@ -26,6 +26,11 @@
> 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 = signed __int128;
Whoops, I suppose this 'signed' qualifier is redundant so consider it
removed.
> +#else
> +using signed_rep_t = long long;
> +#endif
>
> static_assert(sizeof(max_size_t) == sizeof(max_diff_t));
>
> @@ -54,6 +59,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 +195,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 +253,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__;
> --
> 2.40.0.374.g7580f92ffa
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values
2023-04-24 16:23 [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values Patrick Palka
2023-04-24 16:28 ` Patrick Palka
@ 2023-04-24 16:39 ` Jonathan Wakely
1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2023-04-24 16:39 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, libstdc++
On Mon, 24 Apr 2023 at 17:24, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> This patch fixes sign bit propagation when right-shifting a negative
> __max_diff_type value by more than one, a bug which our test coverage
> failed to uncover until r14-159-g03cebd304955a6 fixed the front end's
> 'signed typedef-name' handling (which is a non-standard extension to
> the language grammar).
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13.2/12/11?
Yes, OK for all (but gcc-13 only after 13.1 is released of course).
>
> 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
> 'signed typedef-name'. Add compile-time tests for
> right-shifting a negative __max_diff_type value by more than
> one.
> ---
> libstdc++-v3/include/bits/max_size_type.h | 3 ++-
> .../testsuite/std/ranges/iota/max_size_type.cc | 11 +++++++++--
> 2 files changed, 11 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..985acd5a803 100644
> --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
> @@ -26,6 +26,11 @@
> 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 = signed __int128;
> +#else
> +using signed_rep_t = long long;
> +#endif
>
> static_assert(sizeof(max_size_t) == sizeof(max_diff_t));
>
> @@ -54,6 +59,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 +195,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 +253,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__;
> --
> 2.40.0.374.g7580f92ffa
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-04-24 16:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-24 16:23 [PATCH] libstdc++: Fix __max_diff_type::operator>>= for negative values Patrick Palka
2023-04-24 16:28 ` Patrick Palka
2023-04-24 16:39 ` Jonathan Wakely
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).