C Function alignment in GCC
I am trying to byte-align a function to 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 ; no makefiles or linker scripts used), I get the following error:
FOO.c:99: error: alignment may not be specified for 'function'
I tried aligning to 4,8,32,开发者_如何转开发 etc as well but the error remains the same. I need this to align an Interrupt Service Routine for a powerpc-based processor. What is the correct way of doing so ?
Why don't you just pass the -falign-functions=16 to gcc when compiling?
Adapting from my answer on this GCC question, you might try using #pragma directives, like so:
#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 #pragma push_options
and pop_options
macros are used to control the scope of the optimize
pragma's effect. More details about these macros can be found in the GCC docs.
Alternately, if you prefer GCC's attribute syntax, you should be able to 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;
}
}
You are probably using an older version of gcc that does not support that attribute. The documentation link you provided is for the "current development" of gcc. Looking through the various releases, the attribute only appears in the documentation for gcc 4.3 and beyond.
精彩评论