From 3ff9e900b9d52f154cba77d71f83da32dc5111ea Mon Sep 17 00:00:00 2001 From: Julian Fang Date: Fri, 5 Jul 2019 01:49:42 +0800 Subject: [PATCH] Implement find first set for reducing division --- src/heap.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/heap.c b/src/heap.c index f474b1a0..017b0c5f 100644 --- a/src/heap.c +++ b/src/heap.c @@ -386,6 +386,31 @@ bool mi_check_owned(const void* p) { return mi_heap_check_owned(mi_get_default_heap(), p); } +// find first set: return the index of the lowest set bit. +static inline uint8_t mi_ffs32(uint32_t x); + +#if defined(_MSC_VER) +#include +static inline uint8_t mi_ffs32(uint32_t x) { + uint32_t idx; + if (_BitScanForward(&idx, x) != 0)return idx; + + return 0; +} +#elif defined(__GNUC__) || defined(__clang__) +static inline uint8_t mi_ffs32(uint32_t x) { + return (__builtin_ffs(x))-1; +} +#else +static inline uint8_t mi_ffs32(uint32_t x) { + static const uint8_t debruijn[32] = { + 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, + 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 +}; + return debruijn[((uint32_t)(x&-x)*0x077CB531U) >> 27]; +} +#endif + /* ----------------------------------------------------------- Visit all heap blocks and areas Todo: enable visiting abandoned pages, and