I'm wondering whether compilers (gcc with -O3 more specifically) can/will optimize out nested struct element dereferences (or not nested even).
For example, is there any point in doing the following code
register int i = 0;
register double multiple = struct1->struct2->element1;
for (i = 0; i < 10000; i++)
result[i] = multiple * -struct1->struct3->element3[i];
instead of
register int i = 0;
for (i = 0; i < 10000; i++)
result[i] = struct1->struct2->element1 * -struct1->struct3->element3[i];
I'm looking for the most optimized, but am not going to go through and bring outside of the loop struct dereferences if a compiler will optimize this out. If it does I think my best option is the following
register int i = 0;
register double* R = &result[0];
register double* amount = &struct1->struct3->element[0];
for (i = 0; i < 10000; i++, R++, amount++)
*R = struct1->struct2->element1 * -*amount;
which eliminates all unnecessary dereferences etc. (I think). Would the 2 deferences to get to element3 be optimized?
Any thoughts? Thanks