Search code examples
alignmentsimdssememory-alignmentstack-frame

Why is stack frame a multiple of 16 bytes long?


CSAPP explains that SSE instructions operate on 16-byte blocks of data and it needs memory addresses to be multiple of 16.

But what's the relationship with stack frame? Does it means SSE instructions operate on stack frame? If so, what's the commonly used instructions?


Solution

  • Yes, the alignmentment of stack frame is set so any instruction could work on any data type which you could potentially store in the stack frame.

    So on x86/x86_64 for example there are SSE instructions which suppose that the memory address is aligned to 16 bytes. Compiler then supposes that the stack frame is aligned 16 bytes so it could arrange the local variables that they are aligned too if needed. SSE instructions (like any other) can operate on any memory, including global, heap or stack.

    The same it is actually for heap - when you allocate structure longer than 16 (or equal), the malloc/new must return 16-bytes aligned address so that kind of instruction could work with it.