diff --git a/AssetRipper.TextureDecoder.ColorGenerator/NumericConversionGenerator.cs b/AssetRipper.TextureDecoder.ColorGenerator/NumericConversionGenerator.cs index d01141b..0a980a5 100644 --- a/AssetRipper.TextureDecoder.ColorGenerator/NumericConversionGenerator.cs +++ b/AssetRipper.TextureDecoder.ColorGenerator/NumericConversionGenerator.cs @@ -82,14 +82,14 @@ internal static class NumericConversionGenerator writer.WriteLine("if (typeof(TFrom) == typeof(TTo))"); using (new CurlyBrackets(writer)) { - writer.WriteLine("return Unsafe.As(ref value);"); + writer.WriteLine("return Unsafe.BitCast(value);"); } foreach (CSharpPrimitives.Data from in CSharpPrimitives.List) { writer.WriteLine($"else if (typeof(TFrom) == typeof({from.LangName}))"); using (new CurlyBrackets(writer)) { - writer.WriteLine($"return {ConvertMethodName(from.Type)}(Unsafe.As(ref value));"); + writer.WriteLine($"return {ConvertMethodName(from.Type)}(Unsafe.BitCast(value));"); } } writer.WriteLine("else"); @@ -133,32 +133,32 @@ internal static class NumericConversionGenerator { if (from == to) { - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref value);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(value);"); } else if (to.HasPointerSize(out CSharpPrimitives.Data? bit32To, out CSharpPrimitives.Data? bit64To)) { using (new If(writer, "IntPtr.Size == sizeof(int)")) { writer.WriteLine($"{to.LangName} converted = ({to.LangName}){methodName}<{bit32To.LangName}>(value);"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } using (new Else(writer)) { writer.WriteLine($"{to.LangName} converted = ({to.LangName}){methodName}<{bit64To.LangName}>(value);"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } } else if (to.IsSignedInteger(out CSharpPrimitives.Data? unsignedTo)) { writer.WriteLine($"{to.LangName} converted = {ChangeSign}({methodName}<{unsignedTo.LangName}>(value));"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } else if (from.IsFloatingPoint) { if (to.IsFloatingPoint) { writer.WriteLine($"{to.LangName} converted = ({to.LangName})value;"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } else { @@ -176,7 +176,7 @@ internal static class NumericConversionGenerator string maxValue = to.MaxValue; writer.WriteLine($"{from.LangName} x = value * ({from.LangName}){maxValue};"); writer.WriteLine($"{to.LangName} converted = ({from.LangName}){maxValue} < x ? {maxValue} : (x > ({from.LangName}){minValue} ? ({to.LangName})x : {minValue});"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } } } @@ -191,12 +191,12 @@ internal static class NumericConversionGenerator writer.WriteComment($"There isn't enough precision to convert from {from.LangName} to {to.LangName}, so we convert to {conversionType.LangName} first."); writer.WriteLine($"{conversionType.LangName} x = {methodName}<{conversionType.LangName}>(value);"); writer.WriteLine($"{to.LangName} converted = ({to.LangName})x;"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } else { writer.WriteLine($"{to.LangName} converted = ({to.LangName})value / ({to.LangName}){from.MaxValue};"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } } else @@ -216,7 +216,7 @@ internal static class NumericConversionGenerator writer.Write($"(({conversionType})value << {i * from.Size * 8}) | "); } writer.WriteLine("value);"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } } else @@ -227,14 +227,14 @@ internal static class NumericConversionGenerator writer.WriteComment("This is a special case where we already know an optimal algorithm."); writer.WriteLine("uint x = (value * 255u + 32895u) >> 16;"); writer.WriteLine("byte converted = unchecked((byte)x);"); - writer.WriteLine("return Unsafe.As(ref converted);"); + writer.WriteLine("return Unsafe.BitCast(converted);"); } else if (from.Size < sizeof(double) && to.Size < sizeof(double)) { writer.WriteLine($"double interpolated = (double)value / (double){from.MaxValue};"); writer.WriteLine($"double exact = interpolated * (double){to.MaxValue};"); writer.WriteLine($"{to.LangName} converted = ({to.LangName})double.Round(exact, MidpointRounding.AwayFromZero);"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } else { @@ -245,7 +245,7 @@ internal static class NumericConversionGenerator { int offset = (from.Size / to.Size - 1) * to.Size * 8; writer.WriteLine($"{to.LangName} converted = ({to.LangName})(({conversionType})value >> {offset});"); - writer.WriteLine($"return Unsafe.As<{to.LangName}, TTo>(ref converted);"); + writer.WriteLine($"return Unsafe.BitCast<{to.LangName}, TTo>(converted);"); } } } @@ -276,7 +276,7 @@ internal static class NumericConversionGenerator using (new CurlyBrackets(writer)) { writer.WriteLine($"{from.LangName} value = {methodName}Safe<{from.LangName}>();"); - writer.WriteLine($"return Unsafe.As<{from.LangName}, T>(ref value);"); + writer.WriteLine($"return Unsafe.BitCast<{from.LangName}, T>(value);"); } } writer.WriteLine("else"); diff --git a/AssetRipper.TextureDecoder/Exr/BinaryWriterExtensions.cs b/AssetRipper.TextureDecoder/Exr/BinaryWriterExtensions.cs index 8b1d150..2a1283c 100644 --- a/AssetRipper.TextureDecoder/Exr/BinaryWriterExtensions.cs +++ b/AssetRipper.TextureDecoder/Exr/BinaryWriterExtensions.cs @@ -76,16 +76,16 @@ internal static class BinaryWriterExtensions switch (Unsafe.SizeOf()) { case sizeof(byte): - writer.Write(Unsafe.As(ref value)); + writer.Write(Unsafe.BitCast(value)); break; case sizeof(ushort): - writer.Write(Unsafe.As(ref value)); + writer.Write(Unsafe.BitCast(value)); break; case sizeof(uint): - writer.Write(Unsafe.As(ref value)); + writer.Write(Unsafe.BitCast(value)); break; case sizeof(ulong): - writer.Write(Unsafe.As(ref value)); + writer.Write(Unsafe.BitCast(value)); break; } } diff --git a/AssetRipper.TextureDecoder/Rgb/Color.cs b/AssetRipper.TextureDecoder/Rgb/Color.cs index a4fc41b..dfd871a 100644 --- a/AssetRipper.TextureDecoder/Rgb/Color.cs +++ b/AssetRipper.TextureDecoder/Rgb/Color.cs @@ -82,7 +82,7 @@ public static class Color { if (typeof(TThis) == typeof(TTarget)) { - return Unsafe.As(ref color); + return Unsafe.BitCast(color); } else { diff --git a/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs b/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs index 1116afd..e60a3ac 100644 --- a/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs +++ b/AssetRipper.TextureDecoder/Rgb/NumericConversion.cs @@ -42,7 +42,7 @@ public static partial class NumericConversion TFrom SignBit = TFrom.One << (Unsafe.SizeOf() * 8 - 1); TFrom converted = (SignBit ^ value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] @@ -56,6 +56,6 @@ public static partial class NumericConversion } TTo SignBit = TTo.One << (Unsafe.SizeOf() * 8 - 1); - return SignBit ^ Unsafe.As(ref value); + return SignBit ^ Unsafe.BitCast(value); } } diff --git a/AssetRipper.TextureDecoder/Rgb/NumericConversion.g.cs b/AssetRipper.TextureDecoder/Rgb/NumericConversion.g.cs index 99c734c..599b473 100644 --- a/AssetRipper.TextureDecoder/Rgb/NumericConversion.g.cs +++ b/AssetRipper.TextureDecoder/Rgb/NumericConversion.g.cs @@ -9,75 +9,75 @@ static partial class NumericConversion { if (typeof(TFrom) == typeof(TTo)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TFrom) == typeof(sbyte)) { - return ConvertSByte(Unsafe.As(ref value)); + return ConvertSByte(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(byte)) { - return ConvertByte(Unsafe.As(ref value)); + return ConvertByte(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(short)) { - return ConvertInt16(Unsafe.As(ref value)); + return ConvertInt16(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(ushort)) { - return ConvertUInt16(Unsafe.As(ref value)); + return ConvertUInt16(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(int)) { - return ConvertInt32(Unsafe.As(ref value)); + return ConvertInt32(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(uint)) { - return ConvertUInt32(Unsafe.As(ref value)); + return ConvertUInt32(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(nint)) { - return ConvertIntPtr(Unsafe.As(ref value)); + return ConvertIntPtr(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(nuint)) { - return ConvertUIntPtr(Unsafe.As(ref value)); + return ConvertUIntPtr(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(long)) { - return ConvertInt64(Unsafe.As(ref value)); + return ConvertInt64(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(ulong)) { - return ConvertUInt64(Unsafe.As(ref value)); + return ConvertUInt64(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(Int128)) { - return ConvertInt128(Unsafe.As(ref value)); + return ConvertInt128(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(UInt128)) { - return ConvertUInt128(Unsafe.As(ref value)); + return ConvertUInt128(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(Half)) { - return ConvertHalf(Unsafe.As(ref value)); + return ConvertHalf(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(float)) { - return ConvertSingle(Unsafe.As(ref value)); + return ConvertSingle(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(NFloat)) { - return ConvertNFloat(Unsafe.As(ref value)); + return ConvertNFloat(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(double)) { - return ConvertDouble(Unsafe.As(ref value)); + return ConvertDouble(Unsafe.BitCast(value)); } else if (typeof(TFrom) == typeof(decimal)) { - return ConvertDecimal(Unsafe.As(ref value)); + return ConvertDecimal(Unsafe.BitCast(value)); } else { @@ -97,16 +97,16 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertByte(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertByte(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { @@ -114,13 +114,13 @@ static partial class NumericConversion unchecked { ushort converted = (ushort)(((uint)value << 8) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertByte(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -128,7 +128,7 @@ static partial class NumericConversion unchecked { uint converted = (uint)(((uint)value << 24) | ((uint)value << 16) | ((uint)value << 8) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nint)) @@ -136,12 +136,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -149,18 +149,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertByte(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -168,13 +168,13 @@ static partial class NumericConversion unchecked { ulong converted = (ulong)(((ulong)value << 56) | ((ulong)value << 48) | ((ulong)value << 40) | ((ulong)value << 32) | ((ulong)value << 24) | ((ulong)value << 16) | ((ulong)value << 8) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertByte(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -182,7 +182,7 @@ static partial class NumericConversion unchecked { UInt128 converted = (UInt128)(((UInt128)value << 120) | ((UInt128)value << 112) | ((UInt128)value << 104) | ((UInt128)value << 96) | ((UInt128)value << 88) | ((UInt128)value << 80) | ((UInt128)value << 72) | ((UInt128)value << 64) | ((UInt128)value << 56) | ((UInt128)value << 48) | ((UInt128)value << 40) | ((UInt128)value << 32) | ((UInt128)value << 24) | ((UInt128)value << 16) | ((UInt128)value << 8) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Half)) @@ -190,35 +190,35 @@ static partial class NumericConversion // There isn't enough precision to convert from byte to Half, so we convert to float first. float x = ConvertByte(value); Half converted = (Half)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { float converted = (float)value / (float)byte.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertByte(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value / (double)byte.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value / (decimal)byte.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -238,7 +238,7 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertUInt16(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { @@ -246,21 +246,21 @@ static partial class NumericConversion // This is a special case where we already know an optimal algorithm. uint x = (value * 255u + 32895u) >> 16; byte converted = unchecked((byte)x); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertUInt16(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertUInt16(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -268,7 +268,7 @@ static partial class NumericConversion unchecked { uint converted = (uint)(((uint)value << 16) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nint)) @@ -276,12 +276,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -289,18 +289,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertUInt16(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -308,13 +308,13 @@ static partial class NumericConversion unchecked { ulong converted = (ulong)(((ulong)value << 48) | ((ulong)value << 32) | ((ulong)value << 16) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertUInt16(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -322,7 +322,7 @@ static partial class NumericConversion unchecked { UInt128 converted = (UInt128)(((UInt128)value << 112) | ((UInt128)value << 96) | ((UInt128)value << 80) | ((UInt128)value << 64) | ((UInt128)value << 48) | ((UInt128)value << 32) | ((UInt128)value << 16) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Half)) @@ -330,35 +330,35 @@ static partial class NumericConversion // There isn't enough precision to convert from ushort to Half, so we convert to float first. float x = ConvertUInt16(value); Half converted = (Half)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { float converted = (float)value / (float)ushort.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertUInt16(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value / (double)ushort.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value / (decimal)ushort.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -378,7 +378,7 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertUInt32(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { @@ -386,12 +386,12 @@ static partial class NumericConversion double interpolated = (double)value / (double)uint.MaxValue; double exact = interpolated * (double)byte.MaxValue; byte converted = (byte)double.Round(exact, MidpointRounding.AwayFromZero); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertUInt32(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { @@ -399,28 +399,28 @@ static partial class NumericConversion double interpolated = (double)value / (double)uint.MaxValue; double exact = interpolated * (double)ushort.MaxValue; ushort converted = (ushort)double.Round(exact, MidpointRounding.AwayFromZero); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertUInt32(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(nint)) { if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -428,18 +428,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertUInt32(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -447,13 +447,13 @@ static partial class NumericConversion unchecked { ulong converted = (ulong)(((ulong)value << 32) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertUInt32(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -461,7 +461,7 @@ static partial class NumericConversion unchecked { UInt128 converted = (UInt128)(((UInt128)value << 96) | ((UInt128)value << 64) | ((UInt128)value << 32) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Half)) @@ -469,37 +469,37 @@ static partial class NumericConversion // There isn't enough precision to convert from uint to Half, so we convert to double first. double x = ConvertUInt32(value); Half converted = (Half)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { // There isn't enough precision to convert from uint to float, so we convert to double first. double x = ConvertUInt32(value); float converted = (float)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertUInt32(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value / (double)uint.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value / (decimal)uint.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -545,7 +545,7 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertUInt64(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { @@ -554,13 +554,13 @@ static partial class NumericConversion unchecked { byte converted = (byte)((ulong)value >> 56); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertUInt64(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { @@ -569,13 +569,13 @@ static partial class NumericConversion unchecked { ushort converted = (ushort)((ulong)value >> 48); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertUInt64(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -584,7 +584,7 @@ static partial class NumericConversion unchecked { uint converted = (uint)((ulong)value >> 32); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nint)) @@ -592,12 +592,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -605,27 +605,27 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertUInt64(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertUInt64(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -633,7 +633,7 @@ static partial class NumericConversion unchecked { UInt128 converted = (UInt128)(((UInt128)value << 64) | value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Half)) @@ -641,37 +641,37 @@ static partial class NumericConversion // There isn't enough precision to convert from ulong to Half, so we convert to double first. double x = ConvertUInt64(value); Half converted = (Half)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { // There isn't enough precision to convert from ulong to float, so we convert to double first. double x = ConvertUInt64(value); float converted = (float)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertUInt64(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value / (double)ulong.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value / (decimal)ulong.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -691,7 +691,7 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertUInt128(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { @@ -700,13 +700,13 @@ static partial class NumericConversion unchecked { byte converted = (byte)((UInt128)value >> 120); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertUInt128(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { @@ -715,13 +715,13 @@ static partial class NumericConversion unchecked { ushort converted = (ushort)((UInt128)value >> 112); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertUInt128(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -730,7 +730,7 @@ static partial class NumericConversion unchecked { uint converted = (uint)((UInt128)value >> 96); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nint)) @@ -738,12 +738,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -751,18 +751,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertUInt128(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -771,54 +771,54 @@ static partial class NumericConversion unchecked { ulong converted = (ulong)((UInt128)value >> 64); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertUInt128(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(Half)) { // There isn't enough precision to convert from UInt128 to Half, so we convert to double first. double x = ConvertUInt128(value); Half converted = (Half)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { // There isn't enough precision to convert from UInt128 to float, so we convert to double first. double x = ConvertUInt128(value); float converted = (float)x; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertUInt128(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value / (double)UInt128.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value / (decimal)UInt128.MaxValue; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -832,7 +832,7 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertHalf(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { @@ -842,7 +842,7 @@ static partial class NumericConversion else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertHalf(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { @@ -852,7 +852,7 @@ static partial class NumericConversion else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertHalf(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -864,12 +864,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -877,18 +877,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertHalf(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -898,7 +898,7 @@ static partial class NumericConversion else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertHalf(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -907,35 +907,35 @@ static partial class NumericConversion } else if (typeof(TTo) == typeof(Half)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(float)) { float converted = (float)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertHalf(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -949,31 +949,31 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertSingle(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { // x must be clamped because of rounding errors. float x = value * (float)byte.MaxValue; byte converted = (float)byte.MaxValue < x ? byte.MaxValue : (x > (float)byte.MinValue ? (byte)x : byte.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertSingle(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { // x must be clamped because of rounding errors. float x = value * (float)ushort.MaxValue; ushort converted = (float)ushort.MaxValue < x ? ushort.MaxValue : (x > (float)ushort.MinValue ? (ushort)x : ushort.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertSingle(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { @@ -985,12 +985,12 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -998,18 +998,18 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertSingle(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { @@ -1019,7 +1019,7 @@ static partial class NumericConversion else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertSingle(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { @@ -1029,34 +1029,34 @@ static partial class NumericConversion else if (typeof(TTo) == typeof(Half)) { Half converted = (Half)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertSingle(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -1083,50 +1083,50 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertDouble(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { // x must be clamped because of rounding errors. double x = value * (double)byte.MaxValue; byte converted = (double)byte.MaxValue < x ? byte.MaxValue : (x > (double)byte.MinValue ? (byte)x : byte.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertDouble(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { // x must be clamped because of rounding errors. double x = value * (double)ushort.MaxValue; ushort converted = (double)ushort.MaxValue < x ? ushort.MaxValue : (x > (double)ushort.MinValue ? (ushort)x : ushort.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertDouble(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { // x must be clamped because of rounding errors. double x = value * (double)uint.MaxValue; uint converted = (double)uint.MaxValue < x ? uint.MaxValue : (x > (double)uint.MinValue ? (uint)x : uint.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(nint)) { if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -1134,69 +1134,69 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertDouble(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { // x must be clamped because of rounding errors. double x = value * (double)ulong.MaxValue; ulong converted = (double)ulong.MaxValue < x ? ulong.MaxValue : (x > (double)ulong.MinValue ? (ulong)x : ulong.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertDouble(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { // x must be clamped because of rounding errors. double x = value * (double)UInt128.MaxValue; UInt128 converted = (double)UInt128.MaxValue < x ? UInt128.MaxValue : (x > (double)UInt128.MinValue ? (UInt128)x : UInt128.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(Half)) { Half converted = (Half)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { float converted = (float)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertDouble(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(TTo) == typeof(decimal)) { decimal converted = (decimal)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { @@ -1210,50 +1210,50 @@ static partial class NumericConversion if (typeof(TTo) == typeof(sbyte)) { sbyte converted = ChangeSign(ConvertDecimal(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(byte)) { // x must be clamped because of rounding errors. decimal x = value * (decimal)byte.MaxValue; byte converted = (decimal)byte.MaxValue < x ? byte.MaxValue : (x > (decimal)byte.MinValue ? (byte)x : byte.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(short)) { short converted = ChangeSign(ConvertDecimal(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ushort)) { // x must be clamped because of rounding errors. decimal x = value * (decimal)ushort.MaxValue; ushort converted = (decimal)ushort.MaxValue < x ? ushort.MaxValue : (x > (decimal)ushort.MinValue ? (ushort)x : ushort.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(int)) { int converted = ChangeSign(ConvertDecimal(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(uint)) { // x must be clamped because of rounding errors. decimal x = value * (decimal)uint.MaxValue; uint converted = (decimal)uint.MaxValue < x ? uint.MaxValue : (x > (decimal)uint.MinValue ? (uint)x : uint.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(nint)) { if (IntPtr.Size == sizeof(int)) { nint converted = (nint)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nint converted = (nint)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(nuint)) @@ -1261,69 +1261,69 @@ static partial class NumericConversion if (IntPtr.Size == sizeof(int)) { nuint converted = (nuint)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { nuint converted = (nuint)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(long)) { long converted = ChangeSign(ConvertDecimal(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(ulong)) { // x must be clamped because of rounding errors. decimal x = value * (decimal)ulong.MaxValue; ulong converted = (decimal)ulong.MaxValue < x ? ulong.MaxValue : (x > (decimal)ulong.MinValue ? (ulong)x : ulong.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(Int128)) { Int128 converted = ChangeSign(ConvertDecimal(value)); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(UInt128)) { // x must be clamped because of rounding errors. decimal x = value * (decimal)UInt128.MaxValue; UInt128 converted = (decimal)UInt128.MaxValue < x ? UInt128.MaxValue : (x > (decimal)UInt128.MinValue ? (UInt128)x : UInt128.MinValue); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(Half)) { Half converted = (Half)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(float)) { float converted = (float)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(NFloat)) { if (IntPtr.Size == sizeof(int)) { NFloat converted = (NFloat)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else { NFloat converted = (NFloat)ConvertDecimal(value); - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } } else if (typeof(TTo) == typeof(double)) { double converted = (double)value; - return Unsafe.As(ref converted); + return Unsafe.BitCast(converted); } else if (typeof(TTo) == typeof(decimal)) { - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else { @@ -1337,87 +1337,87 @@ static partial class NumericConversion if (typeof(T) == typeof(sbyte)) { sbyte value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(byte)) { byte value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(short)) { short value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(ushort)) { ushort value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(int)) { int value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(uint)) { uint value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(nint)) { nint value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(nuint)) { nuint value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(long)) { long value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(ulong)) { ulong value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(Int128)) { Int128 value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(UInt128)) { UInt128 value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(Half)) { Half value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(float)) { float value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(NFloat)) { NFloat value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(double)) { double value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(decimal)) { decimal value = GetMinimumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else { @@ -1431,87 +1431,87 @@ static partial class NumericConversion if (typeof(T) == typeof(sbyte)) { sbyte value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(byte)) { byte value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(short)) { short value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(ushort)) { ushort value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(int)) { int value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(uint)) { uint value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(nint)) { nint value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(nuint)) { nuint value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(long)) { long value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(ulong)) { ulong value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(Int128)) { Int128 value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(UInt128)) { UInt128 value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(Half)) { Half value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(float)) { float value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(NFloat)) { NFloat value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(double)) { double value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else if (typeof(T) == typeof(decimal)) { decimal value = GetMaximumValueSafe(); - return Unsafe.As(ref value); + return Unsafe.BitCast(value); } else {