GCC & Alignment

This is a discussion on GCC & Alignment within the Tech Board forums, part of the Community Boards category; I want to share something i discovered yesterday. All i wanted was to have an object aligned in memory by ...

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602

    GCC & Alignment

    I want to share something i discovered yesterday.

    All i wanted was to have an object aligned in memory by 16 in order to use SSE2 instructions for efficiency. But i thought that GCC alignment extends to memory other than the struct itself. How wrong i was! After using the debugger and viewing the disassembly, i can surely say that GCC doesn't respect alignment rules on the stack. Shouldn't it?

    This was the code i compiled with the latest GCC and then viewed with the debugger:
    Code:
    #pragma pack(push, 16)
    struct vec3d_t
    {
        float X;
        float Y;
        float Z;
    };
    #pragma pack(pop)
    
    int main()
    {
        int dummy1 = 0;
        vec3d_t myVec = { 0.0f, 0.0f, 0.0f };
        int dummy2 = 0;
    
        int myRes = (int)(myVec.X+myVec.Y+myVec.Z+dummy1+dummy2); // Don't let the compiler optimize them away
    
        return myRes;
    }
    This was the ASM output:
    ( This isn't actually the version of GCC i compiled it with, as my flash drive i used right now to output this holds a little older version, but it is almost the same )
    Code:
    004012F0	push   %ebp
    004012F1	mov    %esp,%ebp
    004012F3	push   %edi
    004012F4	sub    $0x44,%esp
    004012F7	and    $0xfffffff0,%esp ; Stack aligned to 16
    004012FA	mov    $0x0,%eax
    004012FF	add    $0xf,%eax
    00401302	add    $0xf,%eax
    00401305	shr    $0x4,%eax
    00401308	shl    $0x4,%eax
    0040130B	mov    %eax,-0x3c(%ebp)
    0040130E	mov    -0x3c(%ebp),%eax
    00401311	call   0x401880 <_alloca>
    00401316	call   0x401400 <__main>
    0040131B	movl   $0x0,-0xc(%ebp)
    00401322	lea    -0x28(%ebp),%edi ; Memory alignment UNKNOWN
    00401325	cld
    00401326	mov    $0x0,%edx
    0040132B	mov    $0x3,%eax
    00401330	mov    %eax,%ecx
    00401332	mov    %edx,%eax
    00401334	rep stos %eax,%es:(%edi)
    00401336	movl   $0x0,-0x2c(%ebp)
    0040133D	flds   -0x28(%ebp)
    00401340	fadds  -0x24(%ebp)
    00401343	fadds  -0x20(%ebp)
    00401346	fildl  -0xc(%ebp)
    00401349	faddp  %st,%st(1)
    0040134B	fildl  -0x2c(%ebp)
    0040134E	faddp  %st,%st(1)
    00401350	fnstcw -0x32(%ebp)
    00401353	movzwl -0x32(%ebp),%eax
    00401357	or     $0xc00,%ax
    0040135B	mov    %ax,-0x34(%ebp)
    0040135F	fldcw  -0x34(%ebp)
    00401362	fistpl -0x30(%ebp)
    00401365	fldcw  -0x32(%ebp)
    00401368	mov    -0x30(%ebp),%eax
    0040136B	mov    -0x4(%ebp),%edi
    0040136E	leave
    0040136F	ret
    See?! Very simple! ( Well, to me it is )
    Devoted my life to programming...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Alignment and structure packing are not the same thing.

    When you pack a structure to N, the structures members will be on a boundary that is either a multiple of N, or a multiple of the size of the member, whichever is smaller.

    Also, don't confuse structure alignment with the alignment of it's individual members.

    http://gcc.gnu.org/onlinedocs/gcc/Ty...attribute-2583

    or for MSVC:

    http://msdn.microsoft.com/en-us/library/83ythb65.aspx
    http://msdn.microsoft.com/en-us/library/71kf49f1.aspx

    gg

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Thanks, Codeplug! That link solves my problem.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  3. Undefined reference to...
    By legendus in forum C Programming
    Replies: 19
    Last Post: 10-25-2009, 06:18 AM
  4. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21