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:
parent
b19da8e362
commit
52943917ad
6
src/os.c
6
src/os.c
@ -65,7 +65,11 @@ static void* mi_align_up_ptr(void* p, size_t alignment) {
|
|||||||
return (void*)_mi_align_up((uintptr_t)p, 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;
|
return (sz / alignment) * alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user