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

Consider adding Half support to the BinaryPrimitives class #38456

Closed
eiriktsarpalis opened this issue Jun 26, 2020 · 9 comments · Fixed by #40882
Closed

Consider adding Half support to the BinaryPrimitives class #38456

eiriktsarpalis opened this issue Jun 26, 2020 · 9 comments · Fixed by #40882
Labels
api-approved API was approved in API review, it can be implemented area-System.Numerics help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@eiriktsarpalis
Copy link
Member

Background and Motivation

This came up while working to add Half support to the CBOR components. While not blocking to my work, these methods would be nice to have for the sake of completeness.

Proposed API

public static class BinaryPrimitives
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static Half ReadHalfLittleEndian(ReadOnlySpan<byte> source);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static Half ReadHalfBigEndian(ReadOnlySpan<byte> source);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool TryReadHalfLittleEndian(ReadOnlySpan<byte> source, out Half);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool TryReadHalfBigEndian(ReadOnlySpan<byte> source, out Half);

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void WriteHalfLittleEndian(Span<byte> destination, Half value);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void WriteHalfBigEndian(Span<byte> destination, Half value);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool TryWriteHalfLittleEndian(Span<byte> destination, Half value);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool TryWriteHalfBigEndian(Span<byte> destination, Half value);
}
@eiriktsarpalis eiriktsarpalis added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics labels Jun 26, 2020
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Jun 26, 2020
@ghost
Copy link

ghost commented Jun 26, 2020

Tagging subscribers to this area: @tannergooding
Notify danmosemsft if you want to be subscribed.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jun 26, 2020
@GrabYourPitchforks
Copy link
Member

Saw that you also mentioned this in #38288. Definitely should do both at the same time. :)

@tannergooding tannergooding added api-ready-for-review and removed untriaged New issue has not been triaged by the area owner api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jun 26, 2020
@carlossanlop carlossanlop added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-ready-for-review labels Jul 6, 2020
@GrabYourPitchforks
Copy link
Member

GrabYourPitchforks commented Jul 12, 2020

Can we add these to BinaryReader and BinaryWriter as well? They're useful for throwing data across the wire.

namespace System.IO
{
    public class BinaryReader
    {
        // NEW virtual method on existing type
        // We'll give it a viable default implementation, just like the existing virtual Read* methods
        public virtual Half ReadHalf();
    }

    public class BinaryWriter
    {
        // NEW virtual method on existing type
        // We'll give it a viable default implementation, just like the existing virtual Write overloads
        public virtual void Write(Half value);
    }
}

@terrajobst terrajobst modified the milestones: Future, 5.0.0 Jul 23, 2020
@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 24, 2020
@terrajobst
Copy link
Member

terrajobst commented Jul 24, 2020

Video

  • Looks good
  • But we should also do BinaryReader and BinaryWriter as they are the more high-level counterparts
  • @tannergooding, please scout the framework for support of float and double and see what makes sense for Half
  • We should consider Convert, but the interface dispatch due to lack of type code might be challenging.
namespace System.Buffers.Binary
{
    public static class BinaryPrimitives
    {
        public static Half ReadHalfLittleEndian(ReadOnlySpan<byte> source);
        public static Half ReadHalfBigEndian(ReadOnlySpan<byte> source);
        public static bool TryReadHalfLittleEndian(ReadOnlySpan<byte> source, out Half);
        public static bool TryReadHalfBigEndian(ReadOnlySpan<byte> source, out Half);

        public static void WriteHalfLittleEndian(Span<byte> destination, Half value);
        public static void WriteHalfBigEndian(Span<byte> destination, Half value);
        public static bool TryWriteHalfLittleEndian(Span<byte> destination, Half value);
        public static bool TryWriteHalfBigEndian(Span<byte> destination, Half value);
    }
}
namespace System.IO
{
    public partial class BinaryReader
    {
        public virtual Half ReadHalf();
    }
    public partial class BinaryWriter
    {
        public virtual Half Write(Half value);
    }
}

@akoeplinger
Copy link
Member

Curious why we need separate *LittleEndian and *BigEndian methods? Is it because I might want to read something in big-endian encoding even though I'm on a little-endian machine so using BitConverter.IsLittleEndian internally wouldn't make sense?

@tannergooding
Copy link
Member

Right, many file formats (for example) explicitly call out themselves as being big or little endian. These methods allow you to do the right thing regardless of what machine you are on.

@jeffhandley
Copy link
Member

Moved to 6.0.0 per discussion on #40882. Thanks, @huoyaoyuan.

@tannergooding tannergooding added the help wanted [up-for-grabs] Good issue for external contributors label Aug 27, 2020
@xPaw
Copy link

xPaw commented Sep 24, 2020

Would you reconsider putting this into 5.0 release? Putting this into 6.0 release makes this new type harder to use imo.

I wanted to switch to using native half type, but the missing methods on BinaryReader make this a bit harder.

@jkotas
Copy link
Member

jkotas commented Sep 24, 2020

.NET 5 API surface is done. We are not able to add this to 5.0 release. If you need the method on BinaryReader for your project, you can add it as internal extension method for now. It should be like ~20 lines.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Numerics help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants