Skip to content

Fix the sign of an exact cancellation in bf16_mulAdd - #52

Closed
xpick-ed wants to merge 1 commit into
ucb-bar:masterfrom
xpick-ed:fix-bf16-muladd-cancel-sign
Closed

xpick-ed wants to merge 1 commit into
ucb-bar:masterfrom
xpick-ed:fix-bf16-muladd-cancel-sign

Conversation

@xpick-ed

Copy link
Copy Markdown

bf16_mulAdd forms the product exactly in binary64 and adds the addend in round-to-odd mode to avoid double rounding. When a nonzero product cancels the addend exactly, the round-to-odd sum is +0, so the result is +0 in every rounding mode. IEEE 754-2019 section 6.3 requires -0 under roundTowardNegative:

When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be +0 under all rounding-direction attributes except roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be -0.

The zero-product case already uses the caller's rounding mode. This change applies the same rule when a nonzero product cancels the addend.

Example: bf16_mulAdd(3.0, 3.0, -9.0) (0x4040, 0x4040, 0xc110) with softfloat_round_min returns 0x0000; it should return 0x8000.

Testing: I compared the old and new routine on 20 million random operand triples, half of them constructed so that c = -(a*b), in all five rounding modes. The only results that change are 209,990 exact cancellations under softfloat_round_min, from +0 to -0, with the same exception flags. Every other result is identical, and all 209,990 agree with the host's fmaf() under FE_DOWNWARD.

Developed with AI assistance; I reviewed the change, and the results above are from local runs.

bf16_mulAdd adds the exact binary64 product to the addend in round-to-odd
mode.  When a nonzero product cancels the addend exactly, that sum is +0
whatever the caller's rounding mode, but IEEE 754-2019 section 6.3 requires
-0 under roundTowardNegative.  Give the zero the sign of the caller's mode.

Signed-off-by: xpick-ed <109844902+xpick-ed@users.noreply.github.com>

@aswaterman aswaterman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed. However, I think we can also get rid of the if-statement that conditions whether we round to odd. I'll handle that part.

aswaterman added a commit that referenced this pull request Nov 25, 2026
We were getting it right for e.g. 1x0-0, but not e.g. 1x3-3.

Supersedes #52

Signed-off-by: Andrew Waterman <andrew@sifive.com>
aswaterman added a commit that referenced this pull request Nov 25, 2026
We were getting it right for e.g. 1x0-0, but not e.g. 1x3-3.

Supersedes #52

Signed-off-by: Andrew Waterman <andrew@sifive.com>
aswaterman added a commit that referenced this pull request Nov 25, 2026
We were getting it right for e.g. 1x0-0, but not e.g. 1x3-3.

Supersedes #52

Signed-off-by: Andrew Waterman <andrew@sifive.com>
@aswaterman

Copy link
Copy Markdown
Member

Closing in favor of #53. Thanks for raising the issue.

@aswaterman aswaterman closed this Nov 25, 2026
aswaterman added a commit that referenced this pull request Nov 25, 2026
We were getting it right for e.g. 1x0-0, but not e.g. 1x3-3.

Supersedes #52

Signed-off-by: Andrew Waterman <andrew@sifive.com>
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.

2 participants