Skip to content

Commit

Permalink
Use HW-intrinsics in BitConverter for double <-> long / float <-> int (
Browse files Browse the repository at this point in the history
…#33476)

* Use hw-intrinsics in BitConverter for double <-> long and float <-> int to emit movd instead of using the stack

* Added comment for workaround

Cf. #33476 (comment)
  • Loading branch information
gfoidl authored Mar 20, 2020
1 parent feb5178 commit 107fbc1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

using Internal.Runtime.CompilerServices;

Expand Down Expand Up @@ -449,24 +451,52 @@ public static bool ToBoolean(ReadOnlySpan<byte> value)
[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);
}

[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);
}

[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);
}

[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 Down

0 comments on commit 107fbc1

Please sign in to comment.