Home > Software engineering >  Why is malloc 16 bytes alligned
Why is malloc 16 bytes alligned

Time:01-13

The GNU documentation states that malloc is aligned to 16 byte multiples on 64 bit systems. Why is this?

If my understanding is correct, registers and all instructions operate on values that are a maximum of 8 bytes wide. Thus, it would seem that 8 byte alignment would be the requirement.

Notes:

CodePudding user response:

x86_64 uses xmm registers (heavily -- all fp stuff is done in xmm registers as the 8087 fp registers are deprecated), and xmm registers require 16-byte alignment for (efficient) access.

So most things in x86_64 (both the stack and heap allocated by malloc) are organized to always be 16-byte aligned, so the compiler is always free to use the 'aligned' instructions when xmm registers are involved and does not need to use the (possibly slower) unaligned instructions.

  •  Tags:  
  • Related