Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop workarounds from BitConverter #64046

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -769,16 +769,8 @@ public static bool ToBoolean(ReadOnlySpan<byte> value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A 64-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe long DoubleToInt64Bits(double value)
{
// Workaround for https://github.com/dotnet/runtime/issues/11413
if (Sse2.X64.IsSupported)
{
Vector128<long> vec = Vector128.CreateScalarUnsafe(value).AsInt64();
return Sse2.X64.ConvertToInt64(vec);
}

return *((long*)&value);
}

Expand All @@ -787,16 +779,8 @@ public static unsafe long DoubleToInt64Bits(double value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A double-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe double Int64BitsToDouble(long value)
{
// Workaround for https://github.com/dotnet/runtime/issues/11413
if (Sse2.X64.IsSupported)
{
Vector128<double> vec = Vector128.CreateScalarUnsafe(value).AsDouble();
return vec.ToScalar();
}

return *((double*)&value);
}

Expand All @@ -805,16 +789,8 @@ public static unsafe double Int64BitsToDouble(long value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A 32-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SingleToInt32Bits(float value)
{
// Workaround for https://github.com/dotnet/runtime/issues/11413
if (Sse2.IsSupported)
{
Vector128<int> vec = Vector128.CreateScalarUnsafe(value).AsInt32();
return Sse2.ConvertToInt32(vec);
}

return *((int*)&value);
}

Expand All @@ -823,16 +799,8 @@ public static unsafe int SingleToInt32Bits(float value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A single-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Int32BitsToSingle(int value)
{
// Workaround for https://github.com/dotnet/runtime/issues/11413
if (Sse2.IsSupported)
{
Vector128<float> vec = Vector128.CreateScalarUnsafe(value).AsSingle();
return vec.ToScalar();
}

return *((float*)&value);
}

Expand All @@ -841,7 +809,6 @@ public static unsafe float Int32BitsToSingle(int value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A 16-bit signed integer whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe short HalfToInt16Bits(Half value)
{
return *((short*)&value);
Expand All @@ -852,7 +819,6 @@ public static unsafe short HalfToInt16Bits(Half value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>A half-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Half Int16BitsToHalf(short value)
{
return *(Half*)&value;
Expand All @@ -864,7 +830,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A 64-bit unsigned integer whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong DoubleToUInt64Bits(double value) => (ulong)DoubleToInt64Bits(value);

/// <summary>
Expand All @@ -873,7 +838,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A double-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe double UInt64BitsToDouble(ulong value) => Int64BitsToDouble((long)value);

/// <summary>
Expand All @@ -882,7 +846,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A 32-bit unsigned integer whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe uint SingleToUInt32Bits(float value) => (uint)SingleToInt32Bits(value);

/// <summary>
Expand All @@ -891,7 +854,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A single-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float UInt32BitsToSingle(uint value) => Int32BitsToSingle((int)value);

/// <summary>
Expand All @@ -900,7 +862,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A 16-bit unsigned integer whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ushort HalfToUInt16Bits(Half value) => (ushort)HalfToInt16Bits(value);

/// <summary>
Expand All @@ -909,7 +870,6 @@ public static unsafe Half Int16BitsToHalf(short value)
/// <param name="value">The number to convert.</param>
/// <returns>A half-precision floating point number whose bits are identical to <paramref name="value"/>.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Half UInt16BitsToHalf(ushort value) => Int16BitsToHalf((short)value);
}
}