Chris Low-level systems developer
← Back to all posts

The Receipt for the Bit That Fell Off

Relevance by layertest log
ISA

matters at ISA because the verified property is the F-extension's inexact contract: IEEE 754 defines inexact as the rounded result differing from the mathematically exact one, RISC-V accrues it in fflags bit 0 with no trap, and the module's 0x01-after-inexact versus 0x00-after-exact pair shows the contract firing exactly when the exact sum needs 54 bits and the format holds 53.

MICROARCH

not at Microarch because the property is implementation-invariant: the inexact definition, the NX bit position, and the no-trap rule are fixed in the spec's text, so no implementation choice changes a readback.

FIRMWARE

matters at Firmware because the module runs at firmware's own privilege level: it reads the boot mstatus, moves FS from Off to Initial before the first floating-point instruction (with FS Off an FP instruction raises illegal-instruction), and the trap counter stays 0 across both phases.

KERNEL

matters at Kernel because the no-trap discipline fixes the kernel's contract with user space: there is no exception path to audit and no trap handler to write for a rounding, an inexact operation is detected by polling fflags after the fact, and the module's zero trap count is that discipline holding.

ALGORITHM

not at Algorithm because the verified claim is the hardware's flag contract, not a portable computation: the eighteen checks are CSR readback assertions judged against the spec's text and the module's analytic derivation, and Kahan summation enters the post as the named customer, not as a verified computation.

Add 1.0 to 10^16 and watch the 1 disappear. A bare-metal module ran a single fadd.d on the pair (1e16, 1.0) and read the result bits back: 0x4341C37937E08000, which is exactly 1e16. The +1 vanished without a trace in the result. But it left a receipt. The fcsr register read exactly 0x01 afterward: the NX bit set, the other four exception flags clear. The operation rounded, and the hardware said so in one bit.

A supermarket receipt
The hardware's receipt for one lost bit.

The module states the flag layout in two lines of its source:

#define FFLAGS_NX  0x01UL  // fcsr bit 0: inexact
#define FFLAGS_ALL 0x1FUL  // fcsr bits 4:0: NV|DZ|OF|UF|NX

The vanishing is arithmetic, not a bug, and it is worth doing by hand once. 10^16 factors as 2^16 times 5^16, and 5^16 is 152587890625, so 1e16 is exactly representable as a double: exponent field 0x434, a 53-bit significand whose least significant bit is even. It sits in the binade from 2^53 to 2^54, where representable doubles stand 2 apart. The exact sum 10^16 + 1 sits exactly halfway between 10^16 and 10^16 + 2, and round-to-nearest-ties-to-even picks the even neighbor: 10^16 itself. The exact answer needed 54 significant bits; the format carries 53. A host-side check agrees: 1e16 + 1.0 is bit-identical to 1e16.

So why does the machine keep a flag for this instead of rounding silently, or instead of trapping? Trapping is the obvious alternative, and ordinary arithmetic breaks it. Inexact results are routine: 1.0/3.0 rounds every single time, and a trap per rounding would fire constantly through ordinary arithmetic, which is why the ISA chose a no-trap discipline for these flags. Silence is the other alternative, and it breaks the programs that need to know. Kahan's compensated summation exists precisely to recover the rounding error and feed it back into the sum, and it cannot recover what it cannot see. The flag is the middle path: accrue, don't interrupt. NX sets, the program keeps running, and whoever cares reads the receipt afterward.

The module proved the receipt is exact, not approximate. After clearing fcsr to 0x00, the inexact add left (fcsr & 0x1f) == 0x01: NX set, and NV, DZ, OF, and UF all clear, which is the whole point of the isolation. Nothing else had cause to move: both operands were finite positive normals, the result was finite and nowhere near the subnormal range or the overflow threshold, and no division or invalid operation was involved. The control ran fadd.d on (1.0, 2.0): the exact sum 3.0 is representable, the result bits came back 0x4008000000000000, and fcsr read exactly 0x00. No flag moved at all. Eighteen checks, zero mismatches, three byte-identical runs under QEMU 8.2.2, and the trap counter stayed at 0.

This is the complement of an earlier note on this blog, the one about 0.0 divided by 0.0. That was the operation with no answer, and the NV flag marked the refusal. This one is the operation with an answer that cannot be written down. One sentence to keep: when the exact answer needs more bits than the format holds, the hardware keeps the best approximation and leaves you a one-bit receipt for what fell off.

Related work: crash triage is $250/symptom, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app