Bug 1927858 - Replace mozilla::Clamp by std::clamp r=jgilbert,emilio

Differential Revision: https://phabricator.services.mozilla.com/D227254
This commit is contained in:
serge-sans-paille
2025-01-09 11:06:34 +00:00
parent 8e8d452073
commit 2bd48c820d
3 changed files with 1 additions and 45 deletions

View File

@@ -409,21 +409,6 @@ constexpr bool IsPowerOfTwo(T x) {
return x && (x & (x - 1)) == 0; return x && (x & (x - 1)) == 0;
} }
template <typename T>
inline T Clamp(const T aValue, const T aMin, const T aMax) {
static_assert(std::is_integral_v<T>,
"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 <typename T> template <typename T>
inline uint_fast8_t CountTrailingZeroes(T aValue) { inline uint_fast8_t CountTrailingZeroes(T aValue) {
static_assert(sizeof(T) <= 8); static_assert(sizeof(T) <= 8);

View File

@@ -8,36 +8,8 @@
#include <stdint.h> #include <stdint.h>
using mozilla::Clamp;
using mozilla::IsPowerOfTwo; 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_t>(UINT8_MAX, 0, UINT8_MAX) == UINT8_MAX);
MOZ_RELEASE_ASSERT(Clamp<uint8_t>(0, 0, UINT8_MAX) == 0);
MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MIN, INT8_MIN, INT8_MAX) == INT8_MIN);
MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MIN, 0, INT8_MAX) == 0);
MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MAX, INT8_MIN, INT8_MAX) == INT8_MAX);
MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MAX, INT8_MIN, 0) == 0);
}
static void TestIsPowerOfTwo() { static void TestIsPowerOfTwo() {
static_assert(!IsPowerOfTwo(0u), "0 isn't a power of two"); static_assert(!IsPowerOfTwo(0u), "0 isn't a power of two");
static_assert(IsPowerOfTwo(1u), "1 is a power of two"); static_assert(IsPowerOfTwo(1u), "1 is a power of two");
@@ -730,7 +702,6 @@ void TestGCD() {
int main() { int main() {
TestIsPowerOfTwo(); TestIsPowerOfTwo();
TestClamp();
TestGCD(); TestGCD();
return 0; return 0;

View File

@@ -246,7 +246,7 @@ void HttpTrafficAnalyzer::IncrementHttpConnection(
} }
#define CLAMP_U32(num) \ #define CLAMP_U32(num) \
Clamp<uint32_t>(num, 0, std::numeric_limits<uint32_t>::max()) std::clamp<uint32_t>(num, 0, std::numeric_limits<uint32_t>::max())
void HttpTrafficAnalyzer::AccumulateHttpTransferredSize( void HttpTrafficAnalyzer::AccumulateHttpTransferredSize(
HttpTrafficCategory aCategory, uint64_t aBytesRead, uint64_t aBytesSent) { HttpTrafficCategory aCategory, uint64_t aBytesRead, uint64_t aBytesSent) {