Byte alignment of structures in Unix "C"

Byte alignment of structures in Unix "C"

Post by Walter C. Miller, SHR2-2/B12, Phone:770-55 » Sat, 25 May 1996 04:00:00



I am currently working on a project that will be using an DEC Alpha server
running Unix.

The application that I am developing in "C", also uses DECMessageQ for passing
messages between systems through the network. We have been able to interface
standard Vaxes and PCs without too much trouble, except for the Alpha.

The message structure that we are using needs to be BYTE aligned, but we are
unable to accomplish this. In VMS, there is no problem, and when we perform a
SIZEOF() the structure, VMS will respond with a size of 10, where the Unix
application says 12. We have tried to compile with the -Zp1 option, but this
appears to have no effect.

Any suggestions?

Walter C. Miller
Quantum Corporation
333 South St.
Shrewsbury, Ma. 01545

 
 
 

Byte alignment of structures in Unix "C"

Post by Blake Yeage » Sat, 25 May 1996 04:00:00


The Alpha architecture incurs significant performance penalties for data
elements not "naturally aligned", i.e. longwords on an address evenly
divisible by 4, quadwords on an address evenly divisible by 8 etc. By
default the compilers pad structures for natural alignment. The DEC C
compiler for OpenVMS has a #pragma nomember_alignment to override this,
so I'd guess the Unix verson does as well.

-Blake Yeager

 
 
 

Byte alignment of structures in Unix "C"

Post by Joe Halp » Sun, 26 May 1996 04:00:00



>I am currently working on a project that will be using an DEC Alpha server
>running Unix.

>The application that I am developing in "C", also uses DECMessageQ for passing
>messages between systems through the network. We have been able to interface
>standard Vaxes and PCs without too much trouble, except for the Alpha.

>The message structure that we are using needs to be BYTE aligned, but we are
>unable to accomplish this. In VMS, there is no problem, and when we perform a
>SIZEOF() the structure, VMS will respond with a size of 10, where the Unix
>application says 12. We have tried to compile with the -Zp1 option, but this
>appears to have no effect.

>Any suggestions?

Well, the obvious one is don't presume that processor specific alignment
requirements are portable :-).

Define an intermediate format, and send that between different
architectures.

Joe

 
 
 

Byte alignment of structures in Unix "C"

Post by Dr. Charles E. Campbell J » Fri, 31 May 1996 04:00:00




>>I am currently working on a project that will be using an DEC Alpha server
>>running Unix.

>>The application that I am developing in "C", also uses DECMessageQ for passing
>>messages between systems through the network. We have been able to interface
>>standard Vaxes and PCs without too much trouble, except for the Alpha.

>>The message structure that we are using needs to be BYTE aligned, but we are
>>unable to accomplish this. In VMS, there is no problem, and when we perform a
>>SIZEOF() the structure, VMS will respond with a size of 10, where the Unix
>>application says 12. We have tried to compile with the -Zp1 option, but this
>>appears to have no effect.

>>Any suggestions?

>Well, the obvious one is don't presume that processor specific alignment
>requirements are portable :-).

>Define an intermediate format, and send that between different
>architectures.

>Joe

    Unix systems run on a large number of processors.  If your "message"
    structure includes such things as floats, doubles, arrays, etc, you
    will find a lot of "interesting" effects, all of which will prevent
    proper transfer of information.

        I'm familiar with sockets, but not with DECMessageQ.  The following
        may or may not apply...

    Floats, doubles, and especially structures are difficult to pass along
    using sockets.  Personally, I usually just write-to-ascii and transfer
    that.  There is *no* problem with passing strings between processes
    on the same machine or between the same type of machine.

    Different machines, however, may represent floating point in different
    ways (sometimes the same machine can have different ways to represent
    floats, for example, an SGI's C/C++ compilers' floats are not the same
    as those that get loaded into its homogeneous transform hardware).

    Many machines these days do use IEEE's standard for floating point, and
    so floats between those machines *may* transfer -- test and see.

    Even integers have problems: big-endian, little endian (ie. which byte
    is high, which low).

    Structures are a worse problem.  Not only may they include multibyte
    integers, floats, and doubles, with all the problems mentioned above,
    different compilers may do different things to satisfy memory
    constraints (such as, floats must begin on addresses evenly divisible
    by 8, etc).  They insert "padding" bytes (unused null bytes) in various
    places, and not even all compilers for the same platform may do this
    the same way.

    Your machine may support "XDR: External Data Representation standard"
    (1014 Sun Microsystems, Inc., 1987 June; 20p Format: TXT=39316 bytes)
    If so, check out "man xdr_simple" for such functions as xdr_bool,
    xdr_double, xdr_float, etc.  You may use these functions to describe
    simple data structures in a machine-independent fashion.

    Good Luck!

