Chapter 2 C and C++ Source-Level Optimizations 37
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
2.17 Extracting Common Subexpressions
Optimization
Manually extract common subexpressions where C compilers may be unable to extract them from
floating-point expressions due to the guarantee against reordering of such expressions in the ANSI
standard.
Application
This optimization applies to:
32-bit software
64-bit software
Rationale
Specifically, the compiler cannot rearrange the computation according to algebraic equivalencies
before extracting common subexpressions. Rearranging the expression may give different
computational results due to the lack of associativity of floating-point operations, but the results
usually differ in only the least-significant bits.
Examples
Listing 12. Avoid
double a, b, c, d, e, f;
e = b * c / d;
f = b / d * a;
Listing 13. Preferred
double a, b, c, d, e, f, t;
t = b / d;
e = c * t;
f = a * t;
Listing 14. Avoid
double a, b, c, e, f;
e = a / c;
f = b / c;