Skip to content

fpu: canonicalize NaN results of fadd/fmul/fdiv and fused ops - #259

Open
carlosqwqqwq wants to merge 1 commit into
LekKit:stagingfrom
carlosqwqqwq:fix/fpu-nan-canonicalize
Open

fpu: canonicalize NaN results of fadd/fmul/fdiv and fused ops#259
carlosqwqqwq wants to merge 1 commit into
LekKit:stagingfrom
carlosqwqqwq:fix/fpu-nan-canonicalize

Conversation

@carlosqwqqwq

@carlosqwqqwq carlosqwqqwq commented Aug 11, 2026

Copy link
Copy Markdown

Summary

The FP writeback for fadd, fmul, fdiv and the fused fmadd/fmsub/fnmsub/fnmadd family stores the raw host result, which keeps non-canonical NaN payloads. The fsub/fmin/fmax paths already canonicalize through the write helpers; the RISC-V specification requires the canonical NaN bit pattern for arithmetic results. Route the remaining arithmetic writebacks through the same canonicalizing writers.

Signaling-NaN NV flagging is unchanged.

Validation

  • qNaN-payload, inf*0+1 and 0/0 probes match native RISC-V hardware and QEMU after the fix, with the JIT enabled and disabled.
  • Every arithmetic result is the canonical NaN and fflags match the references.

Fixes #254

@LekKit

LekKit commented Aug 11, 2026

Copy link
Copy Markdown
Owner

@SolAstrius please confirm via your out-of-tree test suite and review if possible, thank you

@carlosqwqqwq

Copy link
Copy Markdown
Author

I reran the fix at head 35fdcb4 with an out-of-tree RV64 probe, built independently from the PR source tree, under the Docker x86_64 lab. The probe output was:

fadd=0xffffffff7fc00000
fsub=0xffffffff7fc00000
fmul=0xffffffff7fc00000
fdiv=0xffffffff7fc00000

Thus the affected fadd/fmul/fdiv writebacks now match the canonical NaN reference. fsqrt is intentionally outside this PR (tracked separately as D-006). The PR head also passes the full GitHub Actions build/CodeQL/analysis checks. @SolAstrius, please confirm with the out-of-tree suite when available.

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.

fadd/fmul/fdiv leak qNaN payloads instead of returning canonical NaNs

2 participants