diff options
Diffstat (limited to 'src/math/utils.hpp')
| -rw-r--r-- | src/math/utils.hpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/math/utils.hpp b/src/math/utils.hpp index 5c8c62d..ac19ddc 100644 --- a/src/math/utils.hpp +++ b/src/math/utils.hpp @@ -3,6 +3,7 @@ #include <array> #include <utility> +#include <concepts> #include "math/vector.hpp" namespace engine::math { @@ -35,6 +36,28 @@ constexpr float map(float x, float from1, float from2, float to1, float to2) { return to1 + (x - from1) * (to2 - to1) / (from2 - from1); } +template<typename UInt> +constexpr UInt log2_floored(UInt n) noexcept +requires std::same_as<UInt, unsigned> || std::same_as<UInt, unsigned long> || std::same_as<UInt, unsigned long long> +{ +#ifdef __GNUG__ + if constexpr (std::is_same_v<UInt, unsigned>) { + return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clz(n); + } else if constexpr (std::is_same_v<UInt, unsigned long>) { + return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clzl(n); + } else { // unsigned long long + return static_cast<UInt>(sizeof(UInt) * 8 - 1) - __builtin_clzll(n); + } +#else + UInt ret = 0; + while (n & 1) { + n >>= 1; + ret++; + } + return ret; +#endif +} + } #endif // MATH_UTILS_HPP |
