Align Stack: What, Why & How for x86 Processors

  • Thread starter Thread starter torelativity
  • Start date Start date
  • Tags Tags
    alignment
Click For Summary

Discussion Overview

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.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants inquire about the specific context of stack alignment and its importance on x86 processors.
  • One participant notes that failing to align double-precision variables to 8-byte boundaries can lead to significant performance penalties, citing a factor of two or more.
  • Another participant mentions that the Intel ABI does not guarantee 8-byte alignment for the stack, but recent compilers like gcc typically maintain this alignment by default.
  • A participant expresses confusion regarding the reasons behind alignment issues and shares a code snippet related to alignment.
  • It is discussed that data alignment is crucial for efficient CPU data fetching, as misaligned data can lead to increased fetch operations.
  • One participant emphasizes that while stack alignment is important, it is the alignment of individual variable addresses on the stack that is critical.
  • Concerns are raised about the potential consequences of accessing non-aligned addresses, including performance degradation and possible fatal errors on some systems.

Areas of Agreement / Disagreement

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.

Contextual Notes

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.

torelativity
Messages
3
Reaction score
0
What is alignment of a stack? And why is it important to align a stack when working on an x86 processor?
 
Computer science news on Phys.org
Can you be more specific in what context you're asking about this?

Anyway http://www.fftw.org/fftw3_doc/Stack-alignment-on-x86.html :

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.
 
Last edited by a moderator:
I had seen the link already

I had googled for the same yesterday and found the link but I didnt understand why that happens... anyways, i am attaching the piece of code the i found initially... the file was supposed to be align.h but i don't know if the messageboard would accept upload of formats other than the given ones...
 

Attachments

Alignment of data in memory is to make sure that data is meaningfully "aligned" in the memory, because when the CPU fetches data, it doesn't fetch one bit or one byte, it fetches a few bytes, maybe 2, 4, 8, or 16 depending on your CPU. If one of your variables you want to perform an operation on is in two "segments", your CPU will then for example need to fetch twice.

In fact it's usually important on any processor. In some processors it's not only recommended to keep the stack aligned, it's required.

I suppose the stack is more "special" in the fact that it is only created at runtime instead of being preallocated. You can read more about its significance in Wikipedia under 'data structure alignment'.
 
Its not necessarily the alignment of the stack itself, but the address of each variable stored on the stack that the header file deals with.

Addresses in memory are odd, even, or are "aligned" meaning the address divides with no remainder, on different boundaries. Those boundaries can be 2 (bytes), 4, 8, 16... page.
A page is defined by the system but could be 4096 bytes - for example.

In some systems accessing a variable with a non-aligned address can cause a fatal bus error, result in bad performance, or interfere with caching memory.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 102 ·
4
Replies
102
Views
4K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K