diff --git a/src/libraries/Common/tests/System/GenericMathHelpers.cs b/src/libraries/Common/tests/System/GenericMathHelpers.cs index 33868e5e26ffbf..fbeebc7b48a545 100644 --- a/src/libraries/Common/tests/System/GenericMathHelpers.cs +++ b/src/libraries/Common/tests/System/GenericMathHelpers.cs @@ -25,10 +25,6 @@ public static class BinaryIntegerHelper { public static (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) => TSelf.DivRem(left, right); - public static int GetByteCount(TSelf value) => value.GetByteCount(); - - public static long GetShortestBitLength(TSelf value) => value.GetShortestBitLength(); - public static TSelf LeadingZeroCount(TSelf value) => TSelf.LeadingZeroCount(value); public static TSelf PopCount(TSelf value) => TSelf.PopCount(value); @@ -39,6 +35,12 @@ public static class BinaryIntegerHelper public static TSelf TrailingZeroCount(TSelf value) => TSelf.TrailingZeroCount(value); + public static int GetByteCount(TSelf value) => value.GetByteCount(); + + public static int GetShortestBitLength(TSelf value) => value.GetShortestBitLength(); + + public static bool TryWriteBigEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteBigEndian(destination, out bytesWritten); + public static bool TryWriteLittleEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteLittleEndian(destination, out bytesWritten); } @@ -103,14 +105,18 @@ public static class FloatingPointHelper { public static int GetExponentByteCount(TSelf value) => value.GetExponentByteCount(); - public static long GetExponentShortestBitLength(TSelf value) => value.GetExponentShortestBitLength(); + public static int GetExponentShortestBitLength(TSelf value) => value.GetExponentShortestBitLength(); public static int GetSignificandByteCount(TSelf value) => value.GetSignificandByteCount(); - public static long GetSignificandBitLength(TSelf value) => value.GetSignificandBitLength(); + public static int GetSignificandBitLength(TSelf value) => value.GetSignificandBitLength(); + + public static bool TryWriteExponentBigEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteExponentBigEndian(destination, out bytesWritten); public static bool TryWriteExponentLittleEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteExponentLittleEndian(destination, out bytesWritten); + public static bool TryWriteSignificandBigEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteSignificandBigEndian(destination, out bytesWritten); + public static bool TryWriteSignificandLittleEndian(TSelf value, Span destination, out int bytesWritten) => value.TryWriteSignificandLittleEndian(destination, out bytesWritten); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Byte.cs b/src/libraries/System.Private.CoreLib/src/System/Byte.cs index c5a6d8f5c0113e..da2d42d6865610 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Byte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Byte.cs @@ -333,11 +333,29 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static byte TrailingZeroCount(byte value) => (byte)(BitOperations.TrailingZeroCount(value << 24) - 24); /// - long IBinaryInteger.GetShortestBitLength() => (sizeof(byte) * 8) - LeadingZeroCount(m_value); + int IBinaryInteger.GetShortestBitLength() => (sizeof(byte) * 8) - LeadingZeroCount(m_value); /// int IBinaryInteger.GetByteCount() => sizeof(byte); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(byte)) + { + byte value = m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(byte); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Char.cs b/src/libraries/System.Private.CoreLib/src/System/Char.cs index 59c73b82dcb520..e5fd2a45060e3f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Char.cs @@ -1166,29 +1166,47 @@ public static int ConvertToUtf32(string s, int index) // /// - public static (char Quotient, char Remainder) DivRem(char left, char right) => ((char, char))Math.DivRem(left, right); + static (char Quotient, char Remainder) IBinaryInteger.DivRem(char left, char right) => ((char, char))Math.DivRem(left, right); /// - public static char LeadingZeroCount(char value) => (char)(BitOperations.LeadingZeroCount(value) - 16); + static char IBinaryInteger.LeadingZeroCount(char value) => (char)(BitOperations.LeadingZeroCount(value) - 16); /// - public static char PopCount(char value) => (char)BitOperations.PopCount(value); + static char IBinaryInteger.PopCount(char value) => (char)BitOperations.PopCount(value); /// - public static char RotateLeft(char value, int rotateAmount) => (char)((value << (rotateAmount & 15)) | (value >> ((16 - rotateAmount) & 15))); + static char IBinaryInteger.RotateLeft(char value, int rotateAmount) => (char)((value << (rotateAmount & 15)) | (value >> ((16 - rotateAmount) & 15))); /// - public static char RotateRight(char value, int rotateAmount) => (char)((value >> (rotateAmount & 15)) | (value << ((16 - rotateAmount) & 15))); + static char IBinaryInteger.RotateRight(char value, int rotateAmount) => (char)((value >> (rotateAmount & 15)) | (value << ((16 - rotateAmount) & 15))); /// - public static char TrailingZeroCount(char value) => (char)(BitOperations.TrailingZeroCount(value << 16) - 16); + static char IBinaryInteger.TrailingZeroCount(char value) => (char)(BitOperations.TrailingZeroCount(value << 16) - 16); /// - long IBinaryInteger.GetShortestBitLength() => (sizeof(char) * 8) - LeadingZeroCount(m_value); + int IBinaryInteger.GetShortestBitLength() => (sizeof(char) * 8) - ushort.LeadingZeroCount(m_value); /// int IBinaryInteger.GetByteCount() => sizeof(char); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(char)) + { + 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; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { @@ -1212,10 +1230,10 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int b // /// - public static bool IsPow2(char value) => BitOperations.IsPow2((uint)value); + static bool IBinaryNumber.IsPow2(char value) => ushort.IsPow2(value); /// - public static char Log2(char value) => (char)BitOperations.Log2(value); + static char IBinaryNumber.Log2(char value) => (char)(ushort.Log2(value)); // // IBitwiseOperators @@ -1331,15 +1349,14 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int b static char INumber.Abs(char value) => value; /// - public static char Clamp(char value, char min, char max) => (char)Math.Clamp(value, min, max); + static char INumber.Clamp(char value, char min, char max) => (char)Math.Clamp(value, min, max); /// static char INumber.CopySign(char value, char sign) => value; /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static char CreateChecked(TOther value) - where TOther : INumber + static char INumber.CreateChecked(TOther value) { if (typeof(TOther) == typeof(byte)) { @@ -1406,8 +1423,7 @@ public static char CreateChecked(TOther value) /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static char CreateSaturating(TOther value) - where TOther : INumber + static char INumber.CreateSaturating(TOther value) { if (typeof(TOther) == typeof(byte)) { @@ -1491,8 +1507,7 @@ public static char CreateSaturating(TOther value) /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static char CreateTruncating(TOther value) - where TOther : INumber + static char INumber.CreateTruncating(TOther value) { if (typeof(TOther) == typeof(byte)) { @@ -1561,24 +1576,23 @@ public static char CreateTruncating(TOther value) static bool INumber.IsNegative(char value) => false; /// - public static char Max(char x, char y) => (char)Math.Max(x, y); + static char INumber.Max(char x, char y) => (char)Math.Max(x, y); /// - static char INumber.MaxMagnitude(char x, char y) => Max(x, y); + static char INumber.MaxMagnitude(char x, char y) => (char)Math.Max(x, y); /// - public static char Min(char x, char y) => (char)Math.Min(x, y); + static char INumber.Min(char x, char y) => (char)Math.Min(x, y); /// - static char INumber.MinMagnitude(char x, char y) => Min(x, y); + static char INumber.MinMagnitude(char x, char y) => (char)Math.Min(x, y); /// - public static int Sign(char value) => (value == 0) ? 0 : 1; + static int INumber.Sign(char value) => (value == 0) ? 0 : 1; /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool TryCreate(TOther value, out char result) - where TOther : INumber + static bool INumber.TryCreate(TOther value, out char result) { if (typeof(TOther) == typeof(byte)) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs index 0427d1842fa58d..12d1ded6f1b644 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs @@ -1150,15 +1150,39 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) // IFloatingPoint // + /// + int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// - long IFloatingPoint.GetExponentShortestBitLength() + int IFloatingPoint.GetExponentShortestBitLength() { sbyte exponent = Exponent; return (sizeof(sbyte) * 8) - sbyte.LeadingZeroCount(exponent); } - /// - int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// + int IFloatingPoint.GetSignificandByteCount() => sizeof(ulong) + sizeof(uint); + + /// + int IFloatingPoint.GetSignificandBitLength() => 96; + + /// + bool IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(sbyte)) + { + sbyte exponent = Exponent; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent); + + bytesWritten = sizeof(sbyte); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) @@ -1178,19 +1202,42 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination } } - /// - long IFloatingPoint.GetSignificandBitLength() => 96; + /// + bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= (sizeof(uint) + sizeof(ulong))) + { + uint hi32 = _hi32; + ulong lo64 = _lo64; - /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(ulong) + sizeof(uint); + if (BitConverter.IsLittleEndian) + { + hi32 = BinaryPrimitives.ReverseEndianness(hi32); + lo64 = BinaryPrimitives.ReverseEndianness(lo64); + } + + ref byte address = ref MemoryMarshal.GetReference(destination); + + Unsafe.WriteUnaligned(ref address, hi32); + Unsafe.WriteUnaligned(ref Unsafe.AddByteOffset(ref address, sizeof(uint)), lo64); + + bytesWritten = sizeof(uint) + sizeof(ulong); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { if (destination.Length >= (sizeof(ulong) + sizeof(uint))) { - var lo64 = _lo64; - var hi32 = _hi32; + ulong lo64 = _lo64; + uint hi32 = _hi32; if (!BitConverter.IsLittleEndian) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Double.cs b/src/libraries/System.Private.CoreLib/src/System/Double.cs index 98fed8885b55b2..1a4d11f7ed253a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Double.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Double.cs @@ -661,8 +661,11 @@ public static bool IsPow2(double value) /// public static double Truncate(double x) => Math.Truncate(x); + /// + int IFloatingPoint.GetExponentByteCount() => sizeof(short); + /// - long IFloatingPoint.GetExponentShortestBitLength() + int IFloatingPoint.GetExponentShortestBitLength() { short exponent = Exponent; @@ -676,8 +679,35 @@ long IFloatingPoint.GetExponentShortestBitLength() } } - /// - int IFloatingPoint.GetExponentByteCount() => sizeof(short); + /// + int IFloatingPoint.GetSignificandByteCount() => sizeof(ulong); + + /// + int IFloatingPoint.GetSignificandBitLength() => 53; + + /// + bool IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(short)) + { + 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; + } + } /// bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) @@ -703,11 +733,29 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, } } - /// - long IFloatingPoint.GetSignificandBitLength() => 53; + /// + bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(ulong)) + { + ulong significand = Significand; - /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(ulong); + if (BitConverter.IsLittleEndian) + { + significand = BinaryPrimitives.ReverseEndianness(significand); + } + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand); + + bytesWritten = sizeof(ulong); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index c538d15878f5e6..f3191bd8a5ee3b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -882,8 +882,11 @@ public static bool IsPow2(Half value) /// public static Half Truncate(Half x) => (Half)MathF.Truncate((float)x); + /// + int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// - long IFloatingPoint.GetExponentShortestBitLength() + int IFloatingPoint.GetExponentShortestBitLength() { sbyte exponent = Exponent; @@ -897,8 +900,29 @@ long IFloatingPoint.GetExponentShortestBitLength() } } - /// - int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// + int IFloatingPoint.GetSignificandByteCount() => sizeof(ushort); + + /// + int IFloatingPoint.GetSignificandBitLength() => 11; + + /// + bool IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(sbyte)) + { + sbyte exponent = Exponent; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent); + + bytesWritten = sizeof(sbyte); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) @@ -918,11 +942,29 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, o } } - /// - long IFloatingPoint.GetSignificandBitLength() => 11; + /// + bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(ushort)) + { + ushort significand = Significand; - /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(ushort); + if (BitConverter.IsLittleEndian) + { + significand = BinaryPrimitives.ReverseEndianness(significand); + } + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand); + + bytesWritten = sizeof(ushort); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) diff --git a/src/libraries/System.Private.CoreLib/src/System/Int128.cs b/src/libraries/System.Private.CoreLib/src/System/Int128.cs index a5ab201d6dcfd7..bad088e35eaceb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int128.cs @@ -799,7 +799,7 @@ public static Int128 TrailingZeroCount(Int128 value) } /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { Int128 value = this; @@ -816,6 +816,35 @@ long IBinaryInteger.GetShortestBitLength() /// int IBinaryInteger.GetByteCount() => Size; + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= Size) + { + 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; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { @@ -826,9 +855,8 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int if (!BitConverter.IsLittleEndian) { - ulong tmp = lower; - lower = BinaryPrimitives.ReverseEndianness(upper); - upper = BinaryPrimitives.ReverseEndianness(tmp); + lower = BinaryPrimitives.ReverseEndianness(lower); + upper = BinaryPrimitives.ReverseEndianness(upper); } ref byte address = ref MemoryMarshal.GetReference(destination); diff --git a/src/libraries/System.Private.CoreLib/src/System/Int16.cs b/src/libraries/System.Private.CoreLib/src/System/Int16.cs index 9003f15af0ceef..2ba1e336856c5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int16.cs @@ -340,7 +340,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static short TrailingZeroCount(short value) => (byte)(BitOperations.TrailingZeroCount(value << 16) - 16); /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { short value = m_value; @@ -357,6 +357,24 @@ long IBinaryInteger.GetShortestBitLength() /// int IBinaryInteger.GetByteCount() => sizeof(short); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(short)) + { + short value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(short); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Int32.cs b/src/libraries/System.Private.CoreLib/src/System/Int32.cs index 6445473568cafe..b800813d06be59 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int32.cs @@ -332,7 +332,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static int TrailingZeroCount(int value) => BitOperations.TrailingZeroCount(value); /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { int value = m_value; @@ -349,6 +349,24 @@ long IBinaryInteger.GetShortestBitLength() /// int IBinaryInteger.GetByteCount() => sizeof(int); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(int)) + { + int value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(int); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Int64.cs b/src/libraries/System.Private.CoreLib/src/System/Int64.cs index 85f94d64200482..4329320f9a43fc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int64.cs @@ -319,23 +319,41 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static long TrailingZeroCount(long value) => BitOperations.TrailingZeroCount(value); /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { long value = m_value; if (value >= 0) { - return (sizeof(long) * 8) - LeadingZeroCount(value); + return (sizeof(long) * 8) - BitOperations.LeadingZeroCount((ulong)(value)); } else { - return (sizeof(long) * 8) + 1 - LeadingZeroCount(~value); + return (sizeof(long) * 8) + 1 - BitOperations.LeadingZeroCount((ulong)(~value)); } } /// int IBinaryInteger.GetByteCount() => sizeof(long); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(long)) + { + long value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(long); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs index 0375302cba5a73..6f524e3a5e1bfc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs @@ -302,32 +302,50 @@ public static bool TryParse(ReadOnlySpan s, NumberStyles style, IFormatPro public static nint TrailingZeroCount(nint value) => BitOperations.TrailingZeroCount(value); /// - unsafe long IBinaryInteger.GetShortestBitLength() + unsafe int IBinaryInteger.GetShortestBitLength() { - nint value = (nint)_value; + nint value = (nint)(_value); if (value >= 0) { - return (sizeof(nint) * 8) - BitOperations.LeadingZeroCount((nuint)value); + return (sizeof(nint_t) * 8) - BitOperations.LeadingZeroCount((nuint)(value)); } else { - return (sizeof(nint) * 8) + 1 - BitOperations.LeadingZeroCount((nuint)(~value)); + return (sizeof(nint_t) * 8) + 1 - BitOperations.LeadingZeroCount((nuint)(~value)); } } /// - unsafe int IBinaryInteger.GetByteCount() => sizeof(nint); + int IBinaryInteger.GetByteCount() => sizeof(nint_t); + + /// + unsafe bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(nint_t)) + { + nint_t value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness((nint_t)(_value)) : (nint_t)(_value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(nint_t); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// unsafe bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { - if (destination.Length >= sizeof(nint)) + if (destination.Length >= sizeof(nint_t)) { - nint value = BitConverter.IsLittleEndian ? (nint)_value : (nint)BinaryPrimitives.ReverseEndianness((nint)_value); + nint_t value = BitConverter.IsLittleEndian ? (nint_t)(_value) : BinaryPrimitives.ReverseEndianness((nint_t)(_value)); Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); - bytesWritten = sizeof(nint); + bytesWritten = sizeof(nint_t); return true; } else diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs index e440a01e6c9316..eb77b66f9a87df 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs @@ -43,20 +43,63 @@ public interface IBinaryInteger /// The number of trailing zeros in . static abstract TSelf TrailingZeroCount(TSelf value); - /// Gets the length, in bits, of the shortest two's complement representation of the current value. - /// The length, in bits, of the shortest two's complement representation of the current value. - long GetShortestBitLength(); - /// Gets the number of bytes that will be written as part of . /// The number of bytes that will be written as part of . int GetByteCount(); + /// Gets the length, in bits, of the shortest two's complement representation of the current value. + /// The length, in bits, of the shortest two's complement representation of the current value. + int GetShortestBitLength(); + + /// Tries to write the current value, in big-endian format, to a given span. + /// The span to which the current value should be written. + /// The number of bytes written to . + /// true if the value was succesfully written to ; otherwise, false. + bool TryWriteBigEndian(Span destination, out int bytesWritten); + /// Tries to write the current value, in little-endian format, to a given span. /// The span to which the current value should be written. /// The number of bytes written to . /// true if the value was succesfully written to ; otherwise, false. bool TryWriteLittleEndian(Span destination, out int bytesWritten); + /// Writes the current value, in big-endian format, to a given array. + /// The array to which the current value should be written. + /// The number of bytes written to . + int WriteBigEndian(byte[] destination) + { + if (!TryWriteBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current value, in big-endian format, to a given array. + /// The array to which the current value should be written. + /// The starting index at which the value should be written. + /// The number of bytes written to starting at . + int WriteBigEndian(byte[] destination, int startIndex) + { + if (!TryWriteBigEndian(destination.AsSpan(startIndex), out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current value, in big-endian format, to a given span. + /// The span to which the current value should be written. + /// The number of bytes written to . + int WriteBigEndian(Span destination) + { + if (!TryWriteBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + /// Writes the current value, in little-endian format, to a given array. /// The array to which the current value should be written. /// The number of bytes written to . diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/IFloatingPoint.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/IFloatingPoint.cs index 12c023b3f99325..b19c0bd728ca8e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/IFloatingPoint.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/IFloatingPoint.cs @@ -51,7 +51,7 @@ public interface IFloatingPoint /// Gets the length, in bits, of the shortest two's complement representation of the current exponent. /// The length, in bits, of the shortest two's complement representation of the current exponent. - long GetExponentShortestBitLength(); + int GetExponentShortestBitLength(); /// Gets the number of bytes that will be written as part of . /// The number of bytes that will be written as part of . @@ -59,24 +59,73 @@ public interface IFloatingPoint /// Gets the length, in bits, of the current significand. /// The length, in bits, of the current significand. - long GetSignificandBitLength(); + int GetSignificandBitLength(); /// Gets the number of bytes that will be written as part of . /// The number of bytes that will be written as part of . int GetSignificandByteCount(); + /// Tries to write the current exponent, in big-endian format, to a given span. + /// The span to which the current exponent should be written. + /// The number of bytes written to . + /// true if the exponent was succesfully written to ; otherwise, false. + bool TryWriteExponentBigEndian(Span destination, out int bytesWritten); + /// Tries to write the current exponent, in little-endian format, to a given span. /// The span to which the current exponent should be written. /// The number of bytes written to . /// true if the exponent was succesfully written to ; otherwise, false. bool TryWriteExponentLittleEndian(Span destination, out int bytesWritten); + /// Tries to write the current significand, in big-endian format, to a given span. + /// The span to which the current significand should be written. + /// The number of bytes written to . + /// true if the significand was succesfully written to ; otherwise, false. + bool TryWriteSignificandBigEndian(Span destination, out int bytesWritten); + /// Tries to write the current significand, in little-endian format, to a given span. /// The span to which the current significand should be written. /// The number of bytes written to . /// true if the significand was succesfully written to ; otherwise, false. bool TryWriteSignificandLittleEndian(Span destination, out int bytesWritten); + /// Writes the current exponent, in big-endian format, to a given array. + /// The array to which the current exponent should be written. + /// The number of bytes written to . + int WriteExponentBigEndian(byte[] destination) + { + if (!TryWriteExponentBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current exponent, in big-endian format, to a given array. + /// The array to which the current exponent should be written. + /// The starting index at which the exponent should be written. + /// The number of bytes written to starting at . + int WriteExponentBigEndian(byte[] destination, int startIndex) + { + if (!TryWriteExponentBigEndian(destination.AsSpan(startIndex), out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current exponent, in big-endian format, to a given span. + /// The span to which the current exponent should be written. + /// The number of bytes written to . + int WriteExponentBigEndian(Span destination) + { + if (!TryWriteExponentBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + /// Writes the current exponent, in little-endian format, to a given array. /// The array to which the current exponent should be written. /// The number of bytes written to . @@ -114,6 +163,43 @@ int WriteExponentLittleEndian(Span destination) return bytesWritten; } + /// Writes the current significand, in big-endian format, to a given array. + /// The array to which the current significand should be written. + /// The number of bytes written to . + int WriteSignificandBigEndian(byte[] destination) + { + if (!TryWriteSignificandBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current significand, in big-endian format, to a given array. + /// The array to which the current significand should be written. + /// The starting index at which the significand should be written. + /// The number of bytes written to starting at . + int WriteSignificandBigEndian(byte[] destination, int startIndex) + { + if (!TryWriteSignificandBigEndian(destination.AsSpan(startIndex), out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + + /// Writes the current significand, in big-endian format, to a given span. + /// The span to which the current significand should be written. + /// The number of bytes written to . + int WriteSignificandBigEndian(Span destination) + { + if (!TryWriteSignificandBigEndian(destination, out int bytesWritten)) + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + return bytesWritten; + } + /// Writes the current significand, in little-endian format, to a given array. /// The array to which the current significand should be written. /// The number of bytes written to . diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs index 3915ac7d9a38a4..3cf7c2fdad59d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NFloat.cs @@ -861,8 +861,11 @@ public int CompareTo(object? obj) /// public static NFloat Truncate(NFloat x) => new NFloat(NativeType.Truncate(x._value)); + /// + int IFloatingPoint.GetExponentByteCount() => sizeof(NativeExponentType); + /// - long IFloatingPoint.GetExponentShortestBitLength() + int IFloatingPoint.GetExponentShortestBitLength() { NativeExponentType exponent = _value.Exponent; @@ -876,8 +879,42 @@ long IFloatingPoint.GetExponentShortestBitLength() } } - /// - int IFloatingPoint.GetExponentByteCount() => sizeof(NativeExponentType); + /// + int IFloatingPoint.GetSignificandByteCount() => sizeof(NativeSignificandType); + + /// + int IFloatingPoint.GetSignificandBitLength() + { +#if TARGET_32BIT + return 24; +#else + return 53; +#endif + } + + /// + bool IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(NativeExponentType)) + { + NativeExponentType exponent = _value.Exponent; + + if (BitConverter.IsLittleEndian) + { + exponent = BinaryPrimitives.ReverseEndianness(exponent); + } + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent); + + bytesWritten = sizeof(NativeExponentType); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) @@ -903,18 +940,29 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, } } - /// - long IFloatingPoint.GetSignificandBitLength() + /// + bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { -#if TARGET_32BIT - return 24; -#else - return 53; -#endif - } + if (destination.Length >= sizeof(NativeSignificandType)) + { + NativeSignificandType significand = _value.Significand; - /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(NativeSignificandType); + if (BitConverter.IsLittleEndian) + { + significand = BinaryPrimitives.ReverseEndianness(significand); + } + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand); + + bytesWritten = sizeof(NativeSignificandType); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) diff --git a/src/libraries/System.Private.CoreLib/src/System/SByte.cs b/src/libraries/System.Private.CoreLib/src/System/SByte.cs index 7fd04196ed9cbc..bf677dca2423c4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SByte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SByte.cs @@ -347,7 +347,7 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static sbyte TrailingZeroCount(sbyte value) => (sbyte)(BitOperations.TrailingZeroCount(value << 24) - 24); /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { sbyte value = m_value; @@ -364,6 +364,24 @@ long IBinaryInteger.GetShortestBitLength() /// int IBinaryInteger.GetByteCount() => sizeof(sbyte); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(sbyte)) + { + sbyte value = m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(sbyte); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Single.cs b/src/libraries/System.Private.CoreLib/src/System/Single.cs index e83ecdd3a3e477..cef0b13768c908 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Single.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Single.cs @@ -656,8 +656,11 @@ public static bool IsPow2(float value) /// public static float Truncate(float x) => MathF.Truncate(x); + /// + int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// - long IFloatingPoint.GetExponentShortestBitLength() + int IFloatingPoint.GetExponentShortestBitLength() { sbyte exponent = Exponent; @@ -671,8 +674,29 @@ long IFloatingPoint.GetExponentShortestBitLength() } } - /// - int IFloatingPoint.GetExponentByteCount() => sizeof(sbyte); + /// + int IFloatingPoint.GetSignificandByteCount() => sizeof(uint); + + /// + int IFloatingPoint.GetSignificandBitLength() => 24; + + /// + bool IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(sbyte)) + { + sbyte exponent = Exponent; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent); + + bytesWritten = sizeof(sbyte); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) @@ -692,11 +716,29 @@ bool IFloatingPoint.TryWriteExponentLittleEndian(Span destination, } } - /// - long IFloatingPoint.GetSignificandBitLength() => 24; + /// + bool IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(uint)) + { + uint significand = Significand; - /// - int IFloatingPoint.GetSignificandByteCount() => sizeof(uint); + if (BitConverter.IsLittleEndian) + { + significand = BinaryPrimitives.ReverseEndianness(significand); + } + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand); + + bytesWritten = sizeof(uint); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// bool IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index 785584a595be17..134fa4a7659266 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -768,9 +768,8 @@ private void WriteLittleEndianUnsafe(Span destination) if (!BitConverter.IsLittleEndian) { - ulong tmp = lower; - lower = BinaryPrimitives.ReverseEndianness(upper); - upper = BinaryPrimitives.ReverseEndianness(tmp); + lower = BinaryPrimitives.ReverseEndianness(lower); + upper = BinaryPrimitives.ReverseEndianness(upper); } ref byte address = ref MemoryMarshal.GetReference(destination); @@ -860,7 +859,7 @@ public static UInt128 TrailingZeroCount(UInt128 value) } /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { UInt128 value = this; return (Size * 8) - BitOperations.LeadingZeroCount(value); @@ -869,6 +868,35 @@ long IBinaryInteger.GetShortestBitLength() /// int IBinaryInteger.GetByteCount() => Size; + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= Size) + { + 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; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs index e3c8c23b758ce4..c54f51d8ebf95f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs @@ -328,11 +328,29 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static ushort TrailingZeroCount(ushort value) => (ushort)(BitOperations.TrailingZeroCount(value << 16) - 16); /// - long IBinaryInteger.GetShortestBitLength() => (sizeof(ushort) * 8) - LeadingZeroCount(m_value); + int IBinaryInteger.GetShortestBitLength() => (sizeof(ushort) * 8) - LeadingZeroCount(m_value); /// int IBinaryInteger.GetByteCount() => sizeof(ushort); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(ushort)) + { + ushort value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(ushort); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs index 3c8a024187216e..5ea2f9d33eb04a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs @@ -314,11 +314,29 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static uint TrailingZeroCount(uint value) => (uint)BitOperations.TrailingZeroCount(value); /// - long IBinaryInteger.GetShortestBitLength() => (sizeof(uint) * 8) - LeadingZeroCount(m_value); + int IBinaryInteger.GetShortestBitLength() => (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(m_value); /// int IBinaryInteger.GetByteCount() => sizeof(uint); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(uint)) + { + uint value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(uint); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs index d19d88c51114ba..57b2ec836d5a5b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs @@ -313,11 +313,29 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) public static ulong TrailingZeroCount(ulong value) => (ulong)BitOperations.TrailingZeroCount(value); /// - long IBinaryInteger.GetShortestBitLength() => (sizeof(ulong) * 8) - (long)LeadingZeroCount(m_value); + int IBinaryInteger.GetShortestBitLength() => (sizeof(ulong) * 8) - BitOperations.LeadingZeroCount(m_value); /// int IBinaryInteger.GetByteCount() => sizeof(ulong); + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(ulong)) + { + ulong value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(m_value) : m_value; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(ulong); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } + /// bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { diff --git a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs index fd259dcbbec048..a3b64026a58d74 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs @@ -298,20 +298,38 @@ public static bool TryParse(ReadOnlySpan s, NumberStyles style, IFormatPro public static nuint TrailingZeroCount(nuint value) => (nuint)BitOperations.TrailingZeroCount(value); /// - unsafe long IBinaryInteger.GetShortestBitLength() => (sizeof(nuint) * 8) - BitOperations.LeadingZeroCount((nuint)_value); + unsafe int IBinaryInteger.GetShortestBitLength() => (sizeof(nuint_t) * 8) - BitOperations.LeadingZeroCount((nuint)(_value)); /// - unsafe int IBinaryInteger.GetByteCount() => sizeof(nuint); + int IBinaryInteger.GetByteCount() => sizeof(nuint_t); + + /// + unsafe bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) + { + if (destination.Length >= sizeof(nuint_t)) + { + nuint_t value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness((nuint_t)(_value)) : (nuint_t)(_value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + + bytesWritten = sizeof(nuint_t); + return true; + } + else + { + bytesWritten = 0; + return false; + } + } /// unsafe bool IBinaryInteger.TryWriteLittleEndian(Span destination, out int bytesWritten) { - if (destination.Length >= sizeof(nuint)) + if (destination.Length >= sizeof(nuint_t)) { - nuint value = BitConverter.IsLittleEndian ? (nuint)_value : (nuint)BinaryPrimitives.ReverseEndianness((nuint)_value); + nuint_t value = BitConverter.IsLittleEndian ? (nuint_t)(_value) : BinaryPrimitives.ReverseEndianness((nuint_t)(_value)); Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); - bytesWritten = sizeof(nuint); + bytesWritten = sizeof(nuint_t); return true; } else diff --git a/src/libraries/System.Runtime.InteropServices/System.Runtime.InteropServices.sln b/src/libraries/System.Runtime.InteropServices/System.Runtime.InteropServices.sln index 0a0c735457a584..5009978c417b90 100644 --- a/src/libraries/System.Runtime.InteropServices/System.Runtime.InteropServices.sln +++ b/src/libraries/System.Runtime.InteropServices/System.Runtime.InteropServices.sln @@ -1,4 +1,8 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32509.446 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Private.CoreLib", "..\..\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj", "{94B59BA0-491F-4B59-ADFF-A057EC3EC835}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}" @@ -41,17 +45,23 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{E1AEBD5D-AE4 EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Checked|Any CPU = Checked|Any CPU + Checked|x64 = Checked|x64 + Checked|x86 = Checked|x86 Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 Release|x86 = Release|x86 - Checked|Any CPU = Checked|Any CPU - Checked|x64 = Checked|x64 - Checked|x86 = Checked|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|Any CPU.ActiveCfg = Checked|x64 + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|Any CPU.Build.0 = Checked|x64 + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x64.ActiveCfg = Checked|x64 + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x64.Build.0 = Checked|x64 + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x86.ActiveCfg = Checked|x86 + {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x86.Build.0 = Checked|x86 {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Debug|Any CPU.ActiveCfg = Debug|x64 {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Debug|Any CPU.Build.0 = Debug|x64 {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Debug|x64.ActiveCfg = Debug|x64 @@ -64,12 +74,12 @@ Global {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Release|x64.Build.0 = Release|x64 {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Release|x86.ActiveCfg = Release|x86 {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Release|x86.Build.0 = Release|x86 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|Any CPU.ActiveCfg = Checked|x64 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|Any CPU.Build.0 = Checked|x64 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x64.ActiveCfg = Checked|x64 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x64.Build.0 = Checked|x64 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x86.ActiveCfg = Checked|x86 - {94B59BA0-491F-4B59-ADFF-A057EC3EC835}.Checked|x86.Build.0 = Checked|x86 + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|Any CPU.Build.0 = Debug|Any CPU + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x64.ActiveCfg = Debug|Any CPU + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x64.Build.0 = Debug|Any CPU + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x86.ActiveCfg = Debug|Any CPU + {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x86.Build.0 = Debug|Any CPU {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Debug|Any CPU.Build.0 = Debug|Any CPU {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -82,12 +92,12 @@ Global {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Release|x64.Build.0 = Release|Any CPU {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Release|x86.ActiveCfg = Release|Any CPU {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Release|x86.Build.0 = Release|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|Any CPU.Build.0 = Debug|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x64.ActiveCfg = Debug|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x64.Build.0 = Debug|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x86.ActiveCfg = Debug|Any CPU - {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA}.Checked|x86.Build.0 = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|Any CPU.Build.0 = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x64.ActiveCfg = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x64.Build.0 = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x86.ActiveCfg = Debug|Any CPU + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x86.Build.0 = Debug|Any CPU {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Debug|Any CPU.Build.0 = Debug|Any CPU {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -100,12 +110,12 @@ Global {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Release|x64.Build.0 = Release|Any CPU {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Release|x86.ActiveCfg = Release|Any CPU {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Release|x86.Build.0 = Release|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|Any CPU.Build.0 = Debug|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x64.ActiveCfg = Debug|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x64.Build.0 = Debug|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x86.ActiveCfg = Debug|Any CPU - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A}.Checked|x86.Build.0 = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|Any CPU.Build.0 = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x64.ActiveCfg = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x64.Build.0 = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x86.ActiveCfg = Debug|Any CPU + {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x86.Build.0 = Debug|Any CPU {1B248B4C-7584-4C04-850A-A50EB592052C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1B248B4C-7584-4C04-850A-A50EB592052C}.Debug|Any CPU.Build.0 = Debug|Any CPU {1B248B4C-7584-4C04-850A-A50EB592052C}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -118,12 +128,12 @@ Global {1B248B4C-7584-4C04-850A-A50EB592052C}.Release|x64.Build.0 = Release|Any CPU {1B248B4C-7584-4C04-850A-A50EB592052C}.Release|x86.ActiveCfg = Release|Any CPU {1B248B4C-7584-4C04-850A-A50EB592052C}.Release|x86.Build.0 = Release|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|Any CPU.Build.0 = Debug|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x64.ActiveCfg = Debug|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x64.Build.0 = Debug|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x86.ActiveCfg = Debug|Any CPU - {1B248B4C-7584-4C04-850A-A50EB592052C}.Checked|x86.Build.0 = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|Any CPU.Build.0 = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x64.ActiveCfg = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x64.Build.0 = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x86.ActiveCfg = Debug|Any CPU + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x86.Build.0 = Debug|Any CPU {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Debug|Any CPU.Build.0 = Debug|Any CPU {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -136,12 +146,12 @@ Global {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Release|x64.Build.0 = Release|Any CPU {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Release|x86.ActiveCfg = Release|Any CPU {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Release|x86.Build.0 = Release|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|Any CPU.Build.0 = Debug|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x64.ActiveCfg = Debug|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x64.Build.0 = Debug|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x86.ActiveCfg = Debug|Any CPU - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40}.Checked|x86.Build.0 = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|Any CPU.Build.0 = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x64.ActiveCfg = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x64.Build.0 = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x86.ActiveCfg = Debug|Any CPU + {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x86.Build.0 = Debug|Any CPU {768B77B0-EA45-469D-B39E-545EB72F5A43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {768B77B0-EA45-469D-B39E-545EB72F5A43}.Debug|Any CPU.Build.0 = Debug|Any CPU {768B77B0-EA45-469D-B39E-545EB72F5A43}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -154,12 +164,12 @@ Global {768B77B0-EA45-469D-B39E-545EB72F5A43}.Release|x64.Build.0 = Release|Any CPU {768B77B0-EA45-469D-B39E-545EB72F5A43}.Release|x86.ActiveCfg = Release|Any CPU {768B77B0-EA45-469D-B39E-545EB72F5A43}.Release|x86.Build.0 = Release|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|Any CPU.Build.0 = Debug|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x64.ActiveCfg = Debug|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x64.Build.0 = Debug|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x86.ActiveCfg = Debug|Any CPU - {768B77B0-EA45-469D-B39E-545EB72F5A43}.Checked|x86.Build.0 = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|Any CPU.Build.0 = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x64.ActiveCfg = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x64.Build.0 = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x86.ActiveCfg = Debug|Any CPU + {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x86.Build.0 = Debug|Any CPU {8671F164-F78C-44FA-93B7-A310F67890FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8671F164-F78C-44FA-93B7-A310F67890FE}.Debug|Any CPU.Build.0 = Debug|Any CPU {8671F164-F78C-44FA-93B7-A310F67890FE}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -172,12 +182,12 @@ Global {8671F164-F78C-44FA-93B7-A310F67890FE}.Release|x64.Build.0 = Release|Any CPU {8671F164-F78C-44FA-93B7-A310F67890FE}.Release|x86.ActiveCfg = Release|Any CPU {8671F164-F78C-44FA-93B7-A310F67890FE}.Release|x86.Build.0 = Release|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|Any CPU.Build.0 = Debug|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x64.ActiveCfg = Debug|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x64.Build.0 = Debug|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x86.ActiveCfg = Debug|Any CPU - {8671F164-F78C-44FA-93B7-A310F67890FE}.Checked|x86.Build.0 = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|Any CPU.Build.0 = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x64.ActiveCfg = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x64.Build.0 = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x86.ActiveCfg = Debug|Any CPU + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x86.Build.0 = Debug|Any CPU {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -190,12 +200,12 @@ Global {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Release|x64.Build.0 = Release|Any CPU {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Release|x86.ActiveCfg = Release|Any CPU {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Release|x86.Build.0 = Release|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|Any CPU.Build.0 = Debug|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x64.ActiveCfg = Debug|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x64.Build.0 = Debug|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x86.ActiveCfg = Debug|Any CPU - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1}.Checked|x86.Build.0 = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|Any CPU.Build.0 = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x64.ActiveCfg = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x64.Build.0 = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x86.ActiveCfg = Debug|Any CPU + {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x86.Build.0 = Debug|Any CPU {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Debug|Any CPU.Build.0 = Debug|Any CPU {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -208,12 +218,12 @@ Global {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Release|x64.Build.0 = Release|Any CPU {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Release|x86.ActiveCfg = Release|Any CPU {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Release|x86.Build.0 = Release|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|Any CPU.Build.0 = Debug|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x64.ActiveCfg = Debug|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x64.Build.0 = Debug|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x86.ActiveCfg = Debug|Any CPU - {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E}.Checked|x86.Build.0 = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|Any CPU.Build.0 = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x64.ActiveCfg = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x64.Build.0 = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x86.ActiveCfg = Debug|Any CPU + {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x86.Build.0 = Debug|Any CPU {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -226,12 +236,12 @@ Global {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Release|x64.Build.0 = Release|Any CPU {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Release|x86.ActiveCfg = Release|Any CPU {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Release|x86.Build.0 = Release|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|Any CPU.Build.0 = Debug|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x64.ActiveCfg = Debug|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x64.Build.0 = Debug|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x86.ActiveCfg = Debug|Any CPU - {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1}.Checked|x86.Build.0 = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|Any CPU.Build.0 = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x64.ActiveCfg = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x64.Build.0 = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x86.ActiveCfg = Debug|Any CPU + {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x86.Build.0 = Debug|Any CPU {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Debug|Any CPU.Build.0 = Debug|Any CPU {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -244,12 +254,12 @@ Global {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Release|x64.Build.0 = Release|Any CPU {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Release|x86.ActiveCfg = Release|Any CPU {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Release|x86.Build.0 = Release|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|Any CPU.Build.0 = Debug|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x64.ActiveCfg = Debug|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x64.Build.0 = Debug|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x86.ActiveCfg = Debug|Any CPU - {EA8DBC12-60BC-433E-ABFF-A89DFA795283}.Checked|x86.Build.0 = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|Any CPU.Build.0 = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x64.ActiveCfg = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x64.Build.0 = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x86.ActiveCfg = Debug|Any CPU + {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x86.Build.0 = Debug|Any CPU {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -262,12 +272,12 @@ Global {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Release|x64.Build.0 = Release|Any CPU {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Release|x86.ActiveCfg = Release|Any CPU {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Release|x86.Build.0 = Release|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|Any CPU.Build.0 = Debug|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x64.ActiveCfg = Debug|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x64.Build.0 = Debug|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x86.ActiveCfg = Debug|Any CPU - {25D66424-2EAF-464D-8460-10C04EDEF3C3}.Checked|x86.Build.0 = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|Any CPU.Build.0 = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x64.ActiveCfg = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x64.Build.0 = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x86.ActiveCfg = Debug|Any CPU + {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x86.Build.0 = Debug|Any CPU {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Debug|Any CPU.Build.0 = Debug|Any CPU {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -280,12 +290,12 @@ Global {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Release|x64.Build.0 = Release|Any CPU {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Release|x86.ActiveCfg = Release|Any CPU {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Release|x86.Build.0 = Release|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|Any CPU.Build.0 = Debug|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x64.ActiveCfg = Debug|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x64.Build.0 = Debug|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x86.ActiveCfg = Debug|Any CPU - {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF}.Checked|x86.Build.0 = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|Any CPU.Build.0 = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|x64.ActiveCfg = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|x64.Build.0 = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|x86.ActiveCfg = Debug|Any CPU + {866D295E-424A-4747-9417-CD7746936138}.Checked|x86.Build.0 = Debug|Any CPU {866D295E-424A-4747-9417-CD7746936138}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {866D295E-424A-4747-9417-CD7746936138}.Debug|Any CPU.Build.0 = Debug|Any CPU {866D295E-424A-4747-9417-CD7746936138}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -298,12 +308,12 @@ Global {866D295E-424A-4747-9417-CD7746936138}.Release|x64.Build.0 = Release|Any CPU {866D295E-424A-4747-9417-CD7746936138}.Release|x86.ActiveCfg = Release|Any CPU {866D295E-424A-4747-9417-CD7746936138}.Release|x86.Build.0 = Release|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|Any CPU.Build.0 = Debug|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|x64.ActiveCfg = Debug|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|x64.Build.0 = Debug|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|x86.ActiveCfg = Debug|Any CPU - {866D295E-424A-4747-9417-CD7746936138}.Checked|x86.Build.0 = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|Any CPU.Build.0 = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x64.ActiveCfg = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x64.Build.0 = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x86.ActiveCfg = Debug|Any CPU + {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x86.Build.0 = Debug|Any CPU {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -316,12 +326,12 @@ Global {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Release|x64.Build.0 = Release|Any CPU {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Release|x86.ActiveCfg = Release|Any CPU {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Release|x86.Build.0 = Release|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|Any CPU.Build.0 = Debug|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x64.ActiveCfg = Debug|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x64.Build.0 = Debug|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x86.ActiveCfg = Debug|Any CPU - {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5}.Checked|x86.Build.0 = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|Any CPU.ActiveCfg = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|Any CPU.Build.0 = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x64.ActiveCfg = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x64.Build.0 = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x86.ActiveCfg = Debug|Any CPU + {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x86.Build.0 = Debug|Any CPU {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Debug|Any CPU.Build.0 = Debug|Any CPU {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -334,20 +344,19 @@ Global {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Release|x64.Build.0 = Release|Any CPU {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Release|x86.ActiveCfg = Release|Any CPU {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Release|x86.Build.0 = Release|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|Any CPU.ActiveCfg = Debug|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|Any CPU.Build.0 = Debug|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x64.ActiveCfg = Debug|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x64.Build.0 = Debug|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x86.ActiveCfg = Debug|Any CPU - {0B5FD0C2-367D-4AD6-8001-80AD79B2441C}.Checked|x86.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {94B59BA0-491F-4B59-ADFF-A057EC3EC835} = {B1678CCD-95C8-4419-B9F9-14A03061BE4B} - {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1} = {B1678CCD-95C8-4419-B9F9-14A03061BE4B} {1FF4CC8E-49C3-42A0-A6E0-2E5908455FBA} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} + {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A} = {D893B9AA-57C5-49E3-97B1-12CC62D84307} + {1B248B4C-7584-4C04-850A-A50EB592052C} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} + {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} + {768B77B0-EA45-469D-B39E-545EB72F5A43} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} + {8671F164-F78C-44FA-93B7-A310F67890FE} = {D893B9AA-57C5-49E3-97B1-12CC62D84307} + {4FC33B9B-1BCF-4D16-B886-DCA8F2B823C1} = {B1678CCD-95C8-4419-B9F9-14A03061BE4B} {79F7BE0E-01AA-4AFB-B047-CF7C0B38F81E} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} {9C2C2B5C-5E75-4935-8A4A-DE3D3A5DBBC1} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} {EA8DBC12-60BC-433E-ABFF-A89DFA795283} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} @@ -355,14 +364,12 @@ Global {049B7FD4-ACEF-4BCD-A7A7-75C9BBEC4EBF} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} {866D295E-424A-4747-9417-CD7746936138} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} {D3A329E3-0FEB-4136-9CB6-B38319B0FFA5} = {FB99AC59-1744-4F12-A4B0-0D54FCA048BF} - {4859BEE3-34B7-48E7-83D4-1ADD8B8F3B3A} = {D893B9AA-57C5-49E3-97B1-12CC62D84307} - {8671F164-F78C-44FA-93B7-A310F67890FE} = {D893B9AA-57C5-49E3-97B1-12CC62D84307} {0B5FD0C2-367D-4AD6-8001-80AD79B2441C} = {D893B9AA-57C5-49E3-97B1-12CC62D84307} - {1B248B4C-7584-4C04-850A-A50EB592052C} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} - {90CDAD9F-3ACC-43B0-9696-0C849FCD8C40} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} - {768B77B0-EA45-469D-B39E-545EB72F5A43} = {E1AEBD5D-AE4E-4F61-B9ED-AEF950B0CC33} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D4031401-FEB5-4CCF-91C1-38F5646B2BFD} EndGlobalSection + GlobalSection(SharedMSBuildProjectFiles) = preSolution + ..\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems*{94b59ba0-491f-4b59-adff-a057ec3ec835}*SharedItemsImports = 5 + EndGlobalSection EndGlobal diff --git a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs index 6c196e7f100621..878f05662fa972 100644 --- a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs +++ b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs @@ -970,10 +970,12 @@ public static void Free(void* ptr) { } static System.Runtime.InteropServices.NFloat System.Numerics.IDecrementOperators.operator checked --(System.Runtime.InteropServices.NFloat value) { throw null; } static System.Runtime.InteropServices.NFloat System.Numerics.IDivisionOperators.operator checked /(System.Runtime.InteropServices.NFloat left, System.Runtime.InteropServices.NFloat right) { throw null; } int System.Numerics.IFloatingPoint.GetExponentByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } int System.Numerics.IFloatingPoint.GetSignificandByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { throw null; } static System.Runtime.InteropServices.NFloat System.Numerics.IIncrementOperators.operator checked ++(System.Runtime.InteropServices.NFloat value) { throw null; } static System.Runtime.InteropServices.NFloat System.Numerics.IMultiplyOperators.operator checked *(System.Runtime.InteropServices.NFloat left, System.Runtime.InteropServices.NFloat right) { throw null; } diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.GenericMath.cs b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.GenericMath.cs index 5ed02150b9e7e9..bdfd4dbea8f425 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.GenericMath.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/NFloatTests.GenericMath.cs @@ -1616,7 +1616,7 @@ public static void GetExponentByteCountTest() [Fact] public static void GetExponentShortestBitLengthTest() { - long expected = Environment.Is64BitProcess ? 11 : 8; + int expected = Environment.Is64BitProcess ? 11 : 8; Assert.Equal(expected, FloatingPointHelper.GetExponentShortestBitLength(NFloat.NegativeInfinity)); Assert.Equal(expected, FloatingPointHelper.GetExponentShortestBitLength(-MinNormal)); @@ -1666,7 +1666,7 @@ public static void GetSignificandByteCountTest() [Fact] public static void GetSignificandBitLengthTest() { - long expected = Environment.Is64BitProcess ? 53 : 24; + int expected = Environment.Is64BitProcess ? 53 : 24; Assert.Equal(expected, FloatingPointHelper.GetSignificandBitLength(NFloat.NegativeInfinity)); Assert.Equal(expected, FloatingPointHelper.GetSignificandBitLength(NFloat.MinValue)); @@ -1685,6 +1685,149 @@ public static void GetSignificandBitLengthTest() Assert.Equal(expected, FloatingPointHelper.GetSignificandBitLength(NFloat.PositiveInfinity)); } + [Fact] + public static void TryWriteExponentBigEndianTest() + { + if (Environment.Is64BitProcess) + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.MinValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xFF }, destination.ToArray()); // +1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x02 }, destination.ToArray()); // -1022 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.NaN, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x02 }, destination.ToArray()); // -1022 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.MaxValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xFF }, destination.ToArray()); // +1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + } + else + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.MinValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); // +127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x82 }, destination.ToArray()); // -126 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.NaN, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x82 }, destination.ToArray()); // -126 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.MaxValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); // +127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NFloat.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); + } + } + [Fact] public static void TryWriteExponentLittleEndianTest() { @@ -1828,6 +1971,149 @@ public static void TryWriteExponentLittleEndianTest() } } + [Fact] + public static void TryWriteSignificandBigEndianTest() + { + if (Environment.Is64BitProcess) + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.MinValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.NaN, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.MaxValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + } + else + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.MinValue, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x7F, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.NaN, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xC0, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.Epsilon, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x7F, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.MaxValue, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NFloat.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + } + } + [Fact] public static void TryWriteSignificandLittleEndianTest() { diff --git a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs index 55d5800bc004f8..293feb4a05c688 100644 --- a/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs +++ b/src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs @@ -186,7 +186,8 @@ namespace System.Numerics public static System.Numerics.BigInteger Subtract(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } static System.Numerics.BigInteger System.Numerics.IAdditionOperators.operator checked +(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static System.Numerics.BigInteger System.Numerics.IDecrementOperators.operator checked --(System.Numerics.BigInteger value) { throw null; } static System.Numerics.BigInteger System.Numerics.IDivisionOperators.operator checked /(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; } diff --git a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs index e541085069e719..e62af6151c752c 100644 --- a/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs +++ b/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs @@ -3450,7 +3450,7 @@ public static BigInteger TrailingZeroCount(BigInteger value) } /// - long IBinaryInteger.GetShortestBitLength() + int IBinaryInteger.GetShortestBitLength() { AssertValid(); uint[]? bits = _bits; @@ -3461,39 +3461,146 @@ long IBinaryInteger.GetShortestBitLength() if (value >= 0) { - return (sizeof(int) * 8) - int.LeadingZeroCount(value); + return (sizeof(int) * 8) - BitOperations.LeadingZeroCount((uint)(value)); } else { - return (sizeof(int) * 8) + 1 - int.LeadingZeroCount(~value); + return (sizeof(int) * 8) + 1 - BitOperations.LeadingZeroCount((uint)(~value)); } } - long result = (bits.Length - 1) * 32; + int result = (bits.Length - 1) * 32; if (_sign >= 0) { - result += (sizeof(uint) * 8) - uint.LeadingZeroCount(bits[^1]); + result += (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(bits[^1]); } else { - result += (sizeof(uint) * 8) + 1 - uint.LeadingZeroCount(~bits[^1]); + uint part = ~bits[^1] + 1; + + // We need to remove the "carry" (the +1) if any of the initial + // bytes are not zero. This ensures we get the correct two's complement + // part for the computation. + + for (int index = 0; index < bits.Length - 1; index++) + { + if (bits[index] != 0) + { + part -= 1; + break; + } + } + + result += (sizeof(uint) * 8) + 1 - BitOperations.LeadingZeroCount(~part); } return result; } /// - int IBinaryInteger.GetByteCount() + int IBinaryInteger.GetByteCount() => GetGenericMathByteCount(); + + /// + bool IBinaryInteger.TryWriteBigEndian(Span destination, out int bytesWritten) { AssertValid(); uint[]? bits = _bits; - if (bits is null) + int byteCount = GetGenericMathByteCount(); + + if (destination.Length >= byteCount) { - return sizeof(int); + if (bits is null) + { + int value = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(_sign) : _sign; + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); + } + else if (_sign >= 0) + { + // When the value is positive, we simply need to copy all bits as big endian + + ref byte startAddress = ref MemoryMarshal.GetReference(destination); + ref byte address = ref Unsafe.Add(ref startAddress, (bits.Length - 1) * sizeof(uint)); + + for (int i = 0; i < bits.Length; i++) + { + uint part = bits[i]; + + if (BitConverter.IsLittleEndian) + { + part = BinaryPrimitives.ReverseEndianness(part); + } + + Unsafe.WriteUnaligned(ref address, part); + address = ref Unsafe.Subtract(ref address, sizeof(uint)); + } + } + else + { + // When the value is negative, we need to copy the two's complement representation + // We'll do this "inline" to avoid needing to unnecessarily allocate. + + ref byte startAddress = ref MemoryMarshal.GetReference(destination); + ref byte address = ref Unsafe.Add(ref startAddress, byteCount - sizeof(uint)); + + int i = 0; + uint part; + + do + { + // first do complement and +1 as long as carry is needed + part = ~bits[i] + 1; + + if (BitConverter.IsLittleEndian) + { + part = BinaryPrimitives.ReverseEndianness(part); + } + + Unsafe.WriteUnaligned(ref address, part); + address = ref Unsafe.Subtract(ref address, sizeof(uint)); + + i++; + } + while ((part == 0) && (i < bits.Length)); + + while (i < bits.Length) + { + // now ones complement is sufficient + part = ~bits[i]; + + if (BitConverter.IsLittleEndian) + { + part = BinaryPrimitives.ReverseEndianness(part); + } + + Unsafe.WriteUnaligned(ref address, part); + address = ref Unsafe.Subtract(ref address, sizeof(uint)); + + i++; + } + + if (Unsafe.AreSame(ref address, ref startAddress)) + { + // We need one extra part to represent the sign as the most + // significant bit of the two's complement value was 0. + Unsafe.WriteUnaligned(ref address, uint.MaxValue); + } + else + { + // Otherwise we should have been precisely one part behind address + Debug.Assert(Unsafe.AreSame(ref startAddress, ref Unsafe.Add(ref address, sizeof(uint)))); + } + } + + bytesWritten = byteCount; + return true; + } + else + { + bytesWritten = 0; + return false; } - return bits.Length * 4; } /// @@ -3502,7 +3609,7 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out AssertValid(); uint[]? bits = _bits; - int byteCount = (bits is null) ? sizeof(int) : bits.Length * 4; + int byteCount = GetGenericMathByteCount(); if (destination.Length >= byteCount) { @@ -3536,14 +3643,21 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out // We'll do this "inline" to avoid needing to unnecessarily allocate. ref byte address = ref MemoryMarshal.GetReference(destination); + ref byte lastAddress = ref Unsafe.Add(ref address, byteCount - sizeof(uint)); int i = 0; uint part; do { + // first do complement and +1 as long as carry is needed part = ~bits[i] + 1; + if (!BitConverter.IsLittleEndian) + { + part = BinaryPrimitives.ReverseEndianness(part); + } + Unsafe.WriteUnaligned(ref address, part); address = ref Unsafe.Add(ref address, sizeof(uint)); @@ -3553,13 +3667,31 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out while (i < bits.Length) { + // now ones complement is sufficient part = ~bits[i]; + if (!BitConverter.IsLittleEndian) + { + part = BinaryPrimitives.ReverseEndianness(part); + } + Unsafe.WriteUnaligned(ref address, part); address = ref Unsafe.Add(ref address, sizeof(uint)); i++; } + + if (Unsafe.AreSame(ref address, ref lastAddress)) + { + // We need one extra part to represent the sign as the most + // significant bit of the two's complement value was 0. + Unsafe.WriteUnaligned(ref address, uint.MaxValue); + } + else + { + // Otherwise we should have been precisely one part ahead address + Debug.Assert(Unsafe.AreSame(ref lastAddress, ref Unsafe.Subtract(ref address, sizeof(uint)))); + } } bytesWritten = byteCount; @@ -3572,6 +3704,46 @@ bool IBinaryInteger.TryWriteLittleEndian(Span destination, out } } + private int GetGenericMathByteCount() + { + AssertValid(); + uint[]? bits = _bits; + + if (bits is null) + { + return sizeof(int); + } + + int result = bits.Length * 4; + + if (_sign < 0) + { + uint part = ~bits[^1] + 1; + + // We need to remove the "carry" (the +1) if any of the initial + // bytes are not zero. This ensures we get the correct two's complement + // part for the computation. + + for (int index = 0; index < bits.Length - 1; index++) + { + if (bits[index] != 0) + { + part -= 1; + break; + } + } + + if ((int)part >= 0) + { + // When the most significant bit of the part is zero + // we need another part to represent the value. + result += sizeof(uint); + } + } + + return result; + } + // // IBinaryNumber // diff --git a/src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs b/src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs index 88d6a915b54419..e84a3257a3c646 100644 --- a/src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs +++ b/src/libraries/System.Runtime.Numerics/tests/BigIntegerTests.GenericMath.cs @@ -44,6 +44,49 @@ public class BigIntegerTests_GenericMath 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, + }); + + internal static readonly BigInteger Int128MaxValue = new BigInteger(new byte[] { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0x7F, + }); + + internal static readonly BigInteger Int128MaxValuePlusOne = new BigInteger(new byte[] { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, + }, isUnsigned: true); + + internal static readonly BigInteger Int128MinValue = new BigInteger(new byte[] { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, + }); + + internal static readonly BigInteger Int128MinValueMinusOne = new BigInteger(new byte[] { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0x7F, + 0xFF, 0xFF, 0xFF, 0xFF + }); + + internal static readonly BigInteger Int128MinValuePlusOne = new BigInteger(new byte[] { + 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x80, + }); + + internal static readonly BigInteger Int128MinValueTimesTwo = new BigInteger(new byte[] { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, }); @@ -93,6 +136,13 @@ public class BigIntegerTests_GenericMath 0x01 }, isUnsigned: true); + internal static readonly BigInteger UInt128MaxValue = new BigInteger(new byte[] { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + }, isUnsigned: true); + internal static readonly BigInteger Zero = new BigInteger(0); [Fact] @@ -327,6 +377,14 @@ public static void GetShortestBitLengthTest() Assert.Equal(0x40, BinaryIntegerHelper.GetShortestBitLength(Int64MaxValuePlusOne)); Assert.Equal(0x40, BinaryIntegerHelper.GetShortestBitLength(UInt64MaxValue)); + + Assert.Equal(0x7F, BinaryIntegerHelper.GetShortestBitLength(Int128MaxValue)); + Assert.Equal(0x80, BinaryIntegerHelper.GetShortestBitLength(Int128MinValue)); + Assert.Equal(0x81, BinaryIntegerHelper.GetShortestBitLength(Int128MinValueMinusOne)); + Assert.Equal(0x80, BinaryIntegerHelper.GetShortestBitLength(Int128MinValuePlusOne)); + Assert.Equal(0x81, BinaryIntegerHelper.GetShortestBitLength(Int128MinValueTimesTwo)); + Assert.Equal(0x80, BinaryIntegerHelper.GetShortestBitLength(Int128MaxValuePlusOne)); + Assert.Equal(0x80, BinaryIntegerHelper.GetShortestBitLength(UInt128MaxValue)); } [Fact] @@ -1397,12 +1455,87 @@ public static void GetByteCountTest() Assert.Equal(8, BinaryIntegerHelper.GetByteCount(Int64MaxValuePlusOne)); Assert.Equal(8, BinaryIntegerHelper.GetByteCount(UInt64MaxValue)); + + Assert.Equal(16, BinaryIntegerHelper.GetByteCount(Int128MaxValue)); + Assert.Equal(16, BinaryIntegerHelper.GetByteCount(Int128MinValue)); + Assert.Equal(20, BinaryIntegerHelper.GetByteCount(Int128MinValueMinusOne)); + Assert.Equal(16, BinaryIntegerHelper.GetByteCount(Int128MinValuePlusOne)); + Assert.Equal(20, BinaryIntegerHelper.GetByteCount(Int128MinValueTimesTwo)); + Assert.Equal(16, BinaryIntegerHelper.GetByteCount(Int128MaxValuePlusOne)); + Assert.Equal(16, BinaryIntegerHelper.GetByteCount(UInt128MaxValue)); + } + + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[20]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Zero, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 4).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(One, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.Slice(0, 4).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int64MaxValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 8).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int64MinValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 8).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 4).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int64MaxValuePlusOne, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 8).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(UInt64MaxValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 8).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MinValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MinValueMinusOne, destination, out bytesWritten)); + Assert.Equal(20, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 20).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MinValuePlusOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MinValueTimesTwo, destination, out bytesWritten)); + Assert.Equal(20, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 20).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MaxValuePlusOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(UInt128MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 16).ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); } [Fact] public static void TryWriteLittleEndianTest() { - Span destination = stackalloc byte[8]; + Span destination = stackalloc byte[20]; int bytesWritten = 0; Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Zero, destination, out bytesWritten)); @@ -1433,9 +1566,37 @@ public static void TryWriteLittleEndianTest() Assert.Equal(8, bytesWritten); Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 8).ToArray()); + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MinValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MinValueMinusOne, destination, out bytesWritten)); + Assert.Equal(20, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 20).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MinValuePlusOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MinValueTimesTwo, destination, out bytesWritten)); + Assert.Equal(20, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 20).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(Int128MaxValuePlusOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }, destination.Slice(0, 16).ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteLittleEndian(UInt128MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.Slice(0, 16).ToArray()); + Assert.False(BinaryIntegerHelper.TryWriteLittleEndian(default, Span.Empty, out bytesWritten)); Assert.Equal(0, bytesWritten); - Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); } [Fact] diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 8afd04908ba66e..dc0468b9b79c1c 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -768,7 +768,8 @@ public static void SetByte(System.Array array, int index, byte value) { } static byte System.Numerics.IAdditionOperators.operator +(byte left, byte right) { throw null; } static byte System.Numerics.IAdditionOperators.operator checked +(byte left, byte right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static byte System.Numerics.IBitwiseOperators.operator &(byte left, byte right) { throw null; } static byte System.Numerics.IBitwiseOperators.operator |(byte left, byte right) { throw null; } @@ -834,16 +835,11 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx static char System.Numerics.IMultiplicativeIdentity.MultiplicativeIdentity { get { throw null; } } static char System.Numerics.INumberBase.One { get { throw null; } } static char System.Numerics.INumberBase.Zero { get { throw null; } } - public static char Clamp(char value, char min, char max) { throw null; } public int CompareTo(char value) { throw null; } public int CompareTo(object? value) { throw null; } public static string ConvertFromUtf32(int utf32) { throw null; } public static int ConvertToUtf32(char highSurrogate, char lowSurrogate) { throw null; } public static int ConvertToUtf32(string s, int index) { throw null; } - public static char CreateChecked(TOther value) where TOther : System.Numerics.INumber { throw null; } - public static char CreateSaturating(TOther value) where TOther : System.Numerics.INumber { throw null; } - public static char CreateTruncating(TOther value) where TOther : System.Numerics.INumber { throw null; } - public static (char Quotient, char Remainder) DivRem(char left, char right) { throw null; } public bool Equals(char obj) { throw null; } public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; } public override int GetHashCode() { throw null; } @@ -878,7 +874,6 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx public static bool IsLowSurrogate(string s, int index) { throw null; } public static bool IsNumber(char c) { throw null; } public static bool IsNumber(string s, int index) { throw null; } - public static bool IsPow2(char value) { throw null; } public static bool IsPunctuation(char c) { throw null; } public static bool IsPunctuation(string s, int index) { throw null; } public static bool IsSeparator(char c) { throw null; } @@ -893,15 +888,7 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx public static bool IsUpper(string s, int index) { throw null; } public static bool IsWhiteSpace(char c) { throw null; } public static bool IsWhiteSpace(string s, int index) { throw null; } - public static char LeadingZeroCount(char value) { throw null; } - public static char Log2(char value) { throw null; } - public static char Max(char x, char y) { throw null; } - public static char Min(char x, char y) { throw null; } public static char Parse(string s) { throw null; } - public static char PopCount(char value) { throw null; } - public static char RotateLeft(char value, int rotateAmount) { throw null; } - public static char RotateRight(char value, int rotateAmount) { throw null; } - public static int Sign(char value) { throw null; } bool System.IConvertible.ToBoolean(System.IFormatProvider? provider) { throw null; } byte System.IConvertible.ToByte(System.IFormatProvider? provider) { throw null; } char System.IConvertible.ToChar(System.IFormatProvider? provider) { throw null; } @@ -925,9 +912,18 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx static bool System.ISpanParsable.TryParse(System.ReadOnlySpan s, System.IFormatProvider? provider, out char result) { throw null; } static char System.Numerics.IAdditionOperators.operator +(char left, char right) { throw null; } static char System.Numerics.IAdditionOperators.operator checked +(char left, char right) { throw null; } + static (char Quotient, char Remainder) System.Numerics.IBinaryInteger.DivRem(char left, char right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + static char System.Numerics.IBinaryInteger.LeadingZeroCount(char value) { throw null; } + static char System.Numerics.IBinaryInteger.PopCount(char value) { throw null; } + static char System.Numerics.IBinaryInteger.RotateLeft(char value, int rotateAmount) { throw null; } + static char System.Numerics.IBinaryInteger.RotateRight(char value, int rotateAmount) { throw null; } + static char System.Numerics.IBinaryInteger.TrailingZeroCount(char value) { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } + static bool System.Numerics.IBinaryNumber.IsPow2(char value) { throw null; } + static char System.Numerics.IBinaryNumber.Log2(char value) { throw null; } static char System.Numerics.IBitwiseOperators.operator &(char left, char right) { throw null; } static char System.Numerics.IBitwiseOperators.operator |(char left, char right) { throw null; } static char System.Numerics.IBitwiseOperators.operator ^(char left, char right) { throw null; } @@ -948,12 +944,20 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx static char System.Numerics.IMultiplyOperators.operator checked *(char left, char right) { throw null; } static char System.Numerics.IMultiplyOperators.operator *(char left, char right) { throw null; } static char System.Numerics.INumber.Abs(char value) { throw null; } + static char System.Numerics.INumber.Clamp(char value, char min, char max) { throw null; } static char System.Numerics.INumber.CopySign(char value, char sign) { throw null; } + static char System.Numerics.INumber.CreateChecked(TOther value) { throw null; } + static char System.Numerics.INumber.CreateSaturating(TOther value) { throw null; } + static char System.Numerics.INumber.CreateTruncating(TOther value) { throw null; } static bool System.Numerics.INumber.IsNegative(char value) { throw null; } + static char System.Numerics.INumber.Max(char x, char y) { throw null; } static char System.Numerics.INumber.MaxMagnitude(char x, char y) { throw null; } + static char System.Numerics.INumber.Min(char x, char y) { throw null; } static char System.Numerics.INumber.MinMagnitude(char x, char y) { throw null; } static char System.Numerics.INumber.Parse(System.ReadOnlySpan s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; } static char System.Numerics.INumber.Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; } + static int System.Numerics.INumber.Sign(char value) { throw null; } + static bool System.Numerics.INumber.TryCreate(TOther value, out char result) { throw null; } static bool System.Numerics.INumber.TryParse(System.ReadOnlySpan s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out char result) { throw null; } static bool System.Numerics.INumber.TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out char result) { throw null; } static char System.Numerics.IShiftOperators.operator <<(char value, int shiftAmount) { throw null; } @@ -973,8 +977,6 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx public static char ToUpper(char c) { throw null; } public static char ToUpper(char c, System.Globalization.CultureInfo culture) { throw null; } public static char ToUpperInvariant(char c) { throw null; } - public static char TrailingZeroCount(char value) { throw null; } - public static bool TryCreate(TOther value, out char result) where TOther : System.Numerics.INumber { throw null; } public static bool TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? s, out char result) { throw null; } } public sealed partial class CharEnumerator : System.Collections.Generic.IEnumerator, System.Collections.IEnumerator, System.ICloneable, System.IDisposable @@ -1941,10 +1943,12 @@ public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, S static decimal System.Numerics.IDecrementOperators.operator checked --(decimal value) { throw null; } static decimal System.Numerics.IDivisionOperators.operator checked /(decimal left, decimal right) { throw null; } int System.Numerics.IFloatingPoint.GetExponentByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } int System.Numerics.IFloatingPoint.GetSignificandByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { throw null; } static decimal System.Numerics.IIncrementOperators.operator checked ++(decimal value) { throw null; } static decimal System.Numerics.ISubtractionOperators.operator checked -(decimal left, decimal right) { throw null; } @@ -2165,10 +2169,12 @@ public DivideByZeroException(string? message, System.Exception? innerException) static double System.Numerics.IDivisionOperators.operator checked /(double left, double right) { throw null; } static double System.Numerics.IDivisionOperators.operator /(double left, double right) { throw null; } int System.Numerics.IFloatingPoint.GetExponentByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } int System.Numerics.IFloatingPoint.GetSignificandByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { throw null; } static double System.Numerics.IIncrementOperators.operator checked ++(double value) { throw null; } static double System.Numerics.IIncrementOperators.operator ++(double value) { throw null; } @@ -2759,10 +2765,12 @@ public GopherStyleUriParser() { } static System.Half System.Numerics.IDecrementOperators.operator checked --(System.Half value) { throw null; } static System.Half System.Numerics.IDivisionOperators.operator checked /(System.Half left, System.Half right) { throw null; } int System.Numerics.IFloatingPoint.GetExponentByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } int System.Numerics.IFloatingPoint.GetSignificandByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { throw null; } static System.Half System.Numerics.IIncrementOperators.operator checked ++(System.Half value) { throw null; } static System.Half System.Numerics.IMultiplyOperators.operator checked *(System.Half left, System.Half right) { throw null; } @@ -3044,7 +3052,8 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep public static System.Int128 RotateRight(System.Int128 value, int rotateAmount) { throw null; } public static int Sign(System.Int128 value) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } public override string ToString() { throw null; } public string ToString(System.IFormatProvider? provider) { throw null; } @@ -3121,7 +3130,8 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static short System.Numerics.IAdditionOperators.operator +(short left, short right) { throw null; } static short System.Numerics.IAdditionOperators.operator checked +(short left, short right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static short System.Numerics.IBitwiseOperators.operator &(short left, short right) { throw null; } static short System.Numerics.IBitwiseOperators.operator |(short left, short right) { throw null; } @@ -3225,7 +3235,8 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static int System.Numerics.IAdditionOperators.operator +(int left, int right) { throw null; } static int System.Numerics.IAdditionOperators.operator checked +(int left, int right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static int System.Numerics.IBitwiseOperators.operator &(int left, int right) { throw null; } static int System.Numerics.IBitwiseOperators.operator |(int left, int right) { throw null; } @@ -3329,7 +3340,8 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static long System.Numerics.IAdditionOperators.operator +(long left, long right) { throw null; } static long System.Numerics.IAdditionOperators.operator checked +(long left, long right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static long System.Numerics.IBitwiseOperators.operator &(long left, long right) { throw null; } static long System.Numerics.IBitwiseOperators.operator |(long left, long right) { throw null; } @@ -3437,7 +3449,8 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static nint System.Numerics.IAdditionOperators.operator +(nint left, nint right) { throw null; } static nint System.Numerics.IAdditionOperators.operator checked +(nint left, nint right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static nint System.Numerics.IBitwiseOperators.operator &(nint left, nint right) { throw null; } static nint System.Numerics.IBitwiseOperators.operator |(nint left, nint right) { throw null; } @@ -4349,7 +4362,8 @@ public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, S static sbyte System.Numerics.IAdditionOperators.operator +(sbyte left, sbyte right) { throw null; } static sbyte System.Numerics.IAdditionOperators.operator checked +(sbyte left, sbyte right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static sbyte System.Numerics.IBitwiseOperators.operator &(sbyte left, sbyte right) { throw null; } static sbyte System.Numerics.IBitwiseOperators.operator |(sbyte left, sbyte right) { throw null; } @@ -4535,10 +4549,12 @@ public SerializableAttribute() { } static float System.Numerics.IDivisionOperators.operator checked /(float left, float right) { throw null; } static float System.Numerics.IDivisionOperators.operator /(float left, float right) { throw null; } int System.Numerics.IFloatingPoint.GetExponentByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetExponentShortestBitLength() { throw null; } int System.Numerics.IFloatingPoint.GetSignificandByteCount() { throw null; } - long System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + int System.Numerics.IFloatingPoint.GetSignificandBitLength() { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteExponentBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteExponentLittleEndian(Span destination, out int bytesWritten) { throw null; } + bool System.Numerics.IFloatingPoint.TryWriteSignificandBigEndian(Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IFloatingPoint.TryWriteSignificandLittleEndian(Span destination, out int bytesWritten) { throw null; } static float System.Numerics.IIncrementOperators.operator checked ++(float value) { throw null; } static float System.Numerics.IIncrementOperators.operator ++(float value) { throw null; } @@ -5869,7 +5885,8 @@ public TypeUnloadedException(string? message, System.Exception? innerException) public static System.UInt128 RotateRight(System.UInt128 value, int rotateAmount) { throw null; } public static int Sign(System.UInt128 value) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static System.UInt128 System.Numerics.INumber.Abs(System.UInt128 value) { throw null; } static System.UInt128 System.Numerics.INumber.CopySign(System.UInt128 value, System.UInt128 sign) { throw null; } @@ -5946,7 +5963,8 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static ushort System.Numerics.IAdditionOperators.operator +(ushort left, ushort right) { throw null; } static ushort System.Numerics.IAdditionOperators.operator checked +(ushort left, ushort right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static ushort System.Numerics.IBitwiseOperators.operator &(ushort left, ushort right) { throw null; } static ushort System.Numerics.IBitwiseOperators.operator |(ushort left, ushort right) { throw null; } @@ -6050,7 +6068,8 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static uint System.Numerics.IAdditionOperators.operator +(uint left, uint right) { throw null; } static uint System.Numerics.IAdditionOperators.operator checked +(uint left, uint right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static uint System.Numerics.IBitwiseOperators.operator &(uint left, uint right) { throw null; } static uint System.Numerics.IBitwiseOperators.operator |(uint left, uint right) { throw null; } @@ -6154,7 +6173,8 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static ulong System.Numerics.IAdditionOperators.operator +(ulong left, ulong right) { throw null; } static ulong System.Numerics.IAdditionOperators.operator checked +(ulong left, ulong right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static ulong System.Numerics.IBitwiseOperators.operator &(ulong left, ulong right) { throw null; } static ulong System.Numerics.IBitwiseOperators.operator |(ulong left, ulong right) { throw null; } @@ -6261,7 +6281,8 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static nuint System.Numerics.IAdditionOperators.operator +(nuint left, nuint right) { throw null; } static nuint System.Numerics.IAdditionOperators.operator checked +(nuint left, nuint right) { throw null; } int System.Numerics.IBinaryInteger.GetByteCount() { throw null; } - long System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + int System.Numerics.IBinaryInteger.GetShortestBitLength() { throw null; } + bool System.Numerics.IBinaryInteger.TryWriteBigEndian(System.Span destination, out int bytesWritten) { throw null; } bool System.Numerics.IBinaryInteger.TryWriteLittleEndian(System.Span destination, out int bytesWritten) { throw null; } static nuint System.Numerics.IBitwiseOperators.operator &(nuint left, nuint right) { throw null; } static nuint System.Numerics.IBitwiseOperators.operator |(nuint left, nuint right) { throw null; } @@ -9905,13 +9926,17 @@ public partial interface IBinaryInteger : System.IComparable, System.ICom { static abstract (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right); int GetByteCount(); - long GetShortestBitLength(); + int GetShortestBitLength(); static abstract TSelf LeadingZeroCount(TSelf value); static abstract TSelf PopCount(TSelf value); static abstract TSelf RotateLeft(TSelf value, int rotateAmount); static abstract TSelf RotateRight(TSelf value, int rotateAmount); static abstract TSelf TrailingZeroCount(TSelf value); + bool TryWriteBigEndian(System.Span destination, out int bytesWritten); bool TryWriteLittleEndian(System.Span destination, out int bytesWritten); + int WriteBigEndian(byte[] destination) { throw null; } + int WriteBigEndian(byte[] destination, int startIndex) { throw null; } + int WriteBigEndian(System.Span destination) { throw null; } int WriteLittleEndian(byte[] destination) { throw null; } int WriteLittleEndian(byte[] destination, int startIndex) { throw null; } int WriteLittleEndian(System.Span destination) { throw null; } @@ -9994,19 +10019,27 @@ public partial interface IFloatingPoint : System.IComparable, System.ICom static abstract TSelf Ceiling(TSelf x); static abstract TSelf Floor(TSelf x); int GetExponentByteCount(); - long GetExponentShortestBitLength(); + int GetExponentShortestBitLength(); int GetSignificandByteCount(); - long GetSignificandBitLength(); + int GetSignificandBitLength(); static abstract TSelf Round(TSelf x); static abstract TSelf Round(TSelf x, int digits); static abstract TSelf Round(TSelf x, int digits, System.MidpointRounding mode); static abstract TSelf Round(TSelf x, System.MidpointRounding mode); static abstract TSelf Truncate(TSelf x); + bool TryWriteExponentBigEndian(Span destination, out int bytesWritten); bool TryWriteExponentLittleEndian(Span destination, out int bytesWritten); + bool TryWriteSignificandBigEndian(Span destination, out int bytesWritten); bool TryWriteSignificandLittleEndian(Span destination, out int bytesWritten); + int WriteExponentBigEndian(byte[] destination) { throw null; } + int WriteExponentBigEndian(byte[] destination, int startIndex) { throw null; } + int WriteExponentBigEndian(System.Span destination) { throw null; } int WriteExponentLittleEndian(byte[] destination) { throw null; } int WriteExponentLittleEndian(byte[] destination, int startIndex) { throw null; } int WriteExponentLittleEndian(System.Span destination) { throw null; } + int WriteSignificandBigEndian(byte[] destination) { throw null; } + int WriteSignificandBigEndian(byte[] destination, int startIndex) { throw null; } + int WriteSignificandBigEndian(System.Span destination) { throw null; } int WriteSignificandLittleEndian(byte[] destination) { throw null; } int WriteSignificandLittleEndian(byte[] destination, int startIndex) { throw null; } int WriteSignificandLittleEndian(System.Span destination) { throw null; } diff --git a/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs index f824be8e34a152..9f3e7b4130abad 100644 --- a/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs @@ -1081,6 +1081,37 @@ public static void GetByteCountTest() Assert.Equal(1, BinaryIntegerHelper.GetByteCount((byte)0xFF)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((byte)0x00, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((byte)0x01, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((byte)0x7F, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((byte)0x80, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((byte)0xFF, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs index 6a19c753a715af..502e26089a2a34 100644 --- a/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs @@ -1080,6 +1080,37 @@ public static void GetByteCountTest() Assert.Equal(2, BinaryIntegerHelper.GetByteCount((char)0xFFFF)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((char)0x0000, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((char)0x0001, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((char)0x7FFF, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((char)0x8000, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((char)0xFFFF, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/DecimalTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/DecimalTests.GenericMath.cs index 77a6ae8d50c22e..6b843275f25116 100644 --- a/src/libraries/System.Runtime/tests/System/DecimalTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/DecimalTests.GenericMath.cs @@ -1125,6 +1125,41 @@ public static void GetSignificandBitLengthTest() Assert.Equal(96, FloatingPointHelper.GetSignificandBitLength(decimal.MaxValue)); } + [Fact] + public static void TryWriteExponentBigEndianTest() + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(decimal.MinValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5F }, destination.ToArray()); // +95 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-1.0m, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5E }, destination.ToArray()); // +94 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-0.0m, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5E }, destination.ToArray()); // +94 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(0.0m, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5E }, destination.ToArray()); // +94 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(1.0m, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5E }, destination.ToArray()); // +94 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(decimal.MaxValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x5F }, destination.ToArray()); // +95 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x5F }, destination.ToArray()); + } + [Fact] public static void TryWriteExponentLittleEndianTest() { @@ -1160,6 +1195,41 @@ public static void TryWriteExponentLittleEndianTest() Assert.Equal(new byte[] { 0x5F }, destination.ToArray()); } + [Fact] + public static void TryWriteSignificandBigEndianTest() + { + Span destination = stackalloc byte[12]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(decimal.MinValue, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-1.0m, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-0.0m, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(0.0m, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(1.0m, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(decimal.MaxValue, destination, out bytesWritten)); + Assert.Equal(12, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteSignificandLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/DoubleTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/DoubleTests.GenericMath.cs index 6c9fc81275c77f..34b2d3ac71e2b7 100644 --- a/src/libraries/System.Runtime/tests/System/DoubleTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/DoubleTests.GenericMath.cs @@ -1418,6 +1418,77 @@ public static void GetSignificandBitLengthTest() Assert.Equal(53, FloatingPointHelper.GetSignificandBitLength(double.PositiveInfinity)); } + [Fact] + public static void TryWriteExponentBigEndianTest() + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.MinValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xFF }, destination.ToArray()); // +1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-1.0, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x02 }, destination.ToArray()); // -1022 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-double.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-0.0, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.NaN, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(0.0, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x01 }, destination.ToArray()); // -1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFC, 0x02 }, destination.ToArray()); // -1022 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(1.0, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.MaxValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xff }, destination.ToArray()); // +1023 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(double.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); // +1024 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + } + [Fact] public static void TryWriteExponentLittleEndianTest() { @@ -1489,6 +1560,77 @@ public static void TryWriteExponentLittleEndianTest() Assert.Equal(new byte[] { 0x00, 0x04 }, destination.ToArray()); } + [Fact] + public static void TryWriteSignificandBigEndianTest() + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.MinValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-1.0, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-double.Epsilon, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-0.0, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.NaN, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(0.0, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.Epsilon, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(1.0, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.MaxValue, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(double.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + } + [Fact] public static void TryWriteSignificandLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/HalfTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/HalfTests.GenericMath.cs index 246fe53b8c8402..a71f7351bec9ec 100644 --- a/src/libraries/System.Runtime/tests/System/HalfTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/HalfTests.GenericMath.cs @@ -1436,6 +1436,77 @@ public static void GetSignificandBitLengthTest() Assert.Equal(11, FloatingPointHelper.GetSignificandBitLength(Half.PositiveInfinity)); } + [Fact] + public static void TryWriteExponentBigEndianTest() + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x10 }, destination.ToArray()); // +16 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.MinValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x0F }, destination.ToArray()); // +15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF2 }, destination.ToArray()); // -14 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-Half.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.NaN, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x10 }, destination.ToArray()); // +16 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF1 }, destination.ToArray()); // -15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xF2 }, destination.ToArray()); // -14 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.MaxValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x0F }, destination.ToArray()); // +15 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(Half.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x10 }, destination.ToArray()); // +16 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x10 }, destination.ToArray()); + } + [Fact] public static void TryWriteExponentLittleEndianTest() { @@ -1507,6 +1578,77 @@ public static void TryWriteExponentLittleEndianTest() Assert.Equal(new byte[] { 0x10 }, destination.ToArray()); } + [Fact] + public static void TryWriteSignificandBigEndianTest() + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.MinValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x07, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-Half.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(NegativeZero, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.NaN, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x06, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveZero, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.Epsilon, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x03, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(PositiveOne, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.MaxValue, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x07, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(Half.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x04, 0x00 }, destination.ToArray()); + } + [Fact] public static void TryWriteSignificandLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int128Tests.GenericMath.cs index 1ff3b1e917a398..1d5aba78ee9e41 100644 --- a/src/libraries/System.Runtime/tests/System/Int128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int128Tests.GenericMath.cs @@ -177,6 +177,37 @@ public static void GetShortestBitLengthTest() Assert.Equal(0x01, BinaryIntegerHelper.GetShortestBitLength(NegativeOne)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[16]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Zero, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(One, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(MinValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(NegativeOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs index 38a8be51f3b612..fc5888901cd987 100644 --- a/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs @@ -1087,6 +1087,37 @@ public static void GetByteCountTest() Assert.Equal(2, BinaryIntegerHelper.GetByteCount(unchecked((short)0xFFFF))); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((short)0x0000, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((short)0x0001, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((short)0x7FFF, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((short)0x8000), destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((short)0xFFFF), destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs index 164fb6d9e609f6..e4fcdde621dfe4 100644 --- a/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs @@ -1087,6 +1087,37 @@ public static void GetByteCountTest() Assert.Equal(4, BinaryIntegerHelper.GetByteCount(unchecked((int)0xFFFFFFFF))); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((int)0x00000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((int)0x00000001, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((int)0x7FFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((int)0x80000000), destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((int)0xFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs index 805964b0d34874..6eb74bbb2168eb 100644 --- a/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs @@ -1087,6 +1087,37 @@ public static void GetByteCountTest() Assert.Equal(8, BinaryIntegerHelper.GetByteCount(unchecked((long)0xFFFFFFFFFFFFFFFF))); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((long)0x0000000000000000, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((long)0x0000000000000001, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((long)0x7FFFFFFFFFFFFFFF, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((long)0x8000000000000000), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((long)0xFFFFFFFFFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs index 61f3828963587c..db87ae156cd036 100644 --- a/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs @@ -1685,6 +1685,69 @@ public static void GetByteCountTest() } } + [Fact] + public static void TryWriteBigEndianTest() + { + if (Environment.Is64BitProcess) + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0x0000000000000000), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0x0000000000000001), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0x7FFFFFFFFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0x8000000000000000), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0xFFFFFFFFFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + else + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nint)0x00000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nint)0x00000001, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nint)0x7FFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0x80000000), destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nint)0xFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs index 9e75484b4005ba..34a1c8d5940938 100644 --- a/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs @@ -1087,6 +1087,37 @@ public static void GetByteCountTest() Assert.Equal(1, BinaryIntegerHelper.GetByteCount(unchecked((sbyte)0xFF))); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((sbyte)0x00, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((sbyte)0x01, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((sbyte)0x7F, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((sbyte)0x80), destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((sbyte)0xFF), destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/SingleTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/SingleTests.GenericMath.cs index a5cf58f14ccaa3..63309cf1ed497f 100644 --- a/src/libraries/System.Runtime/tests/System/SingleTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/SingleTests.GenericMath.cs @@ -1418,6 +1418,77 @@ public static void GetSignificandBitLengthTest() Assert.Equal(24, FloatingPointHelper.GetSignificandBitLength(float.PositiveInfinity)); } + [Fact] + public static void TryWriteExponentBigEndianTest() + { + Span destination = stackalloc byte[1]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.MinValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); // +127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-1.0f, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x82 }, destination.ToArray()); // -126 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-float.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(-0.0f, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.NaN, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(0.0f, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.Epsilon, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x81 }, destination.ToArray()); // -127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x82 }, destination.ToArray()); // -126 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(1.0f, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x00 }, destination.ToArray()); // +0 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.MaxValue, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x7F }, destination.ToArray()); // +127 + + Assert.True(FloatingPointHelper.TryWriteExponentBigEndian(float.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(1, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); // -128 + + Assert.False(FloatingPointHelper.TryWriteExponentBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); + } + [Fact] public static void TryWriteExponentLittleEndianTest() { @@ -1489,6 +1560,77 @@ public static void TryWriteExponentLittleEndianTest() Assert.Equal(new byte[] { 0x80 }, destination.ToArray()); } + [Fact] + public static void TryWriteSignificandBigEndianTest() + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.NegativeInfinity, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.MinValue, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xFF, 0xFF, 0xff }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-1.0f, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MinNormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x7F, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-float.Epsilon, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(-0.0f, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.NaN, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xC0, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(0.0f, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.Epsilon, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MaxSubnormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x7F, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(MinNormal, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(1.0f, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.MaxValue, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(FloatingPointHelper.TryWriteSignificandBigEndian(float.PositiveInfinity, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + + Assert.False(FloatingPointHelper.TryWriteSignificandBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x80, 0x00, 0x00 }, destination.ToArray()); + } + [Fact] public static void TryWriteSignificandLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt128Tests.GenericMath.cs index 8177f0fe9d696f..74ad26be15c309 100644 --- a/src/libraries/System.Runtime/tests/System/UInt128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt128Tests.GenericMath.cs @@ -177,6 +177,37 @@ public static void GetShortestBitLengthTest() Assert.Equal(0x80, BinaryIntegerHelper.GetShortestBitLength(MaxValue)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[16]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Zero, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(One, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(Int128MaxValuePlusOne, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(MaxValue, destination, out bytesWritten)); + Assert.Equal(16, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs index e456553c3df740..037860214726e0 100644 --- a/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs @@ -1081,6 +1081,37 @@ public static void GetByteCountTest() Assert.Equal(2, BinaryIntegerHelper.GetByteCount((ushort)0xFFFF)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[2]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ushort)0x0000, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ushort)0x0001, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ushort)0x7FFF, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ushort)0x8000, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ushort)0xFFFF, destination, out bytesWritten)); + Assert.Equal(2, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs index 5815c07b9c8c3e..514613dda38d19 100644 --- a/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs @@ -1082,6 +1082,37 @@ public static void GetByteCountTest() Assert.Equal(4, BinaryIntegerHelper.GetByteCount((uint)0xFFFFFFFF)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((uint)0x00000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((uint)0x00000001, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((uint)0x7FFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((uint)0x80000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((uint)0xFFFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs index 6765bee80e33af..3dcb11716f4e0e 100644 --- a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs @@ -1081,6 +1081,37 @@ public static void GetByteCountTest() Assert.Equal(8, BinaryIntegerHelper.GetByteCount((ulong)0xFFFFFFFFFFFFFFFF)); } + [Fact] + public static void TryWriteBigEndianTest() + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ulong)0x0000000000000000, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ulong)0x0000000000000001, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ulong)0x7FFFFFFFFFFFFFFF, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ulong)0x8000000000000000, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((ulong)0xFFFFFFFFFFFFFFFF, destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + [Fact] public static void TryWriteLittleEndianTest() { diff --git a/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs index 6bc58d33b85158..62731f0a3d4cc2 100644 --- a/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs @@ -1633,6 +1633,69 @@ public static void GetByteCountTest() } } + [Fact] + public static void TryWriteBigEndianTest() + { + if (Environment.Is64BitProcess) + { + Span destination = stackalloc byte[8]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nuint)0x0000000000000000), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nuint)0x0000000000000001), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nuint)0x7FFFFFFFFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nuint)0x8000000000000000), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian(unchecked((nuint)0xFFFFFFFFFFFFFFFF), destination, out bytesWritten)); + Assert.Equal(8, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + else + { + Span destination = stackalloc byte[4]; + int bytesWritten = 0; + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nuint)0x00000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nuint)0x00000001, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x01 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nuint)0x7FFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x7F, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nuint)0x80000000, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0x80, 0x00, 0x00, 0x00 }, destination.ToArray()); + + Assert.True(BinaryIntegerHelper.TryWriteBigEndian((nuint)0xFFFFFFFF, destination, out bytesWritten)); + Assert.Equal(4, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + + Assert.False(BinaryIntegerHelper.TryWriteBigEndian(default, Span.Empty, out bytesWritten)); + Assert.Equal(0, bytesWritten); + Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, destination.ToArray()); + } + } + [Fact] public static void TryWriteLittleEndianTest() {