views:

756

answers:

1

Various C / C++ compilers have #pragmas to control optimization.

For example:

CodeWarrior

#pragma optimization_level 0
void func_no_opt()
{
    // Some Work - not optimized
}

#pragma optimization_level 3
void func_full_opt()
{
    // Some Work - optimized
}

MSVC

#pragma optimize("g", off)
void func_no_opt()
{
    // Some Work - not optimized
}

#pragma optimize("g", on)
void func_full_opt()
{
    // Some Work - optimized
}

#pragma optimize("", on)
void func_default_opt()
{
    // Some Work - default optimizations
}

For purely performance reasons, I have a couple of functions that need to be optimized in all builds, including debug builds that are otherwise not optimized.

Is there a way in GCC (specifically 4.1.1) to do something similar to these other compilers? Unfortunately, GCC 4.4 is not supported on my target platform (proprietary hardware) so I can not use the optimize attribute -- i.e. __attribute__((optimize(...))).

Also, on the toolchain for this target platform, there is an auto-bulk-build tool; so it's also not possible to just split the functions off into a separate file and change the optimization parameters, since files within a project can be automatically combined for an increase in compilation and link speeds. I would have to make a separate library of just those functions and have it be linked-in in order to do the split-off method. This might be quite a bit more pain than it would be worth -- especially if there is a simpler method of accomplishing this.

+2  A: 

Looks like the __attribute__((optimize(...))) is the only means to control optimization per-function basis in GCC. Thus with GCC 4.1.1 splitting out a separate library of functions to be optimized is indeed your only option.

Laurynas Biveinis
Ugghh.. I was hoping there'd be an easier way. I guess I'll just have to suffer until they start supporting GCC 4.4 :-(
Adisak