This is an archive of the discontinued LLVM Phabricator instance.

[APFloat] Fix fusedMultiplyAdd when `this` equals to `Addend`
ClosedPublic

Authored by ekatz on Nov 19 2019, 12:17 AM.

Details

Summary

The arguments to fusedMultiplyAdd are passed by reference. We must save the Addend value on the beginning of the function, before we modify this, as they may be the same reference.

Fix PR44051.

Diff Detail

Event Timeline

ekatz created this revision.Nov 19 2019, 12:17 AM
hfinkel accepted this revision.Nov 19 2019, 7:06 AM

LGTM

This revision is now accepted and ready to land.Nov 19 2019, 7:06 AM
foad added a subscriber: foad.Nov 19 2019, 1:19 PM

Typo in commit message: "this this"

llvm/lib/Support/APFloat.cpp
1003

Marginally more efficient to omit the , 0 here, to use the one-argument constructor.

Since we are unconditionally constructing the addend here anyway, would it make more sense to force all callers to pass in an addend (using zero if they're not interested in it)?

1039

Is it safe to skip this code when the addend is zero? If the result of the multiply is also zero, adding zero could affect the sign of the result, couldn't it?

ekatz marked 2 inline comments as done.Nov 20 2019, 2:01 PM
ekatz added inline comments.
llvm/lib/Support/APFloat.cpp
1003

Correct. I'll change the constructor to the single parameter version.

Regarding the construction of the addend, that is a good idea.
I suggest making addend passed by value, with default to zero (keep in mind that this function is private, so there is no API change).
What do you think?

1039

Good observation, but adding/subtracting a negative zero, doesn't affect the sign (it is considered the same as positive zero).

ekatz edited the summary of this revision. (Show Details)Nov 20 2019, 2:02 PM
foad added inline comments.Nov 20 2019, 2:16 PM
llvm/lib/Support/APFloat.cpp
1003

Sounds great if you can make it work. I wasn't sure exactly how to implement the "default to zero" part.

1039

A couple of cases where adding zero changes the sign, accoerding to the comment at the end of IEEEFloat::addOrSubtract:
-0 + +0 = +0 (except if rounding to -inf)
+0 + -0 = -0 (if rounding to -inf)
(I realise this code is pre-existing, so no need to address it as part of the current patch.)

ekatz marked an inline comment as done.Nov 22 2019, 11:30 AM
ekatz added inline comments.
llvm/lib/Support/APFloat.cpp
1039

Good catch! Those corner cases never end...
I'll open a new bug for it.

ekatz updated this revision to Diff 230699.EditedNov 22 2019, 11:32 AM

Pass added parameter by value (instead of - by ref), and have a default value of zero.

ekatz added a comment.Dec 2 2019, 9:42 AM

Note that this is a safe overload, as multiplySignificand is private.

This revision was automatically updated to reflect the committed changes.