[libcxx-commits] [libcxx] b00e3a0 - [libc++] Fix numeric_limits::digits and digits10 for _BitInt(N) (#193002)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 21 02:04:23 PDT 2026


Author: Xavier Roche
Date: 2026-04-21T11:04:17+02:00
New Revision: b00e3a0986811d7a03cb469c7aa5f9117d306983

URL: https://github.com/llvm/llvm-project/commit/b00e3a0986811d7a03cb469c7aa5f9117d306983
DIFF: https://github.com/llvm/llvm-project/commit/b00e3a0986811d7a03cb469c7aa5f9117d306983.diff

LOG: [libc++] Fix numeric_limits::digits and digits10 for _BitInt(N) (#193002)

`digits` was `sizeof(T) * CHAR_BIT - is_signed`, which counts padding
bits for non-byte-aligned widths. For example, `unsigned _BitInt(13)`
reported `digits=16` instead of `13`. Computing it from
`__builtin_popcountg(~unsigned_type(0))` gives the right answer: padding
bits stay zero after bitwise NOT, so the popcount matches the value-bit
width.

`digits10 = digits * 3 / 10` was also wrong for `digits >= 256` (gave
76, should be 77). The new formula `digits * 1936274 / 6432163` matches
`floor(digits * log10(2))` exactly for every `digits` in `[1, 8388608]`,
i.e. the currently biggest possible `__BITINT_MAXWIDTH__` on Clang.

This is part of the [_BitInt(N) libc++
effort](https://discourse.llvm.org/t/bitint-n-support-in-libc-investigations-possible-improvements-looking-for-guidance/90063).

Assisted-by: Claude (Anthropic)

---------

Co-authored-by: Claude Opus 4.6 <noreply at anthropic.com>

Added: 
    

Modified: 
    libcxx/include/limits
    libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits.pass.cpp
    libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits10.pass.cpp
    libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/max.pass.cpp
    libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/min.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/limits b/libcxx/include/limits
index ff40d2051d06f..c26041006f082 100644
--- a/libcxx/include/limits
+++ b/libcxx/include/limits
@@ -108,6 +108,7 @@ template<> class numeric_limits<cv long double>;
 #  include <__config>
 #  include <__type_traits/is_arithmetic.h>
 #  include <__type_traits/is_same.h>
+#  include <__type_traits/make_unsigned.h>
 
 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #    pragma GCC system_header
@@ -184,9 +185,16 @@ protected:
 
   static _LIBCPP_CONSTEXPR const bool is_specialized = true;
 
-  static _LIBCPP_CONSTEXPR const bool is_signed   = type(-1) < type(0);
-  static _LIBCPP_CONSTEXPR const int digits       = static_cast<int>(sizeof(type) * __CHAR_BIT__ - is_signed);
-  static _LIBCPP_CONSTEXPR const int digits10     = digits * 3 / 10;
+  static _LIBCPP_CONSTEXPR const bool is_signed = type(-1) < type(0);
+  // Number of value bits in type. Works for any integer type, including
+  // _BitInt(N), whose sizeof(type) * CHAR_BIT may include padding.
+  static _LIBCPP_CONSTEXPR const int digits =
+      __builtin_popcountg(static_cast<__make_unsigned_t<type> >(~__make_unsigned_t<type>(0))) - is_signed;
+  // digits10 = floor(digits * log10(2)). 1936274/6432163 is a continued-fraction
+  // convergent of log10(2) and matches floor(digits * log10(2)) exactly for every
+  // digits in [1, 8388608], i.e. up to __BITINT_MAXWIDTH__ on x86.
+  // TODO: switch to __builtin_log10 once it becomes constexpr.
+  static _LIBCPP_CONSTEXPR const int digits10     = static_cast<int>((digits * 1936274LL) / 6432163);
   static _LIBCPP_CONSTEXPR const int max_digits10 = 0;
   static _LIBCPP_CONSTEXPR const type __min       = is_signed ? _Tp(_Tp(1) << digits) : 0;
   static _LIBCPP_CONSTEXPR const type __max       = is_signed ? type(type(~0) ^ __min) : type(~0);

diff  --git a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits.pass.cpp b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits.pass.cpp
index 4608a3d01ba14..807ea69f07680 100644
--- a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits.pass.cpp
@@ -53,5 +53,43 @@ int main(int, char**)
     test<double, DBL_MANT_DIG>();
     test<long double, LDBL_MANT_DIG>();
 
-  return 0;
+    // _BitInt(N): digits must equal N for unsigned and N-1 for signed,
+    // regardless of padding bits for non-byte-aligned widths.
+#if TEST_HAS_EXTENSION(bit_int)
+    // Byte-aligned widths.
+    test<unsigned _BitInt(8), 8>();
+    test<signed _BitInt(8), 7>();
+    test<unsigned _BitInt(32), 32>();
+    test<signed _BitInt(32), 31>();
+    test<unsigned _BitInt(64), 64>();
+    test<signed _BitInt(64), 63>();
+
+    // Non-byte-aligned widths.
+    test<unsigned _BitInt(7), 7>();
+    test<signed _BitInt(7), 6>();
+    test<unsigned _BitInt(13), 13>();
+    test<signed _BitInt(13), 12>();
+    test<unsigned _BitInt(37), 37>();
+    test<signed _BitInt(37), 36>();
+#  if __BITINT_MAXWIDTH__ >= 128
+    test<unsigned _BitInt(77), 77>();
+    test<signed _BitInt(77), 76>();
+    test<unsigned _BitInt(128), 128>();
+    test<signed _BitInt(128), 127>();
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 256
+    test<unsigned _BitInt(129), 129>();
+    test<signed _BitInt(129), 128>();
+    test<unsigned _BitInt(255), 255>();
+    test<signed _BitInt(255), 254>();
+    test<unsigned _BitInt(256), 256>();
+    test<signed _BitInt(256), 255>();
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 4096
+    test<unsigned _BitInt(4096), 4096>();
+    test<signed _BitInt(4096), 4095>();
+#  endif
+#endif // TEST_HAS_EXTENSION(bit_int)
+
+    return 0;
 }

diff  --git a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits10.pass.cpp b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits10.pass.cpp
index 41f134a706bcd..002f951b2b829 100644
--- a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits10.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/digits10.pass.cpp
@@ -57,5 +57,58 @@ int main(int, char**)
     test<double, DBL_DIG>();
     test<long double, LDBL_DIG>();
 
-  return 0;
+    // _BitInt(N): digits10 = floor((N - is_signed) * log10(2)).
+#if TEST_HAS_EXTENSION(bit_int)
+    test<unsigned _BitInt(8), 2>();   // digits=8,   log10=2.4
+    test<signed _BitInt(8), 2>();     // digits=7,   log10=2.1
+    test<unsigned _BitInt(13), 3>();  // digits=13,  log10=3.9
+    test<signed _BitInt(13), 3>();    // digits=12,  log10=3.6
+    test<unsigned _BitInt(32), 9>();  // digits=32,  log10=9.6
+    test<unsigned _BitInt(37), 11>(); // digits=37,  log10=11.1
+    test<unsigned _BitInt(64), 19>(); // digits=64,  log10=19.3
+    test<signed _BitInt(64), 18>();   // digits=63,  log10=18.9
+#  if __BITINT_MAXWIDTH__ >= 128
+    test<unsigned _BitInt(77), 23>();  // digits=77,  log10=23.2
+    test<signed _BitInt(77), 22>();    // digits=76,  log10=22.9
+    test<unsigned _BitInt(128), 38>(); // digits=128, log10=38.5
+    test<signed _BitInt(128), 38>();   // digits=127, log10=38.2
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 256
+    test<unsigned _BitInt(129), 38>(); // digits=129, log10=38.8
+    test<unsigned _BitInt(255), 76>(); // digits=255, log10=76.8
+    test<unsigned _BitInt(256), 77>(); // digits=256, log10=77.1
+    test<signed _BitInt(256), 76>();   // digits=255, log10=76.8
+    test<unsigned _BitInt(257), 77>(); // digits=257, log10=77.4
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 4096
+    test<unsigned _BitInt(4096), 1233>(); // digits=4096, log10=1233.0
+    test<signed _BitInt(4096), 1232>();   // digits=4095, log10=1232.7
+#  endif
+
+    // Very wide _BitInt: pin the log10(2) approximation used by digits10.
+    // Each width is the first point at which a coarser rational convergent
+    // of log10(2) would give the wrong floor, so these tests bite if the
+    // formula ever regresses.
+#  if __BITINT_MAXWIDTH__ >= 15437
+    // A coarser convergent (643/2136) would give 4646 here; correct is 4647.
+    test<unsigned _BitInt(15437), 4647>();
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 70777
+    // A coarser convergent (8651/28738) would give 21305 here; correct is 21306.
+    test<unsigned _BitInt(70777), 21306>();
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 1000000
+    test<unsigned _BitInt(1000000), 301029>();
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 8388608
+    // Pin the exact upper bound of the approximation.
+    test<unsigned _BitInt(8388608), 2525222>();
+#  endif
+    // The 1936274/6432163 convergent stays exact up to d=51132156. 8388608 is
+    // the largest width tested above, so if Clang raises __BITINT_MAXWIDTH__,
+    // extend the coverage before trusting the formula at the new range.
+    LIBCPP_STATIC_ASSERT(__BITINT_MAXWIDTH__ <= 8388608);
+#endif // TEST_HAS_EXTENSION(bit_int)
+
+    return 0;
 }

diff  --git a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/max.pass.cpp b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/max.pass.cpp
index 2f7c1f899a73f..fe8f039416d3a 100644
--- a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/max.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/max.pass.cpp
@@ -65,5 +65,26 @@ int main(int, char**)
     test<double>(DBL_MAX);
     test<long double>(LDBL_MAX);
 
-  return 0;
+    // _BitInt(N): max is 2^N - 1 for unsigned and 2^(N-1) - 1 for signed.
+    // Exercises the digits fix through `__max = ~0 ^ __min`.
+#if TEST_HAS_EXTENSION(bit_int)
+    test<unsigned _BitInt(8)>((unsigned _BitInt(8)) ~(unsigned _BitInt(8))0);
+    test<signed _BitInt(8)>((signed _BitInt(8))0x7F);
+    test<unsigned _BitInt(13)>((unsigned _BitInt(13))0x1FFF);
+    test<signed _BitInt(13)>((signed _BitInt(13))0x0FFF);
+    test<unsigned _BitInt(64)>((unsigned _BitInt(64)) ~(unsigned _BitInt(64))0);
+    test<signed _BitInt(64)>((signed _BitInt(64))0x7FFFFFFFFFFFFFFFLL);
+#  if __BITINT_MAXWIDTH__ >= 128
+    test<unsigned _BitInt(77)>((unsigned _BitInt(77)) ~(unsigned _BitInt(77))0);
+    test<signed _BitInt(77)>((signed _BitInt(77)) ~((signed _BitInt(77))1 << 76));
+    test<unsigned _BitInt(128)>((unsigned _BitInt(128)) ~(unsigned _BitInt(128))0);
+    test<signed _BitInt(128)>((signed _BitInt(128)) ~((signed _BitInt(128))1 << 127));
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 256
+    test<unsigned _BitInt(256)>((unsigned _BitInt(256)) ~(unsigned _BitInt(256))0);
+    test<signed _BitInt(256)>((signed _BitInt(256)) ~((signed _BitInt(256))1 << 255));
+#  endif
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/min.pass.cpp b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/min.pass.cpp
index 09877362447bc..a9c72da2103b4 100644
--- a/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/min.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/limits/numeric.limits.members/min.pass.cpp
@@ -65,5 +65,27 @@ int main(int, char**)
     test<double>(DBL_MIN);
     test<long double>(LDBL_MIN);
 
-  return 0;
+    // _BitInt(N): min is 0 for unsigned and -2^(N-1) for signed. The shift
+    // `1 << digits` flowed through the buggy digits field, so this also
+    // exercises the digits fix for non-byte-aligned widths.
+#if TEST_HAS_EXTENSION(bit_int)
+    test<unsigned _BitInt(8)>(0);
+    test<signed _BitInt(8)>(-(signed _BitInt(8))(1 << 7));
+    test<unsigned _BitInt(13)>(0);
+    test<signed _BitInt(13)>(-(signed _BitInt(13))(1 << 12));
+    test<unsigned _BitInt(64)>(0);
+    test<signed _BitInt(64)>(-(signed _BitInt(64))(1ULL << 63));
+#  if __BITINT_MAXWIDTH__ >= 128
+    test<unsigned _BitInt(77)>(0);
+    test<signed _BitInt(77)>(-((signed _BitInt(77))1 << 76));
+    test<unsigned _BitInt(128)>(0);
+    test<signed _BitInt(128)>(-((signed _BitInt(128))1 << 127));
+#  endif
+#  if __BITINT_MAXWIDTH__ >= 256
+    test<unsigned _BitInt(256)>(0);
+    test<signed _BitInt(256)>(-((signed _BitInt(256))1 << 255));
+#  endif
+#endif
+
+    return 0;
 }


        


More information about the libcxx-commits mailing list