C Function Alignment in GCC

I am trying to byte-align a function to a 16-byte boundary using the aligned (16) attribute. I did the following: void __attribute__((aligned(16))) function() { }

(Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html )

But when I compile (gcc foo.c; does not use make files or linker scripts), I get the following error:

FOO.c: 99: error: alignment could not be specified for the function

I tried alignment to 4.8.32, etc., but the error remained the same. I need this to align the interrupt service routine for a powerpc based processor. What is the right way to do this?

+6
c gcc alignment memory-alignment
source share
3 answers

Why don't you just pass -falign-functions = 16 to gcc when compiling?

+8
source share

Adapting from my answer to this GCC question , you can try using the #pragma directives , for example:

  #pragma GCC push_options #pragma GCC optimize ("align-functions=16") //add 5 to each element of the int array. void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } } #pragma GCC pop_options 

The macros #pragma push_options and pop_options are used to control the scope of the optimize pragma. More information on these macros can be found in the GCC docs .


Alternatively, if you prefer GCC attribute syntax , you should do something like:

  //add 5 to each element of the int array. __attribute__((optimize("align-functions=16"))) void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } } 
+7
source share

You are probably using an older version of gcc that does not support this attribute. The link to the documentation you provided is for the "current development" of gcc. Looking through various releases, the attribute appears only in the documentation for gcc 4.3 and higher.

+2
source share

All Articles