Skip to content

Instantly share code, notes, and snippets.

@lydonchandra
Last active January 1, 2016 01:49
Show Gist options
  • Save lydonchandra/8074900 to your computer and use it in GitHub Desktop.
Save lydonchandra/8074900 to your computer and use it in GitHub Desktop.
Use LEA as a faster way to do multiplication
//rax = 450 * rbx
imulq $450, %rbx, %rax ## imm = 0x1C2
//another way to do rax = 450 * rbx
//cycle wise, this is on par with imul instruction above
leaq (%rbx,%rbx,8), %rax //rax=9*rbx
leaq (%rax,%rax,4), %rax //rax=rax*5 == 45*rbx
leaq (%rax,%rax,4), %rax //rax=rax*5 == 225*rbx
shlq $1,%rax //rax=rax*2 == 450*rbx
//Result:
//based on micro-benchmark, imulq-$450 ties with leaq-leaq-leaq-shlq instruction series,
//imulq-$225 however, is 2 clock cycle slower than leaq-leaq-leaq instruction series
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment