From 2bd48c820db2cc6d05aa6d6e8b295cdb2b475984 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 9 Jan 2025 11:06:34 +0000 Subject: [PATCH] Bug 1927858 - Replace mozilla::Clamp by std::clamp r=jgilbert,emilio Differential Revision: https://phabricator.services.mozilla.com/D227254 --- mfbt/MathAlgorithms.h | 15 ---------- mfbt/tests/TestMathAlgorithms.cpp | 29 ------------------- netwerk/protocol/http/HttpTrafficAnalyzer.cpp | 2 +- 3 files changed, 1 insertion(+), 45 deletions(-) diff --git a/mfbt/MathAlgorithms.h b/mfbt/MathAlgorithms.h index eeaaca2eee11..d4176f43c09f 100644 --- a/mfbt/MathAlgorithms.h +++ b/mfbt/MathAlgorithms.h @@ -409,21 +409,6 @@ constexpr bool IsPowerOfTwo(T x) { return x && (x & (x - 1)) == 0; } -template -inline T Clamp(const T aValue, const T aMin, const T aMax) { - static_assert(std::is_integral_v, - "Clamp accepts only integral types, so that it doesn't have" - " to distinguish differently-signed zeroes (which users may" - " or may not care to distinguish, likely at a perf cost) or" - " to decide how to clamp NaN or a range with a NaN" - " endpoint."); - MOZ_ASSERT(aMin <= aMax); - - if (aValue <= aMin) return aMin; - if (aValue >= aMax) return aMax; - return aValue; -} - template inline uint_fast8_t CountTrailingZeroes(T aValue) { static_assert(sizeof(T) <= 8); diff --git a/mfbt/tests/TestMathAlgorithms.cpp b/mfbt/tests/TestMathAlgorithms.cpp index f17454041f29..92eafec40fb7 100644 --- a/mfbt/tests/TestMathAlgorithms.cpp +++ b/mfbt/tests/TestMathAlgorithms.cpp @@ -8,36 +8,8 @@ #include -using mozilla::Clamp; using mozilla::IsPowerOfTwo; -static void TestClamp() { - MOZ_RELEASE_ASSERT(Clamp(0, 0, 0) == 0); - MOZ_RELEASE_ASSERT(Clamp(1, 0, 0) == 0); - MOZ_RELEASE_ASSERT(Clamp(-1, 0, 0) == 0); - - MOZ_RELEASE_ASSERT(Clamp(0, 1, 1) == 1); - MOZ_RELEASE_ASSERT(Clamp(0, 1, 2) == 1); - - MOZ_RELEASE_ASSERT(Clamp(0, -1, -1) == -1); - MOZ_RELEASE_ASSERT(Clamp(0, -2, -1) == -1); - - MOZ_RELEASE_ASSERT(Clamp(0, 1, 3) == 1); - MOZ_RELEASE_ASSERT(Clamp(1, 1, 3) == 1); - MOZ_RELEASE_ASSERT(Clamp(2, 1, 3) == 2); - MOZ_RELEASE_ASSERT(Clamp(3, 1, 3) == 3); - MOZ_RELEASE_ASSERT(Clamp(4, 1, 3) == 3); - MOZ_RELEASE_ASSERT(Clamp(5, 1, 3) == 3); - - MOZ_RELEASE_ASSERT(Clamp(UINT8_MAX, 0, UINT8_MAX) == UINT8_MAX); - MOZ_RELEASE_ASSERT(Clamp(0, 0, UINT8_MAX) == 0); - - MOZ_RELEASE_ASSERT(Clamp(INT8_MIN, INT8_MIN, INT8_MAX) == INT8_MIN); - MOZ_RELEASE_ASSERT(Clamp(INT8_MIN, 0, INT8_MAX) == 0); - MOZ_RELEASE_ASSERT(Clamp(INT8_MAX, INT8_MIN, INT8_MAX) == INT8_MAX); - MOZ_RELEASE_ASSERT(Clamp(INT8_MAX, INT8_MIN, 0) == 0); -} - static void TestIsPowerOfTwo() { static_assert(!IsPowerOfTwo(0u), "0 isn't a power of two"); static_assert(IsPowerOfTwo(1u), "1 is a power of two"); @@ -730,7 +702,6 @@ void TestGCD() { int main() { TestIsPowerOfTwo(); - TestClamp(); TestGCD(); return 0; diff --git a/netwerk/protocol/http/HttpTrafficAnalyzer.cpp b/netwerk/protocol/http/HttpTrafficAnalyzer.cpp index 879c4977e9ef..500b7e601c0c 100644 --- a/netwerk/protocol/http/HttpTrafficAnalyzer.cpp +++ b/netwerk/protocol/http/HttpTrafficAnalyzer.cpp @@ -246,7 +246,7 @@ void HttpTrafficAnalyzer::IncrementHttpConnection( } #define CLAMP_U32(num) \ - Clamp(num, 0, std::numeric_limits::max()) + std::clamp(num, 0, std::numeric_limits::max()) void HttpTrafficAnalyzer::AccumulateHttpTransferredSize( HttpTrafficCategory aCategory, uint64_t aBytesRead, uint64_t aBytesSent) {