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

Code clean up around TryWriteBigEndian/TryWriteLittleEndian #110897

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 8 additions & 18 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,37 +1281,27 @@ static bool IBinaryInteger<char>.TryReadLittleEndian(ReadOnlySpan<byte> source,
/// <inheritdoc cref="IBinaryInteger{TSelf}.TryWriteBigEndian(Span{byte}, out int)" />
bool IBinaryInteger<char>.TryWriteBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(char))
if (BinaryPrimitives.TryWriteUInt16BigEndian(destination, m_value))
{
ushort value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);

bytesWritten = sizeof(char);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IBinaryInteger{TSelf}.TryWriteLittleEndian(Span{byte}, out int)" />
bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(char))
if (BinaryPrimitives.TryWriteUInt16LittleEndian(destination, m_value))
{
ushort value = BitConverter.IsLittleEndian ? m_value : BinaryPrimitives.ReverseEndianness(m_value);
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);

bytesWritten = sizeof(char);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

//
Expand Down
24 changes: 8 additions & 16 deletions src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,35 +1146,27 @@ bool IFloatingPoint<decimal>.TryWriteExponentBigEndian(Span<byte> destination, o
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

destination[0] = (byte)Exponent;
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<decimal>.TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

destination[0] = (byte)Exponent;
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandBigEndian(Span{byte}, out int)" />
Expand Down
76 changes: 16 additions & 60 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,97 +725,53 @@ int IFloatingPoint<double>.GetExponentShortestBitLength()
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteExponentBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(short))
if (BinaryPrimitives.TryWriteInt16BigEndian(destination, Exponent))
{
short exponent = Exponent;

if (BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

bytesWritten = sizeof(short);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(short))
if (BinaryPrimitives.TryWriteInt16LittleEndian(destination, Exponent))
{
short exponent = Exponent;

if (!BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

bytesWritten = sizeof(short);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteSignificandBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ulong))
if (BinaryPrimitives.TryWriteUInt64BigEndian(destination, Significand))
{
ulong significand = Significand;

if (BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ulong);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ulong))
if (BinaryPrimitives.TryWriteUInt64LittleEndian(destination, Significand))
{
ulong significand = Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ulong);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

//
Expand Down
62 changes: 16 additions & 46 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,83 +1369,53 @@ bool IFloatingPoint<Half>.TryWriteExponentBigEndian(Span<byte> destination, out
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

destination[0] = (byte)Exponent;
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<Half>.TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

destination[0] = (byte)Exponent;
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<Half>.TryWriteSignificandBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ushort))
if (BinaryPrimitives.TryWriteUInt16BigEndian(destination, Significand))
{
ushort significand = Significand;

if (BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ushort);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<Half>.TryWriteSignificandLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ushort))
if (BinaryPrimitives.TryWriteUInt16LittleEndian(destination, Significand))
{
ushort significand = Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ushort);
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

//
Expand Down
48 changes: 8 additions & 40 deletions src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -940,59 +940,27 @@ int IBinaryInteger<Int128>.GetShortestBitLength()
/// <inheritdoc cref="IBinaryInteger{TSelf}.TryWriteBigEndian(Span{byte}, out int)" />
bool IBinaryInteger<Int128>.TryWriteBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= Size)
if (BinaryPrimitives.TryWriteInt128BigEndian(destination, this))
{
ulong lower = _lower;
ulong upper = _upper;

if (BitConverter.IsLittleEndian)
{
lower = BinaryPrimitives.ReverseEndianness(lower);
upper = BinaryPrimitives.ReverseEndianness(upper);
}

ref byte address = ref MemoryMarshal.GetReference(destination);

Unsafe.WriteUnaligned(ref address, upper);
Unsafe.WriteUnaligned(ref Unsafe.AddByteOffset(ref address, sizeof(ulong)), lower);

bytesWritten = Size;
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

/// <inheritdoc cref="IBinaryInteger{TSelf}.TryWriteLittleEndian(Span{byte}, out int)" />
bool IBinaryInteger<Int128>.TryWriteLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= Size)
if (BinaryPrimitives.TryWriteInt128LittleEndian(destination, this))
{
ulong lower = _lower;
ulong upper = _upper;

if (!BitConverter.IsLittleEndian)
{
lower = BinaryPrimitives.ReverseEndianness(lower);
upper = BinaryPrimitives.ReverseEndianness(upper);
}

ref byte address = ref MemoryMarshal.GetReference(destination);

Unsafe.WriteUnaligned(ref address, lower);
Unsafe.WriteUnaligned(ref Unsafe.AddByteOffset(ref address, sizeof(ulong)), upper);

bytesWritten = Size;
return true;
}
else
{
bytesWritten = 0;
return false;
}

bytesWritten = 0;
return false;
}

//
Expand Down
Loading
Loading