HELP: Alignment suppression in C/UNIX 
Author Message
 HELP: Alignment suppression in C/UNIX

   Hello netters !

   We, at BGU computer center, are interested in performing compilation
   in C, so that alignment will not be forced on the variables. For
   example:

      struct
      {
         char b;
         int  i;
      } sname;

   In the default case, 'b' is allocated one byte, then 3 bytes are
   skipped, and 'i' is allocated 4 bytes starting at the fifth byte.

   We are interested in a solution in which the variables will be
   allocated consequtive bytes, without alignment (in the example above,
   the structure will occupy 5 bytes).

   We'll welcome a solution at any of the following UNIX workstations
   type we have:

   - Sun Spark-2 with SunOS 4.1;
   - DECStation 5000/240 with Ultrix 4.2;
   - IBM RS-6000 970 with AIX 3;
   - SGI Crimson with IRIX  3.


   Thanks in advance.

Marina Markus
Ben-Gurion University Computer Center
Beer-Sheva, Israel
=====================================



Sun, 19 Nov 1995 16:34:26 GMT  
 HELP: Alignment suppression in C/UNIX

Quote:

>      Hello netters !

>      We, at BGU computer center, are interested in performing compilation
>      in C, so that alignment will not be forced on the variables. For
>      example:

>         struct
>         {
>            char b;
>            int  i;
>         } sname;

>      In the default case, 'b' is allocated one byte, then 3 bytes are
>      skipped, and 'i' is allocated 4 bytes starting at the fifth byte.

>      We are interested in a solution in which the variables will be
>      allocated consequtive bytes, without alignment (in the example above,
>      the structure will occupy 5 bytes).

>      We'll welcome a solution at any of the following UNIX workstations
>      type we have:

>      - Sun Spark-2 with SunOS 4.1;
>      - DECStation 5000/240 with Ultrix 4.2;
>      - IBM RS-6000 970 with AIX 3;
>      - SGI Crimson with IRIX  3.

All those machines are RISC-based and therefore require long fields to
be aligned, else the program traps.

(I hear that in Windows NT for MIPS RISC, however, there is support
for unaligned data using R4000-specific instructions.)

Here's a suggestion:

struct packed
{
    char b;
    char i[sizeof(int)];

Quote:
};

This will occupy 5 bytes. (Standards purists, I'm not listening.)
Then to access the i field:

    int i1;
    struct packed *p;
    memcpy(&i1, p->i, sizeof(i1));      /* fetch the unaligned field */
                                        /* diddle with i1 */
    memcpy(p->i, &i1, sizeof(i1));      /* Put it back */
--

OS/2 2.0 will do for me until AmigaDOS for the 386 comes along...



Mon, 20 Nov 1995 18:16:51 GMT  
 HELP: Alignment suppression in C/UNIX

Quote:
(Richard L. Krehbiel) writes:


> >      Hello netters !

> >      We, at BGU computer center, are interested in performing
> compilation
> >      in C, so that alignment will not be forced on the variables.
> For
> >      example:

> >         struct
> >         {
> >            char b;
> >            int  i;
> >         } sname;

> >      We are interested in a solution in which the variables will be
> >      allocated consequtive bytes, without alignment (in the example
> above,
> >      the structure will occupy 5 bytes).

> >      We'll welcome a solution at any of the following UNIX
> workstations
> >      type we have:

> >      - Sun Spark-2 with SunOS 4.1;
> >      - DECStation 5000/240 with Ultrix 4.2;
> >      - IBM RS-6000 970 with AIX 3;
> >      - SGI Crimson with IRIX  3.

> All those machines are RISC-based and therefore require long fields
> to
> be aligned, else the program traps.

> --

> OS/2 2.0 will do for me until AmigaDOS for the 386 comes along...

The RS/6000 can handle unaligned memory accesses without trapping (although
performance is still degraded, of course).  The xlc compiler (version 1.2
or greater) supports

        #pragma options align=packed
        ... struct definitions, which are mapped without any padding ...
        #pragma options align=reset
        ... back to whatever alignment was active before ...

--
   __
   \  \     _ _                ,---------------------------,
    \**\ ___\/ \...............|          ++don;           |
  X*#####*+~~\_\               `---------------------------'
    o/\  \
       \__\



Tue, 21 Nov 1995 02:34:25 GMT  
 HELP: Alignment suppression in C/UNIX

Quote:
>All those machines are RISC-based and therefore require long fields to
>be aligned, else the program traps.

You're assuming that unaligned members would be accessed using the same
instructions as aligned members.  But if the processor traps when trying to
use word instructions on unaligned data, the compiler should generate code
that uses byte instructions and shifts to load an unaligned word into the
register.  No one should expect access to unaligned members to be as fast
as aligned member access.
--
Barry Margolin
System Manager, Thinking Machines Corp.




Wed, 22 Nov 1995 04:21:47 GMT  
 HELP: Alignment suppression in C/UNIX

Quote:


>>All those machines are RISC-based and therefore require long fields to
>>be aligned, else the program traps.
>You're assuming that unaligned members would be accessed using the same
>instructions as aligned members.  But if the processor traps when trying to
>use word instructions on unaligned data, the compiler should generate code
>that uses byte instructions and shifts to load an unaligned word into the
>register.  No one should expect access to unaligned members to be as fast
>as aligned member access.

This is OK as far as it goes, but what about taking the address of a member?

        extern int foo(int&);
        struct X {
            char c;
            int i;
        };
        extern struct X x;
        main() {
            return foo(&x.i);
        }

This code is completely legal, yet can't be reasonably implemented with
a packed structure on a machine with typical alignment requirements.  By
"reasonably" I mean without also having inefficient code for the normal
aligned cases.

(How does 'foo' know whether it is passed a pointer to an aligned or
unaligned object?  We can solve this problem in various ways --
"fat" pointers, special runtime traps with retry for unaligned access,
and so on.  These solutions might be appropriate for some languages, but
aren't really in the spirit of C.)

A compiler might implement packed structures as an extension, signaled by
a new keyword or pragma.  It might then want to prohibit at compile time
taking the address of an unaligned member.

Pascal, for example, has a "packed" keyword.  You can't take arbitrary
addresses at all (in Standard Pascal).  You can't pass any member of a
packed structure to a function by reference, but you can pass a member
of a non-packed structure by reference.
--




Thu, 23 Nov 1995 23:02:54 GMT  
 HELP: Alignment suppression in C/UNIX
|This is OK as far as it goes, but what about taking the address of a member?
|
|          extern int foo(int&);
|          struct X {
|              char c;
|              int i;
|          };
|          extern struct X x;
|          main() {
|              return foo(&x.i);
|          }
|
|This code is completely legal, yet can't be reasonably implemented with
|a packed structure on a machine with typical alignment requirements.  By
|"reasonably" I mean without also having inefficient code for the normal
|aligned cases.
|
|(How does 'foo' know whether it is passed a pointer to an aligned or
|unaligned object?  We can solve this problem in various ways --
|"fat" pointers, special runtime traps with retry for unaligned access,
|and so on.  These solutions might be appropriate for some languages, but
|aren't really in the spirit of C.)

Sure it can be compiled. It requires *two* copies of the implementation
of foo(). Generate them at runtime if you need to....

I know, *intelligence* violates the spirit of C ;-).
----------------------------------------------------------------------

      dfki / stuhlsatzenhausweg 3 / 66123 saarbruecken / germany
----------------------------------------------------------------------



Mon, 27 Nov 1995 01:42:41 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. data alignment (was Re: RISC data alignment, in comp.arch)

3. newbe/cs student, need help w/ code

4. "scanf" function with assignment-suppression

5. file i/o -- assignment suppression

6. Compiler error CS 1595 - Help

7. sscanf assignment suppression character

8. Help!!! Novice CS Advice

9. Windows Application Error Suppression?

10. DocTemplate suppression

11. Suppression d'un DocTemplate

12. Help word Alignment in GNU C

 

 
Powered by phpBB® Forum Software