Thread: alignment (classes, structs, members)

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    alignment (classes, structs, members)

    so i'm using sse2 and i'd like to use movaps (and similar instructions) which requires objects to be laid out at 16 byte boundaries (otherwise it segfaults).

    Code:
    so far i have:
    struct Vector4 {
      float c[4];
    ...
    };
    how can i force the compiler to put all instances at 16 byte bounds?
    i'm using g++. and simply putting a pragma pack around that struct didnt help (wrong usage i guess ^^).

    so if anyone knows how to force alignments of all instances then please tell me.
    (i couldn't find any satisfying anwer with google)
    signature under construction

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How did you attempt the pragma pack statement? IIRC it should have gone something like:
    Code:
    #pragma pack(16)
    I could be wrong though... it's been awhile since I've needed to mess with struct packing/alingment issues.

    You could try padding your structs with a dummy char array to the necessary size:
    Code:
    struct Vector4 {
        float c[4];
        ...
        char dummy[???];  // Pad as much as needed to get correct struct size
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > float c[4]
    I'm assuming this is your way of getting a 16 byte object?

    If so, maybe something like
    Code:
    struct Vector4 __attribute__((aligned(16))) {
      unsigned char d[16];
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ya, i figured that out with that "__attribute__ aligned" before i went to bed.
    but i also figured out that "new" ignores that attribute and does not return aligned memory... sigh...
    signature under construction

  5. #5
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    I don't know if this is correct or possible, i guess you could use a dummy array of chars to force the alignment as someone said, but that would probably be bad coding since it would be highly platform dependant and as such not portable (due to the size of char and size of float) so my sugestion is maybe overloading the new operator and using sizeof() to calculate and allocate dummy memory within your object.

    As i stated initially this might be wrong, bad practice or even not possible, just my weird idea.

    Cheers

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Create a static array of correctly aligned objects, and overload new to allocate from this pool of objects ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 03:26 PM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Typedefining members of structs
    By Verdagon in forum C++ Programming
    Replies: 18
    Last Post: 04-03-2005, 02:06 PM