NetFabric/NetFabric.Numerics / .agents/skills/dotnet-simd
dotnet-simd
VerifiedVectorize hot loops in C#/.NET the right way — reach for System.Numerics.Tensors (TensorPrimitives) first, then System.Runtime.Intrinsics (Vector128/256/512) for hand-written algorithms, hardware intrinsics only as a last resort. USE FOR: choosing between TensorPrimitives, Vector<T>, Vector128/256/512, and X86/Arm/Wasm intrinsics; MemoryMarshal/LoadUnsafe/StoreUnsafe span-to-vector patterns; hardware-acceleration checks (IsHardwareAccelerated, IsSupported); widest-vector-first code paths with scalar/small-input fallbacks; masked remainder handling for non-idempotent vs idempotent ops; lane-crossing pitfalls with Vector256/512 horizontal reductions; eliminating bounds checks/branches in scalar/remainder code; multi-core parallelization via Parallel.Invoke chunking over TensorPrimitives calls; testing SIMD paths via DOTNET_Enable* env vars and .runsettings; benchmarking with BenchmarkDotNet job configs. DO NOT USE FOR: GPU compute (CUDA/TorchSharp); SIMD in non-.NET languages.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.7 KB |
| references/hardware-intrinsics.md | markdown | 1.9 KB |
| references/performance-optimization-ladder.md | markdown | 5.0 KB |
| references/tensor-primitives.md | markdown | 2.7 KB |
| references/testing-and-benchmarking.md | markdown | 4.2 KB |
| references/vector-t-and-fixed-shape.md | markdown | 2.7 KB |
| references/vector128-vectorization.md | markdown | 8.1 KB |
references/hardware-intrinsics.md
View on GitHub# Platform-Specific Hardware Intrinsics
Last resort on the decision ladder — only reach for `System.Runtime.Intrinsics.X86`, `.Arm`, or `.Wasm` when a specific processor instruction gives a measured edge that `Vector128`/`Vector256`/`Vector512` don't expose. See [vector128-vectorization.md](vector128-vectorization.md) first.
Each intrinsic class exposes an `IsSupported` property (a JIT-time constant) so you can guard a specialized path and fall back to portable code elsewhere:
```csharp
public static bool AllBitsClear(Vector128<byte> vector, Vector128<byte> mask)
{
if (Sse41.IsSupported)
{
return Sse41.TestZ(vector, mask); // x86/x64: single ptest
}
else if (AdvSimd.Arm64.IsSupported)
{
var anded = AdvSimd.And(vector, mask); // Arm64: AND + reduce max byte
return AdvSimd.Arm64.MaxAcross(anded).ToScalar() == 0;
}
else if (PackedSimd.IsSupported)
{
return !PackedSimd.AnyTrue(PackedSimd.And(vector, mask)); // WebAssembly
}
else
{
return (vector & mask) == Vector128<byte>.Zero; // portable fallback
}
}
```
**Before writing per-architecture branches like this, check whether the portable expression already does the job.** `(vector & mask) == Vector128<byte>.Zero` already lowers to the optimal instruction on each platform (`ptest` on x86/x64) — the hand-written branches above do the same work with more code to maintain, for no gain. Reach for explicit intrinsics only when a specific instruction measurably beats what the portable APIs generate.
Hardware intrinsics require a separate implementation per instruction set — treat them as an optimization for measured hot paths, never a default. Confirm the difference with a benchmark (see [testing-and-benchmarking.md](testing-and-benchmarking.md)) before committing to the extra maintenance burden.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |