Commit dbf44ac9 authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Change [unsigned] long long to [u]int64_t third_party/blink/renderer/platform/wtf/math_extras.h

- unsigned long long / unsigned long long int -> uint64_t
- long long / long long int -> int64_t
- No logic change.
- Reference: https://google.github.io/styleguide/cppguide.html#Integer_Types

Bug: 929980
Change-Id: I1a44a3752f27b19cc3f2a493fec9540324c61dd9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1504479Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Miyoung Shin <myid.shin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638038}
parent 9624aabd
......@@ -145,7 +145,7 @@ inline LimitType clampToDirectComparison(ValueType value,
return (value <= min) ? min : static_cast<LimitType>(value);
}
// For any floating-point limits, or integral limits smaller than long long, we
// For any floating-point limits, or integral limits smaller than int64_t, we
// can cast the limits to double without losing precision; then the only cases
// where |value| can't be represented accurately as a double are the ones where
// it's outside the limit range anyway. So doing all comparisons as doubles
......@@ -193,8 +193,8 @@ class ClampToNonLongLongHelper<false, LimitType, ValueType> {
};
// The unspecialized version of this templated class handles clamping to
// anything other than [unsigned] long long int limits. It simply uses the
// class above to toggle between the "fast" and "safe" clamp implementations.
// anything other than [u]int64_t limits. It simply uses the class above
// to toggle between the "fast" and "safe" clamp implementations.
template <typename LimitType, typename ValueType>
class ClampToHelper {
public:
......@@ -224,75 +224,66 @@ class ClampToHelper {
}
};
// Clamping to [unsigned] long long int limits requires more care. These may
// not be accurately representable as doubles, so instead we cast |value| to the
// limit type. But that cast is undefined if |value| is floating point and
// Clamping to [u]int64_t limits requires more care. These may not be
// accurately representable as doubles, so instead we cast |value| to the
// limit type. But that cast is undefined if |value| is floating point and
// outside the representable range of the limit type, so we also have to check
// for that case explicitly.
template <typename ValueType>
class ClampToHelper<long long int, ValueType> {
class ClampToHelper<int64_t, ValueType> {
STATIC_ONLY(ClampToHelper);
public:
static inline long long int clampTo(ValueType value,
long long int min,
long long int max) {
static inline int64_t clampTo(ValueType value, int64_t min, int64_t max) {
if (!std::numeric_limits<ValueType>::is_integer) {
if (value > 0) {
if (static_cast<double>(value) >=
static_cast<double>(std::numeric_limits<long long int>::max()))
static_cast<double>(std::numeric_limits<int64_t>::max()))
return max;
} else if (static_cast<double>(value) <=
static_cast<double>(
std::numeric_limits<long long int>::min())) {
static_cast<double>(std::numeric_limits<int64_t>::min())) {
return min;
}
}
// Note: If |value| were unsigned long long int, it could be larger than
// the largest long long int, and this code would be wrong; we handle
// this case with a separate full specialization below.
return clampToDirectComparison(static_cast<long long int>(value), min, max);
// Note: If |value| were uint64_t it could be larger than the largest
// int64_t, and this code would be wrong; we handle this case with
// a separate full specialization below.
return clampToDirectComparison(static_cast<int64_t>(value), min, max);
}
};
// This specialization handles the case where the above partial specialization
// would be potentially incorrect.
template <>
class ClampToHelper<long long int, unsigned long long int> {
class ClampToHelper<int64_t, uint64_t> {
STATIC_ONLY(ClampToHelper);
public:
static inline long long int clampTo(unsigned long long int value,
long long int min,
long long int max) {
if (max <= 0 || value >= static_cast<unsigned long long int>(max))
static inline int64_t clampTo(uint64_t value, int64_t min, int64_t max) {
if (max <= 0 || value >= static_cast<uint64_t>(max))
return max;
const long long int longLongValue = static_cast<long long int>(value);
const int64_t longLongValue = static_cast<int64_t>(value);
return (longLongValue <= min) ? min : longLongValue;
}
};
// This is similar to the partial specialization that clamps to long long int,
// but because the lower-bound check is done for integer value types as well, we
// don't need a <unsigned long long int, long long int> full specialization.
// This is similar to the partial specialization that clamps to int64_t, but
// because the lower-bound check is done for integer value types as well, we
// don't need a <uint64_t, int64_t> full specialization.
template <typename ValueType>
class ClampToHelper<unsigned long long int, ValueType> {
class ClampToHelper<uint64_t, ValueType> {
STATIC_ONLY(ClampToHelper);
public:
static inline unsigned long long int clampTo(ValueType value,
unsigned long long int min,
unsigned long long int max) {
static inline uint64_t clampTo(ValueType value, uint64_t min, uint64_t max) {
if (value <= 0)
return min;
if (!std::numeric_limits<ValueType>::is_integer) {
if (static_cast<double>(value) >=
static_cast<double>(
std::numeric_limits<unsigned long long int>::max()))
static_cast<double>(std::numeric_limits<uint64_t>::max()))
return max;
}
return clampToDirectComparison(static_cast<unsigned long long int>(value),
min, max);
return clampToDirectComparison(static_cast<uint64_t>(value), min, max);
}
};
......
......@@ -71,10 +71,10 @@ TEST(MathExtrasTest, clampToIntLong) {
}
TEST(MathExtrasTest, clampToIntLongLong) {
long long max_int = std::numeric_limits<int>::max();
long long min_int = std::numeric_limits<int>::min();
long long overflow_int = max_int + 1;
long long underflow_int = min_int - 1;
int64_t max_int = std::numeric_limits<int>::max();
int64_t min_int = std::numeric_limits<int>::min();
int64_t overflow_int = max_int + 1;
int64_t underflow_int = min_int - 1;
EXPECT_GT(overflow_int, max_int);
EXPECT_LT(underflow_int, min_int);
......@@ -158,26 +158,25 @@ TEST(MathExtrasTest, clampToDouble) {
EXPECT_EQ(0.0, clampTo<double>(0));
EXPECT_EQ(0.0, clampTo<double>(0.0f));
EXPECT_EQ(0.0, clampTo<double>(0ULL));
EXPECT_EQ(3.5, clampTo<double>(std::numeric_limits<unsigned long long>::max(),
0.0, 3.5));
EXPECT_EQ(3.5,
clampTo<double>(std::numeric_limits<uint64_t>::max(), 0.0, 3.5));
}
TEST(MathExtrasText, clampToLongLongDouble) {
double overflow_ll =
static_cast<double>(std::numeric_limits<long long>::max()) * 2;
EXPECT_EQ(std::numeric_limits<long long>::max(),
clampTo<long long>(overflow_ll));
EXPECT_EQ(std::numeric_limits<long long>::min(),
clampTo<long long>(-overflow_ll));
static_cast<double>(std::numeric_limits<int64_t>::max()) * 2;
EXPECT_EQ(std::numeric_limits<int64_t>::max(), clampTo<int64_t>(overflow_ll));
EXPECT_EQ(std::numeric_limits<int64_t>::min(),
clampTo<int64_t>(-overflow_ll));
}
TEST(MathExtrasText, clampToUnsignedLongLongDouble) {
double overflow_ull =
static_cast<double>(std::numeric_limits<unsigned long long>::max()) * 2;
EXPECT_EQ(std::numeric_limits<unsigned long long>::max(),
clampTo<unsigned long long>(overflow_ull));
EXPECT_EQ(std::numeric_limits<unsigned long long>::min(),
clampTo<unsigned long long>(-overflow_ull));
static_cast<double>(std::numeric_limits<uint64_t>::max()) * 2;
EXPECT_EQ(std::numeric_limits<uint64_t>::max(),
clampTo<uint64_t>(overflow_ull));
EXPECT_EQ(std::numeric_limits<uint64_t>::min(),
clampTo<uint64_t>(-overflow_ull));
}
TEST(MathExtrasTest, clampToUnsignedUnsignedLong) {
......@@ -196,8 +195,8 @@ TEST(MathExtrasTest, clampToUnsignedUnsignedLong) {
}
TEST(MathExtrasTest, clampToUnsignedUnsignedLongLong) {
unsigned long long max_unsigned = std::numeric_limits<unsigned>::max();
unsigned long long overflow_unsigned = max_unsigned + 1;
uint64_t max_unsigned = std::numeric_limits<unsigned>::max();
uint64_t overflow_unsigned = max_unsigned + 1;
EXPECT_GT(overflow_unsigned, max_unsigned);
......@@ -208,31 +207,30 @@ TEST(MathExtrasTest, clampToUnsignedUnsignedLongLong) {
}
TEST(MathExtrasTest, clampToLongLongUnsignedLongLong) {
long long max_long_long_ll = std::numeric_limits<long long>::max();
unsigned long long max_long_long_ull = max_long_long_ll;
unsigned long long overflow_long_long = max_long_long_ull + 1;
int64_t max_long_long_ll = std::numeric_limits<int64_t>::max();
uint64_t max_long_long_ull = max_long_long_ll;
uint64_t overflow_long_long = max_long_long_ull + 1;
EXPECT_GT(overflow_long_long, max_long_long_ull);
EXPECT_EQ(max_long_long_ll, clampTo<long long>(max_long_long_ull));
EXPECT_EQ(max_long_long_ll - 1, clampTo<long long>(max_long_long_ull - 1));
EXPECT_EQ(max_long_long_ll, clampTo<long long>(overflow_long_long));
EXPECT_EQ(max_long_long_ll, clampTo<int64_t>(max_long_long_ull));
EXPECT_EQ(max_long_long_ll - 1, clampTo<int64_t>(max_long_long_ull - 1));
EXPECT_EQ(max_long_long_ll, clampTo<int64_t>(overflow_long_long));
EXPECT_EQ(-3LL, clampTo<long long>(2ULL, -5LL, -3LL));
EXPECT_EQ(-3LL, clampTo<int64_t>(2ULL, -5LL, -3LL));
}
TEST(MathExtrasTest, clampToUnsignedLongLongInt) {
EXPECT_EQ(0ULL, clampTo<unsigned long long>(-1));
EXPECT_EQ(0ULL, clampTo<unsigned long long>(0));
EXPECT_EQ(1ULL, clampTo<unsigned long long>(1));
EXPECT_EQ(0ULL, clampTo<uint64_t>(-1));
EXPECT_EQ(0ULL, clampTo<uint64_t>(0));
EXPECT_EQ(1ULL, clampTo<uint64_t>(1));
}
TEST(MathExtrasTest, clampToUnsignedLongLongUnsignedLongLong) {
EXPECT_EQ(0ULL, clampTo<unsigned long long>(0ULL));
EXPECT_EQ(1ULL, clampTo<unsigned long long>(0ULL, 1ULL, 2ULL));
EXPECT_EQ(2ULL, clampTo<unsigned long long>(3ULL, 1ULL, 2ULL));
EXPECT_EQ(0xFFFFFFFFFFFFFFF5ULL,
clampTo<unsigned long long>(0xFFFFFFFFFFFFFFF5ULL));
EXPECT_EQ(0ULL, clampTo<uint64_t>(0ULL));
EXPECT_EQ(1ULL, clampTo<uint64_t>(0ULL, 1ULL, 2ULL));
EXPECT_EQ(2ULL, clampTo<uint64_t>(3ULL, 1ULL, 2ULL));
EXPECT_EQ(0xFFFFFFFFFFFFFFF5ULL, clampTo<uint64_t>(0xFFFFFFFFFFFFFFF5ULL));
}
// Make sure that various +-inf cases are handled properly (they weren't
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment