Skip to content

§ 7.7. RV32F on the CPU

Without float hardware, the RV32IM core of chapter 5 computes floats in software: every a + b in C becomes a call into libgcc's __addsf3, dozens of integer instructions that unpack, align, add, normalize and round. This section adds RISC-V's F extension to the core: 32 float registers, the fcsr register, and an execute unit built from this chapter's parts. 7.9 measures what it buys.

The F core is an option (rv32imf.core.educational-five-stage); the machines keep the RV32IM core by default, so the results of earlier chapters are unchanged.

Component Description Structural Behavioral
FloatExecuteUnit Runs one F instruction: decode, operands, the units, result and flags Yes No (compact children)
FloatRegisterFile 32 × 32-bit float registers, three read ports Yes No (compact children)
RV32IMFFiveStageCore The RV32IM five-stage core with the F extension and the float CSRs Yes No (compact children)

Catalog: float.execute-unit.fp32, rv32f.register-file, rv32imf.core.educational-five-stage (with its decode control, decode and execute stages: rv32imf.decode-control, rv32imf.pipeline.stage.decode, rv32imf.pipeline.stage.execute).

What the F extension adds

Instructions Meaning
FLW, FSW Load and store a float register
FADD.S, FSUB.S, FMUL.S, FDIV.S, FSQRT.S Arithmetic, rounded by the instruction's mode
FMADD.S, FMSUB.S, FNMSUB.S, FNMADD.S Fused multiply-add, three source registers
FSGNJ.S, FSGNJN.S, FSGNJX.S Sign injection (also FMV.S, FNEG.S, FABS.S)
FMIN.S, FMAX.S Minimum, maximum
FEQ.S, FLT.S, FLE.S Compare into an integer register
FCLASS.S The ten-bit class into an integer register
FCVT.W.S, FCVT.WU.S, FCVT.S.W, FCVT.S.WU Conversions between integer and float registers
FMV.X.W, FMV.W.X Move the bits between integer and float registers
CSRRW, CSRRS, CSRRC, and their immediate forms Read and change fflags (CSR 1), frm (2) and fcsr (3)

An instruction's rounding-mode field selects one of the five modes, or 7 for the dynamic mode in frm. A reserved mode (5 or 6) in the instruction, or a CSR other than 1–3, is an illegal instruction. (A reserved value in frm used dynamically rounds toward zero here, where RISC-V would trap; the programs never set one.)

7.7.1. FloatExecuteUnit

IO pins

Pin Direction Width Meaning
CLK, RST Input 1 each Clock, reset
START Input 1 Take an instruction (when not busy)
INSTRUCTION Input 32 The OP-FP or fused multiply-add word
FRM Input 3 The dynamic rounding mode
FA, FB, FC Input 32 each Float operands (rs1, rs2, rs3)
XA Input 32 Integer operand (rs1)
CANCEL, TAKE Input 1 each Drop the instruction / the result was used
BUSY, DONE Output 1 each Running / result ready
RESULT Output 32 The float or integer result
FLAGS Output 5 The flags the instruction raised

Behavior

The handshake of the integer MulDivUnit: START takes the instruction and its operands into registers, DONE rises when RESULT and FLAGS are ready and holds until TAKE.

