C# Reference ยท Types and Literals
Vector and SIMD types
Reference for SIMD in C#: Vector<T>, Vector128, Vector256, System.Numerics.Vector3 and Matrix4x4, hardware acceleration and when vectorizing pays off.
SIMD (single instruction, multiple data) lets one CPU instruction work on several numbers at once. .NET exposes it through a family of types: Vector<T> (width chosen by the CPU), the fixed-width Vector128<T>, Vector256<T> and Vector512<T> in System.Runtime.Intrinsics, and the small graphics-oriented Vector2, Vector3, Vector4, Quaternion and Matrix4x4 in System.Numerics.
All of these are structs. Their operators (+, *, &, comparisons) apply to every lane in parallel. Used on large arrays of numbers they can be several times faster than a plain loop, and the JIT turns the calls into real vector instructions when the hardware supports it.
using System.Numerics; var sum = new Vector3(1, 2, 3) + new Vector3(10, 20, 30); Console.WriteLine(sum);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public readonly struct Vector<T> where T : struct // System.Numerics public readonly struct Vector128<T> // System.Runtime.Intrinsics public struct Vector3 // X, Y, Z floats
| Type | Description |
|---|---|
Vector<T> |
Width depends on the machine (Vector<T>.Count lanes); write code that works for any width. |
Vector128<T>, Vector256<T>, Vector512<T> |
Fixed widths of 128, 256 and 512 bits; check IsHardwareAccelerated first. |
Vector2, Vector3, Vector4 |
Float vectors for geometry: Dot, Cross, Length, Normalize, Lerp. |
Quaternion, Matrix3x2, Matrix4x4 |
Rotation and transformation math built on the same ideas. |
Static helper class Vector (and Vector128, ...) offers Sum, Min, Max, Abs, Equals, ConditionalSelect, LoadUnsafe and conversions.
Errors: ArgumentOutOfRangeException if a span is shorter than the vector width when constructing from it.
Usage notes
- Width is hardware-dependent.
Vector<int>.Countcan be 4, 8 or 16 on different machines. Never hard-code it, and do not print it in output you expect to be identical everywhere. - Handle the tail. An array length is rarely a multiple of the width, so process the remainder with a plain loop, as in the example.
- Integer overflow wraps silently inside vector operations; there is no
checkedmode. - Floating-point order changes results. Adding lanes in parallel groups the sums differently from a serial loop, so float totals can differ in the last digits.
- Try the simple route first. LINQ
Sum,MaxandMinon arrays and spans, andMemoryExtensionsmethods such asIndexOf, already use SIMD internally. Hand-written vector code is for hot loops after measuring. - Check
Vector.IsHardwareAccelerated. When it isfalse, the generic types still work but fall back to slower software code. - Small vector types are structs of
float.Vector3is 12 bytes and passed by value; for large arrays prefer separatefloat[]columns when SIMD matters. - Platform intrinsics (
Avx2,Sse41,AdvSimd) are lower-level and not portable; reach for them only in specialized libraries.