How to determine if memory is aligned?

后端 未结 8 2343
生来不讨喜
生来不讨喜 2020-11-29 18:27

I am new to optimizing code with SSE/SSE2 instructions and until now I have not gotten very far. To my knowledge a common SSE-optimized function would look like this:

<
8条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 18:38

    Other answers suggest an AND operation with low bits set, and comparing to zero.

    But a more straight-forward test would be to do a MOD with the desired alignment value, and compare to zero.

    #define ALIGNMENT_VALUE     16u
    
    if (((uintptr_t)ptr % ALIGNMENT_VALUE) == 0)
    {
        // ptr is aligned
    }
    

提交回复
热议问题