libstdc++: Implement non-simd part of P3642R7 - Carry-less product: std::clmul

Message ID aun_3IV-gilYbFbc@tucnak
State New
Headers
Series libstdc++: Implement non-simd part of P3642R7 - Carry-less product: std::clmul |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed

Commit Message

Jakub Jelinek Nov. 3, 2026, 3:38 p.m. UTC
  Hi!

The following patch attempts to implement
P3642R7 - Carry-less product: std::clmul
paper (which is not approved yet), on top of
https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736969.html
__builtin_clmulg patch.

So far lightly tested.

2026-11-03  Jakub Jelinek  <jakub@redhat.com>

	* include/bits/version.def (clmul): New FTM.
	* include/std/numeric (__glibcxx_want_clmul): Define before including
	bits/version.h.
	(std::wide_result): New class template.
	(std::widening_clmul, std::clmul): New function templates.
	* src/c++23/std.cc.in (std::wide_result, std::widening_clmul,
	std::clmul): Export for C++29.
	* testsuite/26_numerics/clmul/1.cc: New test.
	* include/bits/version.h: Regenerate.


	Jakub
  

Comments

Jonathan Wakely Nov. 3, 2026, 6:23 p.m. UTC | #1
On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <jakub@redhat.com> wrote:
>
> Hi!
>
> The following patch attempts to implement
> P3642R7 - Carry-less product: std::clmul
> paper (which is not approved yet), on top of
> https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736969.html
> __builtin_clmulg patch.
>
> So far lightly tested.
>
> 2026-11-03  Jakub Jelinek  <jakub@redhat.com>
>
>         * include/bits/version.def (clmul): New FTM.
>         * include/std/numeric (__glibcxx_want_clmul): Define before including
>         bits/version.h.
>         (std::wide_result): New class template.
>         (std::widening_clmul, std::clmul): New function templates.
>         * src/c++23/std.cc.in (std::wide_result, std::widening_clmul,
>         std::clmul): Export for C++29.
>         * testsuite/26_numerics/clmul/1.cc: New test.
>         * include/bits/version.h: Regenerate.
>
> --- a/libstdc++-v3/include/bits/version.def     2026-11-03 10:17:22.408691156 +0100
> +++ b/libstdc++-v3/include/bits/version.def     2026-11-03 13:32:07.741581918 +0100
> @@ -2716,6 +2716,14 @@ ftms = {
>    };
>  };
>
> +ftms = {
> +  name = clmul;
> +  values = {
> +    v = 202611;
> +    cxxmin = 29;
> +  };
> +};
> +
>  // Standard test specifications.
>  stds[97] = ">= 199711L";
>  stds[03] = ">= 199711L";
> --- a/libstdc++-v3/include/std/numeric  2026-01-02 22:41:17.669547090 +0100
> +++ b/libstdc++-v3/include/std/numeric  2026-11-03 15:37:24.215814718 +0100
> @@ -90,6 +90,7 @@
>  #define __glibcxx_want_parallel_algorithm
>  #define __glibcxx_want_ranges_iota
>  #define __glibcxx_want_saturation_arithmetic
> +#define __glibcxx_want_clmul
>  #include <bits/version.h>
>
>  #if __glibcxx_ranges_iota >= 202202L // C++ >= 23
> @@ -765,6 +766,72 @@ namespace ranges
>  } // namespace ranges
>  #endif // __glibcxx_ranges_iota
>
> +#if __glibcxx_clmul >= 202611L // C++29
> +  template<typename _Tp>
> +    class wide_result {
> +    public:
> +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> +       : _M_high(__high), _M_low(__low) {}
> +
> +      constexpr _Tp high() const noexcept { return _M_high; }
> +      constexpr _Tp low() const noexcept { return _M_low; }
> +
> +      friend constexpr bool operator==(const wide_result&,
> +                                      const wide_result&) = default;
> +      friend constexpr strong_ordering operator<=>(const wide_result&,
> +                                                  const wide_result&)
> +       noexcept = default;
> +    private:
> +      _Tp _M_high, _M_low;

LWG wanted this to be a class with private members rather than an
aggregate with public members so that the high and low parts could be
ordered optimally according to the target endianness, e.g. so that
wide_result<uint64_t> would have the same layout as unsigned int128.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result

Do we want to take advantage of that?

I think we need static_assert(__is_unsigned_integer<_Tp>::value);


> +    };
> +
> +  template<__unsigned_integer _Tp>
> +    [[nodiscard]] constexpr wide_result<_Tp>
> +    widening_clmul(_Tp __x, _Tp __y) noexcept
> +    {
> +      _Tp __high, __low;
> +#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
> +      __low = __builtin_clmulg(__x, __y, &__high);
> +      return wide_result<_Tp>(__high, __low);
> +#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul) \
> +      && _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_bitreverseg)
> +      __low = __builtin_elementwise_clmul(__x, __y);
> +      __high = __builtin_elementwise_clmul(__builtin_bitreverseg(__x),
> +                                          __builtin_bitreverseg(__y));
> +      return wide_result<_Tp>(__builtin_bitreverseg(__high) >> 1, __low);
> +#else
> +      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
> +      __high = 0;
> +      __low = (__y & _Tp(1)) ? __x : 0;
> +      for (int __i = 1; __i < _Nd; ++__i)
> +       if ((__y >> __i) & _Tp(1))
> +         {
> +           __low ^= __x << __i;
> +           __high ^= __x >> (_Nd - __i);
> +         }
> +      return wide_result<_Tp>(__high, __low);
> +#endif
> +    }
> +
> +  template<__unsigned_integer _Tp>
> +    [[nodiscard]] constexpr _Tp
> +    clmul(_Tp __x, _Tp __y) noexcept
> +    {
> +#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
> +      return __builtin_clmulg(__x, __y);
> +#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul)
> +      return __builtin_elementwise_clmul(__x, __y);
> +#else
> +      _Tp __res = 0;
> +      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
> +      for (int __i = 0; __i < _Nd; ++__i)
> +       if ((__y >> __i) & _Tp(1))
> +         __res ^= __x << __i;
> +      return __res;
> +#endif
> +    }
> +#endif // C++29
> +
>  _GLIBCXX_END_NAMESPACE_VERSION
>  } // namespace std
>
> --- a/libstdc++-v3/src/c++23/std.cc.in  2026-11-03 11:22:24.260388845 +0100
> +++ b/libstdc++-v3/src/c++23/std.cc.in  2026-11-03 14:56:19.515191718 +0100
> @@ -2488,6 +2488,11 @@ export namespace std
>    using std::saturating_div;
>    using std::saturating_cast;
>  #endif
> +#if __cpp_lib_clmul
> +  using std::wide_result;
> +  using std::widening_clmul;
> +  using std::clmul;
> +#endif
>  }
>
>  // <optional>
> --- a/libstdc++-v3/testsuite/26_numerics/clmul/1.cc     2026-11-03 14:12:06.715065853 +0100
> +++ b/libstdc++-v3/testsuite/26_numerics/clmul/1.cc     2026-11-03 15:51:59.929951366 +0100
> @@ -0,0 +1,205 @@
> +// { dg-do run { target c++29 } }
> +
> +#include <numeric>
> +
> +#ifndef __cpp_lib_clmul
> +# error "Feature-test macro for clmul missing in <numeric>"
> +#elif __cpp_lib_clmul != 202611L
> +# error "Feature-test macro for clmul has wrong value in <numeric>"
> +#endif
> +
> +#include <testsuite_hooks.h>
> +
> +static_assert(std::wide_result<unsigned>(42U, 43U).low() == 43);
> +static_assert(std::wide_result<unsigned>(42U, 43U).high() == 42);
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             == std::wide_result<unsigned>(42U, 43U));
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             != std::wide_result<unsigned>(42U, 44U));
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             != std::wide_result<unsigned>(43U, 43U));
> +static_assert(std::wide_result<unsigned>(42U, 45U)
> +             < std::wide_result<unsigned>(43U, 43U));
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             < std::wide_result<unsigned>(42U, 44U));
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             > std::wide_result<unsigned>(41U, 44U));
> +static_assert(std::wide_result<unsigned>(42U, 43U)
> +             > std::wide_result<unsigned>(42U, 42U));
> +static_assert(std::is_same<decltype(std::widening_clmul((unsigned char) 0,
> +                                                       (unsigned char) 0)),
> +                          std::wide_result<unsigned char>>::value);
> +static_assert(std::is_same<decltype(std::widening_clmul((unsigned short) 0,
> +                                                       (unsigned short) 0)),
> +                          std::wide_result<unsigned short>>::value);
> +static_assert(std::is_same<decltype(std::widening_clmul(0U, 0U)),
> +                          std::wide_result<unsigned int>>::value);
> +static_assert(std::is_same<decltype(std::widening_clmul(0UL, 0UL)),
> +                          std::wide_result<unsigned long>>::value);
> +static_assert(std::is_same<decltype(std::widening_clmul(0ULL, 0ULL)),
> +                          std::wide_result<unsigned long long>>::value);
> +#if __CHAR_BIT__ == 8
> +static_assert(std::clmul((unsigned char) 0x7e,
> +                        (unsigned char) 0x9c) == 0xe8);
> +static_assert(std::widening_clmul((unsigned char) 0x7e,
> +                                 (unsigned char) 0x9c).low() == 0xe8);
> +static_assert(std::widening_clmul((unsigned char) 0x7e,
> +                                 (unsigned char) 0x9c).high() == 0x3a);
> +#if __SIZEOF_SHORT__ == 2
> +static_assert(std::clmul((unsigned short) 0xc8aa,
> +                        (unsigned short) 0x7616) == 0xa55c);
> +static_assert(std::widening_clmul((unsigned short) 0xc8aa,
> +                                 (unsigned short) 0x7616)
> +             == std::wide_result<unsigned short>(0x2508, 0xa55c));
> +#endif
> +#if __SIZEOF_INT__ == 4
> +static_assert(std::clmul(0x9f330680U, 0x4e157eadU) == 0xb23fb880U);
> +static_assert(std::widening_clmul(0x9f330680U, 0x4e157eadU)
> +             == std::wide_result<unsigned>(0x207cd974U, 0xb23fb880U));
> +static_assert(std::clmul(0x11111111U, 0x11111111U) == 0x1010101U);
> +static_assert(std::widening_clmul(0x11111111U, 0x11111111U)
> +             == std::wide_result<unsigned>(0x1010101U, 0x1010101U));
> +static_assert(std::clmul(~0U, ~0U) == 0x55555555U);
> +static_assert(std::widening_clmul(~0U, ~0U)
> +             == std::wide_result<unsigned>(0x55555555U, 0x55555555U));
> +#endif
> +#if __SIZEOF_LONG__ == 4
> +static_assert(std::clmul(0xcd63e2c1UL, 0xab176a37UL) == 0x2f053277UL);
> +static_assert(std::widening_clmul(0xcd63e2c1UL, 0xab176a37UL)
> +             == std::wide_result<unsigned long>(0x782faf0eUL, 0x2f053277UL));
> +#elif __SIZEOF_LONG__ == 8
> +static_assert(std::clmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
> +             == 0xe8465657c87e7a1dUL);
> +static_assert(std::widening_clmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
> +             == std::wide_result<unsigned long>(0xe2f8ec31f25371bUL,
> +                                                0xe8465657c87e7a1dUL));
> +#endif
> +#if __SIZEOF_LONG_LONG__ == 8
> +static_assert(std::clmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
> +             == 0xfaf0ddab7a75024ULL);
> +static_assert(std::widening_clmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
> +             == std::wide_result<unsigned long long>(0x29daaa7ee7cd2860ULL,
> +                                                     0xfaf0ddab7a75024ULL));
> +static_assert(std::clmul(0x11111111ULL, 0x11111111ULL)
> +             == 0x101010101010101ULL);
> +static_assert(std::widening_clmul(0x11111111ULL, 0x11111111ULL)
> +             == std::wide_result<unsigned long long>(0ULL,
> +                                                     0x101010101010101ULL));
> +static_assert(std::clmul(0x1111111111111111ULL, 0x1111111111111111ULL)
> +             == 0x101010101010101ULL);
> +static_assert(std::widening_clmul(0x1111111111111111ULL, 0x1111111111111111ULL)
> +             == std::wide_result<unsigned long long>(0x101010101010101ULL,
> +                                                     0x101010101010101ULL));
> +static_assert(std::clmul(~0ULL, ~0ULL)
> +             == 0x5555555555555555ULL);
> +static_assert(std::widening_clmul(~0ULL, ~0ULL)
> +             == std::wide_result<unsigned long long>(0x5555555555555555ULL,
> +                                                     0x5555555555555555ULL));
> +#endif
> +#if __SIZEOF_INT128__ == 16
> +static_assert(std::clmul((((unsigned __int128) 0x983a442a106b8a83ULL) << 64)
> +                        | 0x1866ccfa2e2b382cULL,
> +                        (((unsigned __int128) 0xe9abf4d856d535c3ULL) << 64)
> +                        | 0x58708f117a52c14cULL)
> +             == ((((unsigned __int128) 0xd3ae65b0bac566b2ULL) << 64)
> +                 | 0xdd6b33f9bb8606d0ULL));
> +static_assert(std::widening_clmul((((unsigned __int128) 0x983a442a106b8a83ULL)
> +                                  << 64) | 0x1866ccfa2e2b382cULL,
> +                                 (((unsigned __int128) 0xe9abf4d856d535c3ULL)
> +                                  << 64) | 0x58708f117a52c14cULL)
> +             == std::wide_result<unsigned __int128>((((unsigned __int128)
> +                                                      0x7d17129902648920ULL)
> +                                                     << 64)
> +                                                    | 0xfdb34e3b7c058047ULL,
> +                                                    (((unsigned __int128)
> +                                                      0xd3ae65b0bac566b2ULL)
> +                                                     << 64)
> +                                                    | 0xdd6b33f9bb8606d0ULL));
> +#endif
> +#endif
> +
> +template<typename T>
> +[[gnu::noipa]] std::wide_result<T>
> +myclmul(T x, T y)
> +{
> +  auto r = std::widening_clmul(x, y);
> +  VERIFY( std::clmul(x, y) == r.low() );
> +  return r;
> +}
> +
> +template<typename T>
> +using W = std::wide_result<T>;
> +
> +void
> +test01()
> +{
> +#if __CHAR_BIT__ == 8
> +  VERIFY( myclmul((unsigned char) 0x7e, (unsigned char) 0x9c)
> +         == W<unsigned char>(0x3a, 0xe8) );
> +#if __SIZEOF_SHORT__ == 2
> +  VERIFY( myclmul((unsigned short) 0xc8aa, (unsigned short) 0x7616)
> +         == W<unsigned short>(0x2508, 0xa55c) );
> +#endif
> +#if __SIZEOF_INT__ == 4
> +  VERIFY( myclmul(0x9f330680U, 0x4e157eadU)
> +         == W<unsigned>(0x207cd974U, 0xb23fb880U) );
> +  VERIFY( myclmul(0x11111111U, 0x11111111U)
> +         == W<unsigned>(0x1010101U, 0x1010101U) );
> +  VERIFY( myclmul(~0U, ~0U)
> +         == W<unsigned>(0x55555555U, 0x55555555U) );
> +#endif
> +#if __SIZEOF_LONG__ == 4
> +  VERIFY( myclmul(0xcd63e2c1UL, 0xab176a37UL)
> +         == W<unsigned long>(0x782faf0eUL, 0x2f053277UL) );
> +#elif __SIZEOF_LONG__ == 8
> +  VERIFY( myclmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
> +         == W<unsigned long>(0xe2f8ec31f25371bUL, 0xe8465657c87e7a1dUL) );
> +#endif
> +#if __SIZEOF_LONG_LONG__ == 8
> +  VERIFY( myclmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
> +         == W<unsigned long long>(0x29daaa7ee7cd2860ULL,
> +                                  0xfaf0ddab7a75024ULL) );
> +  VERIFY( myclmul(0x11111111ULL, 0x11111111ULL)
> +         == W<unsigned long long>(0ULL, 0x101010101010101ULL) );
> +  VERIFY( myclmul(0x1111111111111111ULL, 0x1111111111111111ULL)
> +         == W<unsigned long long>(0x101010101010101ULL,
> +                                  0x101010101010101ULL) );
> +  VERIFY( myclmul(~0ULL, ~0ULL)
> +         == W<unsigned long long>(0x5555555555555555ULL,
> +                                  0x5555555555555555ULL) );
> +#endif
> +#if __SIZEOF_INT128__ == 16
> +  VERIFY( myclmul((((unsigned __int128) 0x983a442a106b8a83ULL) << 64)
> +                 | 0x1866ccfa2e2b382cULL,
> +                 (((unsigned __int128) 0xe9abf4d856d535c3ULL) << 64)
> +                 | 0x58708f117a52c14cULL)
> +         == W<unsigned __int128>((((unsigned __int128) 0x7d17129902648920ULL)
> +                                  << 64)
> +                                 | 0xfdb34e3b7c058047ULL,
> +                                 (((unsigned __int128) 0xd3ae65b0bac566b2ULL)
> +                                  << 64)
> +                                 | 0xdd6b33f9bb8606d0ULL) );
> +#endif
> +#endif
> +}
> +
> +template<typename T>
> +void
> +test02()
> +{
> +  VERIFY( myclmul(T(), T()) == W<T>(T(), T()) );
> +}
> +
> +int
> +main()
> +{
> +  test01();
> +  test02<unsigned char>();
> +  test02<unsigned short>();
> +  test02<unsigned int>();
> +  test02<unsigned long>();
> +  test02<unsigned long long>();
> +#ifdef __SIZEOF_INT128__
> +  test02<unsigned __int128>();
> +#endif
> +}
> --- a/libstdc++-v3/include/bits/version.h       2026-11-03 10:17:22.409724851 +0100
> +++ b/libstdc++-v3/include/bits/version.h       2026-11-03 13:32:50.336902703 +0100
> @@ -2996,4 +2996,14 @@
>  #endif /* !defined(__cpp_lib_hardened_view_interface) */
>  #undef __glibcxx_want_hardened_view_interface
>
> +#if !defined(__cpp_lib_clmul)
> +# if (__cplusplus >  202603L)
> +#  define __glibcxx_clmul 202611L
> +#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_clmul)
> +#   define __cpp_lib_clmul 202611L
> +#  endif
> +# endif
> +#endif /* !defined(__cpp_lib_clmul) */
> +#undef __glibcxx_want_clmul
> +
>  #undef __glibcxx_want_all
>
>         Jakub
>
  
