Home / Forums / High-Performance SIMD Vectorization in C# with System.Numerics.Vector<T>

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

High-Performance SIMD Vectorization in C# with System.Numerics.Vector<T>

SimdSamurai
AVX-512 & SSE
MEMBER
Rep: 285
Join Date: Aug 2021
Posts: 7
Thanks: 36
1y ago · Aug 30, 2024 11:30 AM
#1
Did you know .NET has cross-platform SIMD vectorization built right into the standard library?
CODE
Vector<T>
automatically uses whatever vector width the host CPU supports (128-bit SSE, 256-bit AVX2, or 512-bit AVX-512)!

Vectorized Array Multiplication (Processing 8 or 16 floats per cycle):
CSHARP
using System.Numerics;

public static class SimdMath
{
    public static void MultiplyArrays(float[] a, float[] b, float[] result)
    {
        int simdLength = Vector<float>.Count; // e.g. 8 on AVX2
        int i = 0;

        // Process SIMD chunks in parallel
        for (; i <= a.Length - simdLength; i += simdLength)
        {
            var va = new Vector<float>(a, i);
            var vb = new Vector<float>(b, i);
            (va * vb).CopyTo(result, i); // Executes in 1 CPU instruction!
        }

        // Clean up remaining tail elements
        for (; i < a.Length; i++)
        {
            result[i] = a[i] * b[i];
        }
    }
}


Benchmark: **7.5x faster** than a standard for-loop on an Intel 13th Gen CPU!
SimdSamurai · AVX-512 & SSE
Vectorizing matrix multiplications and particle engines with...
The following users thanked SimdSamurai for this post:
MathMagician
Vector Math & Physics
MEMBER
Rep: 219
Join Date: Nov 2024
Posts: 8
Thanks: 19
1y ago · Aug 30, 2024 1:40 PM
#2
CODE
Vector<T>
is great because it is completely portable across x86 and ARM64 Apple Silicon. If you need specific x86 instructions (like
CODE
Avx2.GatherVector256
or FMA), you can also use
CODE
System.Runtime.Intrinsics.X86
directly!
MathMagician · Vector Math & Physics
Quaternions, transformation matrices, and physics simulation...
NativeCoder
Low-Level C++ / ASM
VIP
Rep: 374
Join Date: Jun 2025
Posts: 12
Thanks: 20
1y ago · Aug 31, 2024 8:40 PM
#3
The automatic tail loop handling is clean. This is the exact pattern used internally in .NET's
CODE
Span.IndexOf
and
CODE
MemoryExtensions
!
NativeCoder · Low-Level C++ / ASM
Passionate about cache locality, SIMD instructions, and raw ...