exp-simd-vectorization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSIMD Vectorization
SIMD向量化
Decision Gate
决策流程
- Check and
Span<T>first. If the operation can be expressed using built-inMemoryExtensionsmethods (e.g.,Span<T>,Contains,IndexOf,CopyTo) orSequenceEqual, use them — no additional dependency is needed and the runtime already vectorizes many of these internally.MemoryExtensions - Check for TensorPrimitives next. If one or more TensorPrimitives methods cover the operation → use them. If the does NOT already reference
.csproj, add the package, for example:System.Numerics.Tensors(or use the versioning approach already used by your solution). Then replace the scalar loop with TP calls and stop. See the full API table below. Compose multiple TP calls when needed (e.g., finding both min and max →<PackageReference Include="System.Numerics.Tensors" />+TensorPrimitives.Min(span)as two calls). Do NOT write manual Vector128 code for operations TP already handles.TensorPrimitives.Max(span) - Scalar loop over contiguous array/span of ,
byte,sbyte,short,ushort,int,uint,long,ulong,nint,nuint,float(anddoublevia reinterpretation aschar)? → Implement with explicitushort/Vector128<T>/Vector256<T>intrinsics using the patterns below.Vector512<T> - No contiguous numeric arrays to process (dictionary lookups, tree traversals, linked lists, state machines, string formatting, small collections, enum comparisons, recursive algorithms, decimal arithmetic)? → Report and write a full paragraph explaining WHY, referencing the specific code characteristics that prevent vectorization (e.g., "State machines require sequential branching on enum values — there are no contiguous numeric arrays to process in parallel, and each transition depends on the previous state"). This explanation is graded.
[NO SIMD OPPORTUNITY]
- 优先检查和
Span<T>。如果操作可以通过内置MemoryExtensions方法(如Span<T>、Contains、IndexOf、CopyTo)或SequenceEqual实现,请直接使用——无需额外依赖,运行时已对其中许多方法进行了向量化优化。MemoryExtensions - 接下来检查TensorPrimitives。如果有一个或多个TensorPrimitives方法可覆盖当前操作→直接使用。如果尚未引用
.csproj,添加该包,例如:System.Numerics.Tensors(或使用解决方案已采用的版本控制方式)。然后用TP调用替换标量循环即可停止操作。请查看下方完整API表。必要时可组合多个TP调用(例如,同时查找最小值和最大值→<PackageReference Include="System.Numerics.Tensors" />+TensorPrimitives.Min(span)两个调用)。对于TP已支持的操作,请勿编写手动Vector128代码。TensorPrimitives.Max(span) - 针对、
byte、sbyte、short、ushort、int、uint、long、ulong、nint、nuint、float的连续数组/span进行标量循环(double可通过重新解释为char处理)?→使用以下模式,通过显式ushort/Vector128<T>/Vector256<T>内在函数实现。Vector512<T> - 无连续数值数组可处理(字典查找、树遍历、链表、状态机、字符串格式化、小型集合、枚举比较、递归算法、十进制算术)?→标记并撰写完整段落说明原因,引用阻止向量化的具体代码特征(例如:“状态机需要基于枚举值的顺序分支——没有可并行处理的连续数值数组,且每个转换都依赖于前一个状态”)。该说明将作为评估依据。
[NO SIMD OPPORTUNITY]
TensorPrimitives API Reference
TensorPrimitives API参考
TensorPrimitives APIs are generic and work for any primitive type that satisfies the method's generic constraints — not just /. For example, requires + and works for all primitive numeric types, while requires and only works for /. If the project doesn't already reference , add it to the . Replace the entire manual loop with one or more calls as needed (prefer a single call when possible):
floatdoubleSumIAdditionOperators<T,T,T>IAdditiveIdentity<T,T>CosineSimilarityIRootFunctions<T>floatdoubleSystem.Numerics.Tensors.csprojTensorPrimitivesTensorPrimitives API是泛型的,适用于满足方法泛型约束的任何基元类型——不仅限于/。例如,要求 + ,适用于所有基元数值类型;而要求,仅适用于/。如果项目尚未引用,请将其添加到中。根据需要用一个或多个调用替换整个手动循环(尽可能优先使用单个调用):
floatdoubleSumIAdditionOperators<T,T,T>IAdditiveIdentity<T,T>CosineSimilarityIRootFunctions<T>floatdoubleSystem.Numerics.Tensors.csprojTensorPrimitivesReductions (span → scalar)
归约运算(span → 标量)
| Operation | API |
|---|---|
| Sum | |
| Sum of squares | |
| Sum of magnitudes (L1 norm) | |
| L2 norm | |
| Product of all elements | |
| Min value | |
| Max value | |
| Index of max | |
| Index of min | |
| Dot product | |
| Cosine similarity | |
| Euclidean distance | |
| 操作 | API |
|---|---|
| 求和 | |
| 平方和 | |
| 绝对值和(L1范数) | |
| L2范数 | |
| 所有元素乘积 | |
| 最小值 | |
| 最大值 | |
| 最大值索引 | |
| 最小值索引 | |
| 点积 | |
| 余弦相似度 | |
| 欧氏距离 | |
Element-wise transforms (span → span)
逐元素转换(span → span)
| Operation | API |
|---|---|
| Negate | |
| Abs | |
| Sqrt | |
| Exp | |
| Log | |
| Log2 | |
| Tanh | |
| Sigmoid | |
| SoftMax | |
| Sinh | |
| Cosh | |
| Round | |
| Floor | |
| Ceiling | |
| CopySign | |
| Pow | |
| 操作 | API |
|---|---|
| 取反 | |
| 绝对值 | |
| 平方根 | |
| 指数运算 | |
| 自然对数 | |
| 以2为底的对数 | |
| 双曲正切 | |
| Sigmoid函数 | |
| SoftMax函数 | |
| 双曲正弦 | |
| 双曲余弦 | |
| 四舍五入 | |
| 向下取整 | |
| 向上取整 | |
| 复制符号 | |
| 幂运算 | |
Two-span operations (a, b → dst)
双span运算(a, b → dst)
| Operation | API |
|---|---|
| Add | |
| Subtract | |
| Multiply | |
| Divide | |
| Element-wise Min | |
| Element-wise Max | |
| 操作 | API |
|---|---|
| 加法 | |
| 减法 | |
| 乘法 | |
| 除法 | |
| 逐元素最小值 | |
| 逐元素最大值 | |
Three-span fused operations
三span融合运算
| Operation | API |
|---|---|
| (x+y)*z | |
| x*y+z | |
| fma(x,y,z) | |
andAddMultiplyare distinct — they optimize differently depending on whether the dependency chain flows from the addend or the multiplier.MultiplyAddis the IEEE 754 fused form of (x*y)+z with a single rounding step.FusedMultiplyAdd
| 操作 | API |
|---|---|
| (x+y)*z | |
| x*y+z | |
| fma(x,y,z) | |
和AddMultiply是不同的——根据依赖链来自加数还是乘数,它们的优化方式不同。MultiplyAdd是(x*y)+z的IEEE 754融合形式,仅执行一次舍入步骤。FusedMultiplyAdd
Manual SIMD with Vector128/Vector256/Vector512
基于Vector128/Vector256/Vector512的手动SIMD实现
Use this when TensorPrimitives doesn't have a single API for the operation. This is required for byte-level operations, character class counting, range validation, bitwise bulk ops, cross-type conversions, and custom patterns.
当TensorPrimitives没有对应单个API的操作时使用此方法。这适用于字节级操作、字符类别计数、范围验证、批量位运算、跨类型转换和自定义模式。
Required imports
必要导入
csharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;Prefer cross-platform APIs (). Only use platform-specific intrinsics (, ) when there is a significant performance advantage that justifies the increased code complexity of maintaining separate code paths.
System.Runtime.IntrinsicsSystem.Runtime.Intrinsics.X86.Armcsharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;优先使用跨平台API()。仅当存在显著性能优势且值得承担维护不同代码路径带来的复杂度提升时,才使用平台特定内在函数(、)。
System.Runtime.IntrinsicsSystem.Runtime.Intrinsics.X86.ArmThree-tier dispatch pattern
三层分派模式
Always include all three tiers. Use / so that small inputs hit only one branch before reaching the scalar fallback — a fallthrough pattern (sequential s) pessimizes the scalar case by requiring up to three not-taken branches that may mispredict. The checks are JIT-time constants, so dead paths are eliminated at compile time:
ifelse ififIsHardwareAcceleratedcsharp
ref var src = ref MemoryMarshal.GetReference(span);
uint i = 0;
uint length = (uint)span.Length;
if (Vector512.IsHardwareAccelerated && Vector512<T>.IsSupported)
{
uint vec512Count = (uint)Vector512<T>.Count;
while (i + vec512Count <= length)
{
var vec = Vector512.LoadUnsafe(ref src, i);
// ... process vec ...
i += vec512Count;
}
}
else if (Vector256.IsHardwareAccelerated && Vector256<T>.IsSupported)
{
uint vec256Count = (uint)Vector256<T>.Count;
while (i + vec256Count <= length)
{
var vec = Vector256.LoadUnsafe(ref src, i);
// ... process vec ...
i += vec256Count;
}
}
else if (Vector128.IsHardwareAccelerated && Vector128<T>.IsSupported)
{
uint vec128Count = (uint)Vector128<T>.Count;
while (i + vec128Count <= length)
{
var vec = Vector128.LoadUnsafe(ref src, i);
// ... process vec ...
i += vec128Count;
}
}
// Scalar fallback for remaining elements (and the only loop hit for small inputs)
for (; i < length; i++)
{
// ... scalar processing ...
}始终包含所有三层。使用/结构,以便小输入只需命中一个分支即可进入标量回退——穿透模式(连续)会通过要求最多三个未命中分支(可能导致预测错误)来降低标量情况的性能。检查是JIT时的常量,因此死代码路径会在编译时被消除:
ifelse ififIsHardwareAcceleratedcsharp
ref var src = ref MemoryMarshal.GetReference(span);
uint i = 0;
uint length = (uint)span.Length;
if (Vector512.IsHardwareAccelerated && Vector512<T>.IsSupported)
{
uint vec512Count = (uint)Vector512<T>.Count;
while (i + vec512Count <= length)
{
var vec = Vector512.LoadUnsafe(ref src, i);
// ... 处理vec ...
i += vec512Count;
}
}
else if (Vector256.IsHardwareAccelerated && Vector256<T>.IsSupported)
{
uint vec256Count = (uint)Vector256<T>.Count;
while (i + vec256Count <= length)
{
var vec = Vector256.LoadUnsafe(ref src, i);
// ... 处理vec ...
i += vec256Count;
}
}
else if (Vector128.IsHardwareAccelerated && Vector128<T>.IsSupported)
{
uint vec128Count = (uint)Vector128<T>.Count;
while (i + vec128Count <= length)
{
var vec = Vector128.LoadUnsafe(ref src, i);
// ... 处理vec ...
i += vec128Count;
}
}
// 剩余元素的标量回退(小输入仅命中此循环)
for (; i < length; i++)
{
// ... 标量处理 ...
}Core SIMD operations
核心SIMD操作
- Load/Store: /
Vector128.LoadUnsafe(ref src, offset).StoreUnsafe(ref dst, offset) - Arithmetic: ,
+,-,*operators on vector types/ - Multiply-add (approximate): — performs a multiply-add with implementation-defined approximation; not guaranteed to be a strict IEEE fused multiply-add. For precise fused semantics, use
Vector128.MultiplyAddEstimate(a, b, c).Vector128.FusedMultiplyAdd(a, b, c) - Comparison: ,
Vector128.Equals,.LessThan— returns mask vector.GreaterThan - Mask ops: ,
Vector128.All(mask),.Any(mask),.None(mask),.Count(mask).CountWhereAllBitsSet(mask) - Horizontal: for reduction;
Vector128.Sum(vec),.Min(a,b)element-wise.Max(a,b) - Broadcast: — fill all lanes with one value
Vector128.Create(scalarValue) - Bitwise: ,
&,|,^operators;~,Vector128.ShiftLeft.ShiftRightLogical - Widening: /
Vector128.WidenLower(v)for byte→short, short→int.WidenUpper(v) - Narrowing: for int→short, short→byte
Vector128.Narrow(lower, upper) - Type convert: ,
Vector128.ConvertToSingle(intVec).ConvertToInt32(floatVec) - Shuffle: — lookup table / permutation
Vector128.Shuffle(vec, indices) - Conditional:
Vector128.ConditionalSelect(mask, trueVec, falseVec)
- 加载/存储: /
Vector128.LoadUnsafe(ref src, offset).StoreUnsafe(ref dst, offset) - 算术运算: 向量类型的、
+、-、*运算符/ - 乘加(近似): ——执行乘加运算,采用实现定义的近似方法;不保证是严格的IEEE融合乘加。如需精确的融合语义,请使用
Vector128.MultiplyAddEstimate(a, b, c)。Vector128.FusedMultiplyAdd(a, b, c) - 比较: 、
Vector128.Equals、.LessThan——返回掩码向量.GreaterThan - 掩码操作: 、
Vector128.All(mask)、.Any(mask)、.None(mask)、.Count(mask).CountWhereAllBitsSet(mask) - 水平运算: 用于归约;
Vector128.Sum(vec)、.Min(a,b)用于逐元素比较.Max(a,b) - 广播: ——用单个值填充所有通道
Vector128.Create(scalarValue) - 位运算: 、
&、|、^运算符;~、Vector128.ShiftLeft.ShiftRightLogical - 拓宽: /
Vector128.WidenLower(v)用于byte→short、short→int转换.WidenUpper(v) - 收窄: 用于int→short、short→byte转换
Vector128.Narrow(lower, upper) - 类型转换: 、
Vector128.ConvertToSingle(intVec).ConvertToInt32(floatVec) - 重排: ——查找表/置换
Vector128.Shuffle(vec, indices) - 条件选择:
Vector128.ConditionalSelect(mask, trueVec, falseVec)
Pattern: Unsigned range check (byte-range validation)
模式:无符号范围检查(字节范围验证)
For checking if all bytes are in range [lo, hi]:
csharp
var vLo = Vector128.Create((byte)lo);
var vRange = Vector128.Create((byte)(hi - lo));
// (b - lo) > range means out-of-range (unsigned wraparound catches b < lo)
var shifted = Vector128.Subtract(vec, vLo);
var inRange = Vector128.LessThanOrEqual(shifted, vRange);
if (!Vector128.All(inRange.AsByte())) return false; // for validation
// or: count += Vector128.CountWhereAllBitsSet(inRange); // for counting用于检查所有字节是否在范围[lo, hi]内:
csharp
var vLo = Vector128.Create((byte)lo);
var vRange = Vector128.Create((byte)(hi - lo));
// (b - lo) > range 表示超出范围(无符号环绕会捕获b < lo的情况)
var shifted = Vector128.Subtract(vec, vLo);
var inRange = Vector128.LessThanOrEqual(shifted, vRange);
if (!Vector128.All(inRange.AsByte())) return false; // 用于验证
// 或:count += Vector128.CountWhereAllBitsSet(inRange); // 用于计数Pattern: Nibble-lookup counting (character classes, popcount, etc.)
模式:半字节查找计数(字符类别、popcount等)
For counting bytes matching a sparse set of values (vowels, digits, punctuation, bit counts) — build two 16-byte lookup tables indexed by low/high nibble:
csharp
var lo_lut = Vector128.Create(/* 16 bytes: bit pattern for low nibble match */);
var hi_lut = Vector128.Create(/* 16 bytes: bit pattern for high nibble match */);
var nibbleMask = Vector128.Create((byte)0x0F);
var lo_nibble = vec & nibbleMask;
var hi_nibble = Vector128.ShiftRightLogical(vec.AsUInt16(), 4).AsByte() & nibbleMask;
var lo_match = Vector128.Shuffle(lo_lut, lo_nibble);
var hi_match = Vector128.Shuffle(hi_lut, hi_nibble);
var match = lo_match & hi_match;
count += Vector128.CountWhereAllBitsSet(~Vector128.Equals(match, Vector128<byte>.Zero));This same technique works for popcount (LUT = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}).
For simpler cases (single byte value, adjacent range), use + or range check instead.
EqualsCount用于统计匹配稀疏值集合的字节数(元音、数字、标点、位计数)——构建两个16字节查找表,按低/高半字节索引:
csharp
var lo_lut = Vector128.Create(/* 16字节:低半字节匹配的位模式 */);
var hi_lut = Vector128.Create(/* 16字节:高半字节匹配的位模式 */);
var nibbleMask = Vector128.Create((byte)0x0F);
var lo_nibble = vec & nibbleMask;
var hi_nibble = Vector128.ShiftRightLogical(vec.AsUInt16(), 4).AsByte() & nibbleMask;
var lo_match = Vector128.Shuffle(lo_lut, lo_nibble);
var hi_match = Vector128.Shuffle(hi_lut, hi_nibble);
var match = lo_match & hi_match;
count += Vector128.CountWhereAllBitsSet(~Vector128.Equals(match, Vector128<byte>.Zero));此技术同样适用于popcount(查找表={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4})。对于更简单的情况(单个字节值、相邻范围),请使用 + 或范围检查替代。
EqualsCountPattern: Cross-type conversion (widening chains)
模式:跨类型转换(拓宽链)
When the source and destination types differ (e.g., byte→float for dequantization, short→byte for narrowing):
csharp
// Widen: byte → short → int → float
var bytes = Vector128.LoadUnsafe(ref src, offset);
var (lo16, hi16) = Vector128.Widen(bytes);
var (lo32a, lo32b) = Vector128.Widen(lo16);
var f0 = Vector128.ConvertToSingle(lo32a.AsInt32());
// Narrow: int → short → byte (with saturation via Min/Max clamping)
var clamped = Vector128.Min(Vector128.Max(vec, Vector128<short>.Zero), Vector128.Create((short)255));
var narrowed = Vector128.Narrow(clamped.AsUInt16(), nextVec.AsUInt16());当源类型和目标类型不同时(例如,byte→float用于反量化,short→byte用于收窄):
csharp
// 拓宽:byte → short → int → float
var bytes = Vector128.LoadUnsafe(ref src, offset);
var (lo16, hi16) = Vector128.Widen(bytes);
var (lo32a, lo32b) = Vector128.Widen(lo16);
var f0 = Vector128.ConvertToSingle(lo32a.AsInt32());
// 收窄:int → short → byte(通过Min/Max钳位实现饱和)
var clamped = Vector128.Min(Vector128.Max(vec, Vector128<short>.Zero), Vector128.Create((short)255));
var narrowed = Vector128.Narrow(clamped.AsUInt16(), nextVec.AsUInt16());Trailing elements
尾部元素处理
- Idempotent ops (validation, search): overlap last vector — re-processing is safe
- Aggregations (sum, count, min/max): scalar loop for remainder to avoid double-counting
- Store ops (transform in-place): use to merge with last stored vector
ConditionalSelect
- 幂等操作(验证、搜索):重叠最后一个向量——重新处理是安全的
- 聚合操作(求和、计数、最小/最大值):对剩余元素使用标量循环,避免重复计数
- 存储操作(原地转换):使用与最后存储的向量合并
ConditionalSelect
Key Rules
关键规则
- Preserve original method signature — drop-in replacement
- Keep scalar code as fallback — never delete it
- Use /
Vector128<T>/Vector256<T>explicitly — neverVector512<T>Vector<T> - Prefer portable /
Vector128<T>/Vector256<T>APIs over platform-specific intrinsics (Vector512<T>,Avx2,Sse42,AdvSimd) unless there is a significant performance advantageFma - Testing: use (NOT
dotnet run) — xunit.v3 is an in-process runnerdotnet test
- 保留原方法签名——实现无缝替换
- 保留标量代码作为回退——切勿删除
- 显式使用/
Vector128<T>/Vector256<T>——绝不使用Vector512<T>Vector<T> - 优先使用可移植的/
Vector128<T>/Vector256<T>API,而非平台特定内在函数(Vector512<T>、Avx2、Sse42、AdvSimd),除非存在显著性能优势Fma - 测试:使用(而非
dotnet run)——xunit.v3是进程内运行器dotnet test