From 471db7ce0a0b8ec295d4f6c27fafa454de309493 Mon Sep 17 00:00:00 2001 From: nimelehin Date: Mon, 23 Mar 2020 17:31:37 +0300 Subject: [PATCH] AK: Add count_trailing_zeroes_32() Add count_trailing_zeroes_32() which is implemented with builtins if available, otherwise there's a generic fallback. --- AK/Platform.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/AK/Platform.h b/AK/Platform.h index 22f0f2e1d0..d8e00c3e4f 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -84,3 +84,17 @@ template return value; #endif } + +[[gnu::always_inline]] inline int count_trailing_zeroes_32(unsigned int val) +{ +#if defined(__GNUC__) || defined(__clang__) + return __builtin_ctz(val); +#else + for (u8 i = 0; i < 32; ++i) { + if ((val >> i) & 1) { + return i; + } + } + return 0; +#endif +} \ No newline at end of file