mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-18 21:19:31 +03:00
Implement find first set for reducing division
This commit is contained in:
parent
1125271c27
commit
3ff9e900b9
1 changed files with 25 additions and 0 deletions
25
src/heap.c
25
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 <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
|
||||
Todo: enable visiting abandoned pages, and
|
||||
|
|
Loading…
Add table
Reference in a new issue