F instructions
Single-precision floating-point
This chapter describes the standard instruction-set extension for single-precision floating-point, which is named "F" and adds single-precision floating-point computational instructions compliant with the IEEE 754-2008 arithmetic standard cite:[ieee754-2008]. The F extension depends on the "Zicsr" extension for control and status register access.
F Register State
The F extension adds 32 floating-point registers, f0-f31, each 32
bits wide, and a floating-point control and status register fcsr,
which contains the operating mode and exception status of the
floating-point unit. This additional state is shown in
fprs. We use the term FLEN to describe the width of
the floating-point registers in the RISC-V ISA, and FLEN=32 for the F
single-precision floating-point extension. Most floating-point
instructions operate on values in the floating-point register file.
Floating-point load and store instructions transfer floating-point
values between registers and memory. Instructions to transfer values to and from the integer register file are also provided.
Tip
We considered a unified register file for both integer and floating-point values as this simplifies software register allocation and calling conventions, and reduces total user state. However, a split organization increases the total number of registers accessible with a given instruction width, simplifies provision of enough regfile ports for wide superscalar issue, supports decoupled floating-point-unit architectures, and simplifies use of internal floating-point encoding techniques. Compiler support and calling conventions for split register file architectures are well understood, and using dirty bits on floating-point register file state can reduce context-switch overhead.
RISC-V standard F extension single-precision floating-point state
| FLEN-1| | 0 |
|---|---|
| f0 | |
| f1 | |
| f2 | |
| f3 | |
| f4 | |
| f5 | |
| f6 | |
| f7 | |
| f8 | |
| f9 | |
| f10 | |
| f11 | |
| f12 | |
| f13 | |
| f14 | |
| f15 | |
| f16 | |
| f17 | |
| f18 | |
| f19 | |
| f20 | |
| f21 | |
| f22 | |
| f23 | |
| f24 | |
| f25 | |
| f26 | |
| f27 | |
| f28 | |
| f29 | |
| f30 | |
| f31 | |
| FLEN | |
| 31| | 0 |
| fcsr | |
| 32 |
Floating-Point Control and Status Register
The floating-point control and status register, fcsr, is a RISC-V
control and status register (CSR). It is a 32-bit read/write register
that selects the dynamic rounding mode for floating-point arithmetic
operations and holds the accrued exception flags, as shown in fcsr.
Floating-point control and status register
include::images/wavedrom/float-csr.adoc[]
The fcsr register can be read and written with the FRCSR and FSCSR
instructions, which are assembler pseudoinstructions built on the
underlying CSR access instructions. FRCSR reads fcsr by copying it
into integer register rd. FSCSR swaps the value in fcsr by copying
the original value into integer register rd, and then writing a new
value obtained from integer register rs1 into fcsr.
The fields within the fcsr can also be accessed individually through
different CSR addresses, and separate assembler pseudoinstructions are defined
for these accesses. The FRRM instruction reads the Rounding Mode field frm
(fcsr bits 7--5) and copies it into the least-significant three bits of
integer register rd, with zero in all other bits. FSRM swaps the value in
frm by copying the original value into integer register rd, and then
writing a new value obtained from the three least-significant bits of integer
register rs1 into frm. FRFLAGS and FSFLAGS are defined analogously for the
Accrued Exception Flags field fflags (fcsr bits 4--0).
Bits 31--8 of the fcsr are reserved for other standard extensions. If
these extensions are not present, implementations shall ignore writes to
these bits and supply a zero value when read. Standard software should
preserve the contents of these bits.
Floating-point operations use either a static rounding mode encoded in
the instruction, or a dynamic rounding mode held in frm. Rounding
modes are encoded as shown in rm. A value of 111 in the
instruction's rm field selects the dynamic rounding mode held in
frm. The behavior of floating-point instructions that depend on
rounding mode when executed with a reserved rounding mode is reserved, including both static reserved rounding modes (101-110) and dynamic reserved rounding modes (101-111). Some instructions, including widening conversions, have the rm field but are nevertheless mathematically unaffected by the rounding mode; software should set their rm field to
RNE (000) but implementations must treat the rm field as usual (in
particular, with regard to decoding legal vs. reserved encodings).
Rounding mode encoding.
| Rounding Mode | Mnemonic | Meaning |
|---|---|---|
| 000 | RNE | Round to Nearest, ties to Even |
| 001 | RTZ | Round towards Zero |
| 010 | RDN | Round Down (towards $-\infty$) |
| 011 | RUP | Round Up (towards $+\infty$) |
| 100 | RMM | Round to Nearest, ties to Max Magnitude |
| 101 | Reserved for future use. | |
| 110 | Reserved for future use. | |
| 111 | DYN | In instruction's rm field, selects dynamic rounding mode; In Rounding Mode register, reserved. |
Note
The C99 language standard effectively mandates the provision of a dynamic rounding mode register. In typical implementations, writes to the dynamic rounding mode CSR state will serialize the pipeline. Static rounding modes are used to implement specialized arithmetic operations that often have to switch frequently between different rounding modes.
The ratified version of the F spec mandated that an illegal-instruction exception was raised when an instruction was executed with a reserved dynamic rounding mode. This has been weakened to reserved, which matches the behavior of static rounding-mode instructions. Raising an illegal-instruction exception is still valid behavior when encountering a reserved encoding, so implementations compatible with the ratified spec are compatible with the weakened spec.
The accrued exception flags indicate the exception conditions that have arisen on any floating-point arithmetic instruction since the field was last reset by software, as shown in bitdef. The base RISC-V ISA does not support generating a trap on the setting of a floating-point exception flag.
Accrued exception flag encoding.
| Flag Mnemonic | Flag Meaning |
|---|---|
| NV | Invalid Operation |
| DZ | Divide by Zero |
| OF | Overflow |
| UF | Underflow |
| NX | Inexact |
Note
As allowed by the standard, we do not support traps on floating-point exceptions in the F extension, but instead require explicit checks of the flags in software. We considered adding branches controlled directly by the contents of the floating-point accrued exception flags, but ultimately chose to omit these instructions to keep the ISA simple.
NaN Generation and Propagation
Except when otherwise stated, if the result of a floating-point
operation is NaN, it is the canonical NaN. The canonical NaN has a
positive sign and all significand bits clear except the MSB, a.k.a. the
quiet bit. For single-precision floating-point, this corresponds to the pattern 0x7fc00000.
Tip
We considered propagating NaN payloads, as is recommended by the standard, but this decision would have increased hardware cost. Moreover, since this feature is optional in the standard, it cannot be used in portable code.
Implementers are free to provide a NaN payload propagation scheme as a nonstandard extension enabled by a nonstandard operating mode. However, the canonical NaN scheme described above must always be supported and should be the default mode.
'''
Note
We require implementations to return the standard-mandated default values in the case of exceptional conditions, without any further intervention on the part of user-level software (unlike the Alpha ISA floating-point trap barriers). We believe full hardware handling of exceptional cases will become more common, and so wish to avoid complicating the user-level ISA to optimize other approaches. Implementations can always trap to machine-mode software handlers to provide exceptional default values.
Subnormal Arithmetic
Operations on subnormal numbers are handled in accordance with the IEEE 754-2008 standard.
In the parlance of the IEEE standard, tininess is detected after rounding.
Note
Detecting tininess after rounding results in fewer spurious underflow signals.
Extension description from riscv-unified-db, reformatted.
Specification chapter: The RISC-V Instruction Set Manual, src/unpriv/f-st-ext.adoc.
Instructions (54)
| Mnemonic | Name (riscv-unified-db) | XLEN |
|---|---|---|
c.flw | Load single-precision | RV32 only |
c.flwsp | Load word into floating-point register from stack | RV32 only |
c.fsw | Store single-precision | RV32 only |
c.fswsp | Store single-precision value to stack | RV32 only |
fabs.s | (pseudo-op) | RV32 and RV64 |
fadd.s | Floating-Point Add Single-Precision | RV32 and RV64 |
fclass.s | Floating-Point Classify Single-Precision | RV32 and RV64 |
fcvt.l.s | Floating-Point Convert Single-Precision to Long | RV64 only |
fcvt.lu.s | Floating-Point Convert Single-Precision to Unsigned Long | RV64 only |
fcvt.s.l | Floating-Point Convert Long to Single-Precision | RV64 only |
fcvt.s.lu | Floating-Point Convert Unsigned Long to Single-Precision | RV64 only |
fcvt.s.w | Floating-Point Convert Word to Single-Precision | RV32 and RV64 |
fcvt.s.wu | Floating-Point Convert Unsigned Word to Single-Precision | RV32 and RV64 |
fcvt.w.s | Floating-Point Convert Single-Precision to Word | RV32 and RV64 |
fcvt.wu.s | Floating-Point Convert Single-Precision to Unsigned Word | RV32 and RV64 |
fdiv.s | Floating-Point Divide Single-Precision | RV32 and RV64 |
feq.s | Floating-Point Equal Single-Precision | RV32 and RV64 |
fle.s | Floating-Point Less Than or Equal Single-Precision | RV32 and RV64 |
fleq.s | Floating-Point Less Than or Equal Quiet Single-Precision | RV32 and RV64 |
fli.s | No synopsis available | RV32 and RV64 |
flt.s | Floating-Point Less Than Single-Precision | RV32 and RV64 |
fltq.s | Floating-Point Less Than Quiet Single-Precision | RV32 and RV64 |
flw | Floating-Point Load Single-Precision | RV32 and RV64 |
fmadd.s | Floating-Point Multiply-Add Single-Precision | RV32 and RV64 |
fmax.s | Floating-Point Maximum-Number Single-Precision | RV32 and RV64 |
fmaxm.s | No synopsis available | RV32 and RV64 |
fmin.s | Floating-Point Minimum-Number Single-Precision | RV32 and RV64 |
fminm.s | No synopsis available | RV32 and RV64 |
fmsub.s | Floating-Point Multiply-Subtract Single-Precision | RV32 and RV64 |
fmul.s | Floating-Point Multiply Single-Precision | RV32 and RV64 |
fmv.s | (pseudo-op) | RV32 and RV64 |
fmv.s.x | (pseudo-op) | RV32 and RV64 |
fmv.w.x | Floating-Point Move Single-Precision Word from Integer Register | RV32 and RV64 |
fmv.x.s | (pseudo-op) | RV32 and RV64 |
fmv.x.w | Floating-Point Move Single-Precision Word to Integer Register | RV32 and RV64 |
fneg.s | (pseudo-op) | RV32 and RV64 |
fnmadd.s | Floating-Point Negate-Multiply-Add Single-Precision | RV32 and RV64 |
fnmsub.s | Floating-Point Negate-Multiply-Subtract Single-Precision | RV32 and RV64 |
frcsr | (pseudo-op) | RV32 and RV64 |
frflags | (pseudo-op) | RV32 and RV64 |
fround.s | Floating-point round single-precision float to integer | RV32 and RV64 |
froundnx.s | Floating-point Round Single-precision to Integer with Inexact | RV32 and RV64 |
frrm | (pseudo-op) | RV32 and RV64 |
fscsr | (pseudo-op) | RV32 and RV64 |
fsflags | (pseudo-op) | RV32 and RV64 |
fsflagsi | (pseudo-op) | RV32 and RV64 |
fsgnj.s | Floating-Point Sign-Inject Single-Precision | RV32 and RV64 |
fsgnjn.s | Floating-Point Sign-Inject Negate Single-Precision | RV32 and RV64 |
fsgnjx.s | Floating-Point Sign-Inject XOR Single-Precision | RV32 and RV64 |
fsqrt.s | Floating-Point Square Root Single-Precision | RV32 and RV64 |
fsrm | (pseudo-op) | RV32 and RV64 |
fsrmi | (pseudo-op) | RV32 and RV64 |
fsub.s | Floating-Point Subtract Single-Precision | RV32 and RV64 |
fsw | Floating-Point Store Single-Precision | RV32 and RV64 |