Skip to content

simd: power sums and extrema of a u8 lane from per-code counts (neighbour-addition chain) - #428

Merged
AdaWorldAPI merged 3 commits into
masterfrom
ccr-1fd55114-ndx7e4
Nov 14, 2026
Merged

AdaWorldAPI merged 3 commits into
masterfrom
ccr-1fd55114-ndx7e4

Conversation

@AdaWorldAPI

Copy link
Copy Markdown
Owner

What

Three functions in src/simd_masking_ops.rs, re-exported from ndarray::simd:

  • masked_code_counts_u8(values, mask_words, &mut [u64; 256]): for every selected row, counts[code] += 1. Accumulates, so tiles and segments add.
  • code_power_sums_u8(&[u64; 256]) -> Option<CodePowerSums>: n, Σx, Σx², Σx³, Σx⁴, min and max from the 256 counts.
  • masked_first_row_of_code_u8(values, mask_words, code) -> Option<usize>: the lowest selected row holding a code; with the min or max code this is the row of that extreme.

No value is multiplied per row and no lane wider than the u8 codes is read.

How the sums are obtained

Walk the bins from 255 down to 1 with five accumulators, each adding its neighbour:

t1 += c[v];  t2 += t1;  t3 += t2;  t4 += t3;  t5 += t4;

At the end t2 ..= t5 are B_k = Σ_u C(u + k − 1, k) · c[u], and

Σx  = B1
Σx² = 2·B2 − Σx
Σx³ = 6·B3 − 3·Σx² − 2·Σx
Σx⁴ = 24·B4 − 6·Σx³ − 11·Σx² − 6·Σx

Accumulators are u128; the largest stays below 2^92 for any u64 counts. Min and max are the first and last non-empty bin.

Validation

The predictions and kill conditions were committed to .claude/blackboard.md before any code (5def8df); the results entry is above it.

prediction result
exact on every single-bin table (256 codes × counts 1, 2, 65,536, 2^40) holds
random tables against Σ counts[u]·u^k holds, 200 tables
against sums taken directly from the selected rows holds, 300 populations, lengths 0 to 70,001, three mask kinds, three value kinds
against the existing masked_group_bounded_power_sums_u8 + widen_bounded_power_sums (n, Σx, Σx²) holds, tiles of 1, 64, 4,096 and 65,536 rows including all-255
counts of pieces add holds, 200,000 rows in 3 pieces
not slower than the bounded register fold, one group, dense mask holds
chain under 2 µs holds

The map from counts to sums is linear, so exactness on every single-bin table implies exactness on every table (no overflow is possible). That is why the first row is the load-bearing one.

  • tests/code_power_sums_u8.rs: 10 tests.
  • crates/simd-masking-parity: rows 0x8d0, 0x8e0, 0x8f0. Arms green: native (AVX-512), v3 (AVX2), avx-qemu, v2-qemu, neon-qemu, wasm, wasm-scalar, nightly.
  • Disable runs, anchor asserted, exit 101 each (10 of 10): a wrong chain link; the Σx³ coefficient; the Σx⁴ coefficient; bin 0 included in the chain; the tail bound; the dense path reading the wrong byte; flush overwriting; max taken from the front; the row finder without the mask; the row finder without the padding clip.
  • cargo clippy --lib --tests -- -D warnings, cargo fmt --check and the facet-stride guard are clean.

Measured

Unit: microseconds (µs) for one fold over 65,536 rows, one group, unless a cell says ns. Intel Xeon 2.10 GHz, AVX-512, release, target-cpu=native, minimum of 7 rounds of 200 calls, range over three runs.

mask counts + chain (µs) existing u8 register fold (µs) existing wide i32 fold (µs)
dense 25.4 to 27.1 173.3 to 174.8 72.8 to 76.7
half 31.2 to 31.6 82.2 to 84.9 43.8 to 45.6
1 in 64 2.35 to 2.55 2.48 to 2.58 2.80 to 2.81
  • The chain alone: 376 to 382 ns, whatever the row count.
  • Row of the minimum: 15 to 25 ns on dense and half masks, 130 to 135 ns at 1 in 64.

How to read this:

  • The register fold's 174 µs is its one-group case, where every row reads and rewrites the same three words. Its own measurement with 16 groups was 1.095 ns per row, about 72 µs per tile. This PR claims the single-population case only.
  • The count step is nearly the whole cost. For that cost the caller also gets Σx³, Σx⁴, min and max, which the register fold does not produce.
  • On a very sparse mask the three routes cost the same.

Not done

  • No grouped form: one table is 256 counters per group, against 16 bytes per group for the register. The group count at which the register wins was not measured.
  • No caller yet in lance-graph-mask-risc or jc.
  • The cross term Σxy is not covered.
  • Only the AVX-512 arm was timed.

Generated by Claude Code

…ddition

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAqKMNPpoyKDKuAgEnu6HU
masked_code_counts_u8 counts selected rows per code. code_power_sums_u8
turns the 256 counts into n, sum x .. sum x^4, min and max with a chain of
five accumulators that each add their neighbour; no value is multiplied
per row and no lane wider than the u8 codes is read.
masked_first_row_of_code_u8 gives the lowest selected row of a code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAqKMNPpoyKDKuAgEnu6HU
…registration

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAqKMNPpoyKDKuAgEnu6HU
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 38db285f-0834-4e7a-8065-16f4b3e683ba
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review November 14, 2026 23:07
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T23:09:24.844674Z 95f6091 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AdaWorldAPI
AdaWorldAPI merged commit c23de1c into master Nov 14, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants