[PATCH] libstdc++, v2: Implement non-simd part of P3642R7 - Carry-less product: std::clmul

Jonathan Wakely jwakely@redhat.com
Thu Nov 5 15:57:20 GMT 2026


On Thu, 5 Nov 2026 at 15:17, Jakub Jelinek <jakub@redhat.com> wrote:
>
> On Wed, Nov 04, 2026 at 09:33:27AM +0000, Jonathan Wakely wrote:
> > 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.
>
> I'm still not convinced we should encourage non-portable assumptions
> about it, but here is a patch which just swaps the order of the two
> members of wide_result for little endian only.
> One still can't assume anything in particular e.g. for PDP endian,
> or for types which do contain padding (e.g. various _BitInt) etc.
> static_assert added too.

OK thanks.

As discussed, I think we can go ahead and commit this for GCC 17 even
though WG21 haven't approved it yet. We can change it or remove it
later if needed.


>
> 2026-11-05  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-05 15:56:32.954284689 +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,87 @@ namespace ranges
>  } // namespace ranges
>  #endif // __glibcxx_ranges_iota
>
> +#if __glibcxx_clmul >= 202611L // C++29
> +  template<typename _Tp>
> +    class wide_result {
> +    public:
> +      static_assert(__is_unsigned_integer<_Tp>::value);
> +      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;
> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
> +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> +       : _M_low(__low), _M_high(__high) {}
> +      friend constexpr strong_ordering operator<=>(const wide_result& __x,
> +                                                  const wide_result& __y)
> +       noexcept
> +      {
> +       if (auto __r = __x._M_high <=> __y._M_high; __r != 0)
> +         return __r;
> +       return __x._M_low <=> __y._M_low;
> +      }
> +    private:
> +      _Tp _M_low, _M_high;
> +#else
> +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> +       : _M_high(__high), _M_low(__low) {}
> +      friend constexpr strong_ordering operator<=>(const wide_result& __x,
> +                                                  const wide_result& __y)
> +       noexcept = default;
> +    private:
> +      _Tp _M_high, _M_low;
> +#endif
> +    };
> +
> +  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
>



More information about the Libstdc++ mailing list