Skip to content

NetFabric/NetFabric.Numerics / .agents/skills/dotnet-simd

dotnet-simd

Verified

Vectorize 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.

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.
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall