Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .claude/blackboard.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## 2026-11-17 — RESULT: Mandelbrot AVX-512 gap closed without a library change

Inner loops of ndarray and wide8 are instruction-identical on v4 (llvm-objdump). The 9% gap was the bench
arm's per-lane saturating `f64 as u32` (≈40 scalar insns per block). Replaced by exact `(count + 2^52)
.to_bits()` + `as_chunks_mut::<8>()` — existing facade methods only. v4, 1 thread: ndarray 149–154 ms vs
wide8 152–157, pulp 155–156 (was 168–175). v3 still behind (bitmask pack/expand around select, unchanged).

## 2026-11-17 — FIXED: AVX2 F64x8::reduce_min/max regression (restore master's array fold)

Operator hint: the simd_ops shapes (add_mul, as_chunks) are the way. Master's reduce body was already that
shape (iterator fold over the fixed lane array); restored it. Probe: 4.03 → 2.0 ns, 27 → 21 insns, same as
before PR #435; fingerprint and IEEE digest unchanged; lane_masks NaN-compat tests 9/9 on v3 and avx; lib
2692 (v3); guard clean. WORKING RULE: write ops as fixed-size-array expressions and read the disassembly
first; intrinsics only where the compiler demonstrably did not vectorize.

## 2026-11-17 — RESULT: PR #435 before/after per method, llvm-objdump + timing (doc § 7)

v4: 30/30 probe symbols disassemble identically before/after — the PR changed no AVX-512 code; timing noise
on this host is ±20% (controls). v3: LLVM had ALREADY vectorized master's lane loops (simd_le = 9 insns, 0
scalar FP); sqrt/round/floor/abs/saturating are byte-identical before/after; compares are now 3 insns longer
(vpxor + 2× vpcmpgtq via to_bitmask). REGRESSION (mine): AVX2 `F64x8::reduce_min/max` 2.15 → 4.03 ns (1.9×
slower; NaN-ignoring blend tree vs LLVM's vminsd chain). Real AVX2 gains: `U8x64::mask_blend` 10.0×
(377 → 30 insns, 65 branches → 0), `F32x16` compare+select 2.4× (80 → 26), `F32x16::reduce_min/max` 1.6×.
First harness was wrong (0.3 ns: out-pointer calls optimized away) — fixed with black_box on every output.
OPEN, operator decision: restore master's bodies where the PR is neutral or worse (the f64 reductions for
certain) — master's bodies are lane loops, which FACET STRIDE R2 forbids as a shape, so rule and measurement
disagree here.

## 2026-11-17 — RESULT: Mandelbrot wide / pulp / ndarray, v4 and v3 (doc § 6 of simd-crate-comparison-wide-pulp)

Single-thread medians, ms (3200², median of 15): v4 — wide8 156, pulp 155, ndarray 170, wide(f64x4) 231,
Expand Down
133 changes: 133 additions & 0 deletions .claude/knowledge/simd-crate-comparison-wide-pulp-2026-11-17.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,136 @@ Why ndarray is still behind, from the disassembly of the hot closure:
`vminsd`/`vmaxsd` saturation) because the facade has no vector `F64x8` → integer conversion; wide uses
`round_int`, pulp counts in `u64` lanes with `select_u64s`. Not measured in isolation.
- wide's AVX2 `any()` lowers to 8–16 scalar `vucomisd` + `cmov` here, yet wide8 is still the fastest v3 arm.

## 7. Before / after PR #435, method by method — llvm-objdump + timing (2026-11-17)

Probe: one `#[inline(never)] #[no_mangle]` function per facade method, built against ndarray `bf112d2`
(before) and `09cdaa1` (after) at `-C target-cpu=x86-64-v3` and `-v4`, opt-level 3, rustc 1.99.0,
disassembled with `llvm-objdump -d`. Timing: 2,000,000 calls, best of 7 inside a run, best of 5 runs
interleaved before/after; every output is passed through `black_box` (a first harness without that
reported 0.3 ns for the out-pointer probes — the calls were being optimized away; those numbers were
discarded). The result fingerprint is identical in all four binaries (`0x4ffeb11112ae9530`).

```
== x86-64-v3: instructions (total / scalar-FP / branches) and ns per call (best of 5 runs)
method insns before insns after ns before ns after change
p_f64x8_simd_le 9/ 0/ 0 12/ 0/ 0 1.81 1.85 0.98x
p_f64x8_simd_lt 9/ 0/ 0 12/ 0/ 0 1.71 1.87 0.91x SLOWER
p_f64x8_simd_eq 9/ 0/ 0 12/ 0/ 0 1.81 1.85 0.98x
p_f64x8_simd_ne 9/ 0/ 0 12/ 0/ 0 1.84 1.74 1.06x
p_f64x8_le_select 22/ 1/ 0 26/ 0/ 0 3.29 3.17 1.04x
p_f64x8_simd_min 14/ 0/ 0 14/ 0/ 0 2.23 2.06 1.08x FASTER
p_f64x8_simd_max 14/ 0/ 0 14/ 0/ 0 2.30 2.12 1.08x FASTER
p_f64x8_sqrt 6/ 0/ 0 6/ 0/ 0 6.90 6.71 1.03x
p_f64x8_round 6/ 0/ 0 6/ 0/ 0 1.44 1.40 1.03x
p_f64x8_floor 6/ 0/ 0 6/ 0/ 0 1.41 1.27 1.11x FASTER
p_f64x8_abs 7/ 0/ 0 7/ 0/ 0 1.21 1.12 1.08x FASTER
p_f64x8_reduce_min 21/10/ 0 27/ 4/ 0 2.15 4.03 0.53x SLOWER
p_f64x8_reduce_max 21/10/ 0 27/ 4/ 0 2.11 4.00 0.53x SLOWER
p_f32x16_simd_le 11/ 0/ 0 14/ 0/ 0 1.82 1.74 1.05x
p_f32x16_simd_lt 11/ 0/ 0 14/ 0/ 0 1.58 1.73 0.91x SLOWER
p_f32x16_simd_eq 11/ 0/ 0 14/ 0/ 0 1.76 1.83 0.96x
p_f32x16_simd_ne 11/ 0/ 0 14/ 0/ 0 1.81 1.83 0.99x
p_f32x16_le_select 80/ 1/ 0 26/ 0/ 0 7.91 3.32 2.38x FASTER
p_f32x16_simd_min 14/ 0/ 0 14/ 0/ 0 2.29 2.21 1.04x
p_f32x16_simd_max 14/ 0/ 0 14/ 0/ 0 2.27 2.22 1.02x
p_f32x16_sqrt 6/ 0/ 0 6/ 0/ 0 3.54 3.42 1.04x
p_f32x16_round 6/ 0/ 0 6/ 0/ 0 1.46 1.42 1.03x
p_f32x16_floor 6/ 0/ 0 6/ 0/ 0 1.47 1.40 1.05x
p_f32x16_reduce_min 53/26/ 0 35/ 4/ 0 9.49 5.81 1.63x FASTER
p_f32x16_reduce_max 53/26/ 0 35/ 4/ 0 9.37 5.75 1.63x FASTER
p_u8x64_saturating_add 8/ 0/ 0 8/ 0/ 0 1.64 1.57 1.04x
p_u8x64_saturating_sub 8/ 0/ 0 8/ 0/ 0 1.62 1.57 1.03x
p_u8x64_mask_blend 377/ 0/65 30/ 0/ 0 28.78 2.89 9.96x FASTER
c_f64x8_add 8/ 0/ 0 8/ 0/ 0 1.58 1.36 1.16x FASTER
c_f64x8_mul_add 10/ 0/ 0 10/ 0/ 0 1.76 1.61 1.09x FASTER

== x86-64-v4: instructions (total / scalar-FP / branches) and ns per call (best of 5 runs)
method insns before insns after ns before ns after change
p_f64x8_simd_le 5/ 0/ 0 5/ 0/ 0 1.76 1.67 1.05x
p_f64x8_simd_lt 5/ 0/ 0 5/ 0/ 0 1.61 1.62 0.99x
p_f64x8_simd_eq 5/ 0/ 0 5/ 0/ 0 1.61 1.60 1.01x
p_f64x8_simd_ne 5/ 0/ 0 5/ 0/ 0 1.59 1.41 1.13x FASTER
p_f64x8_le_select 7/ 0/ 0 7/ 0/ 0 2.10 1.77 1.19x FASTER
p_f64x8_simd_min 5/ 0/ 0 5/ 0/ 0 1.71 1.62 1.06x
p_f64x8_simd_max 5/ 0/ 0 5/ 0/ 0 1.70 1.76 0.97x
p_f64x8_sqrt 4/ 0/ 0 4/ 0/ 0 7.36 7.24 1.02x
p_f64x8_round 4/ 0/ 0 4/ 0/ 0 1.49 1.51 0.99x
p_f64x8_floor 4/ 0/ 0 4/ 0/ 0 1.47 1.46 1.01x
p_f64x8_abs 5/ 0/ 0 5/ 0/ 0 1.27 1.38 0.92x SLOWER
p_f64x8_reduce_min 8/ 1/ 0 8/ 1/ 0 1.17 1.16 1.01x
p_f64x8_reduce_max 8/ 1/ 0 8/ 1/ 0 1.19 1.17 1.02x
p_f32x16_simd_le 5/ 0/ 0 5/ 0/ 0 1.37 1.34 1.02x
p_f32x16_simd_lt 5/ 0/ 0 5/ 0/ 0 1.38 1.39 0.99x
p_f32x16_simd_eq 5/ 0/ 0 5/ 0/ 0 1.53 1.36 1.12x FASTER
p_f32x16_simd_ne 5/ 0/ 0 5/ 0/ 0 1.41 1.35 1.04x
p_f32x16_le_select 7/ 0/ 0 7/ 0/ 0 1.92 1.78 1.08x FASTER
p_f32x16_simd_min 5/ 0/ 0 5/ 0/ 0 1.68 1.48 1.14x FASTER
p_f32x16_simd_max 5/ 0/ 0 5/ 0/ 0 1.65 1.48 1.11x FASTER
p_f32x16_sqrt 4/ 0/ 0 4/ 0/ 0 3.53 3.34 1.06x
p_f32x16_round 4/ 0/ 0 4/ 0/ 0 1.47 1.31 1.12x FASTER
p_f32x16_floor 4/ 0/ 0 4/ 0/ 0 1.48 1.32 1.12x FASTER
p_f32x16_reduce_min 10/ 1/ 0 10/ 1/ 0 1.41 1.16 1.22x FASTER
p_f32x16_reduce_max 10/ 1/ 0 10/ 1/ 0 1.38 1.16 1.19x FASTER
p_u8x64_saturating_add 5/ 0/ 0 5/ 0/ 0 1.63 1.40 1.16x FASTER
p_u8x64_saturating_sub 5/ 0/ 0 5/ 0/ 0 1.65 1.40 1.18x FASTER
p_u8x64_mask_blend 6/ 0/ 0 6/ 0/ 0 1.54 1.29 1.19x FASTER
c_f64x8_add 5/ 0/ 0 5/ 0/ 0 1.62 1.40 1.16x FASTER
c_f64x8_mul_add 6/ 0/ 0 6/ 0/ 0 1.71 1.42 1.20x FASTER
```

Reading it:
- **AVX-512 (v4): the PR changed no generated code.** All 30 probe symbols disassemble identically before
and after. The v4 timing column is therefore pure noise, and it reaches ±20% on this host (see the
untouched controls `c_f64x8_add` 1.16×, `c_f64x8_mul_add` 1.20×). Only v3 changes beyond that band count.
- **AVX2 (v3): LLVM had already vectorized the "scalar lane loops".** Before the PR, `F64x8::simd_le` was 9
instructions with zero scalar FP ops (`vcmplepd` ×2, `vpackssdw`, `vpermq`, `vmovmskps`). sqrt / round /
floor / abs and the U8x64 saturating ops are byte-identical before and after; min / max have the same
instruction count. The hand-written halves bought nothing there.
- **The compares got 3 instructions longer** (12 vs 9, 14 vs 11): going through `F64Mask4::to_bitmask` adds
`vpxor` + `vpcmpgtq` ×2, because LLVM re-derives the sign bits it can no longer see are a compare result.
Timing is inside the noise band.
- **REGRESSION introduced by the PR: `F64x8::reduce_min` / `reduce_max` on AVX2 are 1.9× slower**
(2.15 → 4.03 ns; 21 → 27 instructions). The NaN-ignoring tree (`vminpd` + `vcmpunordpd` + `vblendvpd` per
level) is longer than what LLVM made of master's `fold(INFINITY, f64::min)` (a `vminsd` chain with two
NaN fix-ups).
- **Real gains on AVX2:** `U8x64::mask_blend` 10.0× (377 instructions with 65 branches → 30, branch-free);
`F32x16` compare + select 2.4× (80 → 26 instructions); `F32x16::reduce_min/max` 1.6× (53 → 35, scalar FP
ops 26 → 4).

