# The FPU, paired singles and quantized loads and stores Source: Gekko UM (IBM, v1.2, 2000; facts only) sections 2.1.2.4, 2.1.2.9, 2.2.1, 2.3.4.3.12, 4.5.6, chapter 12 and tables A-30 to A-32. All DOC unless tagged. Unknowns are numbered as in the handoff brief. ## The register format - With HID2[PSE] = 1 an FPR may hold a **paired single**: two single-precision values, ps0 in the high word and ps1 in the low word (UM 2.2.1, figure 2-18). - "The relationship between the internal format for paired single operands and that for double-precision floating-point operands is **unspecified**. It is a programming error to apply double-precision instructions to paired single operands and vice versa" (UM 2.2.1). Yet the same section says "any single-precision value in ps0 can be used as the input operand to a double-precision floating-point instruction, including a store", and that a single-precision result is placed in ps0 and duplicated into ps1. - **So the specification must choose a representation, and the manual does not fix it (U6).** The choice that serves every documented rule: each FPR holds **ps0 as a 64-bit double** and **ps1 as a separate value** (a double whose value is always single- representable, or a 32-bit single). Then double-precision instructions read and write ps0 as a double, single-precision results land in ps0 and are copied to ps1, and paired instructions operate on both. What a double-precision instruction does to ps1 is the unspecified part: the specification leaves ps1 unchanged and tags it HYPOTHESIS. The save/restore pair the manual guarantees (`psq_st frX,0(r1),0,0` then `stfd frX,8(r1)`, restored by `psq_l` then `lfd`; UM 2.2.1) must round-trip under the chosen model, with the documented loss: a denormal ps1 is stored, and so restored, as 0. - Paired-single instructions, loads, stores and moves raise floating-point unavailable when MSR[FP] = 0 (UM 2.2.1). ## Status bits - FPSCR result bits 15-19 (FPRF) come from the ps0 computation, except for `ps_cmpu1`, `ps_cmpo1` and `ps_sum1`, where they come from ps1. Exception bits (0-14, 22-23) may be set by either half (UM 2.2.1). - CR: `ps_cmpo0`/`ps_cmpu0` set crfD from ps0, `ps_cmpo1`/`ps_cmpu1` from ps1; any other paired instruction with Rc = 1 copies FPSCR 0-3 to CR1 (UM 2.2.1). - Which half's exception is reported first, and whether an enabled exception taken on ps0 still writes ps1, is not stated (U7). ## Single-precision instructions in paired-single mode With PSE = 1: `fadds fsubs fmuls fdivs fmadds fmsubs fnmadds fnmsubs fres` operate on ps0 and duplicate the result into ps1 (UM 2.2.1; `fres` pseudo-code, chapter 12). `frsp` rounds the double in frB into frD(ps0) and leaves frD(ps1) **undefined** (UM `frsp`, chapter 12): unknown U8 (the specification copies ps0, HYPOTHESIS). `lfs[u][x]` load into ps0 and duplicate into ps1; `stfs[u][x]` store ps0. `fctiw`/`fctiwz` put the integer in the low word of the double and, with `stfiwx`, behave as without PSE. **Precision.** The manual defines single-precision results by the PEM's rounding model. It does not say how the 64-bit FPU computes `fmuls` or `fmadds` when an input is not single-representable (a double in ps0 used by a single-precision instruction, which the PEM calls undefined), nor whether the paired arithmetic rounds once or twice in its fused multiply-adds (U9). These are exactly the corners in which emulators are known to disagree; only a hardware test with captured results can decide them. No such capture is in hand (validation.md). **Estimates.** `fres` and `ps_res`: "correct to a precision of one part in 4096" of the reciprocal; `frsqrte` and `ps_rsqrte` the same for the reciprocal square root (UM chapter 12). The overview says "Error < 1/4000" (UM 2.2, feature list): contradiction C4, the chapter-12 bound is the definition. "The value placed into register frD may vary between implementations, and between different executions on the same implementation" (UM `fres`). The Gekko's actual table is unknown (U5); the specification needs one fixed table, so that every tier gives the same bits, and must say which evidence it came from. ## The paired-single instructions (UM tables A-31, A-32; chapter 12) | Group | Instructions | | --- | --- | | arithmetic, both halves | `ps_add ps_sub ps_mul ps_div ps_madd ps_msub ps_nmadd ps_nmsub` | | scalar operand | `ps_muls0 ps_muls1` (multiply both halves by frC's ps0 or ps1), `ps_madds0 ps_madds1` | | sums | `ps_sum0` (ps0 = frA.ps0 + frB.ps1, ps1 = frC.ps1), `ps_sum1` (ps1 = frA.ps0 + frB.ps1, ps0 = frC.ps0) | | estimates and select | `ps_res ps_rsqrte ps_sel` | | moves | `ps_mr ps_neg ps_abs ps_nabs ps_merge00 ps_merge01 ps_merge10 ps_merge11` | | compares | `ps_cmpu0 ps_cmpo0 ps_cmpu1 ps_cmpo1` | | quantized memory | `psq_l psq_lu psq_st psq_stu` (D-form, 12-bit displacement), `psq_lx psq_lux psq_stx psq_stux` | The `ps_sum0/1` operand roles above are the manual's pseudo-code as we read it; rung 3 checks each instruction's description against its pseudo-code line by line before it is written, because sums and merges are where a misread role hides. All paired arithmetic is pipelined 1-1-1 except `ps_div` (17 cycles), the same as the single-precision instructions (UM table 6-7; timing.md). ## Quantized loads and stores **Encoding.** The D-forms replace the 16-bit displacement with W (1 bit), I (3 bits) and a 12-bit signed displacement; the indexed forms replace the 10-bit XO with a 6-bit XO, W and I (UM 2.3.4.3.12). W = 1 transfers one value (ps0) and, on a load, sets ps1 to 1.0 (UM `psq_l`: "PS1 is loaded with a floating point value of 1.0"); W = 0 transfers both, the second at EA + c. The pseudo-code sets the element size c to 4 for type 0, "10" for types 4 and 6 and "20" for types 5 and 7 (UM `psq_l`, chapter 12): read as byte counts these must be 1 and 2, so the printed values are a slip or a notation we do not recognise (contradiction C5; the specification uses 1 and 2, DOC by the types' widths in table 2-17). **GQRn** (SPR 912 + n; UM 2.1.2.9, table 2-16): LD_SCALE bits 2-7 and LD_TYPE bits 13-15; ST_SCALE bits 18-23 and ST_TYPE bits 29-31. Types (table 2-17): 0 single precision (no conversion), 1-3 reserved, 4 u8, 5 u16, 6 s8, 7 s16. The scale is a 6-bit two's-complement value S in -32..31. **Load (dequantize):** F = I x 2^-S for an integer type (UM 2.3.4.3.12, with table 2-44's worked values: S = 1 gives 0.5 for I = 1, S = -32 gives 4.29e9). Type 0 passes the single through unchanged, denormals included. **Store (quantize):** I = ROUND(F x 2^S) with **round toward zero**, then clamping on overflow: u8 0..255, u16 0..65535, s8 -128..127, s16 -32768..32767; +Inf and NaN give the positive limit, -Inf the negative limit (UM 2.3.4.3.12, the seven-step algorithm). Type 0 passes the single through, **except a denormal, which is stored as 0.0**. Contradictions and gaps in the manual's own tables (C2, C3): - Table 2-45 (1.00e+2 quantized at each scale) gives **65525** as the u16 result at S = 31, where its own clamping rule gives 65535; and the table's scale column is headed "GQRx[LD_SCALE]" in a store table. Both read as typesetting slips; the algorithm text is the definition. - The alignment rule: "the paired-single quantization load or store will generate an alignment exception when the corresponding GQRn[LD_TYPE] or GQRn[ST_TYPE] are 0 and will not generate an alignment exception when [they] are 4, 5, 6 or 7" (UM 4.5.6). Read literally, every type-0 access traps; read with the PEM's floating-point rule, only a type-0 access that is not word-aligned does (HYPOTHESIS: the second; U10). Unaligned integer-type accesses are then legal, and whether they cost extra bus transactions across a line boundary is a timing unknown. - Reserved types 1-3: behaviour not given (U11). - Little-endian mode: "undefined results" (UM 2.3.4.3.12). The GameCube runs big-endian. ## What the board sees Nothing here leaves the core except loads and stores through the data cache. The quantized forms are ordinary cacheable accesses of 1, 2, 4 or 8 bytes (two values of the type's width); they can also target the locked cache (caches-dma-wgp.md) and, for stores, the write-gather pipe's address (non-cacheable). The GX vertex path uses them heavily (HYPOTHESIS from the design intent stated in UM 1.1: "graphics enhancement").