# SIMD kernels (`darner-simd`) Darner requires AVX2. That does not change here. The title's own code uses it, and `darner-gpu` already has hand-written AVX2 in its hottest paths. `crates/darner-simd` holds the kernels that more than one place can share. Each kernel sits beside a scalar reference, and the vector version must give the same result. No speed-up is claimed. Nothing in this change was measured on a rig. The tests check correctness only. ## Switches All of these are read once, at the first kernel use. That first use also logs a single `SIMD kernels` line listing the processor's extensions and the choices made. | Variable | Values | Default | Effect | | --- | --- | --- | --- | | `DARNER_SIMD` | `auto`, `baseline`, `avx2` | `auto` | `baseline` runs every kernel of this crate on its scalar reference. `avx2` on a processor without AVX2 is refused with a warning, and the baseline runs instead. Any other value warns and runs `auto`. | | `DARNER_SIMD_MEMORY` | `0` turns it off | on | Byte scans in `shadow::changed_runs`. | | `DARNER_SIMD_AUDIO` | `0` turns it off | on | The DSP convolution's multiply-accumulate, FFT and scaling. | | `DARNER_SIMD_FMA` | `1` turns it on | off | Fused multiply-add in the convolution's multiply-accumulate. Results then differ from the reference in the last bits. Ignored when the processor lacks FMA, with a warning. | | `DARNER_SIMD_VERIFY` | `1` turns it on | off | Runs the reference beside each vector kernel on the same inputs and warns on the first difference. This allocates, so it is meant for a test run, not for play. | How the switches combine: - `DARNER_SIMD=baseline` overrides the per-subsystem switches. - The per-subsystem switches follow `toggled!`, which means on unless the value is `0`. - `DARNER_SIMD_FMA` and `DARNER_SIMD_VERIFY` follow `asked_for!`, which means off unless the value is `1`. - The crate sits below `darner-gpu`, so it reads the environment itself, using the same rules as `switch.rs`. Each extension is detected on its own: AVX, AVX2, FMA, BMI1, BMI2, POPCNT, and ERMS (ERMS from CPUID leaf 7). Detection happens once per process. Callers take a kernel table once per buffer, never per element. ## What runs on it | Where | Kernel | Exactness | | --- | --- | --- | | `darner-gpu` `shadow::changed_runs` | `memory::byte_kernels`: common prefix, then differing prefix, 32 bytes per compare and 128 per test while the bytes stay equal | Same runs, byte for byte, as the loop it replaces. That loop stays as the baseline. | | `darner-hle` `dsp::convolve` (multiply-accumulate) | `float::complex_mac` | Bit-exact. FMA is opt-in and checked against a tolerance. | | `darner-hle` `dsp::convolve` (FFT stages with at least 8 butterflies per group) | `float::butterflies` on twiddles laid out per stage at start-up | Bit-exact. Smaller stages keep the scalar loop. | | `darner-hle` `dsp::convolve` (output gain) | `float::scale` | Bit-exact | These are bit-exact because Rust does not contract `a * b + c` into a fused operation. Each lane therefore does the reference's multiplies, adds and subtracts in the same order. This holds for infinities, NaN and subnormals too, and `crates/darner-simd/tests/float.rs` checks it. ## AVX2 that was already there This AVX2 code predates the crate. It is left in place and is not switched by `DARNER_SIMD`: - `safecopy.rs`: - `darner_safe_put` and `darner_safe_merge`: assembly using `vpcmpeqb`, `vpblendvb` and `vpmaskmovd`, with fault labels. - `differ`. - `host/torn.rs`: the readback hash. The CPU and GPU hashes must agree. - `shadow.rs`: - `word_runs_avx2` and `word_runs_and_update_avx2`. - The streaming snapshot copies (`DARNER_RYZEN_PREP`). - `indirect.rs`: the AVX store of indirect arguments. ## Not done, and why The plan this work followed (M1–M9) asked for more than this. These parts were left undone on purpose, to avoid unverified changes: - **Fault-safe copies (M2.1).** - Why: `darner_safe_copy` is `global_asm!`. The exception handler in `watch.rs` recognises it by its labels. Changing it needs fault-injection tests on guarded pages that a bare CI runner cannot exercise faithfully. - Also: `rep movsb` already serves large copies. - **Dirty bitmaps (M2.3).** - Why: `watch.rs` keeps blocks as a sparse `BTreeMap` of word pieces, read under the guards' lock. The plan itself says not to vectorise sparse maps. - **Descriptors, `TextureQueue` and readback merging (M3).** - Why: `descriptor/host.rs` is 12,000 lines of locked, ordered state. Readback merging is already AVX2 assembly. - Doing this properly needs a dedicated change with rig testing, not a drive-by edit. - **Texture detiling (M4) and BCPack (M5).** - Why: `darner-formats` is a zero-dependency crate. Both conversions must be byte-exact against golden outputs that are not in the tree, because of the asset policy. - Also: BCPack's rANS lanes share a byte-consumption schedule that lockstep vector decoding would change. - Suggested next step: a separate change per format, with its own goldens kept out of tree. - **XMA (M6.3), mixing and sample conversion (M6.4).** - XMA is left for its own change, like the formats above. - No CPU mixer or resampler was found to accelerate: output goes through the host's audio path. - **Command preparation (M7) and worker integration (M8).** - Why: the kernels here run inline on the caller's thread. No new threads or scheduling were added. ## Tests - `cargo test -p darner-simd`: the dispatch rules (baseline override, refused requests, the `0` and `1` switch semantics), and differential tests of every kernel against its reference. The tests cover every length to a few vectors, unaligned starts, a difference at every position, and special float values. - `cargo test -p darner-gpu --lib changed_runs`: the runs are the same with the vector scans. - `cargo test -p darner-hle --lib convolve`: the FFT in both directions and the convolution over several blocks give the same output, bit for bit, on both kernel sets. A processor without the extension skips the differential tests.