Last active
January 1, 2016 01:49
-
-
Save lydonchandra/8074900 to your computer and use it in GitHub Desktop.
Use LEA as a faster way to do multiplication
This file contains hidden or 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
//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