This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Variable/structure alignment



Does the __attribute__ ((aligned())) modifier only work with global
variables, or should it work with function-local (stack) variables as
well?

Here is a code snippit:
---------

typedef struct {
  float r[4];
} __m128 __attribute__ ((aligned(16)));

void
chkalign( char * name, void * ptr )
{
  printf("%s:\t", name );
  if( (((unsigned long)ptr) & 15) == 0 )
    printf("16+ byte aligned.\n");
  else if( (((unsigned long)ptr) & 7) == 0 )
    printf("8 byte aligned.\n");
  else if( (((unsigned long)ptr) & 3) == 0 )
    printf("4 byte aligned.\n");
  else if( (((unsigned long)ptr) & 1) == 0 )
    printf("2 byte aligned.\n");
  else
    printf("unaligned.\n");
}

int y __attribute__ ((aligned(8)));
int x __attribute__ ((aligned(16)));

main()
{
  __m128 a, b;
int z __attribute__ ((aligned(16)));

  chkalign( "a", &a );
  chkalign( "b", &b );
  chkalign( "x", &x );
  chkalign( "y", &y );
  chkalign( "z", &z );
}
----
The output from the above is:

a:      8 byte aligned.
b:      8 byte aligned.
x:      16+ byte aligned.
y:      8 byte aligned.
z:      4 byte aligned.
-----

There is a note in the gcc.info file about using aligned() that talks
about the linker potentially not being able to obey certain alignment
constraints.  I can find no documentation on GNU LD that tells me what
the "maximum" alignment is, or how to specify alignments.

I have tried the following as well:

typedef struct {
  float r[4] __attribute__ ((aligned(16)));
} __m128;

It produced the same results as above.

Is this a bug, or how can I specify, with any certainty, what the
variable alignment will be?  I need the alignment of these variables to
always be on 16 byte boundaries.

I am using the egcs release that is distributed with RedHat 6.1, here is
the version information:

gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
ld -v
GNU ld version 2.9.1 (with BFD 2.9.1.0.24)

Thanks.
 
---
Michael M. Morrison
VP/Chief Technical Officer
Hyperion Technologies, Inc.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]