Skip to content
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 Half comparison #39773

Merged
merged 3 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private ushort Significand
// says they should be equal, even if the signs differ.
return leftIsNegative && !AreZero(left, right);
}
return (short)(left._value) < (short)(right._value);
return left._value < right._value ^ leftIsNegative;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it's pretty much the same.

}

public static bool operator >(Half left, Half right)
Expand All @@ -141,7 +141,7 @@ private ushort Significand
// says they should be equal, even if the signs differ.
return leftIsNegative || AreZero(left, right);
}
return (short)(left._value) <= (short)(right._value);
return left._value <= right._value ^ leftIsNegative;
Gnbrkm41 marked this conversation as resolved.
Show resolved Hide resolved
}

public static bool operator >=(Half left, Half right)
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Runtime/tests/System/HalfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ public static IEnumerable<object[]> CompareTo_TestData()
yield return new object[] { Half.NaN, Half.NaN, 0 };
yield return new object[] { Half.NaN, UInt16BitsToHalf(0x0000), -1 };
yield return new object[] { Half.MaxValue, null, 1 };
yield return new object[] { Half.MinValue, Half.NegativeInfinity, 1 };
Gnbrkm41 marked this conversation as resolved.
Show resolved Hide resolved
yield return new object[] { Half.NegativeInfinity, Half.MinValue, -1 };
yield return new object[] { UInt16BitsToHalf(0x8000), Half.NegativeInfinity, 1 }; // Negative zero
yield return new object[] { Half.NegativeInfinity, UInt16BitsToHalf(0x8000), -1 }; // Negative zero
}

[Theory]
Expand Down