Strength reduction attempts to replace slower multiplication operations with faster addition operations. This optimization improves execution speed but increases code size.
Controlling strength reduction explains how to control the optimization for strength reduction.
| Turn control this option from here... | use this setting |
|---|---|
| CodeWarrior IDE | Choose an Optimization Level value (3,4) from the Properties > C/C++ Build > Settings > Tool Settings > PowerPC Compiler > Optimization panel. |
| source code | #pragma opt_strength_reduction on | off | reset |
| command line | -opt [no]strength |
For example, in Before strength reduction , the assignment to elements of the vec array use a multiplication operation that refers to the for loop's counter variable, i .
In After strength reduction , the compiler has replaced the multiplication operation with a hidden variable that is increased by an equivalent addition operation. Processors execute addition operations faster than multiplication operations.
void func_from(int* vec, int max, int fac)
{
int i;
for (i = 0; i < max; ++i)
{
vec[i] = fac * i;
}
}
void func_to(int* vec, int max, int fac)
{
int i;
int hidden_strength_red;
hidden_strength_red = 0;
for (i = 0; i < max; ++i)
{
vec[i] = hidden_strength_red;
hidden_strength_red = hidden_strength_red + fac;
}
}