Align Stack: What, Why & How for x86 Processors

  • Thread starter Thread starter torelativity
  • Start date Start date
  • Tags Tags
    alignment
AI Thread Summary
Stack alignment is crucial when working on x86 processors due to performance implications, particularly for double-precision variables, which require 8-byte alignment. Misalignment can lead to significant performance penalties, often doubling the time needed for data access. The Intel ABI does not guarantee stack alignment, but modern compilers like gcc ensure 8-byte alignment by default. Proper alignment is essential because the CPU fetches data in chunks (2, 4, 8, or 16 bytes), and misaligned variables can cause the CPU to perform additional fetches, degrading performance. While stack alignment is particularly important due to its dynamic nature at runtime, alignment principles apply to all data structures. Non-aligned memory access can lead to severe issues, including bus errors and inefficient caching.
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.
 
In my discussions elsewhere, I've noticed a lot of disagreement regarding AI. A question that comes up is, "Is AI hype?" Unfortunately, when this question is asked, the one asking, as far as I can tell, may mean one of three things which can lead to lots of confusion. I'll list them out now for clarity. 1. Can AI do everything a human can do and how close are we to that? 2. Are corporations and governments using the promise of AI to gain more power for themselves? 3. Are AI and transhumans...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Back
Top