12

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 ?

3
  • 12
    The OP has explained exactly why they need to do this, in the second-last sentence. Commented Dec 16, 2009 at 7:32
  • 3
    Another delightfully evil use for aligning functions is storing additional data in the low bits of function-pointer variables... Commented Dec 20, 2010 at 4:25
  • @R.., evil. Just as storing in the high byte of 32 bit addresses on the 680EC30 or 68000 processors. Very evil indeed. Commented Dec 4, 2011 at 23:16

3 Answers 3

12

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

11

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

3 Comments

Yes I can do that, but that would align all functions to 16byte, whereas I need only the ISR to get 16 byte aligned. But, if there is no other option I will have to do the same.
If you're not using GCC >=4.3, this is the way to go.
@Sukanto, do it and then place the function in a separate C file.
2

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.

1 Comment

I guess that is possible. My compiler is gcc 4.0.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.