[libcxx-commits] [libcxx] [libc++] std::byteswap support for _BitInt(N) (PR #196512)

Xavier Roche via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 29 02:46:44 PDT 2026


================
@@ -24,26 +28,41 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <integral _Tp>
 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
+  // [bit.byteswap]/Mandates: T does not have padding bits.
+  // bool is grandfathered: every shipping implementation admits it and the
+  // size-1 identity path can't shuffle padding bits into value positions.
+  // LWG 4583 proposes relaxing this to allow byte-aligned padding (e.g.
+  // _BitInt(48) where 2 whole bytes are padding); revisit once it resolves.
+  static_assert(is_same_v<remove_cv_t<_Tp>, bool> ||
+                    numeric_limits<_Tp>::digits + numeric_limits<_Tp>::is_signed == sizeof(_Tp) * CHAR_BIT,
+                "std::byteswap requires T to have no padding bits");
+
   if constexpr (sizeof(_Tp) == 1) {
     return __val;
----------------
xroche wrote:

I misread the code; it indeed breaks on clang 21/22 but because of constexpr-eval bug in __builtin_bswapg(bool) (fix f5410565137c, on main only, not in 22 branch)

Basically this is to prevent `static_assert(std::byteswap(true) == true)` to fail

https://github.com/llvm/llvm-project/pull/196512


More information about the libcxx-commits mailing list