GCC gives you a few different ways of handling this:
-
Use #pragma directives, like
#pragma GCC optimize ("string"...)
, as seen in the GCC docs. Note that the pragma makes the optimizations global for the remaining functions. If you used#pragma push_options
andpop_options
macros cleverly, you could probably define this around just one function like so:#pragma GCC push_options #pragma GCC optimize ("unroll-loops") //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
-
Annotate individual functions with GCC’s attribute syntax: check the GCC function attribute docs for a more detailed dissertation on the subject. An example:
//add 5 to each element of the int array. __attribute__((optimize("unroll-loops"))) void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } }
Note: I’m not sure how good GCC is at unrolling reverse-iterated loops (I did it to get Markdown to play nice with my code). The examples should compile fine, though.