Software Optimization Guide for AMD64 Processors | 25112 Rev. 3.06 September 2005 |
Instead, use the equivalent array notation:
typedef struct { float x, y, z, w;
} VERTEX;
typedef struct { float m[4][4];
} MATRIX;
void XForm(float *res, const float *v, const float *m, int numverts) {
int i;
const VERTEX* vv = (VERTEX *)v; const MATRIX* mm = (MATRIX *)m; VERTEX* rr = (VERTEX *)res;
for (i = | 0; i < numverts; i++) { |
rr->x | = vv->x * mm->m[0][0] + vv->y * mm->m[0][1] + |
| vv->z * mm->m[0][2] + vv->w * mm->m[0][3]; |
rr->y | = vv->x * mm->m[1][0] + vv->y * mm->m[1][1] + |
| vv->z * mm->m[1][2] + vv->w * mm->m[1][3]; |
rr->z | = vv->x * mm->m[2][0] + vv->y * mm->m[2][1] + |
| vv->z * mm->m[2][2] + vv->w * mm->m[2][3]; |
rr->w | = vv->x * mm->m[3][0] + vv->y * mm->m[3][1] + |
| vv->z * mm->m[3][2] + vv->w * mm->m[3][3]; |
++rr; | // Increment the results pointer. |
++vv; | // Increment the input vertex pointer. |
} | |
}
Additional Considerations
Source-code transformations interact with a compiler’s code generator, making it difficult to control the generated machine code from the source level. It is even possible that source-code transformations aimed at improving performance may conflict with compiler optimizations. Depending on the compiler and the specific source code, it is possible for pointer-style code to compile into machine code that is faster than that generated from equivalent array-style code. Compare the performance of your code after implementing a source-code transformation with the performance of the original code to be sure that there is an improvement.
12 | C and C++ Source-Level Optimizations | Chapter 2 |