Reputation: 23374
I have some code I am studying where it is written:
(basenameOffset + (basenameTotal+15)) &~0xf
why would someone do this? What does it do? I can see that ~0xf
is 0xfffffff0
. Why would you block the last bit?
Upvotes: 1
Views: 357
Reputation: 30001
This is a common algorithm to round up to a multiple of a power of two:
x = x + (pow2 - 1) & ~(pow2 - 1)
It is almost certainly being used to ensure proper alignment. Typically to make SIMD-optimal (16-byte for SSE, 32-byte for AVX, etc.) base addresses and/or to optimize cache usage.
Upvotes: 1
Reputation: 223083
It rounds up to the nearest multiple of 16. Presumably, this is to fix a size for allocating the buffer for the basename
, whatever it is. :-)
However, if that were what it's for, i.e., deciding how big of a buffer to allocate, then this is not a good strategy. Ideally, you want to expand by a factor of 2 or at least 1.5 each time.
Upvotes: 4