[libcxx-commits] [libcxx] [libc++][NFC] Simplify the implementation of __mul_overflowed (PR #174956)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 9 03:09:12 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
`__builtin_mul_overflow` does the right thing, even for `char` and `short`, so the overloads for these types can simply be dropped. We can also merge the remaining two overloads into a single one now, since we don't do any dispatching for `char` and `short` anymore.
---
Full diff: https://github.com/llvm/llvm-project/pull/174956.diff
1 Files Affected:
- (modified) libcxx/include/__charconv/traits.h (+3-24)
``````````diff
diff --git a/libcxx/include/__charconv/traits.h b/libcxx/include/__charconv/traits.h
index 9fd0092ca79c3..b8c840d1ebe32 100644
--- a/libcxx/include/__charconv/traits.h
+++ b/libcxx/include/__charconv/traits.h
@@ -113,31 +113,10 @@ struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__u
};
# endif
-template <typename _Tp>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
-__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) {
- auto __c = __a * __b;
- __r = __c;
- return __c > numeric_limits<unsigned char>::max();
-}
-
-template <typename _Tp>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
-__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) {
- auto __c = __a * __b;
- __r = __c;
- return __c > numeric_limits<unsigned short>::max();
-}
-
-template <typename _Tp>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) {
- static_assert(is_unsigned<_Tp>::value, "");
- return __builtin_mul_overflow(__a, __b, std::addressof(__r));
-}
-
template <typename _Tp, typename _Up>
-inline _LIBCPP_HIDE_FROM_ABI bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) {
- return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r);
+_LIBCPP_HIDE_FROM_ABI bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) {
+ static_assert(is_unsigned<_Tp>::value);
+ return __builtin_mul_overflow(__a, static_cast<_Tp>(__b), std::addressof(__r));
}
template <typename _Tp>
``````````
</details>
https://github.com/llvm/llvm-project/pull/174956
More information about the libcxx-commits
mailing list