by jdsully · 6y ago · 0 comments
1) Try to load things sequentially don’t load from offset 32, then 0, then 16.
2) Don’t use the loop instruction
3) this code:
dec n
cmp n, 0
jne 1b
Can be replaced with: sub n, 1
jnz 1b
Which will actually be executed by the processor as a single instruction (macro-op fusion).4) Interleave your expensive instructions with less expensive ones. Try to interleave multiple dependency chains to let the processor see more of the parallelism.
Your two divisions one after another will be limited by available execution units capable of doing the divide.
5) Lastly align your loop labels to 16-byte offsets. The assembler will do this for you with the ALIGN directive.
I suspect the assembly generated by the compiler is pretty fancy.
Edit: Just briefly looking at the objdump again, gcc is using a lot of scalar variants of instructions, instead of vector. Maybe this is giving the CPU better hints?
Maybe my code would run better on a more modern CPU (while still constraining the original C program to SSE3 only) ?
If you have a lot of practice the compiler is not difficult to beat. But it definitely takes some effort and study of the optimization manuals.
You get named constants and named labels that way, and you can even choose the sane Intel syntax with -masm=intel which you can also do with objdump.
At a first glance, it seems the compiler version is better at hiding the latency of some of the div instructions. It might be hiding memory access latency, too. But that's more involved analysis.
That being said, you're not going to see the really crazy optimizations that compilers can do, because they need a bit of a higher level view of what you're trying to do.
the original from intel, iaca: https://software.intel.com/en-us/articles/intel-architecture...
newer clone, shipped with llvm: https://llvm.org/docs/CommandGuide/llvm-mca.html
And the follow up https://youtu.be/nAbCKa0FzjQ.
But sometimes things that look like dependencies are fake.
XMM0 = XMM1 + XMM2 XMM3 = XMM0 + XMM4 XMM0 = XMM5 + XMM6 XMM7 = XMM0 + XMM8
At first glance it may look like you have to calculate these instructions serially. But, by renaming the last two `XMM0` you eliminate the dependency on the specific register, and can calculate instructions 1 and 3 in parallel, followed by 2 and 4 in parallel.
Regiser renaming is designed assuming the optimizer