-
Notifications
You must be signed in to change notification settings - Fork 5.5k
JIT: Use faster mod for uint16 values #128509
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
Draft
MihaZupan
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
MihaZupan:uint16-mod
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
169 changes: 169 additions & 0 deletions
169
src/tests/JIT/opt/Divide/Regressions/Regression4_Divide.cs
This file contains hidden or 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,169 @@ | ||
| // 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.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.Intrinsics.X86; | ||
| using Xunit; | ||
|
|
||
| public class Program | ||
| { | ||
| private static ushort s_field1; | ||
| private static ulong s_field2; | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static uint Umod_U4_CharByZero(char c) | ||
| { | ||
| return (uint)c % 0; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static ushort Umod_U2_CharByConst(char c) | ||
| { | ||
| return (ushort)(c % 42); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static int Umod_I4_CharByConst(char c) | ||
| { | ||
| return c % 42; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static uint Umod_U4_CharByConst(char c) | ||
| { | ||
| return (uint)c % 42; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static long Umod_I8_CharByConst(char c) | ||
| { | ||
| return (long)c % 42; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static ulong Umod_U8_CharByConst(char c) | ||
| { | ||
| return (ulong)c % 42; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static bool TestIsWhiteSpace(char c) | ||
| { | ||
| ReadOnlySpan<char> HashEntries = [' ', ' ', '\u00A0', ' ', ' ', ' ', ' ', ' ', ' ', '\t', '\n', '\v', '\f', '\r', ' ', ' ', '\u2028', '\u2029', ' ', ' ', ' ', ' ', ' ', '\u202F', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\u3000', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\u0085', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200A', ' ', ' ', ' ', ' ', ' ', '\u205F', '\u1680', ' ', ' ', ' ', ' ', ' ', ' ']; | ||
| return HashEntries[c % HashEntries.Length] == c; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static ushort Umod_TZC(ulong value) | ||
| { | ||
| return (ushort)(BitOperations.TrailingZeroCount(value) % 42); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static ushort Umod_TZC_Intrinsic(ulong value) | ||
| { | ||
| return (ushort)(Bmi1.X64.TrailingZeroCount(value) % 42); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static int Umod_UInt16Range_ByConst(int value) | ||
| { | ||
| if (value is > 0 and < 1234) | ||
| { | ||
| return value % 123; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveOptimization)] | ||
| private static void Test1() | ||
| { | ||
| for (int i = 0; i < 2; i++) | ||
| { | ||
| Core(); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| static void Core() | ||
| { | ||
| s_field1 = (ushort)(Bmi1.X64.TrailingZeroCount(s_field2) % 42); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static int TestEntryPoint() | ||
| { | ||
| try | ||
| { | ||
| Umod_U4_CharByZero('a'); | ||
| return 0; | ||
| } | ||
| catch (DivideByZeroException) { } | ||
|
|
||
| if (Umod_U2_CharByConst('a') != 13) | ||
| return 0; | ||
|
|
||
| if (Umod_I4_CharByConst('a') != 13) | ||
| return 0; | ||
|
|
||
| if (Umod_U4_CharByConst('a') != 13) | ||
| return 0; | ||
|
|
||
| if (Umod_I8_CharByConst('a') != 13) | ||
| return 0; | ||
|
|
||
| if (Umod_U8_CharByConst('a') != 13) | ||
| return 0; | ||
|
|
||
| if (!TestIsWhiteSpace(' ')) | ||
| return 0; | ||
|
|
||
| if (!TestIsWhiteSpace('\u2029')) | ||
| return 0; | ||
|
|
||
| if (TestIsWhiteSpace('\0')) | ||
| return 0; | ||
|
|
||
| if (TestIsWhiteSpace('a')) | ||
| return 0; | ||
|
|
||
| if (Umod_TZC(1L << 40) != 40) | ||
| return 0; | ||
|
|
||
| if (Umod_TZC(1L << 50) != 8) | ||
| return 0; | ||
|
|
||
| if (Bmi1.X64.IsSupported) | ||
| { | ||
| if (Umod_TZC_Intrinsic(1L << 40) != 40) | ||
| return 0; | ||
|
|
||
| if (Umod_TZC_Intrinsic(1L << 50) != 8) | ||
| return 0; | ||
| } | ||
|
|
||
| if (Umod_UInt16Range_ByConst(0) != -1) | ||
| return 0; | ||
|
|
||
| if (Umod_UInt16Range_ByConst(42) != 42) | ||
| return 0; | ||
|
|
||
| if (Umod_UInt16Range_ByConst(123) != 0) | ||
| return 0; | ||
|
|
||
| if (Bmi1.X64.IsSupported) | ||
| { | ||
| s_field1 = 1; | ||
| s_field2 = 1L << 50; | ||
| Test1(); | ||
|
|
||
| if (s_field1 != 8) | ||
| return 0; | ||
| } | ||
|
|
||
| return 100; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.