strength-reduction

How to multiply a register by 37 using only 2 consecutive leal instructions in x86?

不打扰是莪最后的温柔 提交于 2019-11-27 09:33:21
Say %edi contains x and I want to end up with 37*x using only 2 consecutive leal instructions, how would I go about this? For example to get 45x you would do leal (%edi, %edi, 8), %edi leal (%edi, %edi, 4), %eax (to be returned) I cannot for the life of me figure out what numbers to put in place of the 8 and 4 so that the result (%eax) will be 37x At -O3 , gcc will emit (Godbolt compiler explorer) : int mul37(int a) { return a*37; } leal (%rdi,%rdi,8), %eax # eax = a * 9 leal (%rdi,%rax,4), %eax # eax = a + 4*(a*9) ret That's using 37 = 9*4 + 1 , not destroying the original a value with the

How to multiply a register by 37 using only 2 consecutive leal instructions in x86?

你。 提交于 2019-11-26 14:41:30
问题 Say %edi contains x and I want to end up with 37*x using only 2 consecutive leal instructions, how would I go about this? For example to get 45x you would do leal (%edi, %edi, 8), %edi leal (%edi, %edi, 4), %eax (to be returned) I cannot for the life of me figure out what numbers to put in place of the 8 and 4 so that the result (%eax) will be 37x 回答1: At -O3, gcc will emit (Godbolt compiler explorer): int mul37(int a) { return a*37; } leal (%rdi,%rdi,8), %eax # eax = a * 9 leal (%rdi,%rax,4)