Skip to content

JIT does not recognize Vector<T>.Count and Vector{128|256}.Count is unsigned in div / mod #62326

@gfoidl

Description

@gfoidl
public int VecSize => Vector<float>.Count;

public int M1(int length) => (int)((uint)length % Vector<float>.Count);
public int M2(int length) => (int)((uint)length % (uint)Vector<float>.Count);

public int Vec256Size => Vector256<float>.Count;

public int N1(int length) => (int)((uint)length % Vector256<float>.Count);
public int N2(int length) => (int)((uint)length % (uint)Vector256<float>.Count);

The .Count are treated as constant, but for the division and modulo it's not treated as unsigned to allow bit-hack fast computation.

; Core CLR 6.0.21.52210 on amd64

C.get_VecSize()
    L0000: mov eax, 8
    L0005: ret

C.M1(Int32)
    L0000: mov eax, edx
    L0002: mov rdx, rax
    L0005: sar rdx, 0x3f
    L0009: and rdx, 7
    L000d: add rdx, rax
    L0010: and rdx, 0xfffffffffffffff8
    L0014: sub rax, rdx
    L0017: ret

C.M2(Int32)
    L0000: mov eax, edx
    L0002: and eax, 7
    L0005: ret

C.get_Vec256Size()
    L0000: mov eax, 8
    L0005: ret

C.N1(Int32)
    L0000: mov eax, edx
    L0002: mov rdx, rax
    L0005: sar rdx, 0x3f
    L0009: and rdx, 7
    L000d: add rdx, rax
    L0010: and rdx, 0xfffffffffffffff8
    L0014: sub rax, rdx
    L0017: ret

C.N2(Int32)
    L0000: mov eax, edx
    L0002: and eax, 7
    L0005: ret

Edit: this link is wrong, see comment below.
At least fo .NET 6 on sharplab -- didn't find any issue or PR addressing this for .NET 7.

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions