torelativity
- 3
- 0
What is alignment of a stack? And why is it important to align a stack when working on an x86 processor?
On the Pentium and subsequent x86 processors, there is a substantial performance penalty if double-precision variables [that is, high-precision floating point numbers] are not stored 8-byte aligned [that is, at an address a such that ((int)a)%8 == 0)]; a factor of two or more is not unusual. Unfortunately, the stack (the place that local variables and subroutine arguments live) is not guaranteed by the Intel ABI to be 8-byte aligned.
Recent versions of gcc (as well as most other compilers, we are told, such as Intel's, Metrowerks', and Microsoft's) are able to keep the stack 8-byte aligned; gcc does this by default (see -mpreferred-stack-boundary in the gcc documentation). If you are not certain whether your compiler maintains stack alignment by default, it is a good idea to make sure.