Is 8-byte alignment for “double” type necessary?

前端 未结 5 820
滥情空心
滥情空心 2021-01-20 03:19

I understand word-alignment, which makes the cpu only need to read once when reading an integer into a register.

But is 8-byte alignment (let\'s assume 32bit system

5条回答
  •  爱一瞬间的悲伤
    2021-01-20 04:20

    On many architectures, unaligned access of any load/store unit (short, int, long) is simply an exception. Compilers are responsible for ensuring it doesn't happen on potentially mis-aligned data, by emitting smaller access instructions and re-assembling in registers if they can't prove a given pointer is OK.

    Performance-wise, 8-byte alignment of doubles on 32-bit systems can be valuable for a few reasons. The most apparent is that 4-byte alignment of an 8-byte double means that one element could cross the boundary of two cache lines. Memory access occurs in units of whole cache lines, and so misalignment doubles the cost of access.

提交回复
热议问题