Developer knowledge network · moderated exchange

مجتمع الكود غير الموثوق به

أبحاث المطورين، مجتمع الهندسة العكسية والترميز

Knowledge indexيعيش
4Categories
919Threads
2.8Kدعامات
Tutorial

SIMD hardware intrinsics (Vector256 / AVX2) in C# for pattern scanning [v2.4 Technical Discussion]

dotnet_native_aot
C# Veteran
MEMBER
مندوب: 174
تاريخ الانضمام: Sep 2021
دعامات: 16
شكرًا: 62
4 weeks ago · Jul 26, 2026 6:40 AM
#1

Using System.Runtime.Intrinsics.X86.Avx2 directly in C#:

CSHARP
public static unsafe IntPtr ScanPatternAVX2(byte* pBase, int size, byte targetByte) {
    Vector256<byte> target = Vector256.Create(targetByte);
    for (int i = 0; i < size - 32; i += 32) {
        Vector256<byte> chunk = Avx2.LoadVector256(pBase + i);
        Vector256<byte> cmp = Avx2.CompareEqual(chunk, target);
        int mask = Avx2.MoveMask(cmp);
        if (mask != 0) {
            int bit = System.Numerics.BitOperations.TrailingZeroCount((uint)mask);
            return (IntPtr)(pBase + i + bit);
        }
    }
    return IntPtr.Zero;
}

Scans at 4.5 GB/s in pure C#!

sig_scanner_sam
Pattern Master
MEMBER
مندوب: 210
تاريخ الانضمام: Dec 2019
دعامات: 11
شكرًا: 51
4 weeks ago · Jul 26, 2026 9:08 AM
#2

C# JIT compiler emits native vpcmpeqb and vpmovmskb instructions matching MSVC C++ output identically. Very impressive.

ptr_arithmetic
C++ Wizard
MEMBER
مندوب: 162
تاريخ الانضمام: May 2018
دعامات: 73
شكرًا: 42
4 weeks ago · Jul 26, 2026 4:15 PM
#3

Hardware intrinsics in .NET are fantastic.