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.
Why is gcc aligning arrays that way?
- Combuster
- 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?
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.
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.