Jakub Jelinek Nov. 3, 2026, 6:39 p.m. UTC | #2
On Tue, Nov 03, 2026 at 06:23:19PM +0000, Jonathan Wakely wrote:
> On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <jakub@redhat.com> wrote:
> > +  template<typename _Tp>
> > +    class wide_result {
> > +    public:
> > +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> > +       : _M_high(__high), _M_low(__low) {}
> > +
> > +      constexpr _Tp high() const noexcept { return _M_high; }
> > +      constexpr _Tp low() const noexcept { return _M_low; }
> > +
> > +      friend constexpr bool operator==(const wide_result&,
> > +                                      const wide_result&) = default;
> > +      friend constexpr strong_ordering operator<=>(const wide_result&,
> > +                                                  const wide_result&)
> > +       noexcept = default;
> > +    private:
> > +      _Tp _M_high, _M_low;
> 
> LWG wanted this to be a class with private members rather than an
> aggregate with public members so that the high and low parts could be
> ordered optimally according to the target endianness, e.g. so that
> wide_result<uint64_t> would have the same layout as unsigned int128.

I don't see any advantages in doing that.
Sure, having the private bits actually be the twice as wide unsigned
type is an implementation option, but one then needs to specialize
those just for types that do have such a twice as wide type at all.
Also, given the way I've introduced __builtin_clmulg which returns
the low part and sets what optional third argument points at to the high
part, using that strategy actually would be pessimization, especially for
say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
then better over having to combine it into unsigned _BitInt(62).
And, without _BitInt there is no 2x wider type for unsigned __int128,
on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
etc.
I think it is fine if the standard allows the implementations to choose
what they want though.

> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result
> 
> Do we want to take advantage of that?
> 
> I think we need static_assert(__is_unsigned_integer<_Tp>::value);

Ah, I've missed
"A specialization wide_result<T> is ill-formed unless T is an unsigned integer
type"

Should that be
static_assert(__is_unsigned_integer<_Tp>::value);
or instead
template<__unsigned_integer _Tp>
class wide_result ?

	Jakub
  
Tomasz Kaminski Nov. 3, 2026, 6:52 p.m. UTC | #3
On Tue, Nov 3, 2026 at 7:39 PM Jakub Jelinek <jakub@redhat.com> wrote:

> On Tue, Nov 03, 2026 at 06:23:19PM +0000, Jonathan Wakely wrote:
> > On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <jakub@redhat.com> wrote:
> > > +  template<typename _Tp>
> > > +    class wide_result {
> > > +    public:
> > > +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> > > +       : _M_high(__high), _M_low(__low) {}
> > > +
> > > +      constexpr _Tp high() const noexcept { return _M_high; }
> > > +      constexpr _Tp low() const noexcept { return _M_low; }
> > > +
> > > +      friend constexpr bool operator==(const wide_result&,
> > > +                                      const wide_result&) = default;
> > > +      friend constexpr strong_ordering operator<=>(const wide_result&,
> > > +                                                  const wide_result&)
> > > +       noexcept = default;
> > > +    private:
> > > +      _Tp _M_high, _M_low;
> >
> > LWG wanted this to be a class with private members rather than an
> > aggregate with public members so that the high and low parts could be
> > ordered optimally according to the target endianness, e.g. so that
> > wide_result<uint64_t> would have the same layout as unsigned int128.
>
> I don't see any advantages in doing that.

Sure, having the private bits actually be the twice as wide unsigned
> type is an implementation option, but one then needs to specialize
> those just for types that do have such a twice as wide type at all.
> Also, given the way I've introduced __builtin_clmulg which returns
> the low part and sets what optional third argument points at to the high
> part, using that strategy actually would be pessimization, especially for
> say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
> then better over having to combine it into unsigned _BitInt(62).
> And, without _BitInt there is no 2x wider type for unsigned __int128,
> on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
> targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
> etc.
> I think it is fine if the standard allows the implementations to choose
> what they want though.
>
The context here, is that the same layout was originally suggested to allow
bitcasting/memcpy into a wider integer type. We didn't want to guarantee
using a,
and instead suggested adding a member function that would allow extraction
of low and high combined in single type. I would imagine something like:
   template<typename W = implementation-defined>
   W wide() { };
W should have a default argument only if a type at least twice as wide as T
exists.

I think this function could compile to fewer instructions for some
combination of W and T,
if we order the _M_high and _M_low members according to endianess of target,
without combining them into a single member.

>
> >
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result
> >
> > Do we want to take advantage of that?
> >
> > I think we need static_assert(__is_unsigned_integer<_Tp>::value);
>
> Ah, I've missed
> "A specialization wide_result<T> is ill-formed unless T is an unsigned
> integer
> type"
>
> Should that be
> static_assert(__is_unsigned_integer<_Tp>::value);
>
static_assert.

> or instead
> template<__unsigned_integer _Tp>
> class wide_result ?
>
>         Jakub
>
>
  
Jakub Jelinek Nov. 3, 2026, 7:06 p.m. UTC | #4
On Tue, Nov 03, 2026 at 07:52:50PM +0100, Tomasz Kaminski wrote:
> Sure, having the private bits actually be the twice as wide unsigned
> > type is an implementation option, but one then needs to specialize
> > those just for types that do have such a twice as wide type at all.
> > Also, given the way I've introduced __builtin_clmulg which returns
> > the low part and sets what optional third argument points at to the high
> > part, using that strategy actually would be pessimization, especially for
> > say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
> > then better over having to combine it into unsigned _BitInt(62).
> > And, without _BitInt there is no 2x wider type for unsigned __int128,
> > on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
> > targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
> > etc.
> > I think it is fine if the standard allows the implementations to choose
> > what they want though.
> >
> The context here, is that the same layout was originally suggested to allow
> bitcasting/memcpy into a wider integer type. We didn't want to guarantee
> using a,
> and instead suggested adding a member function that would allow extraction
> of low and high combined in single type. I would imagine something like:
>    template<typename W = implementation-defined>
>    W wide() { };
> W should have a default argument only if a type at least twice as wide as T
> exists.
> 
> I think this function could compile to fewer instructions for some
> combination of W and T,
> if we order the _M_high and _M_low members according to endianess of target,
> without combining them into a single member.

It depends on how the user is going to use it.
If it is two separate fields and that is how the builtin produces those,
then it is cheaper to construct it as two separate fields.  If user uses it
as two separate fields (so checks .low() and .high() separately), then
paying the extra cost of combining the two parts into one is wasted.
If user uses the hypothetical wide() on it, then that would be likely
cheaper if it is just one private wider field, but then low() and high()
will be slower.

Anyway, given experimental C++29 support for a while, we can change it if
needed depending on what will be most useful to users.
Anyway, if there is a 2x wider type and user knows what it is, they can
always just use std::clmul<that_2xwider_type>.

> > Should that be
> > static_assert(__is_unsigned_integer<_Tp>::value);
> >
> static_assert.

Changed in my copy.

	Jakub
  
Jonathan Wakely Nov. 4, 2026, 7:44 a.m. UTC | #5
On Tue, 3 Nov 2026, 18:39 Jakub Jelinek, <jakub@redhat.com> wrote:

> On Tue, Nov 03, 2026 at 06:23:19PM +0000, Jonathan Wakely wrote:
> > On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <jakub@redhat.com> wrote:
> > > +  template<typename _Tp>
> > > +    class wide_result {
> > > +    public:
> > > +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> > > +       : _M_high(__high), _M_low(__low) {}
> > > +
> > > +      constexpr _Tp high() const noexcept { return _M_high; }
> > > +      constexpr _Tp low() const noexcept { return _M_low; }
> > > +
> > > +      friend constexpr bool operator==(const wide_result&,
> > > +                                      const wide_result&) = default;
> > > +      friend constexpr strong_ordering operator<=>(const wide_result&,
> > > +                                                  const wide_result&)
> > > +       noexcept = default;
> > > +    private:
> > > +      _Tp _M_high, _M_low;
> >
> > LWG wanted this to be a class with private members rather than an
> > aggregate with public members so that the high and low parts could be
> > ordered optimally according to the target endianness, e.g. so that
> > wide_result<uint64_t> would have the same layout as unsigned int128.
>
> I don't see any advantages in doing that.
> Sure, having the private bits actually be the twice as wide unsigned
> type is an implementation option, but one then needs to specialize
> those just for types that do have such a twice as wide type at all.
>

I wasn't thinking that we'd actually use a data member of the wider type,
but because wide_result is trivially copyable you could memcpy or bit_cast
wide_result<uint32_t> into a uint64_t. The value of the uin64_t world only
be correct if the high and low parts are in the right order, the value of
the uint64_t would be wrong.



Also, given the way I've introduced __builtin_clmulg which returns
> the low part and sets what optional third argument points at to the high
> part, using that strategy actually would be pessimization, especially for
> say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
> then better over having to combine it into unsigned _BitInt(62).
> And, without _BitInt there is no 2x wider type for unsigned __int128,
> on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
> targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
> etc.
> I think it is fine if the standard allows the implementations to choose
> what they want though.
>
> >
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result
> >
> > Do we want to take advantage of that?
> >
> > I think we need static_assert(__is_unsigned_integer<_Tp>::value);
>
> Ah, I've missed
> "A specialization wide_result<T> is ill-formed unless T is an unsigned
> integer
> type"
>
> Should that be
> static_assert(__is_unsigned_integer<_Tp>::value);
> or instead
> template<__unsigned_integer _Tp>
> class wide_result ?
>
>         Jakub
>
>
  
Jonathan Wakely Nov. 4, 2026, 9:33 a.m. UTC | #6
On Wed, 4 Nov 2026 at 07:44, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
>
>
> On Tue, 3 Nov 2026, 18:39 Jakub Jelinek, <jakub@redhat.com> wrote:
>>
>> On Tue, Nov 03, 2026 at 06:23:19PM +0000, Jonathan Wakely wrote:
>> > On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <jakub@redhat.com> wrote:
>> > > +  template<typename _Tp>
>> > > +    class wide_result {
>> > > +    public:
>> > > +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
>> > > +       : _M_high(__high), _M_low(__low) {}
>> > > +
>> > > +      constexpr _Tp high() const noexcept { return _M_high; }
>> > > +      constexpr _Tp low() const noexcept { return _M_low; }
>> > > +
>> > > +      friend constexpr bool operator==(const wide_result&,
>> > > +                                      const wide_result&) = default;
>> > > +      friend constexpr strong_ordering operator<=>(const wide_result&,
>> > > +                                                  const wide_result&)
>> > > +       noexcept = default;
>> > > +    private:
>> > > +      _Tp _M_high, _M_low;
>> >
>> > LWG wanted this to be a class with private members rather than an
>> > aggregate with public members so that the high and low parts could be
>> > ordered optimally according to the target endianness, e.g. so that
>> > wide_result<uint64_t> would have the same layout as unsigned int128.
>>
>> I don't see any advantages in doing that.
>> Sure, having the private bits actually be the twice as wide unsigned
>> type is an implementation option, but one then needs to specialize
>> those just for types that do have such a twice as wide type at all.
>
>
> I wasn't thinking that we'd actually use a data member of the wider type, but because wide_result is trivially copyable you could memcpy or bit_cast wide_result<uint32_t> into a uint64_t. The value of the uin64_t world only be correct if the high and low parts are in the right order, the value of the uint64_t would be wrong.

Sorry for the garbled sentence, typed on my phone before I was properly awake.

I meant to say the value of the uin64_t would only be correct if the
high and low parts are in the right order.



>
>
>
>> Also, given the way I've introduced __builtin_clmulg which returns
>> the low part and sets what optional third argument points at to the high
>> part, using that strategy actually would be pessimization, especially for
>> say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
>> then better over having to combine it into unsigned _BitInt(62).
>> And, without _BitInt there is no 2x wider type for unsigned __int128,
>> on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
>> targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
>> etc.
>> I think it is fine if the standard allows the implementations to choose
>> what they want though.
>>
>> > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result
>> >
>> > Do we want to take advantage of that?
>> >
>> > I think we need static_assert(__is_unsigned_integer<_Tp>::value);
>>
>> Ah, I've missed
>> "A specialization wide_result<T> is ill-formed unless T is an unsigned integer
>> type"
>>
>> Should that be
>> static_assert(__is_unsigned_integer<_Tp>::value);
>> or instead
>> template<__unsigned_integer _Tp>
>> class wide_result ?
>>
>>         Jakub
>>
  

Patch

--- a/libstdc++-v3/include/bits/version.def	2026-11-03 10:17:22.408691156 +0100
+++ b/libstdc++-v3/include/bits/version.def	2026-11-03 13:32:07.741581918 +0100
@@ -2716,6 +2716,14 @@  ftms = {
   };
 };
 
+ftms = {
+  name = clmul;
+  values = {
+    v = 202611;
+    cxxmin = 29;
+  };
+};
+
 // Standard test specifications.
 stds[97] = ">= 199711L";
 stds[03] = ">= 199711L";
--- a/libstdc++-v3/include/std/numeric	2026-01-02 22:41:17.669547090 +0100
+++ b/libstdc++-v3/include/std/numeric	2026-11-03 15:37:24.215814718 +0100
@@ -90,6 +90,7 @@ 
 #define __glibcxx_want_parallel_algorithm
 #define __glibcxx_want_ranges_iota
 #define __glibcxx_want_saturation_arithmetic
+#define __glibcxx_want_clmul
 #include <bits/version.h>
 
 #if __glibcxx_ranges_iota >= 202202L // C++ >= 23
@@ -765,6 +766,72 @@  namespace ranges
 } // namespace ranges
 #endif // __glibcxx_ranges_iota
 
+#if __glibcxx_clmul >= 202611L // C++29
+  template<typename _Tp>
+    class wide_result {
+    public:
+      constexpr wide_result(_Tp __high, _Tp __low) noexcept
+	: _M_high(__high), _M_low(__low) {}
+
+      constexpr _Tp high() const noexcept { return _M_high; }
+      constexpr _Tp low() const noexcept { return _M_low; }
+
+      friend constexpr bool operator==(const wide_result&,
+				       const wide_result&) = default;
+      friend constexpr strong_ordering operator<=>(const wide_result&,
+						   const wide_result&)
+	noexcept = default;
+    private:
+      _Tp _M_high, _M_low;
+    };
+
+  template<__unsigned_integer _Tp>
+    [[nodiscard]] constexpr wide_result<_Tp>
+    widening_clmul(_Tp __x, _Tp __y) noexcept
+    {
+      _Tp __high, __low;
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
+      __low = __builtin_clmulg(__x, __y, &__high);
+      return wide_result<_Tp>(__high, __low);
+#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul) \
+      && _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_bitreverseg)
+      __low = __builtin_elementwise_clmul(__x, __y);
+      __high = __builtin_elementwise_clmul(__builtin_bitreverseg(__x),
+					   __builtin_bitreverseg(__y));
+      return wide_result<_Tp>(__builtin_bitreverseg(__high) >> 1, __low);
+#else
+      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
+      __high = 0;
+      __low = (__y & _Tp(1)) ? __x : 0;
+      for (int __i = 1; __i < _Nd; ++__i)
+	if ((__y >> __i) & _Tp(1))
+	  {
+	    __low ^= __x << __i;
+	    __high ^= __x >> (_Nd - __i);
+	  }
+      return wide_result<_Tp>(__high, __low);
+#endif
+    }
+
+  template<__unsigned_integer _Tp>
+    [[nodiscard]] constexpr _Tp
+    clmul(_Tp __x, _Tp __y) noexcept
+    {
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
+      return __builtin_clmulg(__x, __y);
+#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul)
+      return __builtin_elementwise_clmul(__x, __y);
+#else
+      _Tp __res = 0;
+      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
+      for (int __i = 0; __i < _Nd; ++__i)
+	if ((__y >> __i) & _Tp(1))
+	  __res ^= __x << __i;
+      return __res;
+#endif
+    }
+#endif // C++29
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
--- a/libstdc++-v3/src/c++23/std.cc.in	2026-11-03 11:22:24.260388845 +0100
+++ b/libstdc++-v3/src/c++23/std.cc.in	2026-11-03 14:56:19.515191718 +0100
@@ -2488,6 +2488,11 @@  export namespace std
   using std::saturating_div;
   using std::saturating_cast;
 #endif
+#if __cpp_lib_clmul
+  using std::wide_result;
+  using std::widening_clmul;
+  using std::clmul;
+#endif
 }
 
 // <optional>
