Strength reduction attempts to replace slower multiplication operations with faster addition operations. This optimization improves execution speed but increases code size.
The following table explains how to control the optimization for strength reduction.
| Turn control this option from here... | use this setting |
|---|---|
| CodeWarrior IDE | Choose Level 3 or Level 4 in the Optimization Level settings panel. |
| source code | #pragma opt_strength_reduction on | off | reset |
| command line | -opt [no]strength |
For example, in the following listing, the assignment to elements of the vec array use a multiplication operation that refers to the for loop's counter variable, i.
void func_from(int* vec, int max, int fac) { int i; for (i = 0; i < max; ++i) { vec[i] = fac * i; } }
In the code listed below, 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_to(int* vec, int max, int fac) { int i; int strength_red; hidden_strength_red = 0; for (i = 0; i < max; ++i) { vec[i] = hidden_strength_red; hidden_strength_red = hidden_strength_red + i; } }