CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES
int64_t f2(int64_t a)
{
return a*28;
};
int64_t f3(int64_t a)
{
return a*17;
};
x64
Listing 16.9: Optimizing MSVC 2012
; a7
f1:
lea rax, [0+rdi8]
; RAX=RDI8=a8
sub rax, rdi
; RAX=RAX-RDI=a8-a=a7
ret
; a28
f2:
lea rax, [0+rdi4]
; RAX=RDI4=a4
sal rdi, 5
; RDI=RDI<<5=RDI32=a32
sub rdi, rax
; RDI=RDI-RAX=a32-a4=a*28
mov rax, rdi
ret
; a17
f3:
mov rax, rdi
sal rax, 4
; RAX=RAX<<4=a16
add rax, rdi
; RAX=a16+a=a17
ret
ARM64
GCC 4.9 for ARM64 is also terse, thanks to the shift modifiers:
Listing 16.10: Optimizing GCC (Linaro) 4.9 ARM64
; a7
f1:
lsl x1, x0, 3
; X1=X0<<3=X08=a8
sub x0, x1, x0
; X0=X1-X0=a8-a=a*7
ret
; a28
f2:
lsl x1, x0, 5
; X1=X0<<5=a32
sub x0, x1, x0, lsl 2
; X0=X1-X0<<2=a32-a<<2=a32-a4=a28
ret
; a*17