Graphics Reference
In-Depth Information
float f;
}u;
u.f=f;
uint32 i = u.i;
However, to guarantee compliance with the language standard and thus gener-
ation of correct code, the float would have to be accessed by a char array, with the
uint32 assembled from its elements.
13.6.2 Restricted Pointers
The 1999 ANSI/ISO C standard defines a new qualifier keyword, restrict , which
allows programmers to at least partly help compilers resolve some of the aliasing
issues in a program. As a result, the compilers are able to more aggressively optimize
the code. Some C++ compilers also provide the restrict keyword as a language
extension. Declaring a pointer (or reference) with the restrict qualifier is basically a
promise to the compiler that for the scope of the pointer the target of the pointer will
only be accessed through that pointer (and pointers copied from it). Using restricted
pointers and references, the earlier transformation code can be written to read:
void TransformPoint(Point * restrict pOut, Matrix & restrict m, Point & restrict in)
{
pOut- > x = m[0][0] * in.x + m[0][1] * in.y + m[0][2] * in.z;
pOut- > y = m[1][0] * in.x + m[1][1] * in.y + m[1][2] * in.z;
pOut- > z = m[2][0] * in.x + m[2][1] * in.y + m[2][2] * in.z;
}
A good compiler should now be able to produce optimal code for the function as
it now knows that writing to pOut cannot possibly affect any of the inputs. Before
relying on the use of restricted pointers, it is a good idea to verify that the target
compiler is in fact using it as a hint to improve optimization, as even ignoring the
keyword conforms to the language standard. As another example of the benefits of
using restricted pointers, consider computing the product of two complex numbers,
( a
+
bi )( c
+
di )
=
( ac
bd )
+
( ad
+
bc ) i , as naively implemented by the following code.
void ComplexMult(float *a, float *b, float *c)
{
a[0] = b[0]*c[0] - b[1]*c[1];
// real part in a[0] (b[0] and c[0])
a[1] = b[0]*c[1] + b[1]*c[0];
// imaginary part in a[1] (b[1] and c[1])
}
 
Search WWH ::




Custom Search