# FMA instruction set

The FMA instruction set is an extension to the 128- and 256-bit Streaming SIMD Extensions instructions in the x86 microprocessor instruction set to perform fused multiply–add (FMA) operations. There are two variants:
* FMA4 is supported in AMD processors starting with the Bulldozer architecture. FMA4 was performed in hardware before FMA3 was. Support for FMA4 has been removed since Zen 1.
* FMA3 is supported in AMD processors starting with the Piledriver architecture and Intel starting with Haswell processors and Broadwell processors since 2014.

## Instructions

FMA3 and FMA4 instructions have almost identical functionality, but are not compatible. Both contain fused multiply–add (FMA) instructions for floating-point scalar and SIMD operations, but FMA3 instructions have three operands, while FMA4 ones have four. The FMA operation has the form d = round(a · b + c), where the round function performs a rounding to allow the result to fit within the destination register if there are too many significant bits to fit within the destination.
The four-operand form (FMA4) allows a, b, c and d to be four different registers, while the three-operand form (FMA3) requires that d be the same register as a, b or c. The three-operand form makes the code shorter and the hardware implementation slightly simpler, while the four-operand form provides more programming flexibility.
See XOP instruction set for more discussion of compatibility issues between Intel and AMD.

## FMA3 instruction set
### CPUs with FMA3

* AMD
** Piledriver (2012) and newer microarchitectures
*** 2nd gen APUs, "Trinity" (32nm), May 15, 2012
*** 2nd gen "Bulldozer" (bdver2) with Piledriver cores, October 23, 2012
* Intel
** Haswell (2013) and newer processors, except Pentiums and Celerons

### Excerpt from FMA3

Supported commands include
| Mnemonic | Operation | Mnemonic | Operation |
| VFMADD | result = + a · b + c | VFMADDSUB | result = a · b + c for i = 1, 3, ... result = a · b − c for i = 0, 2, ... |
| VFNMADD | result = − a · b + c | VFMADDSUB | result = a · b + c for i = 1, 3, ... result = a · b − c for i = 0, 2, ... |
| VFMSUB | result = + a · b − c | VFMSUBADD | result = a · b − c for i = 1, 3, ... result = a · b + c for i = 0, 2, ... |
| VFNMSUB | result = − a · b − c | VFMSUBADD | result = a · b − c for i = 1, 3, ... result = a · b + c for i = 0, 2, ... |

#### Note

* VFNMADD is result = − a · b + c, not result = − (a · b + c).
* VFNMSUB generates a −0 for all inputs are zero.
Explicit order of operands is included in the mnemonic using numbers "132", "213", and "231":
| Postfix 1 | Operation | possible memory operand | overwrites |
| 132 | a = a · c + b | c (factor) | a (other factor) |
| 213 | a = b · a + c | c (summand) | a (factor) |
| 231 | a = b · c + a | c (factor) | a (summand) |
as well as operand format (packed or scalar) and size (single or double).
| Postfix 2 | precision | size | Postfix 2 | precision | size |
| SS | Single | 00× 32 bit | SD | Double | 0× 64 bit |
| PSx | Single | 04× 32 bit | PDx | Double | 2× 64 bit |
| PSy | Single | 08× 32 bit | PDy | Double | 4× 64 bit |
| PSz | Single | 16× 32 bit | PDz | Double | 8× 64 bit |
This results in
| Encoding | Mnemonic | Operands | Operation |
| VEX.256.66.0F38.W1 98 /r | VFMADD132PDy | ymm, ymm, ymm/m256 | a = a · c + b |
| VEX.256.66.0F38.W0 98 /r | VFMADD132PSy | ymm, ymm, ymm/m256 | a = a · c + b |
| VEX.128.66.0F38.W1 98 /r | VFMADD132PDx | xmm, xmm, xmm/m128 | a = a · c + b |
| VEX.128.66.0F38.W0 98 /r | VFMADD132PSx | xmm, xmm, xmm/m128 | a = a · c + b |
| VEX.LIG.66.0F38.W1 99 /r | VFMADD132SD | xmm, xmm, xmm/m64 | a = a · c + b |
| VEX.LIG.66.0F38.W0 99 /r | VFMADD132SS | xmm, xmm, xmm/m32 | a = a · c + b |
| VEX.256.66.0F38.W1 A8 /r | VFMADD213PDy | ymm, ymm, ymm/m256 | a = b · a + c |
| VEX.256.66.0F38.W0 A8 /r | VFMADD213PSy | ymm, ymm, ymm/m256 | a = b · a + c |
| VEX.128.66.0F38.W1 A8 /r | VFMADD213PDx | xmm, xmm, xmm/m128 | a = b · a + c |
| VEX.128.66.0F38.W0 A8 /r | VFMADD213PSx | xmm, xmm, xmm/m128 | a = b · a + c |
| VEX.LIG.66.0F38.W1 A9 /r | VFMADD213SD | xmm, xmm, xmm/m64 | a = b · a + c |
| VEX.LIG.66.0F38.W0 A9 /r | VFMADD213SS | xmm, xmm, xmm/m32 | a = b · a + c |
| VEX.256.66.0F38.W1 B8 /r | VFMADD231PDy | ymm, ymm, ymm/m256 | a = b · c + a |
| VEX.256.66.0F38.W0 B8 /r | VFMADD231PSy | ymm, ymm, ymm/m256 | a = b · c + a |
| VEX.128.66.0F38.W1 B8 /r | VFMADD231PDx | xmm, xmm, xmm/m128 | a = b · c + a |
| VEX.128.66.0F38.W0 B8 /r | VFMADD231PSx | xmm, xmm, xmm/m128 | a = b · c + a |
| VEX.LIG.66.0F38.W1 B9 /r | VFMADD231SD | xmm, xmm, xmm/m64 | a = b · c + a |
| VEX.LIG.66.0F38.W0 B9 /r | VFMADD231SS | xmm, xmm, xmm/m32 | a = b · c + a |

