-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Could emit branchless form of i >= 0 && j >= 0 for signed integers #61940
Comments
Tagging subscribers to this area: @JulieLeeMSFT Issue DetailsGiven this code: bool BothZero(int i, int j) => i == 0 && j == 0;
bool BothGTZ(int i, int j) => i >= 0 && j >= 0; The JIT optimises L0000: or edx, ecx
L0002: sete al
L0005: movzx eax, al
L0008: ret However L0000: test ecx, ecx
L0002: jl short L000c
L0004: mov eax, edx
L0006: not eax
L0008: shr eax, 0x1f
L000b: ret
L000c: xor eax, eax
L000e: ret The branchless form, equivalent to C# L0000: or edx, ecx
L0002: mov eax, edx
L0004: not eax
L0006: shr eax, 0x1f
L0009: ret
|
CC @dotnet/jit-contrib |
Marking as up-for-grabs as it should not be difficult to alter |
Hi I would like to work on this issue |
Great! I am assigning it to you. If you have questions or need help, just post a note here. |
@pedrobsaila you might also like to optimise this similar case: bool EitherNonZero(int i, int j) => i != 0 || j != 0; Currently emits: L0000: test edx, edx
L0002: jne short L0012
L0004: cmp dword ptr [esp+4], 0
L0009: setne al
L000c: movzx eax, al
L000f: ret 4
L0012: mov eax, 1
L0017: ret 4 The branchless form, equivalent to C# L0000: or edx, [esp+4]
L0004: setne al
L0007: movzx eax, al
L000a: ret 4 |
Given this code:
The JIT optimises
BothZero
with:However
BothGTZ
introduces branching:The branchless form, equivalent to C#
(i | j) >= 0
could have better performance here:category:cq
theme:basic-cq
skill-level:beginner
cost:small
impact:small
The text was updated successfully, but these errors were encountered: