←back to thread

170 points judicious | 1 comments | | HN request time: 0s | source
Show context
vdupras ◴[] No.45407294[source]
In the part about "abs", there's an assembly breakdown:

mov eax, edi

sar eax, 31

mov ecx, eax

add edi, ecx

xor eax, edi

Has this been generated by a C compiler? If yes, it's a bit puzzling, because can't you remove "mov ecx, eax", replace "add edi, ecx" by "add edi, eax" and have the exact same result?

replies(2): >>45407475 #>>45409238 #
1. senfiaj ◴[] No.45409238[source]
This what was generated with clang / gcc x64 with O2/O3 flag on godbolt.org

  abs_branchless(int):
        mov     eax, edi
        neg     eax
        cmovs   eax, edi
        ret

  abs_branch(int):
        mov     eax, edi
        neg     eax
        cmovs   eax, edi
        ret