[libcxx-commits] [libcxx] [libc++][test] Cover byteswap _BitInt padding on every ABI with width 72 (PR #206360)

Xavier Roche via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 28 12:11:45 PDT 2026


https://github.com/xroche created https://github.com/llvm/llvm-project/pull/206360

`std::byteswap` is required to reject integer types that have padding bits, and one test case checks that it does. The case used `unsigned _BitInt(96)`, but #205295 had to skip it on 32-bit x86. On the i386 System V ABI, `_BitInt(96)` is exactly 12 bytes with no padding, so `std::byteswap` accepts it and the expected error never fires. The skip used `!defined(__i386__)`, a target name that stands in for an object layout the preprocessor cannot see.

This change uses `unsigned _BitInt(72)` instead. Its 9 value bytes round up to 12 bytes on 32-bit x86 and 16 bytes everywhere else, so the type always has padding and `std::byteswap` always rejects it. The case now runs on every target, 32-bit x86 included, with no target guard.

Follows up on #205295.

Assisted-by: Claude (Anthropic)

>From d0e80a203ca2f05b5a737d90c8c7fc9f1fb89856 Mon Sep 17 00:00:00 2001
From: Xavier Roche <xavier.roche at algolia.com>
Date: Sun, 28 Jun 2026 21:10:43 +0200
Subject: [PATCH] [libc++][test] Cover byteswap _BitInt padding on every ABI
 with width 72

The byteswap padding-bit test added in #196512 used unsigned _BitInt(96),
which #205295 had to skip on 32-bit x86: the i386 System V ABI aligns
_BitInt to 4 bytes and packs _BitInt(96) into exactly 12 bytes with no
padding, so std::byteswap accepts it and the expected diagnostic never
fires. The skip used !defined(__i386__) as a stand-in because the object
layout that drives the result is not visible to the preprocessor.

Use unsigned _BitInt(72) instead. Its 9 value bytes round up to 12 on
32-bit x86 and 16 everywhere else, so the type always carries padding and
std::byteswap always rejects it. The case now runs on every target,
including 32-bit x86, with no target check.

Assisted-by: Claude (Anthropic)
Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
---
 .../test/std/numerics/bit/byteswap.verify.cpp | 24 +++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/libcxx/test/std/numerics/bit/byteswap.verify.cpp b/libcxx/test/std/numerics/bit/byteswap.verify.cpp
index f9b6954f525da..f2b75781ed5e1 100644
--- a/libcxx/test/std/numerics/bit/byteswap.verify.cpp
+++ b/libcxx/test/std/numerics/bit/byteswap.verify.cpp
@@ -103,23 +103,23 @@ void test_unsigned_56() {
 
 // Same dispatch-availability guard as test_unsigned_65 above.
 #  if TEST_HAS_BUILTIN(__builtin_bswapg) || !defined(TEST_HAS_NO_INT128)
-#    if __BITINT_MAXWIDTH__ >= 80
-void test_unsigned_80() {
-  // sizeof(_BitInt(80)) == 16 on x86_64; 48 padding bits. Width 80 is also
-  // a multiple of 16, so bswapg would accept it without the static_assert.
-  unsigned _BitInt(80) v = 0;
+// 72 value bits occupy 9 bytes, which round up to 12 on 32-bit x86 and 16
+// elsewhere, so padding exists on every ABI. _BitInt(96) is exactly 12 bytes
+// with no padding on 32-bit x86, so it needs a target guard (#205295); 72
+// doesn't.
+#    if __BITINT_MAXWIDTH__ >= 72
+void test_unsigned_72() {
+  unsigned _BitInt(72) v = 0;
   // expected-error-re@*:* {{{{(std::byteswap requires T to have no padding bits|byteswap is unimplemented for integral types of this size)}}}}
   (void)std::byteswap(v);
 }
 #    endif
 
-// 32-bit x86 packs _BitInt(96) into 12 bytes (no padding), so std::byteswap
-// accepts it there; gate the case out. sizeof isn't available to the
-// preprocessor, so this is a target check rather than a has-padding predicate.
-#    if __BITINT_MAXWIDTH__ >= 96 && !defined(__i386__)
-void test_unsigned_96() {
-  // sizeof(_BitInt(96)) == 16 here; 32 padding bits.
-  unsigned _BitInt(96) v = 0;
+#    if __BITINT_MAXWIDTH__ >= 80
+void test_unsigned_80() {
+  // sizeof(_BitInt(80)) == 16 on x86_64; 48 padding bits. Width 80 is also
+  // a multiple of 16, so bswapg would accept it without the static_assert.
+  unsigned _BitInt(80) v = 0;
   // expected-error-re@*:* {{{{(std::byteswap requires T to have no padding bits|byteswap is unimplemented for integral types of this size)}}}}
   (void)std::byteswap(v);
 }



More information about the libcxx-commits mailing list