Search code examples
cassemblyx86multiplicationfma

Multiply-add `a = a*2 + b` instruction on CPU?


The classical Multiply-Accumulate operation is a = a + b*c. But I currently wonder if there exist an instruction that allows to do the following operations on integer in 1 clock cycle: (a and b are unsigned 64-bit integers: unsigned long long int)

a = a*2-1
a = a*2+b

Currently, I use:

a *= 2
--a

for the first one and

a *= 2
a += b

for the second one. And I think that each one is translated to 2 instructions in ASM. But is there a way to use 1 ASM instruction instead (and with which instruction set extension on Intel CPU)?

(I search that because I do this operation billions times)


Solution

    1. For Intel CPU, see the LEA instruction. It can do both of your tasks in one instruction (not sure about cycles though) each. (eg. LEA EAX, [EAX*2+EBX]). Note that this wasn't really meant as a multiply-add, hence its funny name (load effective address).

    2. In C and C++, you shouldn't bother. The compiler will do what it thinks is best and you can probably just hinder its effort. I'd stay with good old a = a*2-1.

    PS: If you think something's translated as two instructions, there is nothing easier than looking in the assembly. Then you would know.