# The FPU Sources: HM section 6, SM section 6, the instruction pages of SM section 9. Tier 2. ## Registers and modes - 32 single-precision registers in two banks of 16 (FR0-FR15 and XF0-XF15), swapped by FPSCR.FR (`FRCHG`). Pairs form DR0-DR14 (double precision) and XD0-XD14; groups of four form FV0, FV4, FV8, FV12; XF0-XF15 together are XMTRX, the 4x4 matrix of `FTRV`. - FPUL: the 32-bit transfer register between CPU and FPU (`LDS Rm,FPUL`, `FLDS`, `FSTS`, `FLOAT`, `FTRC`). - FPSCR: RM (round to nearest or to zero only: SM table 1.1 "Two rounding modes"), flag, enable and cause fields (V, Z, O, U, I, and E for the cause field), DN (denormals: 0 "treated as such", 1 "treated as zero"; HM 2), PR, SZ, FR. ## Arithmetic that a reader has to get exactly right - **Rounding is once per instruction**: FMAC, FIPR and FTRV round once, so their results differ from the same sum built from FMUL and FADD (HM 6.4). Python's floats are doubles: a single-precision result must be rounded from an exact (or sufficiently wide) intermediate, never from a double that was itself rounded (double rounding). The core will need an explicit binary32 rounding routine; `struct.pack('f', x)` rounds to nearest even from the double and is wrong for round-to-zero and for FMAC's fused result. - **Denormals**: with DN=1 a denormal source or result is flushed to zero in every operation that generates a value (HM 6.2.3). With DN=0, SM 9's pseudo-code (FADD, SM p. 254) sets the cause bit E, "FPU error", when an input is denormal, and E cannot be disabled (SM 6, around p. 125: "FPU errors cannot be disabled"): the hardware leaves denormal arithmetic to a software handler (SM table 1.1: "Truncation to zero or interrupt generation for compliance with IEEE754"). Which mode Dreamcast software runs in is not checked yet (HYPOTHESIS DN=1: to read from the first `LDS Rm,FPSCR` in a trace). - **FIPR and FTRV are approximations.** HM 6.6.1: "the FPU ignores comparatively small values in the partial computation results of four multiplications", with a stated maximum error, and "In future version of SuperH RISC engine family, the above error is guaranteed, but the same result as SH7750 is not guaranteed." They always set the inexact flag (I). HM 6.7.2 and 6.7.3 are errata: the overflow flag can be set on a zero result, and the sign of a result built from infinities can be wrong. **Nothing we hold gives the bit-exact algorithm.** This is the largest single gap in the core (handoff U5): Dreamcast games transform every vertex with FTRV, so a different last bit moves vertices and changes pixels. Until a judge appears the core computes the products exactly and sums them with a single rounding, tagged HYPOTHESIS with the HM's error bound asserted as a property (every result within it). - **Double precision** is slow on this FPU (latency 8, pitch 6 for FADD/FSUB/FMUL: SM table 1.1) and rare in games; correctness first, the same rounding rules. - **NaN encodings and propagation**: the SH-4's default quiet NaN and which operand's NaN propagates are defined in SM 6 (to transcribe; the SingleStepTests corpus records an Apple M2 host's NaN behaviour under Reicast's interpreter, so it cannot judge them). ## Timing (SM table 8.3, see [timing.md](timing.md)) FADD/FSUB/FMUL/FMAC single: latency 3, pitch 1. FIPR: latency 4, pitch 1. FTRV: latency 7, pitch 4 (SM table 1.1; the per-instruction rows of table 8.3 are the authority, with their lock stages). FDIV and FSQRT occupy the F3 stage for many cycles (the "FDIV/FSQRT pipeline", SM figure 8.1). ## Judges No hardware-tier oracle. Tier 2: the manuals' operation pseudo-code (SM 9 gives C code for each instruction, e.g. `FADD` p. 254, `FIPR` and `FTRV`, with a special-cases table per FPU instruction; `FIPR`'s and `FTRV`'s code describes the operation, not the approximation's bits). Tier 5: the SingleStepTests sh4 corpus (generated by Reicast's interpreter on an Apple M2, so FPU results are the host's IEEE arithmetic, not the SH-4's: its README says so) and MAME. A real Dreamcast running a test program would be tier 1; Aubrey has none (the family's hardware list), so FIPR/FTRV bit-exactness stays open.