12 “F” Standard Extension for Single-Precision Floating-Point, Version 2.0
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 [ieee754-2008].
12.1 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
Figure 1.1. 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.
12.2 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 Figure 1.2.
The fcsr
register can be read and written with the FRCSR and
FSCSR instructions, which are assembler pseudo-ops 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 pseudo-ops are
defined for these accesses. The FRRM instruction reads the Rounding
Mode field frm
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
. Additional
pseudo-instructions FSRMI and FSFLAGSI swap values using an immediate
value instead of register rs1.
Bits 31–8 of the fcsr
are reserved for other standard extensions,
including the “L” standard extension for decimal floating-point. 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 Table 1.3. A value of 111 in the instruction’s
rm field selects the dynamic rounding mode held in frm
. If frm
is set to an invalid value (101–111), any subsequent attempt to execute
a floating-point operation with a dynamic rounding mode will cause an illegal
instruction trap. Some instructions that have the rm field are
nevertheless unaffected by the rounding mode; they should have their rm
field set to RNE (000).
The C99 language standard effectively mandates the provision of a dynamic rounding mode register.
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 Table 1.4.
As allowed by the standard, we do not support traps on floating-point exceptions in the base ISA, 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.
12.3 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
.
For FMIN and FMAX, if at least one input is a signaling NaN, or if both inputs are quiet NaNs, the result is the canonical NaN. If one operand is a quiet NaN and the other is not a NaN, the result is the non-NaN operand.
The sign-injection instructions (FSGNJ, FSGNJN, FSGNJX) do not canonicalize NaNs; they manipulate the underlying bit patterns directly.
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.
Implementors 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.
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.
12.4 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.
Detecting tininess after rounding results in fewer spurious underflow signals.
12.5 Single-Precision Load and Store Instructions
Floating-point loads and stores use the same base+offset addressing mode as the integer base ISA, with a base address in register rs1 and a 12-bit signed byte offset. The FLW instruction loads a single-precision floating-point value from memory into floating-point register rd. FSW stores a single-precision value from floating-point register rs2 to memory.
FLW and FSW are only guaranteed to execute atomically if the effective address is naturally aligned.
12.6 Single-Precision Floating-Point Computational Instructions
Floating-point arithmetic instructions with one or two source operands use the R-type format with the OP-FP major opcode. FADD.S, FSUB.S, FMUL.S, and FDIV.S perform single-precision floating-point addition, subtraction, multiplication, and division, respectively, between rs1 and rs2, writing the result to rd. FMIN.S and FMAX.S write, respectively, the smaller or larger of rs1 and rs2 to rd. FSQRT.S computes the square root of rs1 and writes the result to rd.
The 2-bit floating-point format field fmt is encoded as shown in Table [tab:fmt]. It is set to S (00) for all instructions in the F extension.
fmt field | Mnemonic | Meaning |
---|---|---|
00 | S | 32-bit single-precision |
01 | D | 64-bit double-precision |
10 | - | reserved |
11 | Q | 128-bit quad-precision |
All floating-point operations that perform rounding can select the rounding mode using the rm field with the encoding shown in Table 1.3.
Floating-point fused multiply-add instructions require a new standard instruction format. R4-type instructions specify three source registers (rs1, rs2, and rs3) and a destination register (rd). This format is only used by the floating-point fused multiply-add instructions. Fused multiply-add instructions multiply the values in rs1 and rs2, optionally negate the product, then add or subtract the value in rs3, writing the final result to rd. FMADD.S computes rs1×rs2+rs3; FMSUB.S computes rs1×rs2-rs3; FNMSUB.S computes -rs1×rs2+rs3; and FNMADD.S computes -rs1×rs2-rs3.
The fused multiply-add instructions must raise the invalid operation exception when the multiplicands are ∞ and zero, even when the addend is a quiet NaN.
The IEEE 754-2008 standard permits, but does not require, raising the invalid exception for the operation ∞ × 0 + qNaN .
12.7 Single-Precision Floating-Point Conversion and Move Instructions
Floating-point-to-integer and integer-to-floating-point conversion instructions are encoded in the OP-FP major opcode space. FCVT.W.S or FCVT.L.S converts a floating-point number in floating-point register rs1 to a signed 32-bit or 64-bit integer, respectively, in integer register rd. FCVT.S.W or FCVT.S.L converts a 32-bit or 64-bit signed integer, respectively, in integer register rs1 into a floating-point number in floating-point register rd. FCVT.WU.S, FCVT.LU.S, FCVT.S.WU, and FCVT.S.LU variants convert to or from unsigned integer values. FCVT.L[U].S and FCVT.S.L[U] are illegal in RV32. If the rounded result is not representable in the destination format, it is clipped to the nearest value and the invalid flag is set. Table [tab:int_conv] gives the range of valid inputs for FCVT.int.S and the behavior for invalid inputs.
FCVT.W.S | FCVT.WU.S | FCVT.L.S | FCVT.LU.S | |
---|---|---|---|---|
Minimum valid input (after rounding) | − 231 | 0 | − 263 | 0 |
Maximum valid input (after rounding) | 231 − 1 | 232 − 1 | 263 − 1 | 264 − 1 |
Output for out-of-range negative input | − 231 | 0 | − 263 | 0 |
Output for − ∞ | − 231 | 0 | − 263 | 0 |
Output for out-of-range positive input | 231 − 1 | 232 − 1 | 263 − 1 | 264 − 1 |
Output for + ∞ or NaN | 231 − 1 | 232 − 1 | 263 − 1 | 264 − 1 |
All floating-point to integer and integer to floating-point conversion
instructions round according to the rm field. A floating-point register
can be initialized to floating-point positive zero using FCVT.S.W rd,
x0
, which will never raise any exceptions.
Floating-point to floating-point sign-injection instructions, FSGNJ.S, FSGNJN.S, and FSGNJX.S, produce a result that takes all bits except the sign bit from rs1. For FSGNJ, the result’s sign bit is rs2’s sign bit; for FSGNJN, the result’s sign bit is the opposite of rs2’s sign bit; and for FSGNJX, the sign bit is the XOR of the sign bits of rs1 and rs2. Sign-injection instructions do not set floating-point exception flags. Note, FSGNJ.S rx, ry, ry moves ry to rx (assembler pseudo-op FMV.S rx, ry); FSGNJN.S rx, ry, ry moves the negation of ry to rx (assembler pseudo-op FNEG.S rx, ry); and FSGNJX.S rx, ry, ry moves the absolute value of ry to rx (assembler pseudo-op FABS.S rx, ry).
The sign-injection instructions provide floating-point MV, ABS, and NEG, as well as supporting a few other operations, including the IEEE copySign operation and sign manipulation in transcendental math function libraries. Although MV, ABS, and NEG only need a single register operand, whereas FSGNJ instructions need two, it is unlikely most microarchitectures would add optimizations to benefit from the reduced number of register reads for these relatively infrequent instructions. Even in this case, a microarchitecture can simply detect when both source registers are the same for FSGNJ instructions and only read a single copy.
Instructions are provided to move bit patterns between the floating-point and integer registers. FMV.X.W moves the single-precision value in floating-point register rs1 represented in IEEE 754-2008 encoding to the lower 32 bits of integer register rd. For RV64, the higher 32 bits of the destination register are filled with copies of the floating-point number’s sign bit. FMV.W.X moves the single-precision value encoded in IEEE 754-2008 standard encoding from the lower 32 bits of integer register rs1 to the floating-point register rd. The bits are not modified in the transfer, and in particular, the payloads of non-canonical NaNs are preserved.
The FMV.W.X and FMV.X.W instructions were previously called FMV.S.X and FMV.X.S. The use of W is more consistent with their semantics as an instruction that moves 32 bits without interpreting them. This became clearer after defining NaN-boxing. To avoid disturbing existing code, both the W and S versions will be supported by tools.
The base floating-point ISA was defined so as to allow implementations to employ an internal recoding of the floating-point format in registers to simplify handling of subnormal values and possibly to reduce functional unit latency. To this end, the base ISA avoids representing integer values in the floating-point registers by defining conversion and comparison operations that read and write the integer register file directly. This also removes many of the common cases where explicit moves between integer and floating-point registers are required, reducing instruction count and critical paths for common mixed-format code sequences.
12.8 Single-Precision Floating-Point Compare Instructions
Floating-point compare instructions perform the specified comparison (equal, less than, or less than or equal) between floating-point registers rs1 and rs2 and record the Boolean result in integer register rd.
FLT.S and FLE.S perform what the IEEE 754-2008 standard refers to as signaling comparisons: that is, an Invalid Operation exception is raised if either input is NaN. FEQ.S performs a quiet comparison: only signaling NaN inputs cause an Invalid Operation exception. For all three instructions, the result is 0 if either operand is NaN.
12.9 Single-Precision Floating-Point Classify Instruction
The FCLASS.S instruction examines the value in floating-point register rs1 and writes to integer register rd a 10-bit mask that indicates the class of the floating-point number. The format of the mask is described in Table [tab:fclass]. The corresponding bit in rd will be set if the property is true and clear otherwise. All other bits in rd are cleared. Note that exactly one bit in rd will be set.
rd bit | Meaning |
---|---|
0 | rs1 is − ∞. |
1 | rs1 is a negative normal number. |
2 | rs1 is a negative subnormal number. |
3 | rs1 is − 0. |
4 | rs1 is + 0. |
5 | rs1 is a positive subnormal number. |
6 | rs1 is a positive normal number. |
7 | rs1 is + ∞. |
8 | rs1 is a signaling NaN. |
9 | rs1 is a quiet NaN. |
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.