From 9873073400416701f3deb37a628b0859551a6b5d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 29 Jun 2023 03:19:26 -0700 Subject: [PATCH] Util: Add ctz32 function --- include/mgba-util/math.h | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/include/mgba-util/math.h b/include/mgba-util/math.h index 8402da3db..f7b3269c9 100644 --- a/include/mgba-util/math.h +++ b/include/mgba-util/math.h @@ -25,7 +25,7 @@ static inline unsigned clz32(uint32_t bits) { } return __builtin_clz(bits); #else - static const int table[256] = { + static const int8_t table[256] = { 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -55,6 +55,43 @@ static inline unsigned clz32(uint32_t bits) { #endif } +static inline unsigned ctz32(uint32_t bits) { +#if defined(__GNUC__) || __clang__ + if (!bits) { + return 32; + } + return __builtin_ctz(bits); +#else + static const int8_t table[256] = { + 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + }; + + if (bits & 0x000000FF) { + return table[bits & 0xFF]; + } else if (bits & 0x0000FF00) { + return table[(bits >> 8) & 0xFF] + 8; + } else if (bits & 0x00FF0000) { + return table[(bits >> 16) & 0xFF] + 16; + } + return table[bits >> 24] + 24; +#endif +} + static inline uint32_t toPow2(uint32_t bits) { if (!bits) { return 0;