Last active
August 29, 2015 14:07
-
-
Save nkurz/1ecb46486746c4e078b3 to your computer and use it in GitHub Desktop.
Switching from SUB to SBB changes runtime by 15%. Can you explain why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Minimal example, see also http://stackoverflow.com/q/26266953/3766665 | |
; To build (Linux): | |
; nasm -felf64 func.asm | |
; ld func.o | |
; Then run: | |
; perf stat -r10 ./a.out | |
; On Haswell and Sandy Bridge, observed runtime varies | |
; ~15% depending on whether sub or sbb is used in the loop | |
section .text | |
global _start | |
_start: | |
push qword 0h ; put counter variable on stack | |
jmp loop ; jump to function | |
align 64 ; function alignment. | |
loop: | |
mov rcx, 1000000000 | |
align 64 ; loop alignment. | |
l: | |
mov rax, [rsp] | |
add rax, 1h | |
mov [rsp], rax | |
; sbb rcx, 1h ; which is faster: sbb or sub? | |
sub rcx, 1h ; switch, time it, and find out | |
jne l ; (rot13 spoiler: foo vf snfgre ol 15%) | |
fin: ; If that was too easy, explain why. | |
mov eax, 60 | |
xor edi, edi ; End of program. Exit with code 0 | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment