Skip to content

riscv_fpu: honor the rounding mode in the FMA ops#237

Closed
SolAstrius wants to merge 5 commits into
LekKit:stagingfrom
pufit:fix/fpu-fma-rounding
Closed

riscv_fpu: honor the rounding mode in the FMA ops#237
SolAstrius wants to merge 5 commits into
LekKit:stagingfrom
pufit:fix/fpu-fma-rounding

Conversation

@SolAstrius

Copy link
Copy Markdown
Contributor

Summary

Makes the fused-multiply-add family honor the rounding mode. Two issues:

  1. The rounding mode was ignored unless it came from the dynamic frm CSR. fpu_fma rounds in the host mode (which only tracks frm), so a static rm field on an FMA (fmadd.s …,rtz etc.) was computed in whatever frm left set, and RMM had no host mode at all. A small wrapper (riscv_fma_round_f32/64, mirroring the OP-FP dispatch in riscv_fpu: correct RMM rounding, honor static rounding modes, optimize FP dispatch #233) applies the effective mode around the op: static host-native modes are set/restored, RMM runs in RNE.
  2. fnmadd rounded the wrong value. It computed -(fma(a,b,c)) — rounding a*b+c and then negating, which flips the rounding direction under the directed modes. fnmadd = -(rs1*rs2) - rs3 must be fma(-a, b, -c) so the single rounding sees the correctly-signed result. (fmsub/fnmsub already negated operands, not the result — only fnmadd was wrong.)

Stacked on #236#233. Until those merge this shows their commits plus the one here. Independent in logic; based on #236 only because both touch the FMA handlers.

Validation

Out of scope (pre-existing, separate)

  • RMM exact ties in FMA (~10 tests): an exact FMA halfway case still rounds to-nearest-even, not away — the same ties-away gap shared with the rest of the FP path; the exact FMA residual needs an error-free FMA, deferred.
  • Spurious UF on a few near-underflow FMAs (0x03 vs 0x01): an FMA exception-flag bug, not a rounding bug.

Signed-off-by: Sol Astrius Phoenix <sol@astrius.ink>
Signed-off-by: Sol Astrius Phoenix <sol@astrius.ink>
Signed-off-by: Sol Astrius Phoenix <sol@astrius.ink>
Signed-off-by: Sol Astrius Phoenix <sol@astrius.ink>
Signed-off-by: Sol Astrius Phoenix <sol@astrius.ink>
@SolAstrius

Copy link
Copy Markdown
Contributor Author

Closing in favor of a single consolidated patch, per the maintainer's request to submit this as one patch rather than a stacked-PR series. The combined FPU work (RMM rounding, static rounding modes, NaN canonicalization, FMA rounding/ties/underflow, fmul ties, fcvt-to-int flags) now passes the riscv-arch-test F/D/I/M suites 260/260. Rebased onto current staging (which already includes #219).

@SolAstrius SolAstrius closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant