FEXCore: Adds DividePow2 helper

This commit is contained in:
Ryan Houdek committed 2023-10-04 20:56:29 -07:00
1 parent 3ba1c7912c
commit ee6debe8fd
2 files changed
+14

No files matched your search

@@ -25,4 +25,12 @@ requires(std::is_unsigned_v<T>)
return std::countr_zero(Value);
}
// Divide a number by a power-of-2 by avoiding integer division.
// Can be a faster implementation than regular integer divide.
// Divisor requires to be power-of-2, is enforced in ilog2 helper.
template<typename T, typename TT>
requires(std::is_unsigned_v<T> && std::is_unsigned_v<TT>)
[[nodiscard]] constexpr T DividePow2(T Dividend, TT Divisor) {
return Dividend >> ilog2(Divisor);
}
} // namespace FEXCore
+6
View File
@@ -5,3 +5,9 @@ TEST_CASE("ILog2") {
auto i = GENERATE(range(0, 64));
REQUIRE(FEXCore::ilog2(1ull << i) == i);
}
TEST_CASE("DividePow2") {
auto j = GENERATE(range(0, 64));
auto i = GENERATE(range(0, 64));
REQUIRE(FEXCore::DividePow2(1ull << j, 1ull << i) == ((1ull << j) / (1ull << i)));
}