Skip to content

Fix finite-range checks and rounding carries in HexFloat::castTo - #6902

Open
davidegrohmann wants to merge 1 commit into
KhronosGroup:mainfrom
davidegrohmann:fix-hex-float-finite-range-rounding
Open

davidegrohmann wants to merge 1 commit into
KhronosGroup:mainfrom
davidegrohmann:fix-hex-float-finite-range-rounding

Conversation

@davidegrohmann

Copy link
Copy Markdown
Contributor

The exponent bias is not always the largest finite exponent. E4M3 and the finite-only FP4/FP6 formats use the all-ones exponent for finite values. Comparing against the bias incorrectly treats these values as overflow; for example, parsing E4M3 decimal 256 produces 448.

Check the rounded exponent and significand against the destination's largest finite value. Include the significand to prevent rounding into E4M3's reserved NaN encoding, and propagate the rounding carry when constructing the result.

Handle source infinity separately from conversion overflow. Use has_infinity to select infinity or the largest finite result.

Add exhaustive finite-value conversions for FP4, FP6 and FP8 across both signs and all rounding modes, plus E4M3 rounding-boundary and decimal-parsing regressions. Cover overflow and infinity saturation for FP4, FP6 and E4M3 across both signs and all rounding modes.

@davidegrohmann
davidegrohmann force-pushed the fix-hex-float-finite-range-rounding branch from da45563 to 8d69de7 Compare September 23, 2026 13:32
@kpet
kpet self-requested a review September 23, 2026 15:19

@kpet kpet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for making all the changes I requested offline. This is now much easier to read. I can't fault the change after another round of review but other pairs of eyes are most welcome to try.

@s-perron
s-perron self-requested a review September 29, 2026 01:28

@s-perron s-perron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The code looks good. My only concern is maintaining the tests. Some tests will be testing many things. Because you use loops to generate the different subtest, it might be hard to figure out which subtest failed from the error message generated by GTEST. Please try to separate it out is some reasonable way. You can have parameterized tests. See HexFloatFP32ToE5M2Tests in the same test file.

Comment thread test/hex_float_test.cpp Outdated
@davidegrohmann
davidegrohmann force-pushed the fix-hex-float-finite-range-rounding branch from 8d69de7 to 8a8d461 Compare October 2, 2026 11:01
@davidegrohmann

Copy link
Copy Markdown
Contributor Author

The code looks good. My only concern is maintaining the tests. Some tests will be testing many things. Because you use loops to generate the different subtest, it might be hard to figure out which subtest failed from the error message generated by GTEST. Please try to separate it out is some reasonable way. You can have parameterized tests. See HexFloatFP32ToE5M2Tests in the same test file.

Thanks for the review.
I have refactored the tests as asked and I have improved the error messages so the failures should be very clear.
Please let me know if this now resolves all your concerns.

@s-perron
s-perron enabled auto-merge (squash) October 2, 2026 12:22
The exponent bias is not always the largest finite exponent. E4M3 and
the finite-only FP4/FP6 formats use the all-ones exponent for finite
values. Comparing against the bias incorrectly treats these values as
overflow; for example, parsing E4M3 decimal 256 produces 448.

Check the rounded exponent and significand against the destination's
largest finite value. Include the significand to prevent rounding into
E4M3's reserved NaN encoding, and propagate the rounding carry when
constructing the result.

Handle source infinity separately from conversion overflow. Use
has_infinity to select infinity or the largest finite result.

Add exhaustive finite-value conversions for FP4, FP6 and FP8 across
both signs and all rounding modes, plus E4M3 rounding-boundary and
decimal-parsing regressions. Cover overflow and infinity saturation
for FP4, FP6 and E4M3 across both signs and all rounding modes.

Signed-off-by: Davide Grohmann <davide.grohmann@arm.com>
auto-merge was automatically disabled October 2, 2026 13:37

Head branch was pushed to by a user without write access

@davidegrohmann
davidegrohmann force-pushed the fix-hex-float-finite-range-rounding branch from 8a8d461 to 5ffe6d1 Compare October 2, 2026 13:37
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.

4 participants