[libcxx-commits] [libcxx] [libc++] std::abs support for _BitInt(N) and __int128 (PR #196532)

Xavier Roche via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 16 11:53:58 PDT 2026


================
@@ -63,6 +63,19 @@ template <class = int>
   return __builtin_llabs(__x);
 }
 
+#if _LIBCPP_HAS_INT128
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inline __int128_t abs(__int128_t __x) _NOEXCEPT { return __x < 0 ? -__x : __x; }
+#endif
+
+#if defined(__BITINT_MAXWIDTH__)
+// Narrower widths (e.g. _BitInt(7)) stay unsupported, like signed types narrower
+// than int with no same-type abs. The gate is on sizeof, not bit width.
+template <int _Np, __enable_if_t<(sizeof(signed _BitInt(_Np)) >= sizeof(int)), int> = 0>
----------------
xroche wrote:

Honestly it is no longer required, I'll drop it - it's a leftover from the earlier version of the patch. 

Now that we have a dedicated _BitInt overload, the only thing the check still did was make std::abs refuse to compile for small _BitInt types (narrower than int), since those don't promote to int the way short/char do. I've removed it, so abs now works for every signed _BitInt size, and extended the test to cover the small ones.


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


More information about the libcxx-commits mailing list