Codechange: replace hand written function to find first/last bit with C++ variant

This commit is contained in:
Rubidium
2024-01-18 18:40:52 +01:00
committed by rubidium42
parent 903119115b
commit 8faaedeff9
4 changed files with 31 additions and 60 deletions

View File

@@ -222,8 +222,35 @@ inline uint8_t FindFirstBit2x64(const int value)
}
}
uint8_t FindFirstBit(uint64_t x);
uint8_t FindLastBit(uint64_t x);
/**
* Search the first set bit in a value.
* When no bit is set, it returns 0.
*
* @param x The value to search.
* @return The position of the first bit set.
*/
template <typename T>
constexpr uint8_t FindFirstBit(T x)
{
if (x == 0) return 0;
return std::countr_zero(x);
}
/**
* Search the last set bit in a value.
* When no bit is set, it returns 0.
*
* @param x The value to search.
* @return The position of the last bit set.
*/
template <typename T>
constexpr uint8_t FindLastBit(T x)
{
if (x == 0) return 0;
return std::numeric_limits<T>::digits - std::countl_zero(x) - 1;
}
/**
* Clear the first bit in an integer.