Why is gcc aligning arrays that way?

Programming, for all ages and all languages.
Post Reply
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Why is gcc aligning arrays that way?

Post by no92 »

While working on my compiler and implementing arrays, I analyzed how gcc does things. The thing that kept me wondering was how it's aligning the arrays. I seems to align the array to the next-smallest power of 2. Well, except for aligning the 8-byte big array to a 4-byte boundary. Why does it align it that way?

You can see the assembly output here.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Why is gcc aligning arrays that way?

Post by Combuster »

There are two things in play: A 32-bit number has a natural alignment of four, so it will start with that because the performance stinks otherwise.

If you have four dwords, you can fit them together into an xmm register. But for that to work, they need to be aligned as such or you have to use special instructions. AVX registers are double that size, so it happens again there. Both sizes are also common cache line sizes, allowing you to get each of these arrays loaded in one memory clock if you touch just one item in it. A 2-3-item array has no natural benefits, so space is probably considered the best next choice.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Why is gcc aligning arrays that way?

Post by no92 »

Thank you, problem solved :D
Post Reply