-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eliminate bound checks for "arr[index % arr.Length]" (#84231)
- Loading branch information
Showing
9 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
public class ModLength | ||
{ | ||
public static int Main() | ||
{ | ||
Throws<DivideByZeroException>(() => Test1(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test2(new int[0], 1)); | ||
Throws<DivideByZeroException>(() => Test3(new int[0], int.MaxValue)); | ||
Throws<DivideByZeroException>(() => Test4(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test5(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test6(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test7(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test8(new int[0], 0)); | ||
Throws<DivideByZeroException>(() => Test9(new int[0], 0)); | ||
Test1(new int[1], 1); | ||
Test2(new int[1], 1); | ||
Throws<IndexOutOfRangeException>(() => Test9(new int[1], 2)); | ||
Test3(new int[1], int.MaxValue); | ||
Throws<IndexOutOfRangeException>(() => Test4(new int[1], int.MinValue)); | ||
Test5(new int[1], -1); | ||
Test6(new int[1], 1); | ||
Test7(new int[1], 1); | ||
Test8(new int[1], 1); | ||
Test1(new int[10], 10); | ||
Test2(new int[10], 11); | ||
Test3(new int[10], int.MaxValue); | ||
Throws<IndexOutOfRangeException>(() => Test4(new int[10], int.MinValue)); | ||
Throws<IndexOutOfRangeException>(() => Test5(new int[10], -1)); | ||
Test6(new int[10], 0); | ||
Test7(new int[10], 0); | ||
Throws<DivideByZeroException>(() => Test8(new int[10], 0)); | ||
return 100; | ||
} | ||
|
||
static void Throws<T>(Action action, [CallerLineNumber] int line = 0) | ||
{ | ||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception e) | ||
{ | ||
if (e is T) | ||
{ | ||
return; | ||
} | ||
throw new InvalidOperationException($"{typeof(T)} exception was expected, actual: {e.GetType()}"); | ||
} | ||
throw new InvalidOperationException($"{typeof(T)} exception was expected"); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test1(int[] arr, int index) => arr[index % arr.Length]; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test2(int[] arr, int index) => arr[(int)index % (int)arr.Length]; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test3(int[] arr, int index) => arr[(int)((uint)index % (uint)arr.Length)]; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test4(int[] arr, int index) => arr[arr.Length % index]; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test5(int[] arr, int index) | ||
{ | ||
var span = arr.AsSpan(); | ||
return arr[index % arr.Length]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test6(int[] arr, int index) | ||
{ | ||
var span = arr.AsSpan(); | ||
return span[(int)index % (int)span.Length]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test7(int[] arr, int index) | ||
{ | ||
var span = arr.AsSpan(); | ||
return span[(int)((uint)index % (uint)span.Length)]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test8(int[] arr, int index) | ||
{ | ||
var span = arr.AsSpan(); | ||
return span[span.Length % index]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Test9(int[] arr, int index) => arr[index / arr.Length]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |