Skip to content

arch-riscv: NaN-box finite fminm.s and fmaxm.s results - #3605

Merged
erin-le merged 1 commit into
gem5:developfrom
vera-rubin:fix/zfa-fminm-fmaxm-nan-box
Oct 17, 2026
Merged

erin-le merged 1 commit into
gem5:developfrom
vera-rubin:fix/zfa-fminm-fmaxm-nan-box

Conversation

@vera-rubin

@vera-rubin vera-rubin commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

With FLEN=64, the Zfa fminm.s and fmaxm.s instructions wrote finite results to the destination register without NaN-boxing them, so bits 63:32 were zero. A following single-precision instruction then read the register as the canonical NaN. For example, fadd.s of fminm.s(1.0, 2.0) and 2.0 returned NaN instead of 3.0.

This change boxes the selected value with freg(), as the NaN path of these instructions and the base fmin.s/fmax.s already do. The issue's reproducer and the base FMIN/FMAX controls pass with the fix on Atomic, Timing, Minor and O3.

Fixes #3599

— ChipLab

@powerjg

powerjg commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Maybe this is a bug, though I'm not totally clear since this is purely an AI agent. If a human wants to chime in...

That said, we don't want to commit a test program to gem5. Test programs go in gem5-resources, but generally we don't have tests in these cases.

@gem5-ai-review gem5-ai-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This is an automated review by gem5's AI reviewer. It is advisory only and does not replace review by a maintainer.

This fixes fminm.s/fmaxm.s (Zfa) writing finite results unboxed with FLEN=64, by routing the selected operand through freg() exactly as the NaN path and the base fmin.s/fmax.s already do. I read the surrounding decode block and the other Zfa single-precision writers (fround.s, froundnx.s, fli.s); they already box, so these two were the only gaps. The change is a two-line, ISA-semantic fix with no timing-model impact. I applied the lower bar (opt-in Zfa instructions) plus a normal check of the test plumbing, which follows the existing riscv-counter-csr layout.

I ran the new bare-metal binary on a build/ALL/gem5.opt built at the PR head with the riscv_baremetal.py config used by the registered test; it exited via semihosting ADP_Stopped_ApplicationExit at tick 19000 with no failure. I did not re-run it against the base to see it fail (that would need a full rebuild), so I'm relying on the author's before/after report for that.

Merge strategy (for the maintainer): Squash and merge (single commit; keep the arch-riscv,tests: tag and a header of 65 characters or fewer).

Takeaway: LGTM -- 0 blocking issues; 1 optional test-coverage nit noted inline.

Detailed feedback is anchored inline on the respective files.

Was this helpful? React with 👍 or 👎 so we can measure how useful these automated reviews are.

Comment on lines +43 to +51
fminm.s f2, f0, f1
fmv.x.d a0, f2
li a1, 0xffffffff3f800000
bne a0, a1, fail

fmaxm.s f2, f0, f1
fmv.x.d a0, f2
li a1, 0xffffffff40000000
bne a0, a1, fail

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: with f0=1.0, f1=2.0, fminm.s takes the fs1 arm of the new freg(less ? fs1 : fs2) and fmaxm.s takes the fs2 arm, so the other arm of each ternary (fs2 for fminm.s, fs1 for fmaxm.s) isn't executed. Both arms go through the same freg() wrapper, so this isn't a correctness concern. Adding the swapped-operand cases (fminm.s f2, f1, f0 and fmaxm.s f2, f1, f0, same expected values) would make the regression cover both arms for one extra bne each. Optional.

Was this helpful? React with 👍 or 👎 so we can measure how useful these automated reviews are.

With FLEN=64, a single-precision result must be NaN-boxed. The Zfa
`fminm.s` and `fmaxm.s` instructions wrote the selected binary32 value
to `Fd_bits` without boxing it, so bits 63:32 of the destination were
zero for finite results. A following single-precision instruction then
read that register as the canonical NaN.

Box the selected value with `freg()`, as the NaN path of these
instructions and `fmin.s`/`fmax.s` already do.
@vera-rubin
vera-rubin force-pushed the fix/zfa-fminm-fmaxm-nan-box branch from a7e7049 to 3b823ba Compare October 13, 2026 23:24
@vera-rubin vera-rubin changed the title arch-riscv,tests: NaN-box finite fminm.s and fmaxm.s results arch-riscv: NaN-box finite fminm.s and fmaxm.s results Oct 13, 2026
@vera-rubin

Copy link
Copy Markdown
Contributor Author

@powerjg Human here, it's my PR, been around the whole time. Yes it's a real bug, and I dropped the test program so it's just the two decoder lines now. Thanks for the quick review!

@erin-le erin-le linked an issue Oct 17, 2026 that may be closed by this pull request
@erin-le
erin-le merged commit b857ae2 into gem5:develop Oct 17, 2026
BobbyRBruce pushed a commit that referenced this pull request Oct 19, 2026
With FLEN=64, the Zfa `fminm.s` and `fmaxm.s` instructions wrote finite
results to the destination register without NaN-boxing them, so bits
63:32 were zero. A following single-precision instruction then read the
register as the canonical NaN. For example, `fadd.s` of `fminm.s(1.0,
2.0)` and 2.0 returned NaN instead of 3.0.

This change boxes the selected value with `freg()`, as the NaN path of
these instructions and the base `fmin.s`/`fmax.s` already do. The
issue's reproducer and the base FMIN/FMAX controls pass with the fix on
Atomic, Timing, Minor and O3.

Fixes #3599

— [ChipLab](https://chiplab.donmakino.com/findings#clab-2026-589)

(cherry picked from commit b857ae2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-riscv The RISC-V ISA tests gem5's Testing Infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RISC-V Zfa: finite fminm.s/fmaxm.s results are not NaN-boxed

3 participants