--- a/libstdc++-v3/testsuite/26_numerics/clmul/1.cc	2026-11-03 14:12:06.715065853 +0100
+++ b/libstdc++-v3/testsuite/26_numerics/clmul/1.cc	2026-11-03 15:51:59.929951366 +0100
@@ -0,0 +1,205 @@ 
+// { dg-do run { target c++29 } }
+
+#include <numeric>
+
+#ifndef __cpp_lib_clmul
+# error "Feature-test macro for clmul missing in <numeric>"
+#elif __cpp_lib_clmul != 202611L
+# error "Feature-test macro for clmul has wrong value in <numeric>"
+#endif
+
+#include <testsuite_hooks.h>
+
+static_assert(std::wide_result<unsigned>(42U, 43U).low() == 43);
+static_assert(std::wide_result<unsigned>(42U, 43U).high() == 42);
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      == std::wide_result<unsigned>(42U, 43U));
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      != std::wide_result<unsigned>(42U, 44U));
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      != std::wide_result<unsigned>(43U, 43U));
+static_assert(std::wide_result<unsigned>(42U, 45U)
+	      < std::wide_result<unsigned>(43U, 43U));
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      < std::wide_result<unsigned>(42U, 44U));
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      > std::wide_result<unsigned>(41U, 44U));
+static_assert(std::wide_result<unsigned>(42U, 43U)
+	      > std::wide_result<unsigned>(42U, 42U));
+static_assert(std::is_same<decltype(std::widening_clmul((unsigned char) 0,
+							(unsigned char) 0)),
+			   std::wide_result<unsigned char>>::value);
+static_assert(std::is_same<decltype(std::widening_clmul((unsigned short) 0,
+							(unsigned short) 0)),
+			   std::wide_result<unsigned short>>::value);
+static_assert(std::is_same<decltype(std::widening_clmul(0U, 0U)),
+			   std::wide_result<unsigned int>>::value);
+static_assert(std::is_same<decltype(std::widening_clmul(0UL, 0UL)),
+			   std::wide_result<unsigned long>>::value);
+static_assert(std::is_same<decltype(std::widening_clmul(0ULL, 0ULL)),
+			   std::wide_result<unsigned long long>>::value);
+#if __CHAR_BIT__ == 8
+static_assert(std::clmul((unsigned char) 0x7e,
+			 (unsigned char) 0x9c) == 0xe8);
+static_assert(std::widening_clmul((unsigned char) 0x7e,
+				  (unsigned char) 0x9c).low() == 0xe8);
+static_assert(std::widening_clmul((unsigned char) 0x7e,
+				  (unsigned char) 0x9c).high() == 0x3a);
+#if __SIZEOF_SHORT__ == 2
+static_assert(std::clmul((unsigned short) 0xc8aa,
+			 (unsigned short) 0x7616) == 0xa55c);
+static_assert(std::widening_clmul((unsigned short) 0xc8aa,
+				  (unsigned short) 0x7616)
+	      == std::wide_result<unsigned short>(0x2508, 0xa55c));
+#endif
+#if __SIZEOF_INT__ == 4
+static_assert(std::clmul(0x9f330680U, 0x4e157eadU) == 0xb23fb880U);
+static_assert(std::widening_clmul(0x9f330680U, 0x4e157eadU)
+	      == std::wide_result<unsigned>(0x207cd974U, 0xb23fb880U));
+static_assert(std::clmul(0x11111111U, 0x11111111U) == 0x1010101U);
+static_assert(std::widening_clmul(0x11111111U, 0x11111111U)
+	      == std::wide_result<unsigned>(0x1010101U, 0x1010101U));
+static_assert(std::clmul(~0U, ~0U) == 0x55555555U);
+static_assert(std::widening_clmul(~0U, ~0U)
+	      == std::wide_result<unsigned>(0x55555555U, 0x55555555U));
+#endif
+#if __SIZEOF_LONG__ == 4
+static_assert(std::clmul(0xcd63e2c1UL, 0xab176a37UL) == 0x2f053277UL);
+static_assert(std::widening_clmul(0xcd63e2c1UL, 0xab176a37UL)
+	      == std::wide_result<unsigned long>(0x782faf0eUL, 0x2f053277UL));
+#elif __SIZEOF_LONG__ == 8
+static_assert(std::clmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
+	      == 0xe8465657c87e7a1dUL);
+static_assert(std::widening_clmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
+	      == std::wide_result<unsigned long>(0xe2f8ec31f25371bUL,
+						 0xe8465657c87e7a1dUL));
+#endif
+#if __SIZEOF_LONG_LONG__ == 8
+static_assert(std::clmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
+	      == 0xfaf0ddab7a75024ULL);
+static_assert(std::widening_clmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
+	      == std::wide_result<unsigned long long>(0x29daaa7ee7cd2860ULL,
+						      0xfaf0ddab7a75024ULL));
+static_assert(std::clmul(0x11111111ULL, 0x11111111ULL)
+	      == 0x101010101010101ULL);
+static_assert(std::widening_clmul(0x11111111ULL, 0x11111111ULL)
+	      == std::wide_result<unsigned long long>(0ULL,
+						      0x101010101010101ULL));
+static_assert(std::clmul(0x1111111111111111ULL, 0x1111111111111111ULL)
+	      == 0x101010101010101ULL);
+static_assert(std::widening_clmul(0x1111111111111111ULL, 0x1111111111111111ULL)
+	      == std::wide_result<unsigned long long>(0x101010101010101ULL,
+						      0x101010101010101ULL));
+static_assert(std::clmul(~0ULL, ~0ULL)
+	      == 0x5555555555555555ULL);
+static_assert(std::widening_clmul(~0ULL, ~0ULL)
+	      == std::wide_result<unsigned long long>(0x5555555555555555ULL,
+						      0x5555555555555555ULL));
+#endif
+#if __SIZEOF_INT128__ == 16
+static_assert(std::clmul((((unsigned __int128) 0x983a442a106b8a83ULL) << 64)
+			 | 0x1866ccfa2e2b382cULL,
+			 (((unsigned __int128) 0xe9abf4d856d535c3ULL) << 64)
+			 | 0x58708f117a52c14cULL)
+	      == ((((unsigned __int128) 0xd3ae65b0bac566b2ULL) << 64)
+		  | 0xdd6b33f9bb8606d0ULL));
+static_assert(std::widening_clmul((((unsigned __int128) 0x983a442a106b8a83ULL)
+				   << 64) | 0x1866ccfa2e2b382cULL,
+				  (((unsigned __int128) 0xe9abf4d856d535c3ULL)
+				   << 64) | 0x58708f117a52c14cULL)
+	      == std::wide_result<unsigned __int128>((((unsigned __int128)
+						       0x7d17129902648920ULL)
+						      << 64)
+						     | 0xfdb34e3b7c058047ULL,
+						     (((unsigned __int128)
+						       0xd3ae65b0bac566b2ULL)
+						      << 64)
+						     | 0xdd6b33f9bb8606d0ULL));
+#endif
+#endif
+
+template<typename T>
+[[gnu::noipa]] std::wide_result<T>
+myclmul(T x, T y)
+{
+  auto r = std::widening_clmul(x, y);
+  VERIFY( std::clmul(x, y) == r.low() );
+  return r;
+}
+
+template<typename T>
+using W = std::wide_result<T>;
+
+void
+test01()
+{
+#if __CHAR_BIT__ == 8
+  VERIFY( myclmul((unsigned char) 0x7e, (unsigned char) 0x9c)
+	  == W<unsigned char>(0x3a, 0xe8) );
+#if __SIZEOF_SHORT__ == 2
+  VERIFY( myclmul((unsigned short) 0xc8aa, (unsigned short) 0x7616)
+	  == W<unsigned short>(0x2508, 0xa55c) );
+#endif
+#if __SIZEOF_INT__ == 4
+  VERIFY( myclmul(0x9f330680U, 0x4e157eadU)
+	  == W<unsigned>(0x207cd974U, 0xb23fb880U) );
+  VERIFY( myclmul(0x11111111U, 0x11111111U)
+	  == W<unsigned>(0x1010101U, 0x1010101U) );
+  VERIFY( myclmul(~0U, ~0U)
+	  == W<unsigned>(0x55555555U, 0x55555555U) );
+#endif
+#if __SIZEOF_LONG__ == 4
+  VERIFY( myclmul(0xcd63e2c1UL, 0xab176a37UL)
+	  == W<unsigned long>(0x782faf0eUL, 0x2f053277UL) );
+#elif __SIZEOF_LONG__ == 8
+  VERIFY( myclmul(0x43e13a77bec666afUL, 0x39c165e2ed3f98f7UL)
+	  == W<unsigned long>(0xe2f8ec31f25371bUL, 0xe8465657c87e7a1dUL) );
+#endif
+#if __SIZEOF_LONG_LONG__ == 8
+  VERIFY( myclmul(0xe38ebed3ef3cc8deULL, 0x75224424100f8096ULL)
+	  == W<unsigned long long>(0x29daaa7ee7cd2860ULL,
+				   0xfaf0ddab7a75024ULL) );
+  VERIFY( myclmul(0x11111111ULL, 0x11111111ULL)
+	  == W<unsigned long long>(0ULL, 0x101010101010101ULL) );
+  VERIFY( myclmul(0x1111111111111111ULL, 0x1111111111111111ULL)
+	  == W<unsigned long long>(0x101010101010101ULL,
+				   0x101010101010101ULL) );
+  VERIFY( myclmul(~0ULL, ~0ULL)
+	  == W<unsigned long long>(0x5555555555555555ULL,
+				   0x5555555555555555ULL) );
+#endif
+#if __SIZEOF_INT128__ == 16
+  VERIFY( myclmul((((unsigned __int128) 0x983a442a106b8a83ULL) << 64)
+		  | 0x1866ccfa2e2b382cULL,
+		  (((unsigned __int128) 0xe9abf4d856d535c3ULL) << 64)
+		  | 0x58708f117a52c14cULL)
+	  == W<unsigned __int128>((((unsigned __int128) 0x7d17129902648920ULL)
+				   << 64)
+				  | 0xfdb34e3b7c058047ULL,
+				  (((unsigned __int128) 0xd3ae65b0bac566b2ULL)
+				   << 64)
+				  | 0xdd6b33f9bb8606d0ULL) );
+#endif
+#endif
+}
+
+template<typename T>
+void
+test02()
+{
+  VERIFY( myclmul(T(), T()) == W<T>(T(), T()) );
+}
+
+int
+main()
+{
+  test01();
+  test02<unsigned char>();
+  test02<unsigned short>();
+  test02<unsigned int>();
+  test02<unsigned long>();
+  test02<unsigned long long>();
+#ifdef __SIZEOF_INT128__
+  test02<unsigned __int128>();
+#endif
+}
--- a/libstdc++-v3/include/bits/version.h	2026-11-03 10:17:22.409724851 +0100
+++ b/libstdc++-v3/include/bits/version.h	2026-11-03 13:32:50.336902703 +0100
@@ -2996,4 +2996,14 @@ 
 #endif /* !defined(__cpp_lib_hardened_view_interface) */
 #undef __glibcxx_want_hardened_view_interface
 
+#if !defined(__cpp_lib_clmul)
+# if (__cplusplus >  202603L)
+#  define __glibcxx_clmul 202611L
+#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_clmul)
+#   define __cpp_lib_clmul 202611L
+#  endif
+# endif
+#endif /* !defined(__cpp_lib_clmul) */
+#undef __glibcxx_want_clmul
+
 #undef __glibcxx_want_all