Skip to content

Could emit branchless form of i >= 0 && j >= 0 for signed integers #61940

@drewnoakes

Description

@drewnoakes

Given 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 BothZero with:

    L0000: or edx, ecx
    L0002: sete al
    L0005: movzx eax, al
    L0008: ret

However BothGTZ introduces branching:

    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# (i | j) >= 0 could have better performance here:

    L0000: or edx, ecx
    L0002: mov eax, edx
    L0004: not eax
    L0006: shr eax, 0x1f
    L0009: ret

category:cq
theme:basic-cq
skill-level:beginner
cost:small
impact:small

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIgood first issueIssue should be easy to implement, good for first-time contributors

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions