[libcxx-commits] [libcxx] [libc++] Recognize _BitInt(N) as signed/unsigned integer type (PR #185027)
Xavier Roche via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Mar 6 07:39:37 PST 2026
https://github.com/xroche created https://github.com/llvm/llvm-project/pull/185027
Replace the explicit specialization lists in `__is_signed_integer_v` and
`__is_unsigned_integer_v` with builtin-based detection using `__is_integral`,
`__is_signed`, and `__is_unsigned`. This automatically covers `_BitInt(N)` for
any N, in addition to all standard and extended integer types.
Character types (`char`, `wchar_t`, `char8_t`, `char16_t`, `char32_t`) and `bool`
are excluded via a `__is_character_or_bool_v` helper, matching the standard
definition of signed/unsigned integer types per [basic.fundamental]/p1-2.
CV-qualified types are excluded to match the behavior of the original explicit
specializations (template specializations don't match cv-qualified types),
preserving the existing semantics of library features like `std::formattable`.
This unblocks all `<bit>` header operations for `_BitInt(N)`:
`std::popcount`, `std::countl_zero`, `std::countr_zero`, `std::bit_width`,
`std::has_single_bit`, `std::bit_ceil`, `std::bit_floor`, `std::rotl`, `std::rotr`.
## Context
This is part of the effort to improve `_BitInt(N)` support in libc++, discussed
in [RFC: _BitInt(N) support in libc++](https://discourse.llvm.org/t/bitint-n-support-in-libc-investigations-possible-improvements-looking-for-guidance/90063).
## Known limitation
For non-byte-aligned `_BitInt(N)` (e.g. `_BitInt(7)`, `_BitInt(129)`),
`numeric_limits::digits` currently uses `sizeof * CHAR_BIT` which exceeds `N`.
This causes `bit_width`, `has_single_bit`, `bit_ceil`, `bit_floor` to give
incorrect results for those widths. This is a pre-existing issue in
`numeric_limits`, not introduced by this change, and will be fixed separately.
## Test plan
- New test: `libcxx/test/libcxx/type_traits/bitint.pass.cpp`
- Type traits (`is_integral`, `is_signed`, `is_unsigned`, `is_arithmetic`) for 20+ widths
- Bit operations (`popcount`, `countl_zero`, `countr_zero`) for all widths including odd (7, 9, 15, 33, 65, 127)
- `bit_width`/`has_single_bit` for byte-aligned widths only (see known limitation)
- Wide `_BitInt` (N > 128) guarded behind `__BITINT_MAXWIDTH__`
- Big-number tests with Python-verified expected values
- Regression: type traits (164/164), bit operations, format formattable concept -- all pass
## AI Disclosure
This implementation was developed with assistance from Claude (Anthropic). The
commit carries `Assisted-by: Claude (Anthropic)` and
`Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>` trailers, per the
[LLVM AI Tool Policy](https://llvm.org/docs/DeveloperPolicy.html#use-of-ai-generated-contributions).
All code has been reviewed and validated by the author.
>From b33ccf1bad9cfad7fe57156910bc437fc9139c56 Mon Sep 17 00:00:00 2001
From: Xavier Roche <xavier.roche at algolia.com>
Date: Fri, 6 Mar 2026 16:38:23 +0100
Subject: [PATCH] [libc++] Recognize _BitInt(N) as signed/unsigned integer type
Replace the explicit specialization lists in __is_signed_integer_v and
__is_unsigned_integer_v with builtin-based detection using __is_integral,
__is_signed, and __is_unsigned. This automatically covers _BitInt(N) for
any N, in addition to all standard and extended integer types.
Character types (char, wchar_t, char8_t, char16_t, char32_t) and bool
are excluded via a __is_character_or_bool_v helper, matching the standard
definition of signed/unsigned integer types per [basic.fundamental]/p1-2.
CV-qualified types are excluded to match the behavior of the original
explicit specializations (template specializations don't match
cv-qualified types), preserving the existing semantics of library
features like std::formattable.
This unblocks all <bit> header operations for _BitInt(N):
std::popcount, std::countl_zero, std::countr_zero, std::bit_width,
std::has_single_bit, std::bit_ceil, std::bit_floor, std::rotl, std::rotr.
Assisted-by: Claude (Anthropic)
Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
---
libcxx/include/__type_traits/integer_traits.h | 69 +++---
.../test/libcxx/type_traits/bitint.pass.cpp | 217 ++++++++++++++++++
2 files changed, 252 insertions(+), 34 deletions(-)
create mode 100644 libcxx/test/libcxx/type_traits/bitint.pass.cpp
diff --git a/libcxx/include/__type_traits/integer_traits.h b/libcxx/include/__type_traits/integer_traits.h
index fad502c44e301..531857935347f 100644
--- a/libcxx/include/__type_traits/integer_traits.h
+++ b/libcxx/include/__type_traits/integer_traits.h
@@ -17,43 +17,44 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-// This trait is to determine whether a type is a /signed integer type/
-// See [basic.fundamental]/p1
-template <class _Tp>
-inline const bool __is_signed_integer_v = false;
-template <>
-inline const bool __is_signed_integer_v<signed char> = true;
-template <>
-inline const bool __is_signed_integer_v<signed short> = true;
-template <>
-inline const bool __is_signed_integer_v<signed int> = true;
-template <>
-inline const bool __is_signed_integer_v<signed long> = true;
-template <>
-inline const bool __is_signed_integer_v<signed long long> = true;
-#if _LIBCPP_HAS_INT128
-template <>
-inline const bool __is_signed_integer_v<__int128_t> = true;
+// These traits determine whether a type is a /signed integer type/ or
+// /unsigned integer type/ per [basic.fundamental]/p1-2.
+//
+// Signed/unsigned integer types include the standard types (signed char,
+// short, int, long, long long), extended integer types (__int128), and
+// bit-precise integer types (_BitInt(N)).
+//
+// Character types (char, wchar_t, char8_t, char16_t, char32_t) and bool
+// are integral but are NOT signed/unsigned integer types.
+
+// clang-format off
+template <class _Tp> inline const bool __is_character_or_bool_v = false;
+template <> inline const bool __is_character_or_bool_v<bool> = true;
+template <> inline const bool __is_character_or_bool_v<char> = true;
+#if _LIBCPP_HAS_WIDE_CHARACTERS
+template <> inline const bool __is_character_or_bool_v<wchar_t> = true;
#endif
+#if _LIBCPP_HAS_CHAR8_T
+template <> inline const bool __is_character_or_bool_v<char8_t> = true;
+#endif
+template <> inline const bool __is_character_or_bool_v<char16_t> = true;
+template <> inline const bool __is_character_or_bool_v<char32_t> = true;
+// clang-format on
-// This trait is to determine whether a type is an /unsigned integer type/
-// See [basic.fundamental]/p2
+// Signed integer types: all signed integral types except character types.
+// Uses compiler builtins to automatically cover _BitInt(N) for any N.
+// CV-qualified types are excluded to match the behavior of the original
+// explicit specializations and to avoid accidentally enabling library
+// features (e.g. std::formattable) for volatile-qualified types.
template <class _Tp>
-inline const bool __is_unsigned_integer_v = false;
-template <>
-inline const bool __is_unsigned_integer_v<unsigned char> = true;
-template <>
-inline const bool __is_unsigned_integer_v<unsigned short> = true;
-template <>
-inline const bool __is_unsigned_integer_v<unsigned int> = true;
-template <>
-inline const bool __is_unsigned_integer_v<unsigned long> = true;
-template <>
-inline const bool __is_unsigned_integer_v<unsigned long long> = true;
-#if _LIBCPP_HAS_INT128
-template <>
-inline const bool __is_unsigned_integer_v<__uint128_t> = true;
-#endif
+inline const bool __is_signed_integer_v =
+ !__is_const(_Tp) && !__is_volatile(_Tp) && __is_integral(_Tp) && __is_signed(_Tp) && !__is_character_or_bool_v<_Tp>;
+
+// Unsigned integer types: all unsigned integral types except character types and bool.
+template <class _Tp>
+inline const bool __is_unsigned_integer_v =
+ !__is_const(_Tp) && !__is_volatile(_Tp) && __is_integral(_Tp) && __is_unsigned(_Tp) &&
+ !__is_character_or_bool_v<_Tp>;
#if _LIBCPP_STD_VER >= 20
template <class _Tp>
diff --git a/libcxx/test/libcxx/type_traits/bitint.pass.cpp b/libcxx/test/libcxx/type_traits/bitint.pass.cpp
new file mode 100644
index 0000000000000..906f25f972bab
--- /dev/null
+++ b/libcxx/test/libcxx/type_traits/bitint.pass.cpp
@@ -0,0 +1,217 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Test that _BitInt(N) is recognized as a signed/unsigned integer type by
+// libc++ internal traits, and that <bit> operations work for all valid widths.
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <bit>
+#include <cassert>
+#include <limits>
+#include <type_traits>
+
+// ===== Type traits =====
+// _BitInt(N) must satisfy is_integral, is_signed/is_unsigned, is_arithmetic
+
+template <int N>
+void test_signed_traits() {
+ using T = _BitInt(N);
+ static_assert(std::is_integral_v<T>);
+ static_assert(std::is_signed_v<T>);
+ static_assert(!std::is_unsigned_v<T>);
+ static_assert(std::is_arithmetic_v<T>);
+ static_assert(std::numeric_limits<T>::is_specialized);
+}
+
+template <int N>
+void test_unsigned_traits() {
+ using T = unsigned _BitInt(N);
+ static_assert(std::is_integral_v<T>);
+ static_assert(!std::is_signed_v<T>);
+ static_assert(std::is_unsigned_v<T>);
+ static_assert(std::is_arithmetic_v<T>);
+ static_assert(std::numeric_limits<T>::is_specialized);
+}
+
+// ===== Negative tests =====
+// Character types and bool must NOT satisfy __signed_integer/__unsigned_integer
+// (they are integral but not "integer types" per [basic.fundamental])
+
+static_assert(std::is_integral_v<bool>);
+static_assert(std::is_integral_v<char>);
+static_assert(std::is_integral_v<wchar_t>);
+static_assert(std::is_integral_v<char16_t>);
+static_assert(std::is_integral_v<char32_t>);
+// These types are integral but we cannot directly test the internal
+// __is_signed_integer_v trait here. Instead we verify that the <bit>
+// operations correctly reject them (they require __unsigned_integer).
+
+// ===== Bit operations =====
+
+template <int N>
+void test_popcount() {
+ using T = unsigned _BitInt(N);
+ assert(std::popcount(T(0)) == 0);
+ assert(std::popcount(T(1)) == 1);
+ if constexpr (N >= 8)
+ assert(std::popcount(T(0xFF)) == 8);
+}
+
+template <int N>
+void test_countl_zero() {
+ using T = unsigned _BitInt(N);
+ // countl_zero(0) returns the declared width N (all bits are leading zeros)
+ assert(std::countl_zero(T(0)) == N);
+ // countl_zero(1) returns N - 1
+ assert(std::countl_zero(T(1)) == N - 1);
+ // Max value: all N bits set, zero leading zeros
+ assert(std::countl_zero(T(~T(0))) == 0);
+}
+
+template <int N>
+void test_countr_zero() {
+ using T = unsigned _BitInt(N);
+ assert(std::countr_zero(T(0)) == N);
+ assert(std::countr_zero(T(1)) == 0);
+ assert(std::countr_zero(T(T(1) << (N - 1))) == N - 1);
+}
+
+// bit_width and has_single_bit depend on numeric_limits::digits being correct.
+// For non-byte-aligned _BitInt(N), digits uses sizeof*CHAR_BIT which exceeds N.
+// Only test byte-aligned widths here; a separate fix for numeric_limits::digits
+// will enable testing all widths.
+
+template <int N>
+void test_bit_width() {
+ using T = unsigned _BitInt(N);
+ assert(std::bit_width(T(0)) == 0);
+ assert(std::bit_width(T(1)) == 1);
+ if constexpr (N >= 11)
+ assert(std::bit_width(T(1024)) == 11);
+ assert(std::bit_width(T(~T(0))) == N);
+}
+
+template <int N>
+void test_has_single_bit() {
+ using T = unsigned _BitInt(N);
+ assert(!std::has_single_bit(T(0)));
+ assert(std::has_single_bit(T(1)));
+ if constexpr (N >= 8) {
+ assert(std::has_single_bit(T(128)));
+ assert(!std::has_single_bit(T(129)));
+ }
+}
+
+// Big-number popcount test: verified with Python
+void test_popcount_big_numbers() {
+#if __BITINT_MAXWIDTH__ >= 256
+ {
+ // (1 << 200) - 1 has exactly 200 bits set
+ unsigned _BitInt(256) v = (unsigned _BitInt(256))(1) << 200;
+ v -= 1;
+ assert(std::popcount(v) == 200);
+ }
+ {
+ // Exactly 4 bits set at positions 0, 64, 128, 255
+ unsigned _BitInt(256) v = (unsigned _BitInt(256))(1) | ((unsigned _BitInt(256))(1) << 64) |
+ ((unsigned _BitInt(256))(1) << 128) | ((unsigned _BitInt(256))(1) << 255);
+ assert(std::popcount(v) == 4);
+ }
+#endif
+#if __BITINT_MAXWIDTH__ >= 4096
+ {
+ unsigned _BitInt(4096) v = ~(unsigned _BitInt(4096))(0);
+ assert(std::popcount(v) == 4096);
+ }
+#endif
+}
+
+// Big-number countl_zero test
+void test_countl_zero_big_numbers() {
+#if __BITINT_MAXWIDTH__ >= 256
+ {
+ // Bit set at position 200 in a 256-bit integer: 55 leading zeros
+ unsigned _BitInt(256) v = (unsigned _BitInt(256))(1) << 200;
+ assert(std::countl_zero(v) == 55);
+ }
+#endif
+#if __BITINT_MAXWIDTH__ >= 4096
+ {
+ unsigned _BitInt(4096) v = (unsigned _BitInt(4096))(1) << 4000;
+ assert(std::countl_zero(v) == 95);
+ }
+#endif
+}
+
+template <int N>
+void test_all() {
+ test_signed_traits<N>();
+ test_unsigned_traits<N>();
+ test_popcount<N>();
+ test_countl_zero<N>();
+ test_countr_zero<N>();
+}
+
+// Only test bit_width/has_single_bit for byte-aligned widths where
+// numeric_limits::digits == N (see comment above).
+template <int N>
+void test_all_with_bit_width() {
+ test_all<N>();
+ test_bit_width<N>();
+ test_has_single_bit<N>();
+}
+
+int main(int, char**) {
+ // unsigned _BitInt(1) is the minimum unsigned width.
+ // signed _BitInt(1) is illegal -- minimum signed width is 2.
+ test_unsigned_traits<1>();
+ test_popcount<1>();
+
+ // _BitInt(2): minimum signed width
+ test_signed_traits<2>();
+ test_unsigned_traits<2>();
+ test_popcount<2>();
+
+ // Standard power-of-2 widths: byte-aligned, so bit_width/has_single_bit work
+ test_all_with_bit_width<8>();
+ test_all_with_bit_width<16>();
+ test_all_with_bit_width<32>();
+ test_all_with_bit_width<64>();
+ test_all_with_bit_width<128>();
+
+ // Odd widths -- popcount/countl_zero/countr_zero work, but bit_width and
+ // has_single_bit may give wrong results due to numeric_limits::digits using
+ // sizeof*CHAR_BIT instead of the actual bit width N.
+ test_all<7>();
+ test_all<9>();
+ test_all<15>();
+ test_all<17>();
+ test_all<33>();
+ test_all<65>();
+ test_all<127>();
+
+ // Wide _BitInt (N > 128) is only supported on some targets.
+#if __BITINT_MAXWIDTH__ >= 256
+ test_all_with_bit_width<256>();
+ test_all<129>();
+ test_all<255>();
+ test_all<257>();
+ test_all<512>();
+ test_all<1024>();
+#endif
+#if __BITINT_MAXWIDTH__ >= 4096
+ test_all_with_bit_width<4096>();
+#endif
+
+ // Big number tests (Python-verified expected values)
+ test_popcount_big_numbers();
+ test_countl_zero_big_numbers();
+
+ return 0;
+}
More information about the libcxx-commits
mailing list