Add methods to manipulate Half in binary#40882
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
Tagging subscribers to this area: @tannergooding, @pgovind |
src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs
Outdated
Show resolved
Hide resolved
|
Merging master fixes half write tests. The new failing tests looks like unrelated. |
|
I'm OK with 6.0. This should be only a subset of operations for |
|
@GrabYourPitchforks, @adamsitnik, could you give this a second review since you own System.Memory? |
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM, I've left some comments, but mostly nits
src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/ReaderBigEndian.cs
Show resolved
Hide resolved
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static unsafe short HalfToInt16Bits(Half value) | ||
| public static unsafe short HalfToInt16Bits(Half value) |
There was a problem hiding this comment.
not related to your changes: I would be really surprised if this method was not inlined without [MethodImpl(MethodImplOptions.AggressiveInlining)] applied
src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/ReaderLittleEndian.cs
Show resolved
Hide resolved
| return !BitConverter.IsLittleEndian ? | ||
| BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read<short>(source))) : | ||
| MemoryMarshal.Read<Half>(source); |
There was a problem hiding this comment.
pedantic nit: removing extra ! can simplify the code?
| return !BitConverter.IsLittleEndian ? | |
| BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read<short>(source))) : | |
| MemoryMarshal.Read<Half>(source); | |
| return BitConverter.IsLittleEndian ? | |
| MemoryMarshal.Read<Half>(source) : | |
| BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read<short>(source))); |
There was a problem hiding this comment.
I just copied the code body from double overload and changed the types. They should be consistent after this PR right?
| if (BitConverter.IsLittleEndian) | ||
| { | ||
| short tmp = ReverseEndianness(BitConverter.HalfToInt16Bits(value)); | ||
| MemoryMarshal.Write(destination, ref tmp); | ||
| } | ||
| else | ||
| { | ||
| MemoryMarshal.Write(destination, ref value); | ||
| } |
There was a problem hiding this comment.
very-very pedantic nit: you are sometimes using else block, sometimes not`
if (condition)
{
// do one thing
// return sth;
}
// return sth elseif (condition)
{
// do one thing
// return sth;
}
else
{
// return sth else
}it would be good to stick to one pattern and follow it in the entire PR
| _buffer[1] = (byte)(TmpValue >> 8); | ||
| _buffer[2] = (byte)(TmpValue >> 16); | ||
| _buffer[3] = (byte)(TmpValue >> 24); | ||
| BinaryPrimitives.WriteSingleLittleEndian(_buffer, value); |
There was a problem hiding this comment.
since you have changed it for single overload, should we do the same for int, uint, short, ushort and other methods that don't use BinaryPrimitives? (just for consistency)
runtime/src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs
Lines 242 to 283 in 88cf0ed
There was a problem hiding this comment.
This was explored in #37582 (comment) . Using the Span-based helpers for <8 byte types is not profitable (converting array to Span is cheap, but it is not free).
There was a problem hiding this comment.
Using the Span-based helpers for <8 byte types is not profitable
@jkotas thanks for another valuable comment! Does it mean that the single change should be reverted and the new Write(Half value) method should not be using BinaryPrimitives as well?
There was a problem hiding this comment.
I would think so, unless we have data that suggest otherwise.
|
huoyaoyuan Thank you! |
Closes #38456 . Closes #38288 .