Implement find first set for reducing division

This commit is contained in:
Julian Fang 2019-07-05 01:49:42 +08:00
parent 1125271c27
commit 3ff9e900b9

View file

@ -386,6 +386,31 @@ bool mi_check_owned(const void* p) {
return mi_heap_check_owned(mi_get_default_heap(), 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 <intrin.h>
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 Visit all heap blocks and areas
Todo: enable visiting abandoned pages, and Todo: enable visiting abandoned pages, and