Rewrite align_down with bitwise operations

mi_align_down_ptr was implemented with multiplication and division,
which can be converted to equivalent and deterministic bit operations.
This commit is contained in:
Jim Huang 2021-04-21 13:14:53 +00:00
parent b19da8e362
commit 52943917ad

View File

@ -65,7 +65,11 @@ static void* mi_align_up_ptr(void* p, size_t alignment) {
return (void*)_mi_align_up((uintptr_t)p, alignment);
}
static uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
static inline uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
mi_assert_internal(alignment != 0);
uintptr_t mask = alignment - 1;
if ((alignment & mask) == 0) // power of two?
return sz & ~mask;
return (sz / alignment) * alignment;
}