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

[CBOR] Use System.Half for encoding and decoding half-precision data items #38466

Merged
merged 4 commits into from
Jun 29, 2020

Conversation

eiriktsarpalis
Copy link
Member

  • Replaces the bespoke half-precision decoding implementation with System.Half.
  • For non CTAP2 conformance modes, support lowering precision of encoded floats.
  • Removes #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.

@ghost
Copy link

ghost commented Jun 26, 2020

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq
Notify danmosemsft if you want to be subscribed.

// Remove class once https://github.com/dotnet/runtime/issues/38288 has been addressed
internal static class HalfHelpers
{
public const int SizeOfHalf = sizeof(short);
Copy link
Member

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?

Copy link
Member Author

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);
Copy link
Member

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?

Copy link
Member Author

@eiriktsarpalis eiriktsarpalis Jun 29, 2020

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?

@eiriktsarpalis eiriktsarpalis merged commit 3d32abd into dotnet:master Jun 29, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@eiriktsarpalis eiriktsarpalis deleted the cbor-half-support branch June 2, 2021 20:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants