-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix conversions from subnormal System.Half values #46536
Conversation
Tagging subscribers to this area: @tannergooding, @pgovind |
@@ -683,10 +683,10 @@ private static double CreateDoubleNaN(bool sign, ulong significand) | |||
} | |||
|
|||
private static float CreateSingle(bool sign, byte exp, uint sig) | |||
=> BitConverter.Int32BitsToSingle((int)(((sign ? 1U : 0U) << float.SignShift) | ((uint)exp << float.ExponentShift) | sig)); | |||
=> BitConverter.Int32BitsToSingle((int)(((sign ? 1U : 0U) << float.SignShift) + ((uint)exp << float.ExponentShift) + sig)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue was that this was doing a bitwise or when it should have been using addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original implementation was doing the correct thing: https://github.com/ucb-bar/berkeley-softfloat-3/blob/master/source/include/internals.h#L107
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yup, my bad. LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should submit this for 5.0 servicing approval. In the servicing template on the backport PR, please include notes for which boundary conditions we were previously testing and which ones we missed.
We will need to backport the test updates too (first), which can be done without approval.
I've just uncovered another corner case where .NET's |
I made the same mistake when I adapted http://half.sourceforge.net/ for my own use. I thought the |
Nope, they still disagree for the |
I've found the issue and a local fix, will push it up with a commit momentarily |
@@ -635,6 +635,7 @@ private static ushort RoundPackToHalf(bool sign, short exp, ushort sig) | |||
{ | |||
sig = (ushort)ShiftRightJam(sig, -exp); | |||
exp = 0; | |||
roundBits = sig & 0xF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other issue was that this was missing adjusting the roundBits based on the updated sig.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original implementation was doing the correct thing: https://github.com/ucb-bar/berkeley-softfloat-3/blob/b64af41c3276f97f0e181920400ee056b9c88037/source/s_roundPackToF16.c#L77
I imagine this was accidentally dropped when eliding the unnecessary exception handling logic.
I think the Sourceforge implementation was correct for the MinSubnormal/2 case. Ties round to even, so Sorry if this all sounds elementary, but I've never needed to deal with the gory inner workings of IEEE floats before. 😄 |
Feel free to borrow (and verify) some of my test vectors from here while you're at it: https://gist.github.com/ecorm/3b5963749752f2cc2f96fb493d29fcba No attribution necessary (or wanted). |
6ee4b0c
to
a5984ea
Compare
/backport to release/5.0 |
Started backporting to release/5.0: https://github.com/dotnet/runtime/actions/runs/466823164 |
@tannergooding backporting to release/5.0 failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Fix conversions from subnormal System.Half values
error: sha1 information is lacking or useless (src/libraries/System.Private.CoreLib/src/System/Half.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fix conversions from subnormal System.Half values
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
* Fix conversions from subnormal System.Half values * Fix conversions from subnormal System.Single/Double to System.Half values * Fixing the HalfTests parameters
…to .NET 5.0 (#46641) * Half conversion test cases for subnormal corner cases (#46523) * Half conversion test cases for subnormal corner cases * Single/Double->Half rounding corner test cases * Single/Double->Half conversion subnormal rounding corner test cases * Fix conversions from subnormal System.Half values (#46536) * Fix conversions from subnormal System.Half values * Fix conversions from subnormal System.Single/Double to System.Half values * Fixing the HalfTests parameters Co-authored-by: Emile Cormier <ecorm@users.noreply.github.com>
This resolves #46515.
CC. @pgovind, @ecorm