4 ms·
I'm surprised they didn't try something less clever to optimize their code first. The assembly code could be rewritten like this, so the test requires one branc
by trealira 2y ago
I'm surprised they didn't try something less clever to optimize their code first. The assembly code could be rewritten like this, so the test requires one branch at the bottom of the loop instead of two (one to jump to the top and one to branch conditionally), as well as one instead of two ALU operations per loop for X1 (one subtraction to compare X1 to zero, and one to decrement X1, as opposed to using the result of the latter).
stp x29, x30, [sp, #-16]!
fmov s0, #0.0
cbnz x1, exit // Skip loop if x1 == 0.
loop:
ldr s1, [x0], #4
bl foo
// Decrement x1. If x1 != 0, repeat the loop.
subs x1, x1, #1
b.ne loop
// Fall through to exit after the loop finishes.
exit:
ldp x29, x30, [sp], #16
ret
foo:
// ...
ret
Going further, you could just inline foo and omit the RET instruction without doing any trickery with mismatched BL and RET instructions.
I haven't benchmarked this, though, so I don't actually know how much this speeds the code up.
- trealira 2y agoTypo: that line with "cbnz" should be "cbz". The CBZ instruction branches to a label if a register is zero, and CBNZ branches if the register isn't zero.