Add countLeadingZeros

This commit is contained in:
Phillip Stephens 2020-09-19 15:43:35 -07:00
parent 58c8a902ac
commit eec855018a
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
1 changed files with 20 additions and 0 deletions

View File

@ -159,6 +159,26 @@ template <typename E>
return PopCount(static_cast<typename std::underlying_type<E>::type>(e));
}
template <typename U>
[[nodiscard]] typename std::enable_if<!std::is_enum<U>::value && std::is_integral<U>::value, int>::type
countLeadingZeros(U x) {
#if __GNUC__ >= 4
return __builtin_clz(x);
#else
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
return PopCount(~x);
#endif
}
template <typename E>
[[nodiscard]] typename std::enable_if<std::is_enum<E>::value, int>::type countLeadingZeros(E e) {
return countLeadingZeros(static_cast<typename std::underlying_type<E>::type>(e));
}
[[nodiscard]] bool close_enough(const CVector3f& a, const CVector3f& b, float epsilon = FLT_EPSILON);
[[nodiscard]] bool close_enough(const CVector2f& a, const CVector2f& b, float epsilon = FLT_EPSILON);