(Math) Added CountBits function
Former-commit-id: 11e825a95a7bda300e46eb321f0fdb51bea93343
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
|
||||
template<typename T> T NzApproach(T value, T objective, T increment);
|
||||
template<typename T> constexpr T NzClamp(T value, T min, T max);
|
||||
template<typename T> T NzCountBits(T value);
|
||||
template<typename T> constexpr T NzFromDegrees(T degrees);
|
||||
template<typename T> constexpr T NzFromRadians(T radians);
|
||||
template<typename T> constexpr T NzDegreeToRadian(T degrees);
|
||||
|
||||
@@ -114,6 +114,20 @@ constexpr T NzClamp(T value, T min, T max)
|
||||
return std::max(std::min(value, max), min);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T NzCountBits(T value)
|
||||
{
|
||||
// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
|
||||
unsigned int count = 0;
|
||||
while (value)
|
||||
{
|
||||
value &= value - 1;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr T NzDegreeToRadian(T degrees)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user