Chris Low-level systems developer
← Back to all posts

The Rounding Mode Written Into the Instruction

ISA

The claim is the unprivileged specification's floating-point rounding rule: a value of 111 in the instruction's rm field selects the dynamic rounding mode held in fcsr.frm, and every other rm value names its own mode. The run shows both halves: a static-RNE divide ignores frm under RDN and RUP alike, while the dynamic control under RUP diverges by one ulp.

MICROARCH

No claim depends on how any core implements floating-point rounding. QEMU serves as a functional executor only; no pipeline, latency, or rounding-unit behavior is asserted or measured, and the honest limit states the result is emulator-observed, not silicon-proved.

FIRMWARE

No firmware service takes part in the claim. The binary runs in M-mode on bare QEMU with no SBI or firmware calls; the behavior under test is the floating-point instruction's own rounding semantic.

KERNEL

No kernel is involved at any step. The test binary runs on bare QEMU with no operating system; no system call or kernel data structure appears in the argument.

ALGORITHM

The claim is one fixed instruction semantic exercised as four fixed divides with register readback, not a computation proved against an independent oracle. The static-then-dynamic contrast is shown as the consequence the rounding rule forces, not as a verified algorithm.

A floating-point divide gets its rounding mode from two places: the instruction itself, or the fcsr register. Most code leaves the choice to fcsr.frm and never thinks about it again. But the instruction carries its own rm field, and when that field names a mode, the register does not get a vote.

Measured on QEMU 8.2.2, dividing 1.0 by 3.0 in double precision. With fcsr.frm set to RDN, round toward negative infinity, the static-RNE divide returned 0x3FD5555555555555, and frm still read back RDN afterward. Under frm set to RUP, round toward positive infinity, the same static-RNE divide returned the same 0x3FD5555555555555, and frm still read back RUP. The instruction's mode won twice, and the register came back untouched both times. [1]

The witness, the static divide and its dynamic control:

fdiv.d fa0, fa0, fa1, rne   # static RNE, rm field 000
# under frm=RDN: q=0x3FD5555555555555, frm still RDN
# under frm=RUP: q=0x3FD5555555555555, frm still RUP
fdiv.d fa0, fa0, fa1        # dynamic, rm field 111
# under frm=RUP: q=0x3FD5555555555556, one ulp higher
Blueprint-style diagram of the static rounding mode experiment in three panels: FRM SAYS RDN shows fcsr.frm set to RDN while the instruction says RNE; THE INSTRUCTION WINS shows the static-RNE divide returning 0x3FD5555555555555 with frm still reading RDN; THE CONTROL DIVERGES shows the dynamic divide under frm RUP returning 0x3FD5555555555556, one ulp higher
The static mode in the instruction beat frm twice and left frm untouched, while the dynamic control under RUP moved one ulp.
Diagram: hand-built by Chris Dillinger for this post

The control is what makes the override real. The same divide with the rounding operand omitted encodes rm 111, dynamic, so it follows frm. Under frm set to RUP, that dynamic divide returned 0x3FD5555555555556, one ulp above the static result. The rounding path is live; the static agreement is not a dead rounding unit ignoring the mode. All four divides showed fflags NX only, the inexact flag, as 1.0/3.0 is inexact with no other exception. Nineteen checks, zero mismatches, three runs byte-identical, checksum 0xaaf26cfb83385742. [1]

The unprivileged manual states the rule plainly: a value of 111 in the instruction's rm field selects the dynamic rounding mode held in frm. Every other value names its own mode: RNE is 000, RDN is 010, RUP is 011. The operands here traveled as raw bit patterns in integer registers and were moved into floating-point registers with fmv.d.x inside volatile asm, so the compiler could not constant-fold the divides; every division executed on the hart. [1] [2]

The bench habit: when one computation needs a specific rounding, a correctly rounded constant or a directed-rounding bound, and the rest of the program runs under a global frm, write the mode into that one instruction instead of saving frm, setting it, dividing, and restoring it. The save, set, and restore dance disappears, and a forgotten restore cannot leak the temporary mode into later arithmetic.

One measured boundary, stated honestly: these are emulator results, not silicon. And the static mode leaves frm untouched, which is the point, but it also means code that reads frm afterward learns nothing about how that divide rounded. The rounding decision lives in the instruction stream, so it is visible only in the disassembly. [1]

One sentence to carry away: the rounding mode you write into the instruction beats the rounding mode in the register, and leaves the register exactly as it found it.

Sources

  1. 01
    src/frm-dynamic-vs-static: module PROOF.md and the QEMU runChris Dillinger

    The QEMU 8.2.2 run behind the post: fdiv.d 1.0/3.0 with a static RNE encoding under frm=RDN returned 0x3FD5555555555555 with frm still RDN, and under frm=RUP returned the same 0x3FD5555555555555 with frm still RUP. The dynamic control under frm=RUP returned 0x3FD5555555555556. All four divides showed fflags NX only; traps 0; 19 checks, 0 mismatches, byte-identical across three runs, checksum 0xaaf26cfb83385742; the post's own rebuild and rerun on the build VM reproduced the PASS and the checksum in three runs. The disassembly confirmed the static encoding (rm bits 14:12 = 000) and the dynamic encoding (rm bits 14:12 = 111).

  2. 02
    RISC-V ISA Manual, unprivileged architecture (F extension, rounding modes)RISC-V International

    The rounding rule, read from the pinned release. A value of 111 in the instruction's rm field selects the dynamic rounding mode held in frm. RNE is 000, RDN is 010, RUP is 011. The manual's rounding-mode table gives the same encodings for the instruction rm field and the frm register, with 111 meaning dynamic only in the instruction field.

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app