* [PATCH] libstdc++: Use C++20 constraints in <bit>
@ 2023-09-14 14:12 Patrick Palka
2023-09-14 17:54 ` Jonathan Wakely
0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2023-09-14 14:12 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Patrick Palka
Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
-- >8 --
By now it's probably safe to assume supported compilers have full
concepts support in C++20 mode. And using a requires-clase instead
enable_if_t inside the return type greatly reduces the sizes of the
corresponding symbol names.
libstdc++-v3/ChangeLog:
* include/std/bit: Include <concepts>.
(byteswap): Use a requires-clause instead of enable_if_t
inside the return type.
(_If_is_unsigned_integer): Replace with ...
(__unsigned_integer): ... this.
(rotl): Use a requires-clause instead of enable_if_t
inside the return type.
(countl_zero): Likewise.
(countl_one): Likewise.
(countr_zero): Likewise.
(countr_one): Likewise.
(popcount): Likewise.
(has_single_bit): Likewise.
(bit_ceil): Likewise.
(bit_floor): Likewise.
(bit_width): Likewise.
---
libstdc++-v3/include/std/bit | 54 ++++++++++++++++++------------------
1 file changed, 27 insertions(+), 27 deletions(-)
diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
index 987b6cdbb35..dce61b440c5 100644
--- a/libstdc++-v3/include/std/bit
+++ b/libstdc++-v3/include/std/bit
@@ -33,6 +33,7 @@
#if __cplusplus >= 201402L
+#include <concepts> // for std::integral
#include <type_traits>
#if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
@@ -103,9 +104,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @return An object of the same type, with the bytes reversed.
* @since C++23
*/
- template<typename _Tp>
+ template<integral _Tp>
[[nodiscard]]
- constexpr enable_if_t<is_integral<_Tp>::value, _Tp>
+ constexpr _Tp
byteswap(_Tp __value) noexcept
{
if constexpr (sizeof(_Tp) == 1)
@@ -378,54 +379,53 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifdef __cpp_lib_bitops // C++ >= 20
/// @cond undocumented
- template<typename _Tp, typename _Up = _Tp>
- using _If_is_unsigned_integer
- = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
+ template<typename _Tp>
+ concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
/// @endcond
// [bit.rot], rotating
/// Rotate `x` to the left by `s` bits.
- template<typename _Tp>
- [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
+ template<__unsigned_integer _Tp>
+ [[nodiscard]] constexpr _Tp
rotl(_Tp __x, int __s) noexcept
{ return std::__rotl(__x, __s); }
/// Rotate `x` to the right by `s` bits.
- template<typename _Tp>
- [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
+ template<__unsigned_integer _Tp>
+ [[nodiscard]] constexpr _Tp
rotr(_Tp __x, int __s) noexcept
{ return std::__rotr(__x, __s); }
// [bit.count], counting
/// The number of contiguous zero bits, starting from the highest bit.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
countl_zero(_Tp __x) noexcept
{ return std::__countl_zero(__x); }
/// The number of contiguous one bits, starting from the highest bit.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
countl_one(_Tp __x) noexcept
{ return std::__countl_one(__x); }
/// The number of contiguous zero bits, starting from the lowest bit.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
countr_zero(_Tp __x) noexcept
{ return std::__countr_zero(__x); }
/// The number of contiguous one bits, starting from the lowest bit.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
countr_one(_Tp __x) noexcept
{ return std::__countr_one(__x); }
/// The number of bits set in `x`.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
popcount(_Tp __x) noexcept
{ return std::__popcount(__x); }
#endif // __cpp_lib_bitops
@@ -434,28 +434,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// [bit.pow.two], integral powers of 2
/// True if `x` is a power of two, false otherwise.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, bool>
+ template<__unsigned_integer _Tp>
+ constexpr bool
has_single_bit(_Tp __x) noexcept
{ return std::__has_single_bit(__x); }
/// The smallest power-of-two not less than `x`.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp>
+ template<__unsigned_integer _Tp>
+ constexpr _Tp
bit_ceil(_Tp __x) noexcept
{ return std::__bit_ceil(__x); }
/// The largest power-of-two not greater than `x`.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp>
+ template<__unsigned_integer _Tp>
+ constexpr _Tp
bit_floor(_Tp __x) noexcept
{ return std::__bit_floor(__x); }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3656. Inconsistent bit operations returning a count
/// The smallest integer greater than the base-2 logarithm of `x`.
- template<typename _Tp>
- constexpr _If_is_unsigned_integer<_Tp, int>
+ template<__unsigned_integer _Tp>
+ constexpr int
bit_width(_Tp __x) noexcept
{ return std::__bit_width(__x); }
#endif // defined (__cpp_lib_int_pow2)
--
2.42.0.158.g94e83dcf5b
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libstdc++: Use C++20 constraints in <bit>
2023-09-14 14:12 [PATCH] libstdc++: Use C++20 constraints in <bit> Patrick Palka
@ 2023-09-14 17:54 ` Jonathan Wakely
2023-09-14 18:58 ` Patrick Palka
0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2023-09-14 17:54 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches, libstdc++
On Thu, 14 Sept 2023 at 15:13, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
>
> -- >8 --
>
> By now it's probably safe to assume supported compilers have full
> concepts support in C++20 mode.
Clang 14.0.0 is good enough for these uses. It doesn't handle our
<ranges> properly, or conditionally trivial special member functions,
but that's not a problem here.
> And using a requires-clase instead
> enable_if_t inside the return type greatly reduces the sizes of the
> corresponding symbol names.
Will that change when we start to mangle constraints? :-)
OK for trunk.
>
> libstdc++-v3/ChangeLog:
>
> * include/std/bit: Include <concepts>.
> (byteswap): Use a requires-clause instead of enable_if_t
> inside the return type.
> (_If_is_unsigned_integer): Replace with ...
> (__unsigned_integer): ... this.
> (rotl): Use a requires-clause instead of enable_if_t
> inside the return type.
> (countl_zero): Likewise.
> (countl_one): Likewise.
> (countr_zero): Likewise.
> (countr_one): Likewise.
> (popcount): Likewise.
> (has_single_bit): Likewise.
> (bit_ceil): Likewise.
> (bit_floor): Likewise.
> (bit_width): Likewise.
> ---
> libstdc++-v3/include/std/bit | 54 ++++++++++++++++++------------------
> 1 file changed, 27 insertions(+), 27 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
> index 987b6cdbb35..dce61b440c5 100644
> --- a/libstdc++-v3/include/std/bit
> +++ b/libstdc++-v3/include/std/bit
> @@ -33,6 +33,7 @@
>
> #if __cplusplus >= 201402L
>
> +#include <concepts> // for std::integral
> #include <type_traits>
>
> #if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
> @@ -103,9 +104,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> * @return An object of the same type, with the bytes reversed.
> * @since C++23
> */
> - template<typename _Tp>
> + template<integral _Tp>
> [[nodiscard]]
> - constexpr enable_if_t<is_integral<_Tp>::value, _Tp>
> + constexpr _Tp
> byteswap(_Tp __value) noexcept
> {
> if constexpr (sizeof(_Tp) == 1)
> @@ -378,54 +379,53 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> #ifdef __cpp_lib_bitops // C++ >= 20
>
> /// @cond undocumented
> - template<typename _Tp, typename _Up = _Tp>
> - using _If_is_unsigned_integer
> - = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
> + template<typename _Tp>
> + concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
> /// @endcond
>
> // [bit.rot], rotating
>
> /// Rotate `x` to the left by `s` bits.
> - template<typename _Tp>
> - [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
> + template<__unsigned_integer _Tp>
> + [[nodiscard]] constexpr _Tp
> rotl(_Tp __x, int __s) noexcept
> { return std::__rotl(__x, __s); }
>
> /// Rotate `x` to the right by `s` bits.
> - template<typename _Tp>
> - [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
> + template<__unsigned_integer _Tp>
> + [[nodiscard]] constexpr _Tp
> rotr(_Tp __x, int __s) noexcept
> { return std::__rotr(__x, __s); }
>
> // [bit.count], counting
>
> /// The number of contiguous zero bits, starting from the highest bit.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> countl_zero(_Tp __x) noexcept
> { return std::__countl_zero(__x); }
>
> /// The number of contiguous one bits, starting from the highest bit.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> countl_one(_Tp __x) noexcept
> { return std::__countl_one(__x); }
>
> /// The number of contiguous zero bits, starting from the lowest bit.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> countr_zero(_Tp __x) noexcept
> { return std::__countr_zero(__x); }
>
> /// The number of contiguous one bits, starting from the lowest bit.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> countr_one(_Tp __x) noexcept
> { return std::__countr_one(__x); }
>
> /// The number of bits set in `x`.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> popcount(_Tp __x) noexcept
> { return std::__popcount(__x); }
> #endif // __cpp_lib_bitops
> @@ -434,28 +434,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> // [bit.pow.two], integral powers of 2
>
> /// True if `x` is a power of two, false otherwise.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, bool>
> + template<__unsigned_integer _Tp>
> + constexpr bool
> has_single_bit(_Tp __x) noexcept
> { return std::__has_single_bit(__x); }
>
> /// The smallest power-of-two not less than `x`.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp>
> + template<__unsigned_integer _Tp>
> + constexpr _Tp
> bit_ceil(_Tp __x) noexcept
> { return std::__bit_ceil(__x); }
>
> /// The largest power-of-two not greater than `x`.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp>
> + template<__unsigned_integer _Tp>
> + constexpr _Tp
> bit_floor(_Tp __x) noexcept
> { return std::__bit_floor(__x); }
>
> // _GLIBCXX_RESOLVE_LIB_DEFECTS
> // 3656. Inconsistent bit operations returning a count
> /// The smallest integer greater than the base-2 logarithm of `x`.
> - template<typename _Tp>
> - constexpr _If_is_unsigned_integer<_Tp, int>
> + template<__unsigned_integer _Tp>
> + constexpr int
> bit_width(_Tp __x) noexcept
> { return std::__bit_width(__x); }
> #endif // defined (__cpp_lib_int_pow2)
> --
> 2.42.0.158.g94e83dcf5b
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libstdc++: Use C++20 constraints in <bit>
2023-09-14 17:54 ` Jonathan Wakely
@ 2023-09-14 18:58 ` Patrick Palka
0 siblings, 0 replies; 3+ messages in thread
From: Patrick Palka @ 2023-09-14 18:58 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: Patrick Palka, gcc-patches, libstdc++
On Thu, 14 Sep 2023, Jonathan Wakely wrote:
> On Thu, 14 Sept 2023 at 15:13, Patrick Palka via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> >
> > -- >8 --
> >
> > By now it's probably safe to assume supported compilers have full
> > concepts support in C++20 mode.
>
> Clang 14.0.0 is good enough for these uses. It doesn't handle our
> <ranges> properly, or conditionally trivial special member functions,
> but that's not a problem here.
Makes sense.
>
> > And using a requires-clase instead
> > enable_if_t inside the return type greatly reduces the sizes of the
> > corresponding symbol names.
>
> Will that change when we start to mangle constraints? :-)
In this specific case it should still be very worthwhile, since the
mangling of _If_is_unsigned_integer<_Tp> is around 200 characters due to
alias template transparency. IIUC the mangling for the new constraint
__unsigned_integer<_Tp> should be around the same size as written since
we won't normalize concept-ids when mangling them like we do alias
template-ids.
>
> OK for trunk.
Thanks!
>
>
> >
> > libstdc++-v3/ChangeLog:
> >
> > * include/std/bit: Include <concepts>.
> > (byteswap): Use a requires-clause instead of enable_if_t
> > inside the return type.
> > (_If_is_unsigned_integer): Replace with ...
> > (__unsigned_integer): ... this.
> > (rotl): Use a requires-clause instead of enable_if_t
> > inside the return type.
> > (countl_zero): Likewise.
> > (countl_one): Likewise.
> > (countr_zero): Likewise.
> > (countr_one): Likewise.
> > (popcount): Likewise.
> > (has_single_bit): Likewise.
> > (bit_ceil): Likewise.
> > (bit_floor): Likewise.
> > (bit_width): Likewise.
> > ---
> > libstdc++-v3/include/std/bit | 54 ++++++++++++++++++------------------
> > 1 file changed, 27 insertions(+), 27 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit
> > index 987b6cdbb35..dce61b440c5 100644
> > --- a/libstdc++-v3/include/std/bit
> > +++ b/libstdc++-v3/include/std/bit
> > @@ -33,6 +33,7 @@
> >
> > #if __cplusplus >= 201402L
> >
> > +#include <concepts> // for std::integral
> > #include <type_traits>
> >
> > #if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
> > @@ -103,9 +104,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > * @return An object of the same type, with the bytes reversed.
> > * @since C++23
> > */
> > - template<typename _Tp>
> > + template<integral _Tp>
> > [[nodiscard]]
> > - constexpr enable_if_t<is_integral<_Tp>::value, _Tp>
> > + constexpr _Tp
> > byteswap(_Tp __value) noexcept
> > {
> > if constexpr (sizeof(_Tp) == 1)
> > @@ -378,54 +379,53 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > #ifdef __cpp_lib_bitops // C++ >= 20
> >
> > /// @cond undocumented
> > - template<typename _Tp, typename _Up = _Tp>
> > - using _If_is_unsigned_integer
> > - = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
> > + template<typename _Tp>
> > + concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
> > /// @endcond
> >
> > // [bit.rot], rotating
> >
> > /// Rotate `x` to the left by `s` bits.
> > - template<typename _Tp>
> > - [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
> > + template<__unsigned_integer _Tp>
> > + [[nodiscard]] constexpr _Tp
> > rotl(_Tp __x, int __s) noexcept
> > { return std::__rotl(__x, __s); }
> >
> > /// Rotate `x` to the right by `s` bits.
> > - template<typename _Tp>
> > - [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
> > + template<__unsigned_integer _Tp>
> > + [[nodiscard]] constexpr _Tp
> > rotr(_Tp __x, int __s) noexcept
> > { return std::__rotr(__x, __s); }
> >
> > // [bit.count], counting
> >
> > /// The number of contiguous zero bits, starting from the highest bit.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > countl_zero(_Tp __x) noexcept
> > { return std::__countl_zero(__x); }
> >
> > /// The number of contiguous one bits, starting from the highest bit.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > countl_one(_Tp __x) noexcept
> > { return std::__countl_one(__x); }
> >
> > /// The number of contiguous zero bits, starting from the lowest bit.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > countr_zero(_Tp __x) noexcept
> > { return std::__countr_zero(__x); }
> >
> > /// The number of contiguous one bits, starting from the lowest bit.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > countr_one(_Tp __x) noexcept
> > { return std::__countr_one(__x); }
> >
> > /// The number of bits set in `x`.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > popcount(_Tp __x) noexcept
> > { return std::__popcount(__x); }
> > #endif // __cpp_lib_bitops
> > @@ -434,28 +434,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> > // [bit.pow.two], integral powers of 2
> >
> > /// True if `x` is a power of two, false otherwise.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, bool>
> > + template<__unsigned_integer _Tp>
> > + constexpr bool
> > has_single_bit(_Tp __x) noexcept
> > { return std::__has_single_bit(__x); }
> >
> > /// The smallest power-of-two not less than `x`.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp>
> > + template<__unsigned_integer _Tp>
> > + constexpr _Tp
> > bit_ceil(_Tp __x) noexcept
> > { return std::__bit_ceil(__x); }
> >
> > /// The largest power-of-two not greater than `x`.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp>
> > + template<__unsigned_integer _Tp>
> > + constexpr _Tp
> > bit_floor(_Tp __x) noexcept
> > { return std::__bit_floor(__x); }
> >
> > // _GLIBCXX_RESOLVE_LIB_DEFECTS
> > // 3656. Inconsistent bit operations returning a count
> > /// The smallest integer greater than the base-2 logarithm of `x`.
> > - template<typename _Tp>
> > - constexpr _If_is_unsigned_integer<_Tp, int>
> > + template<__unsigned_integer _Tp>
> > + constexpr int
> > bit_width(_Tp __x) noexcept
> > { return std::__bit_width(__x); }
> > #endif // defined (__cpp_lib_int_pow2)
> > --
> > 2.42.0.158.g94e83dcf5b
> >
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-14 18:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-14 14:12 [PATCH] libstdc++: Use C++20 constraints in <bit> Patrick Palka
2023-09-14 17:54 ` Jonathan Wakely
2023-09-14 18:58 ` Patrick Palka
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).