This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add isCommutable to FADD/FMUL/FMIN/FMAX/FEQ.
ClosedPublic

Authored by craig.topper on Apr 18 2022, 6:49 PM.

Diff Detail

Event Timeline

craig.topper created this revision.Apr 18 2022, 6:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 18 2022, 6:49 PM
craig.topper requested review of this revision.Apr 18 2022, 6:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 18 2022, 6:49 PM

Add Commutable to FEQ.H.
Add some tests.

reames added a subscriber: reames.Apr 26 2022, 12:08 PM

LGTM, though a second read from someone familiar with floating point is probably warranted.

fadd and fmul are clearly okay.

Reading the RISCV specification, the FEQ case appears okay. The FMIN and FMAX cases give me the most pause, but the description seems to be commutative.

craig.topper edited the summary of this revision. (Show Details)Apr 29 2022, 1:53 PM
craig.topper added reviewers: kito-cheng, khchen, arcbbb.
arcbbb accepted this revision.May 2 2022, 7:31 PM

As mentioned in spec, For the purposes of these instructions only, the value −0.0 is considered to be less than the value +0.0
I think FMIN & FMAX is commutative.
LGTM.

This revision is now accepted and ready to land.May 2 2022, 7:31 PM

As mentioned in spec, For the purposes of these instructions only, the value −0.0 is considered to be less than the value +0.0
I think FMIN & FMAX is commutative.
LGTM.

For some additional detail.

In C++, std::max of 2 FP numbers is not commutable but std::fmax is commutable. std::max is implemented as something like a > b ? a : b which returns b when a or b is a nan since relative comparisons return false if either input is a nan. std::fmax on the other hand returns the non-nan input unless both inputs are nan.

RISCV's implementation of FMAX/FMIN is similar to std::fmax (though there is a difference for snan). X86 SSE on the other hand implement FP max/min instructions that are closer to std::max. For X86, we allowed max/min to be commuted under fast math using CodeGenOnly instructions.

This revision was landed with ongoing or failed builds.May 2 2022, 8:32 PM
This revision was automatically updated to reflect the committed changes.