If I have some mathematical equations which rely on inputs which can be zero or non-zero (template argument, known at compile time), will the optimiser evaluate the equations and optimise out expressions it knows will evaluate to 0 or 1.
For example:
double x = y * Eval<type>::value;
if Eval<type>::value is 0, x will always be 0.
double x = exp(y * Eval<type>::value);
if Eval<type>::value is 0, x will always be 1.
Can the optimiser figure this out and replace x with 0 or 1 elsewhere in the code, or will these calculations be carried out at runtime?
I am using gcc 4.7 with -O3
EDIT: I was wrong, the compiler works as expected when using a floating point number.
Well gcc 4.6.3 in
-O3certainly does seem to do this, as long as the expression is integer related.Example code:
Resulting assembly