AIX, compiler xlC: alignment of double-member in structure

Discussion in 'C++' started by Alex Vinokur, Apr 8, 2010.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,

    I have the following problem on AIX (compiler xlC) with alignment of
    double-member in structure.

    1. cpp-file

    // ------- File align1.cpp : BEGIN -------
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>

    struct SampleLong
    {
    char m_char;
    long m_long;
    };

    struct SampleDouble
    {
    char m_char;
    // --------------
    // On AIX, compiler xlC:
    // 1) sizeof (m_double) == 8, but offset == 4,
    // 2) pointer &m_double has only two binary nulls at the end,
    // i.e. is not aligned to 8 bytes
    double m_double;
    // --------------
    };

    #define SHOW(s,m) std::cout << "" \
    << #s \
    << "." \
    << std::setw(8) \
    << std::left \
    << #m \
    << std::right \
    << ": " \
    << "struct sizeof = " << sizeof(s) \
    << ", " \
    << "member sizeof = " << sizeof(s.m) \
    << ", " \
    << "member offset = " <<
    (reinterpret_cast<std::size_t>(&s.m) -
    reinterpret_cast<std::size_t>(&s)) \
    << ", " \
    << "struct ptr = " <<
    reinterpret_cast<void*>(&s) \
    << ", " \
    << "member ptr = " <<
    reinterpret_cast<void*>(&s.m) \
    << std::endl


    int main()
    {

    system ("uname -a");
    std::cout << std::endl;
    #ifdef _AIX
    system ("xlC -qversion");
    std::cout << std::endl;
    #endif

    #ifdef __hpux
    system ("aCC -V");
    std::cout << std::endl;
    #endif

    SampleLong instL;
    SampleDouble instD;

    SHOW (instL, m_long);
    SHOW (instD, m_double);

    return 0;
    }

    // ------- File align1.cpp : END -------



    2.1. Compilation and running on AIX 5.3 (compiler xlC V8.0)

    > xlC -q64 -qwarn64 align1.cpp


    > ./a.out

    AIX ep5710g 3 5 00C58FE04C00

    IBM XL C/C++ Enterprise Edition V8.0 for AIX
    Version: 08.00.0000.0014

    instL.m_long : struct sizeof = 16, member sizeof = 8, member offset =
    8, struct ptr = fffffffffff3eb0, member ptr = fffffffffff3eb8
    instD.m_double: struct sizeof = 12, member sizeof = 8, member offset =
    4, struct ptr = fffffffffff3ed0, member ptr = fffffffffff3ed4


    2.2. Compilation and running on AIX 6.1 (compiler xlC V10.1)

    > xlC -q64 -qwarn64 align1.cpp


    AIX ep5512b 1 6 000497A2D900

    IBM XL C/C++ for AIX, V10.1
    Version: 10.01.0000.0000

    instL.m_long : struct sizeof = 16, member sizeof = 8, member offset =
    8, struct ptr = fffffffffff0790, member ptr = fffffffffff0798
    instD.m_double: struct sizeof = 12, member sizeof = 8, member offset =
    4, struct ptr = fffffffffff07b0, member ptr = fffffffffff07b4



    2.3. Compilation and running on HP-UX v2 (compiler xlC aCC A.06.15)

    HP-UX hpx418 B.11.23 U ia64 1139467043 unlimited-user license

    aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]

    instL.m_long : struct sizeof = 16, member sizeof = 8, member offset =
    8, struct ptr = 9ffffffffffec9e0, member ptr = 9ffffffffffec9e8
    instD.m_double: struct sizeof = 16, member sizeof = 8, member offset =
    8, struct ptr = 9ffffffffffec9f0, member ptr = 9ffffffffffec9f8


    3. Analysis

    We can see some problem on AIX compiler with m_double:
    its sizeof = 8, but its offset = 4
    and
    its pointer is not aligned to 8 bytes

    P.S. No problem with double-member on HP-UX compiler.



    Alex Vinokur
    Alex Vinokur, Apr 8, 2010
    #1
    1. Advertising

  2. Alex Vinokur

    Joerg Guest

    > 3. Analysis
    >
    > We can see some problem on AIX compiler with m_double:
    >   its sizeof = 8, but its offset = 4
    >      and
    >   its pointer is not aligned to 8 bytes


    Hi,

    When the double is not the first member, the alignment is 4. The AIX
    ABI specifies it to be so.
    This is because some old AIX compiler introduced this bug. All newer
    compilers behave the same
    to be backward compatible.


    Jörg
    Joerg, Apr 8, 2010
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Joerg" <> wrote in message
    news:...
    >> 3. Analysis
    >>
    >> We can see some problem on AIX compiler with m_double:
    >> its sizeof = 8, but its offset = 4
    >> and
    >> its pointer is not aligned to 8 bytes

    >
    > Hi,
    >
    > When the double is not the first member, the alignment is 4. The AIX
    > ABI specifies it to be so.
    > This is because some old AIX compiler introduced this bug. All newer
    > compilers behave the same
    > to be backward compatible.
    >
    >
    > Jörg


    Thanks.

    Does that affect performance of processing double-members?

    Alex Vinokur
    Alex Vinokur, Apr 9, 2010
    #3
  4. Alex Vinokur

    Joerg Guest

    On 9 Apr., 07:02, "Alex Vinokur" <> wrote:

    > Does that affect performance of processing double-members?


    Hi,

    I haven't measured, so I can't tell you.
    But I wouldn't be surprised if it does.

    Jörg
    Joerg, Apr 9, 2010
    #4
  5. Alex Vinokur

    Gary R. Hook Guest

    On 4/9/2010 12:02 AM, Alex Vinokur wrote:
    >
    > Does that affect performance of processing double-members?


    On AIX?

    IIRC yes. You want doubles and long-longs to be double-word aligned.
    I think you still get alignment exceptions when they're not.

    Rule: put the member with the strictest alignment requirements first in
    your structure.
    Gary R. Hook, Apr 10, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pervinder
    Replies:
    1
    Views:
    1,352
    David Lindauer
    Sep 24, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,425
    Victor Bazarov
    Feb 17, 2005
  3. alban

    Compilation Problems on a AIX with XLC

    alban, Feb 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    866
    Jordan Abel
    Feb 9, 2006
  4. ChrisG
    Replies:
    1
    Views:
    444
    Thomas Tutone
    Nov 28, 2005
  5. Replies:
    1
    Views:
    293
    Victor Bazarov
    Mar 26, 2007
Loading...

Share This Page