The FMA instruction set is an extension to the x86 microprocessor instruction set to perform fused multiply–add (FMA) operations on 128-bit and 256-bit operands. These operations were extended to 512-bit operands by the AVX-512 extension introduced by Intel and later supported by AMD. There are two incompatible variants of the FMA instruction set. They have the same operations but differ in the number of operands:
FMA4 is the four-operand variant (three source operands and a destination register). AMD added FMA4 with the Bulldozer (2011) architecture. Zen 1 (2017) processors still execute FMA4 instructions but do not report them as supported through CPUID, and Zen 2 (2019) removed them. Intel originally specified FMA4 in 2008, but changed its design to three operands later that year. FMA3 is the three-operand variant (three source operands, one of which is also the destination). AMD added FMA3 with the Piledriver (2012) architecture, for compatibility with Intel's planned Haswell processors. Intel added FMA3, alongside AVX2, with Haswell (2013) processors. Earlier processors, including AMD's Bulldozer and Intel's Sandy Bridge and Ivy Bridge, do not support it. AVX-512 adds 512-bit forms of the FMA3 instructions.
Operations An FMA operation has the form d=round(±a⋅b±c), where the operands are floating-point scalars or SIMD vectors. The exact value of ±a⋅b±c is rounded only once to the precision of the destination format (i.e. single or double precision). In the IEEE 754-2008 standard for floating-point arithmetic, the operation is called fusedMultiplyAdd and conforming implementations must provide it. The result is usually more accurate than that of separate multiply and add instructions. FMA3 and FMA4 have the same six operations. The operations differ only in the signs of the product and of c.
The two alternating operations exist only for vectors. Vector elements are numbered from 0.
Compatibility Although FMA3 and FMA4 have the same operations, their instructions differ in operands and encoding. Their instructions have different opcodes in different opcode maps. An instruction of one variant is an invalid opcode on a processor that supports only the other. Each variant has its own CPUID feature flag: FMA for FMA3 and FMA4 for FMA4. Their operands differ as follows:
FMA4 instructions have a destination and three sources. Their operands a, b, c, and d can be four different registers. Either of the last two sources can be in memory. The fourth register is encoded in an extra byte. Each FMA4 instruction is one byte longer than the matching FMA3 instruction. FMA3 instructions have three operands. The result is written over the first operand. The destination d is therefore the same register as a, b, or c. Only the third operand can be in memory. Each FMA3 operation has three operand orders numbered 132, 213, and 231. The orders differ in whether the overwritten register and the memory operand are factors or the summand. The input to be overwritten must first be copied to another register if all three inputs are still needed afterwards. The examples below contain such copies. The split between FMA3 and FMA4 followed changes of plan by Intel and AMD. AMD Fellow Dave Christie wrote in May 2009 that AMD could not adopt Intel's December 2008 change of FMA from four to three operands without unacceptable risk to its product schedules. AMD therefore kept the four-operand form from Intel's earlier specification. Josh Walrath of PC Perspective wrote in 2009 that it was "unfortunate that AMD and Intel cannot work together a bit better on these things". Agner Fog wrote in 2009 that Intel, AMD, and VIA "are assigning different codes to identical or equivalent instructions because each keep their innovations secret for as long as possible". In 2011, he listed FMA3 and FMA4 among several pairs of AMD and Intel instructions with incompatible encodings. He wrote that Intel had given its own VEX encodings to instructions that AMD had first defined in the XOP encoding.
Naming Intel and AMD define the same FMA3 mnemonics in their instruction-set manuals. The FMA4 mnemonics are defined only in AMD's manual. Each FMA mnemonic consists of a stem from the table above and a postfix for the data format:
PS: packed single precision PD: packed double precision SS: scalar single precision SD: scalar double precision In a packed instruction, the operation is applied to every element of a vector. In a scalar instruction, it is applied only to the lowest element. An FMA4 mnemonic is the stem followed directly by the postfix, as in VFMADDPS. In an FMA3 mnemonic, the operand order 132, 213, or 231 comes between the stem and the postfix, as in VFMADD213PS.
Rounding
The exact intermediate value of an FMA instruction exists only inside the processor and cannot be examined by software. FMA results are rounded in the rounding mode set in the processor's MXCSR control and status register. For AVX-512 instructions, a rounding mode can instead be encoded in the 512-bit and scalar forms whose operands are all registers. Under IEEE 754-2008, overflow, underflow, and inexact exceptions in a fused multiply–add can arise only from the addition and not from the multiplication. With separate multiply and add instructions, the product is rounded before the addition. The result is then rounded twice: round(round(a⋅b)+c). On x86 processors without FMA3 or FMA4, a result rounded only once is still available from the C fma function. Intel describes its own implementation as having some cost in speed.
Contraction by C compilers In the C programming language, since the 1999 revision, an expression such as a * b + c may be contracted into a single fused operation. Whether contraction is allowed is controlled by the STDC FP_CONTRACT pragma, and its default status is implementation-defined. When compiling for a processor with FMA instructions:
… excerpt ends here. Continue reading the full article.