Closed
Description
Sharplab
Given
using System.Numerics;
public unsafe class C
{
const int Factor = 2048; // 2 ^ 11
const int Shift = 11;
readonly Vector<int> VectorFactor = new Vector<int>(Factor);
public Vector<int> Divide(Vector<int> v) => v / VectorFactor;
}
Output
The assembly output for x64 is:
; Core CLR v4.700.20.41105 on amd64
C..ctor()
L0000: vzeroupper
L0003: mov eax, 0x800
L0008: vmovd xmm0, eax
L000c: vpbroadcastd ymm0, xmm0
L0011: vmovupd [rcx+8], ymm0
L0016: vzeroupper
L0019: ret
C.Divide(System.Numerics.Vector`1<Int32>)
L0000: push rsi
L0001: sub rsp, 0x50
L0005: vzeroupper
L0008: mov rsi, rdx
L000b: mov rdx, rsi
L000e: vmovupd ymm0, [rcx+8]
L0013: vmovupd [rsp+0x20], ymm0
L0019: mov rcx, rdx
L001c: mov rdx, r8
L001f: lea r8, [rsp+0x20]
L0024: call System.Numerics.Vector`1[[System.Int32, System.Private.CoreLib]].op_Division(System.Numerics.Vector`1<Int32>, System.Numerics.Vector`1<Int32>)
L0029: mov rax, rsi
L002c: vzeroupper
L002f: add rsp, 0x50
L0033: pop rsi
L0034: ret
Expected
The division is inlined and division by constant is recognized and "optimal" SIMD is emitted e.g. with shifts or similar.
cc: @tannergooding
category:cq
theme:inlining
skill-level:intermediate
cost:medium