torelativity
- 3
- 0
What is alignment of a stack? And why is it important to align a stack when working on an x86 processor?
The discussion centers around the concept of stack alignment in the context of x86 processors, exploring its importance, implications for performance, and the behavior of various compilers regarding stack alignment. The scope includes technical explanations and conceptual clarifications related to memory alignment and its effects on processing efficiency.
Participants express varying levels of understanding regarding stack alignment, with some agreeing on its importance while others seek clarification. There is no consensus on the specific mechanisms or implications of stack alignment, indicating multiple competing views and unresolved questions.
Participants mention the dependence on compiler behavior and system architecture, as well as the potential for different alignment requirements across various processors. The discussion reflects uncertainty about the exact nature of alignment issues and their implications.
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.