for (int i = 0; i < 10; i++) { h[i] = f[i] + g[i]; }
Which is the same as:
int i = 0; while (i < 10) { ... i++; }
Likely faster than moving to intermediate variables that are then added together. Not that such detail optimization matter if the consequence is bad code.
Mapping is in practice also looping.
Then there's the question of whether h is visible outside the function. What's fe?
for this code to make sense, so h has to be visible outside the function otherwise this whole function would be optimized away.
Typedeffing an array is a bit of a perversion already.
The humorous thing is that the simple (and far-more legible, imo) unrolling of
void fe_add( fe h, const fe f, const fe g) { h[0]=f[0]+g[0]; h[1]=f[1]+g[1]; .... } Outperforms, at gcc -O2 (v9.3, 4E9 function calls) the bad-code unrolled loop and a for-loop version. However, at -O3 they all perform similarly. I guess the freedom to unroll loops and perform calculations at -O3 makes the functions similar in the end. Actually, dis-assembelling these at -O3 shows that all versions these functions get inlined and replaced by a handful of SSE instructions.
The funny thing is: if you run this through gcc or clang, they will detect the pattern and unroll it to a few lines of assembly code. And if you have a CPU with a decent vector unit, the result will probably not need a loop.
14
u/trBlueJ Jan 12 '22
I don't know much about optimization but I know there are probably some cases where it is more efficient to unroll loops in such a manner.
7
u/Frajnla Jan 12 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/MightyMeepleMaster Jan 13 '22
The funny thing is: if you run this through gcc or clang, they will detect the pattern and unroll it to a few lines of assembly code. And if you have a CPU with a decent vector unit, the result will probably not need a loop.