Multiply-add is a good choice because it corresponds to the relatively common operation of computing the address of a field of a struct in an array so you can operate on that field.
(e.g. &(points[5].x) is &points + (5 * sizeof(point)) + offsetof(point, x)).
There are a fair number of insns in the A64 instruction set that make use of this trick to provide one flexible instruction that as a special case provides useful simpler functionality under an alias. (Register-to-register 'mov' being an alias of 'orr' is another.)
This is only relatively common inside loops. Inside loops you will usually index with the loop counter or some other value that is derived from it linearly. Compilers will typically use induction variable arithmetic that doesn't involve multiplication.
In general, outwith ALUs as well as in, it is very cheap to fold any (reasonable) number of additions and subtractions, even ones with constant left/right shifts/rotates to the addends and subtrahends, into multipliers.
> Scottish Twitter users 'shocked' after discovering the word 'outwith' is only used in Scotland [0]
[0] https://www.dailyrecord.co.uk/scotland-now/scottish-twitter-...
Having looked in the ARM reference manual, the "MUL" instruction is just an alias for MADD with an addition of zero!
I can't find timings for this instruction with 30 seconds of googling, has anyone got a spec with instruction timings?
Source: https://dougallj.github.io/applecpu/firestorm-simd.html
It’s an ALU, way more complex than a logic gate (of which it’s composed), but yes fused multiply-add units are standard on every modern CPU. In fact if your processor is recent (more so than Haswell) odds are good it only has FMA FP ALU, no pure adder or multiplier.
struct X
{
float a;
int b[10];
};
X x;
If you want to access x.b[3], then you have to add sizeof float to the address of x, and then add sizeof int times 3.It's still commonly said that RISC processors are faster than CISC because they are "reduced", as in they have fewer instructions. But really it's very beneficial to add instructions that do a lot, if it's something that can easily be done in hardware and replaces several simpler ones.
Multiply-add is an example of one; others are bitfield extraction and rotation, SIMD shuffle, AES encryption, and some of the complex memory operands x86 and ARM have. I even still think x86's memcpy instruction is a good idea.
I'm curious (I know very little about assembly or what's on the CPU so pardon me if what I'm asking makes no sense), what's the benefit of having a whole instruction that both multiplies and adds? Is there a logical gate on the processor that does this? Or is this just going through a binary multiplier before going through an adder? Does what I'm asking even make any sense?