Instructions Unit DONE after START
FADD (as A × 1 + B), FSUB (A × 1 − B), FMUL (A × B + ±0, the zero with the product's sign), the four fused FloatFmaPipelined<FP32> (7.4.2) 4 edges
FDIV, FSQRT FloatDivSqrt<FP32> (7.6) 27 edges
Sign injection, min/max, compares, class, moves, conversions FloatCompare, FloatClassify, FloatToInt, IntToFloat (7.5), wires 1 edge

Using the FMA for add, subtract and multiply is what GPUs do; the zero added to a product takes the product's sign so that a zero product keeps its sign in every rounding mode. The flags are the same as the separate operations'.

Examples

fadd, fmul, fmadd, fdiv, fcvt.w.s (toward zero), fcvt.s.w and flt, each from START to DONE:

All 14 recorded rows of FloatExecuteUnitScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.

Default scenario profile: fidelity-logic-v1 / structural (3c048584c5332bb2). Changing fidelity or the circuit can change these times.

Checkpointzero-based number · timeline label Timesimulator units CANCELInput · 1 bit CLKInput · 1 bit FAInput · 32 bit FBInput · 32 bit FCInput · 32 bit FRMInput · 3 bit INSTRUCTIONInput · 32 bit RSTInput · 1 bit STARTInput · 1 bit TAKEInput · 1 bit XAInput · 32 bit BUSYExpected output · 1 bit DONEExpected output · 1 bit FLAGSExpected output · 5 bit RESULTExpected output · 32 bit
#0fadd-start99 0 1 0x3FC00000 0x40100000 0x00000000 0x0 0x00208253 0 1 0 0x00000000 1 0 not checked not checked
#1fadd-done242 0 1 0x3FC00000 0x40100000 0x00000000 0x0 0x00208253 0 0 0 0x00000000 1 1 0x00 0x40700000
#2fmul-start311 0 1 0xC0400000 0x3F000000 0x00000000 0x0 0x10208253 0 1 0 0x00000000 1 0 not checked not checked
#3fmul-done430 0 1 0xC0400000 0x3F000000 0x00000000 0x0 0x10208253 0 0 0 0x00000000 1 1 0x00 0xBFC00000
#4fmadd-start484 0 1 0x40000000 0x40400000 0x3F800000 0x0 0x18208243 0 1 0 0x00000000 1 0 not checked not checked
#5fmadd-done599 0 1 0x40000000 0x40400000 0x3F800000 0x0 0x18208243 0 0 0 0x00000000 1 1 0x00 0x40E00000
#6fdiv-start662 0 1 0x3F800000 0x40400000 0x00000000 0x0 0x18208253 0 1 0 0x00000000 1 0 not checked not checked
#7fdiv-done2251 0 1 0x3F800000 0x40400000 0x00000000 0x0 0x18208253 0 0 0 0x00000000 1 1 0x01 0x3EAAAAAB
#8fcvt-w-s-rtz-start2323 0 1 0xC0300000 0x00000000 0x00000000 0x0 0xC0009253 0 1 0 0x00000000 1 0 not checked not checked
#9fcvt-w-s-rtz-done2354 0 1 0xC0300000 0x00000000 0x00000000 0x0 0xC0009253 0 0 0 0x00000000 1 1 0x01 0xFFFFFFFE
#10fcvt-s-w-start2448 0 1 0x00000000 0x00000000 0x00000000 0x0 0xD0008253 0 1 0 0xFFFFFFF9 1 0 not checked not checked
#11fcvt-s-w-done2468 0 1 0x00000000 0x00000000 0x00000000 0x0 0xD0008253 0 0 0 0xFFFFFFF9 1 1 0x00 0xC0E00000
#12flt-start2533 0 1 0x3F800000 0x40000000 0x00000000 0x0 0xA0209253 0 1 0 0x00000000 1 0 not checked not checked
#13flt-done2558 0 1 0x3F800000 0x40000000 0x00000000 0x0 0xA0209253 0 0 0 0x00000000 1 1 0x00 0x00000001

Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.

Open the full visualizer

7.7.2. FloatRegisterFile

Pin Direction Width Meaning
CLK, RST Input 1 each Clock; reset clears every register
WE, WADDR, WDATA Input 1, 5, 32 Write port, at the rising edge
RADDR1–RADDR3 Input 5 each Read addresses
RDATA1–RDATA3 Output 32 each Read data (combinational)

Thirty-two Register<32>s, a 5-to-32 decoder for the write enables and three 32-input word multiplexers. Unlike x0, f0 is an ordinary register. Three read ports because the fused multiply-adds read three registers.

7.7.3. RV32IMFFiveStageCore

Where the F parts sit

The five stages and their registers are those of the RV32IM core (§ 4.6, § 5.1); the F extension changes three stages:

Stage Change
Decode FLW and FSW decode as LW and SW (bit 2 of the opcode cleared): the same address and memory control, without the integer write (FLW) or the integer rs2 (FSW). The OP-FP, fused and CSR words get their own control: an integer destination (compares, class, FMV.X.W, FCVT.W, CSR reads) or source (FCVT.S.W, FMV.W.X, CSR register forms) where they have one, the result written back from the ALU result.
Execute The float registers are read here, with forwarding from EX/MEM and MEM/WB, so no new decode-to-execute fields are needed. The FloatExecuteUnit runs the instruction and holds the stage (EXECUTION_WAIT), as the M unit does. A float operand still being loaded (FLW in the memory stage) also holds it. FSW's store data is the float rs2. fcsr lives here.
Writeback The float register file is written when an instruction with a float destination commits; its instruction word travels to writeback anyway.

fcsr and commit. An F or CSR instruction changes fcsr (its flags, or the CSR's new value) when it leaves the execute stage. In this core that is its commit point: an older instruction that will trap or halt cancels it before it leaves, and branches resolve in execute, so an instruction past execute is never discarded. A younger instruction reaches execute after it, so it sees the new frm, and the flags accumulate in program order.

Fidelity

Gate level, the F core is the RV32IM core plus the float parts. It has no compact form of its own; "compact" builds it from compact parts (each float unit as its compiled gate list).

Source and tests

Execute unit, core, instruction reference, tests.

Test Coverage
RV32FInstructionOracleTest The instruction reference: results, disassembly, illegal CSR numbers and rounding modes
FloatExecuteUnitTest Gates and compact children: 300 random F instructions of every kind on random operands and modes (dynamic included), each against the instruction reference executing the same word, with DONE at the expected edge
RV32IMFCoreTest A program with every instruction class, float load-use, back-to-back and three-operand dependencies, CSR reads and writes, a dynamic rounding mode and a divide by zero, on the core (compact children) with memory answering after varying waits: every commit against the instruction reference (PC, instruction, integer and float registers), then fcsr and memory; with the iterative and the array multiplier