Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IsVolatile.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IteratorStateMachineAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\ITuple.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\JitHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\LoadHint.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\MethodCodeType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\MethodImplAttribute.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/mscorlib/shared/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public static short Max(short val1, short val2)
[NonVersionable]
public static int Max(int val1, int val2)
{
return (val1 >= val2) ? val1 : val2;
return JitHelpers.ConditionalSelect((val1 >= val2), val1, val2);
}

[NonVersionable]
Expand Down Expand Up @@ -568,7 +568,7 @@ public static short Min(short val1, short val2)
[NonVersionable]
public static int Min(int val1, int val2)
{
return (val1 <= val2) ? val1 : val2;
return JitHelpers.ConditionalSelect((val1 <= val2), val1, val2);
}

[NonVersionable]
Expand Down
29 changes: 29 additions & 0 deletions src/mscorlib/shared/System/Runtime/CompilerServices/JitHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Internal.Runtime.CompilerServices;

namespace System.Runtime.CompilerServices
{
internal static partial class JitHelpers
{
// From https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf
// Example 3.2. We're taking advantage of the fact that bools are passed by value as 32-bit integers,
// so we'll blit it directly into a 1 or a 0 without a jump.
// Codegen will emit a movzx, but on Ivy Bridge (and later) the CPU itself elides the instruction.

// 'valueIfTrue' and 'valueIfFalse' really should be compile-time constants for maximum throughput.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ConditionalSelect(bool condition, int valueIfTrue, int valueIfFalse)
{
return ((-Unsafe.As<bool, int>(ref condition)) & (valueIfTrue - valueIfFalse)) + valueIfFalse;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ConditionalSelect(bool condition, uint valueIfTrue, uint valueIfFalse)
{
return (uint)(((-Unsafe.As<bool, int>(ref condition)) & ((int)valueIfTrue - (int)valueIfFalse)) + (int)valueIfFalse);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal class ArrayPinningHelper
public byte m_arrayData;
}

internal static class JitHelpers
internal static partial class JitHelpers
{
// The special dll name to be used for DllImport of QCalls
internal const string QCall = "QCall";
Expand Down