Graphics Reference
In-Depth Information
written to through pOut . Overall, const declarations promise almost nothing and do
little to help the compiler optimize code. Instead, their primary use is to help detect
programming errors.
That said, for the code given earlier, one way of solving the problem is to rewrite
the code to read:
void TransformPoint(Point *pOut, Matrix &m, Point &in)
{
float tmpx, tmpy, tmpz;
tmpx = m[0][0] * in.x + m[0][1] * in.y + m[0][2] * in.z;
tmpy = m[1][0] * in.x + m[1][1] * in.y + m[1][2] * in.z;
tmpz = m[2][0] * in.x + m[2][1] * in.y + m[2][2] * in.z;
pOut- > x = tmpx; pOut- > y = tmpy; pOut- > z = tmpz;
}
Inputs are now used before memory is written to, and the same variable can be
given as both input and output arguments without causing aliasing problems. Good
compilers should now be able to optimize the code properly. Some architectures
benefit more from ensuring that inputs are consumed before output is generated. In
particular, RISC-like architectures with enough general-purpose registers to hold all
the inputs usually benefit.
Whenever possible it is good practice to help the compiler in this way (using local
variables to hold global variables, referenced values, and temporary results), thereby
explicitly helping the compiler recognize that no aliasing is present. Unfortunately, it
is not always easy to perform this rewrite manually. For example, with the higher level
of data encapsulation and abstraction provided by the class and template concepts
of C++, code is often broken up into smaller generic subunits, with the intent of
hiding internal structure and operations. Thus, something as simple as making a
local variable copy of a buffer pointer during a data structure traversal, or moving
invariant expressions out of a loop, cannot always be made without exposing the data
structure or by unfolding template instantiations.
The problem of higher levels of abstraction having a negative impact on optimiza-
tion, especially with respect to aliasing, is known as the abstraction penalty problem .
Further information is provided in [Stepanov], [Robison96a], and [Robison96b].
Some of the C++ abstraction penalty performance issues, especially relating to over-
head in vector and matrix expressions, can be successfully addressed by the expression
template technique as pioneered by [Veldhuizen95], albeit at a high development cost.
13.6.1 Type-based Alias Analysis
Some modern C/C++ compilers use type-based alias analysis to help address the prob-
lem of aliasing. The compiler uses the fact that the language standard states that only
 
Search WWH ::




Custom Search