fcsr
Floating-point control and status register (frm
+ fflags
)
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 Floating-Point Control and Status Register.
Unresolved include directive in modules/csrs/pages/fcsr.adoc - 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 Rounding mode encoding.. 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).
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 Accrued exception flag encoding.. The base RISC-V ISA does not support generating a trap on the setting of a floating-point exception flag.
Flag Mnemonic | Flag Meaning |
---|---|
NV |
Invalid Operation |
DZ |
Divide by Zero |
OF |
Overflow |
UF |
Underflow |
NX |
Inexact |
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. |
Fields
FRM
- Location
-
fcsr[7:5]
- Description
-
Rounding modes are encoded as follows:
Table 2. 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. 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). - Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL
NV
- Location
-
fcsr[4]
- Description
-
Invalid Operation
Cumulative error flag for floating point operations.
Set by hardware when a floating point operation is invalid and stays set until explicitly cleared by software.
- Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL
DZ
- Location
-
fcsr[3]
- Description
-
Divide by zero
Cumulative error flag for floating point operations.
Set by hardware when a floating point divide attempts to divide by zero and stays set until explicitly cleared by software.
- Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL
OF
- Location
-
fcsr[2]
- Description
-
Overflow
Cumulative error flag for floating point operations.
Set by hardware when a floating point operation overflows and stays set until explicitly cleared by software.
- Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL
UF
- Location
-
fcsr[1]
- Description
-
Underflow
Cumulative error flag for floating point operations.
Set by hardware when a floating point operation underflows and stays set until explicitly cleared by software.
- Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL
NX
- Location
-
fcsr[0]
- Description
-
Inexact
Cumulative error flag for floating point operations.
Set by hardware when a floating point operation is inexact and stays set until explicitly cleared by software.
- Type
RW-H |
Read-Write with Hardware update Field is writable by software. Any value that fits in the field is acceptable. Hardware also updates the field without an explicit software write. |
- Reset value
-
UNDEFINED_LEGAL