--
        Charles E Campbell, Jr, PhD            _   __   __      
        Goddard Space Flight Center           / /_/\_\_/ /      


 
 
 

Byte alignment of structures in Unix "C"

Post by Rich Peterson ZKO2-3/N3 » Tue, 04 Jun 1996 04:00:00



Quote:> The message structure that we are using needs to be BYTE aligned,
> but we are unable to accomplish this. In VMS, there is no problem,
> and when we perform a SIZEOF() the structure, VMS will respond with
> a size of 10, where the Unix application says 12. We have tried to
> compile with the -Zp1 option, but this appears to have no effect.

It would really help to know the exact version of Digital UNIX you
are using, the exact command line, and the exact type whose size is
reported as 12 instead of 10.  In fact a little testcase shouldn't
be hard to construct.  Here's one showing that -Zp1 seems to work
as intended on Digital UNIX V4.0:

% head -1 /etc/motd
Digital UNIX V4.0 (Rev. 386); Mon Jun  3 11:16:33 EDT 1996
% cat zp.c
typedef struct {
    char c;
    long l;

Quote:} st;

main()
{
    printf("sizeof(st) == %d (Zp1 should be 9).\n", sizeof(st));
Quote:}

% cc zp.c
% ./a.out
sizeof(st) == 16 (Zp1 should be 9).
% cc -Zp1 zp.c
% ./a.out
sizeof(st) == 9 (Zp1 should be 9).

A previous response mentioned #pragma nomember_alignment on VMS,
but on UNIX #pragma pack(1) and -Zp1 are accepted.  Note that
the command line qualifier can get you in trouble by changing
the layout of structs declared in system header files compared
to the way they are layed out in the runtime library code.
On VMS, the system header files are generally written to avoid
this problem.  This is not currently the case for UNIX.

The previous response also noted that this kind of packing can
significantly affect performance on Alpha, which is true.  If
you can't change the whole interface to use aligned layouts
(which would work fine on the other machines), then if there
is significant processing of these structures you should consider
writing pack/unpack routines so that the layout is aligned during
the bulk of accesses and only packed within the network buffers.

 -Rich

 
 
 

1. Byte alignment of structures in Unix "C"


        Hi,

        either go for the compiler switch -nomember_alignment
        or the more general #pragma_nomember_alignment

        Remember you may still encounter system structures that need
        to be native ie word aligned so you need to track down
        every structure you are using versus every call using
        structures versus the systems use of those structures.

        When porting between SUN,HP,SGI and Alpha I've found it enough
        using '-std1 -Zp1 -unsigned' for the Alpha
        -std1 will make it ANSI
        -Zp or -Zp1 will pack on byte boundries
        -unsigned might not be interesting for you, its unsigned chars

        also - does your structure contain int and/or long ?
        -if a SUN Sparc uses long the Alpha MUST use int
         ie check use of short,int and long wrt Alpha
         VMS is 32 bit, Alpha is 64 bit
peter
BMC, Uppsala, Sweden

2. Doing Video Editing on Linux

3. ***NYC***UNIX "C" and VAX "C" Programer/Analysts $75K-85K

4. FTP superuser

5. USD $100 guaranteed to anyone who can make jdk1.3 and tomcat work on FreeBSD4.2

6. ***NYC***UNIX "C" and VAX "C" Programer/Analysts

7. pseudo-ttys

8. ***NYC***UNIX "C" and VAX "C" Programer/Analysts $75K-85K

9. Byte Alignment/Packing with structures

10. How to force structure to byte alignment ?

11. GCC byte alignment flag for structures

12. How to convert "A+B+C" into "A B C" in C shell (sh)?