Commit 0697fe84 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[utils] Templatize IsInRange so it can be specialized types other than int

Change-Id: I2d67f93f10ab07c3725cc1a799f9a74f4850b34e
Reviewed-on: https://chromium-review.googlesource.com/1194230Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55498}
parent 19a8f1ba
......@@ -58,10 +58,13 @@ inline bool CStringEquals(const char* s1, const char* s2) {
// Checks if value is in range [lower_limit, higher_limit] using a single
// branch.
inline bool IsInRange(int value, int lower_limit, int higher_limit) {
template <typename T, typename U>
inline bool IsInRange(T value, U lower_limit, U higher_limit) {
DCHECK_LE(lower_limit, higher_limit);
return static_cast<unsigned int>(value - lower_limit) <=
static_cast<unsigned int>(higher_limit - lower_limit);
STATIC_ASSERT(sizeof(U) <= sizeof(T));
typedef typename std::make_unsigned<T>::type unsigned_T;
return static_cast<unsigned_T>(value - lower_limit) <=
static_cast<unsigned_T>(higher_limit - lower_limit);
}
// X must be a power of 2. Returns the number of trailing zeros.
......
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