From 3f9942f4ea5c7d7f7e446beda9f451a99a66c343 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Wed, 1 May 2024 20:25:51 +0300 Subject: [PATCH 1/3] ``: unify min/max limit --- stl/inc/xutility | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/stl/inc/xutility b/stl/inc/xutility index 0fc9b352a0c..f7cf7338820 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -5938,9 +5938,8 @@ _NODISCARD constexpr bool _Could_compare_equal_to_value_type(const _Ty& _Val) { if constexpr (is_same_v<_Elem, bool>) { return _Val == true || _Val == false; } else if constexpr (is_signed_v<_Elem>) { - // use instead of numeric_limits::min/max; avoid dependency - constexpr _Elem _Min = static_cast<_Elem>(_Elem{1} << (sizeof(_Elem) * CHAR_BIT - 1)); - constexpr _Elem _Max = static_cast<_Elem>(~_Min); + constexpr _Elem _Min = _STD _Min_limit<_Elem>(); + constexpr _Elem _Max = _STD _Max_limit<_Elem>(); if constexpr (is_signed_v<_Ty>) { // signed _Elem, signed _Ty @@ -5956,7 +5955,7 @@ _NODISCARD constexpr bool _Could_compare_equal_to_value_type(const _Ty& _Val) { } } } else { - constexpr _Elem _Max = static_cast<_Elem>(~_Elem{0}); + constexpr _Elem _Max = _STD _Max_limit<_Elem>(); if constexpr (is_unsigned_v<_Ty>) { // unsigned _Elem, unsigned _Ty @@ -7387,8 +7386,7 @@ _NODISCARD constexpr bool _Mul_overflow(const _Int _Left, const _Int _Right, _In #endif // defined(__clang__) { if constexpr (!_Signed_integer_like<_Int>) { - // use instead of numeric_limits::max; avoid dependency - constexpr auto _UInt_max = static_cast<_Int>(-1); + constexpr auto _UInt_max = _Max_limit<_Int>(); const bool _Overflow = _Left != 0 && _Right > _UInt_max / _Left; if (!_Overflow) { _Out = static_cast<_Int>(_Left * _Right); @@ -7417,8 +7415,7 @@ _NODISCARD constexpr bool _Mul_overflow(const _Int _Left, const _Int _Right, _In return false; } - // use instead of numeric_limits::max; avoid dependency - constexpr auto _Int_max = static_cast<_UInt>(static_cast<_UInt>(-1) / 2); + constexpr auto _Int_max = _Max_limit<_Int>(); if (_Negative) { return _ULeft > (_Int_max + _UInt{1}) / _URight; } else { From 426973ab5612eacca7fc4fb618527ccf7b6164b3 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Wed, 1 May 2024 21:00:03 +0300 Subject: [PATCH 2/3] odd types --- stl/inc/utility | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/utility b/stl/inc/utility index 070899fb3fa..49acc0badd9 100644 --- a/stl/inc/utility +++ b/stl/inc/utility @@ -840,7 +840,7 @@ _NODISCARD constexpr bool _Cmp_greater_equal(const _Ty1 _Left, const _Ty2 _Right template _NODISCARD constexpr _Ty _Min_limit() noexcept { // same as (numeric_limits<_Ty>::min)(), less throughput cost - _STL_INTERNAL_STATIC_ASSERT(_Is_standard_integer<_Ty>); // doesn't attempt to handle all types + _STL_INTERNAL_STATIC_ASSERT(is_integral_v<_Ty>); // doesn't attempt to handle all types if constexpr (is_signed_v<_Ty>) { constexpr auto _Unsigned_max = static_cast>(-1); return static_cast<_Ty>((_Unsigned_max >> 1) + 1); // well-defined, N4950 [conv.integral]/3 @@ -851,7 +851,7 @@ _NODISCARD constexpr _Ty _Min_limit() noexcept { // same as (numeric_limits<_Ty> template _NODISCARD constexpr _Ty _Max_limit() noexcept { // same as (numeric_limits<_Ty>::max)(), less throughput cost - _STL_INTERNAL_STATIC_ASSERT(_Is_standard_integer<_Ty>); // doesn't attempt to handle all types + _STL_INTERNAL_STATIC_ASSERT(is_integral_v<_Ty>); // doesn't attempt to handle all types if constexpr (is_signed_v<_Ty>) { constexpr auto _Unsigned_max = static_cast>(-1); return static_cast<_Ty>(_Unsigned_max >> 1); From b982814d2026f99fee9e419f71060f8bab195b48 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Wed, 1 May 2024 21:23:06 +0300 Subject: [PATCH 3/3] Can't do this yet --- stl/inc/xutility | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stl/inc/xutility b/stl/inc/xutility index f7cf7338820..a7bc1079bf3 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -7386,7 +7386,8 @@ _NODISCARD constexpr bool _Mul_overflow(const _Int _Left, const _Int _Right, _In #endif // defined(__clang__) { if constexpr (!_Signed_integer_like<_Int>) { - constexpr auto _UInt_max = _Max_limit<_Int>(); + // use instead of numeric_limits::max; avoid dependency + constexpr auto _UInt_max = static_cast<_Int>(-1); const bool _Overflow = _Left != 0 && _Right > _UInt_max / _Left; if (!_Overflow) { _Out = static_cast<_Int>(_Left * _Right); @@ -7415,7 +7416,8 @@ _NODISCARD constexpr bool _Mul_overflow(const _Int _Left, const _Int _Right, _In return false; } - constexpr auto _Int_max = _Max_limit<_Int>(); + // use instead of numeric_limits::max; avoid dependency + constexpr auto _Int_max = static_cast<_UInt>(static_cast<_UInt>(-1) / 2); if (_Negative) { return _ULeft > (_Int_max + _UInt{1}) / _URight; } else {