Graphics Reference
In-Depth Information
two objects of the same compatible type may alias each other and that each area of
memory can only be associated with one type during its lifetime.
For example, under the standard rules floats and integers are not compatible types.
To allow block moves and similar operations to be performed, signed and unsigned
chars are compatible with all types. Thanks to these compatibility rules, more sophis-
ticated compilers enhanced with type-based alias analysis can deduce that there is
no aliasing between v[i] and *n in the code
void Foo(float *v, int *n)
{
for(inti=0;i<*n;i++)
v[i] += 1.0f;
}
and optimize it as if it was written as:
void Foo(float *v, int *n)
{
int t = *n;
for(inti=0;i<t;i++)
v[i] += 1.0f;
}
The difference is small, but important. In the former code, without type-based
alias analysis the value of *n would have to be fetched from memory as many times
as the loop iterates. In the second version, the value of *n has been moved out of
the loop and is therefore fetched only once. It is important to note that according
to the C/C++ language standards it is illegal to write code similar to the following
type-punning operation.
uint32 i;
float f;
i = *((int32 *)&f);
// Illegal way of getting the integer representation of the float f
This operation is treating the memory holding f as having two different types.
A compiler that applies type-based alias analysis on this code is therefore free to
generate code that does not behave as intended. Because compilers have built-in
knowledge about the deliberate aliasing of unions, many compilers allow access
through a union to be a legal way to perform the following operation:
union {
uint32 i;
 
Search WWH ::




Custom Search