diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs index 7095ca0a3f10d8..9cb4fb9e09da08 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs @@ -116,11 +116,11 @@ internal sbyte Exponent } } - internal ushort Significand + internal byte Significand { get { - return (ushort)(TrailingSignificand | ((BiasedExponent != 0) ? (1U << BiasedExponentShift) : 0U)); + return (byte)(TrailingSignificand | ((BiasedExponent != 0) ? (1U << BiasedExponentShift) : 0U)); } } @@ -1010,7 +1010,7 @@ int IFloatingPoint.GetExponentShortestBitLength() } /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(ushort); + int IFloatingPoint.GetSignificandByteCount() => sizeof(byte); /// int IFloatingPoint.GetSignificandBitLength() => SignificandLength; @@ -1046,9 +1046,10 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destinatio /// bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { - if (BinaryPrimitives.TryWriteUInt16BigEndian(destination, Significand)) + if (destination.Length >= sizeof(byte)) { - bytesWritten = sizeof(uint); + destination[0] = Significand; + bytesWritten = sizeof(byte); return true; } @@ -1059,9 +1060,10 @@ bool IFloatingPoint.TryWriteSignificandBigEndian(Span destinatio /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { - if (BinaryPrimitives.TryWriteUInt16LittleEndian(destination, Significand)) + if (destination.Length >= sizeof(byte)) { - bytesWritten = sizeof(uint); + destination[0] = Significand; + bytesWritten = sizeof(byte); return true; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs index 041dccf8528618..c4e3e40c3adce7 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs @@ -2271,6 +2271,66 @@ public static void RadiansToDegreesTest(BFloat16 value, BFloat16 expectedResult, AssertEqual(+expectedResult, BFloat16.RadiansToDegrees(+value), allowedVariance); } + public static IEnumerable TryWriteSignificandBigEndianTest_TestData() => + [ + [BFloat16.NegativeInfinity, 1, new byte[] { 0x80 }], + [BFloat16.MinValue, 1, new byte[] { 0xFF }], + [(BFloat16)(-1.0f), 1, new byte[] { 0x80 }], + [-BFloat16.Epsilon, 1, new byte[] { 0x01 }], + [BFloat16.NaN, 1, new byte[] { 0xC0 }], + [(BFloat16)0.0f, 1, new byte[] { 0x00 }], + [(BFloat16)1.0f, 1, new byte[] { 0x80 }], + [BFloat16.MaxValue, 1, new byte[] { 0xFF }], + [BFloat16.PositiveInfinity, 1, new byte[] { 0x80 }], + ]; + + [Theory] + [MemberData(nameof(TryWriteSignificandBigEndianTest_TestData))] + public static void TryWriteSignificandBigEndianTest(BFloat16 value, int expectedBytesWritten, byte[] expectedBytes) + { + Span destination = [0]; + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(value, destination, out int bytesWritten)); + Assert.Equal(expectedBytesWritten, bytesWritten); + Assert.Equal(expectedBytes, destination.ToArray()); + } + + [Fact] + public static void TryWriteSignificandBigEndianTest_EmptyDestination() + { + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out int bytesWritten)); + Assert.Equal(0, bytesWritten); + } + + public static IEnumerable TryWriteSignificandLittleEndianTest_TestData() => + [ + [BFloat16.NegativeInfinity, 1, new byte[] { 0x80 }], + [BFloat16.MinValue, 1, new byte[] { 0xFF }], + [(BFloat16)(-1.0f), 1, new byte[] { 0x80 }], + [-BFloat16.Epsilon, 1, new byte[] { 0x01 }], + [BFloat16.NaN, 1, new byte[] { 0xC0 }], + [(BFloat16)0.0f, 1, new byte[] { 0x00 }], + [(BFloat16)1.0f, 1, new byte[] { 0x80 }], + [BFloat16.MaxValue, 1, new byte[] { 0xFF }], + [BFloat16.PositiveInfinity, 1, new byte[] { 0x80 }], + ]; + + [Theory] + [MemberData(nameof(TryWriteSignificandLittleEndianTest_TestData))] + public static void TryWriteSignificandLittleEndianTest(BFloat16 value, int expectedBytesWritten, byte[] expectedBytes) + { + Span destination = [0]; + Assert.True(FloatingPointHelper.TryWriteSignificandLittleEndian(value, destination, out int bytesWritten)); + Assert.Equal(expectedBytesWritten, bytesWritten); + Assert.Equal(expectedBytes, destination.ToArray()); + } + + [Fact] + public static void TryWriteSignificandLittleEndianTest_EmptyDestination() + { + Assert.False(FloatingPointHelper.TryWriteSignificandLittleEndian(default, Span.Empty, out int bytesWritten)); + Assert.Equal(0, bytesWritten); + } + #region AssertExtentions static bool IsNegativeZero(BFloat16 value) {