Introduction
RISC-V handles floating-point overflow by setting a sticky overflow
flag in the Floating-Point Control and Status Register (fcsr)
rather than triggering an immediate hardware trap.
How Floating-Point Overflow Works in RISC-V
- IEEE 754 Compliance: The standard Floating-Point (F, D, or Q)
extensions in RISC-V conform to the IEEE 754-2008 floating-point
standard. When a computed value exceeds the maximum representable
finite magnitude for the destination format, an overflow occurs.
- Accrued Exception Flags: When an overflow happens, the OF
(Overflow) flag inside the accrued exception flags field of the
fcsr register is set to 1.
- Default Results: Depending on the active rounding mode, the
instruction returns an infinity (+inf or -inf) or the maximum
finite value as defined by the IEEE 754 standard.
Detecting and Handling Overflow in Software
- No Direct Traps: The base RISC-V ISA does not support generating
a hardware trap or exception automatically when a floating-point
exception flag is set.
- Explicit Software Checks: Software must explicitly read the
fcsr register (or the individual fflags CSR) using instructions
like csrr to check if the overflow flag was accrued, and clear
it using csrw or fsflags when needed.