-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[CBOR] Use System.Half for encoding and decoding half-precision data items #38466
[CBOR] Use System.Half for encoding and decoding half-precision data items #38466
Conversation
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs
Show resolved
Hide resolved
// Remove class once https://github.com/dotnet/runtime/issues/38288 has been addressed | ||
internal static class HalfHelpers | ||
{ | ||
public const int SizeOfHalf = sizeof(short); |
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.
Why not just use sizeof(Half)
inline where needed?
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.
Compiler complains that it doesn't have a predefined size, so would have to mark as unsafe. Presumably because it's a new type?
if (!CborConformanceModeHelpers.RequiresPreservingFloatPrecision(ConformanceMode) && | ||
FloatSerializationHelpers.TryConvertSingleToHalf(value, out Half half)) | ||
{ | ||
WriteHalf(half); |
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.
Why WriteHalf
vs WriteSingleCore
and WriteDoubleCore
?
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 Write[Float]Core
methods implement serialization for the particular precision without doing conformance mode checks and potential downsizing. WriteHalf
doesn't need downsizing since it's the smallest type. Perhaps calling it something like WriteSingleNoDownsizing
might reduce confusion?
#nullable
directives which are no longer required.The implementation also adds a reader and writer method for System.Half which are currently marked internal. I will follow up with an API proposal for these.