Bug 1927858 - Replace mozilla::Clamp by std::clamp r=jgilbert,emilio
Differential Revision: https://phabricator.services.mozilla.com/D227254
This commit is contained in:
@@ -409,21 +409,6 @@ constexpr bool IsPowerOfTwo(T x) {
|
||||
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>
|
||||
inline uint_fast8_t CountTrailingZeroes(T aValue) {
|
||||
static_assert(sizeof(T) <= 8);
|
||||
|
||||
@@ -8,36 +8,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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_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_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;
|
||||
|
||||
@@ -246,7 +246,7 @@ void HttpTrafficAnalyzer::IncrementHttpConnection(
|
||||
}
|
||||
|
||||
#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(
|
||||
HttpTrafficCategory aCategory, uint64_t aBytesRead, uint64_t aBytesSent) {
|
||||
|
||||
Reference in New Issue
Block a user