Net: on AVX2 the PR's measurable value is three things (mask_blend, f32 compare+select, f32 reductions) and
the new API; the rest of F2's rewrite is neutral, and the f64 reductions are worse. Lesson: read the
disassembly of the existing code BEFORE calling a lane loop "scalar" — the source shape is not the codegen.

**Fixed 2026-11-17 (operator: "add_mul, as_chunks etc in simd_ops should help").** The AVX2
`F64x8::reduce_min` / `reduce_max` are back to master's body — an iterator fold over the fixed-size lane
array, the shape `simd_ops` is built on. Measured on the same probe: 2.0 ns and 21 instructions again
(before 2.15 / after-PR 4.03 / fixed 1.7–2.2 over three runs); fingerprint unchanged; guard clean (a fold
over a fixed array is not an R2 lane loop). Rule taken from it: express an op as a fixed-size-array
expression (`as_chunks`, `array_chunks`, the closure-parameterized `simd_ops` helpers, the typed vectors'
own methods) and let the compiler lower it; reach for intrinsics only where the disassembly shows it did
not vectorize (`U8x64::mask_blend`, `F32x16` compare+select, `F32x16` reductions).

## 8. Mandelbrot follow-up: the AVX-512 gap was in the benchmark arm, not in ndarray (2026-11-17)

llvm-objdump of the hot closure on x86-64-v4: the ndarray and wide8 inner iteration loops are identical,
instruction for instruction (28 instructions; only the compare predicate differs, `vcmplepd` vs
`vcmple_oqpd`). The 9% gap of § 6 was around the loop, in code I wrote for the arm:
- the per-block count conversion was a saturating `f64 as u32` per lane — about 40 scalar instructions
(8× `vmaxsd`/`vminsd`/`vcvttsd2si` + inserts) against wide's 7 (`vcvtpd2qq` + `vpmovqd`). Writing it as a
fixed-size-array expression did not make LLVM vectorize the saturating cast.
- (a zero-filled output vector, where the other arms write into uninitialized capacity — fixed too; no
measurable effect on its own.)

Fix, with existing facade methods only: `(count + F64x8::splat(2^52)).to_bits()` — exact for the small
integer counts, one vector add and a reinterpretation — and `as_chunks_mut::<8>()` for the row.
Measured, 1 thread, median of 15, three runs, mandelbrot-simd `HEAD`:

| build | wide8 | pulp | ndarray before the fix | ndarray after |
|---|---|---|---|---|
| x86-64-v4 | 152–157 | 155–156 | 168–175 | **149–154** |
| x86-64-v3 | 153–158 | 154–156 | 186–198 | 168–194 (noisy) |

On AVX-512 ndarray is level with wide and pulp. On AVX2 it stays behind: the compare result is packed to
the public bitmask and expanded again for `select` (§ 6), which is unchanged. § 6's "v4 ≈ 9% behind … not
measured in isolation" is hereby measured: it was the conversion.
10 changes: 8 additions & 2 deletions src/simd_avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,17 @@ impl F64x8 {
}
#[inline(always)]
pub fn reduce_min(self) -> f64 {
self.0.reduce_min().min(self.1.reduce_min())
// A fold over the fixed-size lane array (the `simd_ops` shape). LLVM
// lowers it to a short `vminsd` chain; measured 2.15 ns against
// 4.03 ns for a hand-written NaN-ignoring `vminpd`/`vblendvpd` tree
// (knowledge doc, wide/pulp comparison § 7).
let a = self.to_array();
a.iter().copied().fold(f64::INFINITY, f64::min)
}
#[inline(always)]
pub fn reduce_max(self) -> f64 {
self.0.reduce_max().max(self.1.reduce_max())
let a = self.to_array();
a.iter().copied().fold(f64::NEG_INFINITY, f64::max)
}
#[inline(always)]
pub fn abs(self) -> Self {
Expand Down
Loading