## FMA4 instruction set
### CPUs with FMA4

* AMD
** "Heavy Equipment" processors
*** Bulldozer-based processors, October 12, 2011
*** Piledriver-based processors
*** Steamroller-based processors
*** Excavator-based processors (including "v2")
** Zen: WikiChip's testing shows FMA4 still appears to work (under the conditions of the tests) despite not being officially supported and not even reported by CPUID. This has also been confirmed by Agner Fog. But other tests gave wrong results. AMD Official Web Site FMA4 Support Note ZEN CPUs = AMD ThreadRipper 1900x, R7 Pro 1800, 1700, R5 Pro 1600, 1500, R3 Pro 1300, 1200, R3 2200G, R5 2400G.
* Intel
** Intel has not released CPUs with support for FMA4.

### Excerpt from FMA4

| Mnemonic (AT&T) | Operands | Operation |
| VFMADDPDx | xmm, xmm, xmm/m128, xmm/m128 | a = b·c + d |
| VFMADDPDy | ymm, ymm, ymm/m256, ymm/m256 | a = b·c + d |
| VFMADDPSx | xmm, xmm, xmm/m128, xmm/m128 | a = b·c + d |
| VFMADDPSy | ymm, ymm, ymm/m256, ymm/m256 | a = b·c + d |
| VFMADDSD | xmm, xmm, xmm/m64, xmm/m64 | a = b·c + d |
| VFMADDSS | xmm, xmm, xmm/m32, xmm/m32 | a = b·c + d |

## History

The incompatibility between Intel's FMA3 and AMD's FMA4 is due to both companies changing plans without coordinating coding details with each other. AMD changed their plans from FMA3 to FMA4 while Intel changed their plans from FMA4 to FMA3 almost at the same time. The history can be summarized as follows:
* August 2007: AMD announces the SSE5 instruction set, which includes 3-operand FMA instructions. A new coding scheme (DREX) is introduced for allowing instructions to have three operands.
* April 2008: Intel announces their AVX and FMA instruction sets, including 4-operand FMA instructions. The coding of these instructions uses the new VEX coding scheme, which is more flexible than AMD's DREX scheme.
* December 2008: Intel changes the specification for their FMA instructions from 4-operand to 3-operand instructions. The VEX coding scheme is still used.
* May 2009: AMD changes the specification of their FMA instructions from the 3-operand DREX form to the 4-operand VEX form, compatible with the April 2008 Intel specification rather than the December 2008 Intel specification.
* October 2011: AMD Bulldozer processor supports FMA4.
* January 2012: AMD announces FMA3 support in future processors codenamed Trinity and Vishera; they are based on the Piledriver architecture.
* May 2012: AMD Piledriver processor supports both FMA3 and FMA4.
* June 2013: Intel Haswell processor supports FMA3.
* February 2017: The first generation of AMD Ryzen processors officially supports FMA3, but not FMA4 according to the CPUID instruction. There has been confusion regarding whether FMA4 was implemented or not on this processor due to errata in the initial patch to the GNU Binutils package that has since been rectified. One unconfirmed report of wrong results led to some doubt, but Mysticial (Alexander Yee, developer of y-cruncher) debunked it: FMA4 worked for bit-exact bignum calculations on his Zen 1 system for years, and the one report on Reddit never had any followup investigation to rule out mistakes in the testing software before being widely repeated. The initial Ryzen CPUs could be crashed by a particular sequence of FMA3 instructions, but updated CPU microcode fixes the problem.
* July 2019: AMD Zen 2 and later Ryzen processors don't support FMA4 at all. They continue to support FMA3. Only Zen 1 and Zen+ have unofficial FMA4 support.

## Compiler and assembler support

Different compilers provide different levels of support for FMA:
* GCC supports FMA4 with -mfma4 since version 4.5.0 and FMA3 with -mfma since version 4.7.0.
* Microsoft Visual C++ 2010 SP1 supports FMA4 instructions.
* Microsoft Visual C++ 2012 supports FMA3 instructions (if the processor also supports AVX2 instruction set extension).
* Microsoft Visual C++ since VC 2013
* PathScale supports FMA4 with -mfma.
* LLVM 3.1 adds FMA4 support, along with preliminary FMA3 support.
* Open64 5.0 adds "limited support".
* Intel compilers support only FMA3 instructions.
* NASM supports FMA3 instructions since version 2.03 and FMA4 instructions since 2.06.
* FASM supports both FMA3 and FMA4 instructions.