Skip to content

test(simd): add parity contract tests for DSP-ext accelerated u8x16 ops (refs #2628) - #2870

Merged
zackees merged 1 commit into
masterfrom
feat/2628-simd-arm-dsp
Jun 6, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/2628-simd-arm-dsp

Conversation

@zackees

@zackees zackees commented Jun 6, 2026 •

Copy link
Copy Markdown
Member

Refs #2628.

Summary

PR #2808 wired the minimal ARM Cortex-M DSP-extension SIMD backend (src/platforms/arm/teensy/simd_arm_dsp.hpp) and added a dispatch sanity test, but it didn't extend the cross-backend contract suite in tests/fl/math/simd.cpp. That left sub_sat_u8_16, avg_u8_16, avg_round_u8_16, min/max_u8_16, the four bitwise *_u8_16 ops, and add_u16_8 running on every backend without an assertion of their shared semantics.

This adds focused contract tests for each of those ops. They drive CI on the host noop / x86 paths and act as a free parity oracle on the Teensy 3.x / 4.x cross-compile (which dispatches through the DSP-ext header). When the deferred follow-ups in the DSP-ext file (scale_u8_16 via SMUAD/PKHBT, min/max_u8_16 via USUB8+SEL, narrow_u16_to_u8 via USAT16) get filled in, these tests remain unchanged — they verify the contract, not the implementation.

Notable design choices

  • avg_u8_16: inputs constrained to even-sum pairs so floor and round-half-up agree on every lane. Host SSE (_mm_avg_epu8) rounds while noop and ARM DSP-ext (UHADD8) floor — that's a real cross-backend divergence on odd sums that is out of scope here. The constrained test pins down the intersection contract; a floor-only or round-only behavioural assertion belongs in a backend-specific suite.
  • add_u16_8: round-trips through widen_lo/hi_u8_to_u16 + narrow_u16_to_u8 rather than touching simd_u16x8 fields directly, because the public API leaves the type opaque (__m128i on SSE, struct on noop / DSP-ext, hardware vector on Xtensa). Inputs clamped to u8 range so the narrow's saturation doesn't obscure the modular add_u16_8 semantics.

Tests covered

Op DSP-ext implementation Coverage before Coverage after
sub_sat_u8_16 UQSUB8 x4 — ✅
avg_u8_16 UHADD8 x4 (floor) — ✅
avg_round_u8_16 scalar (deferred) — ✅
min_u8_16 scalar (deferred USUB8+SEL) — ✅
max_u8_16 scalar (deferred) — ✅
and_u8_16 32-bit AND x4 — ✅
or_u8_16 32-bit ORR x4 — ✅
xor_u8_16 32-bit EOR x4 — ✅
andnot_u8_16 32-bit BIC x4 — ✅
add_u16_8 UADD16 x4 — ✅

Test plan

  • bash test fl_math_simd — 1/1 passed (host noop + x86)
  • bash test --cpp — 310/310 passed (no regression)
  • bash compile teensy40 --examples AutoResearch — clean (DSP-ext dispatch path on Cortex-M7)

Out of scope (deferred to #2628 follow-ups)

  • The DSP-ext implementations of scale_u8_16 (SMUAD-based), blend_u8_16 (weighted blend), min/max_u8_16 (USUB8 + SEL with GE flags), and narrow_u16_to_u8 (USAT16). These remain TODO markers in simd_arm_dsp.hpp; the tests added here will validate them automatically once the intrinsics land.
  • Resolving the avg_u8_16 floor/round divergence between x86 and noop/DSP-ext.
  • On-device cycles-per-op micro-benchmark on Teensy 4.x — requires hardware.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added extensive test coverage for vector operations including saturation, rounding, and bitwise operations to ensure correctness across hardware platforms and edge cases.

Refs #2628.

PR #2808 wired the minimal ARM Cortex-M DSP extension backend (`src/
platforms/arm/teensy/simd_arm_dsp.hpp`) and added a dispatch sanity
test, but it didn't extend the cross-backend contract suite in
`tests/fl/math/simd.cpp`. That left `sub_sat_u8_16`, `avg_u8_16`,
`avg_round_u8_16`, `min/max_u8_16`, the four bitwise `*_u8_16` ops,
and `add_u16_8` running on every backend without an assertion of the
shared semantics.

Add focused contract tests for each of these. They drive CI on the
host noop / x86 paths and act as a free parity oracle on the Teensy
3.x/4.x cross-compile (which routes through the DSP-ext header that
issue #2628 originally landed). When the deferred follow-ups in the
DSP-ext file (`scale_u8_16` via SMUAD/PKHBT, `min/max_u8_16` via
USUB8+SEL, `narrow_u16_to_u8` via USAT16) get filled in, these tests
remain unchanged — they verify the *contract*, not the implementation.

Notable design choices:

- The `avg_u8_16` test constrains inputs to even-sum pairs so floor
  and round-half-up agree on every lane. Host SSE (`_mm_avg_epu8`)
  rounds while noop and ARM DSP-ext (`UHADD8`) floor — that's a real
  cross-backend divergence on odd sums that is out of scope here.
  The constrained test pins down the intersection contract; a
  floor-only or round-only behavioural assertion belongs in a
  backend-specific suite.
- The `add_u16_8` test round-trips through `widen_lo/hi_u8_to_u16`
  and `narrow_u16_to_u8` rather than touching `simd_u16x8` fields
  directly, because the public API leaves the type opaque (`__m128i`
  on SSE, a struct on noop / DSP-ext, a hardware vector on Xtensa).
  Inputs are clamped to u8 range so the narrow's saturation doesn't
  obscure the test of `add_u16_8`'s modular semantics.

Verified with:

- `bash test fl_math_simd` — 1/1 passed (noop+x86 host build)
- `bash test --cpp` — 310/310 passed (no regression elsewhere)
- `bash compile teensy40 --examples AutoResearch` — clean (DSP-ext
  dispatch path on the Teensy 4.0 Cortex-M7 target)

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds a comprehensive contract test suite for u8x16 SIMD operations in the FastLED math test file, covering saturation arithmetic, averaging with rounding, min/max comparisons, bitwise logical operations, and composite widen/add/narrow sequences targeting ARM Cortex-M DSP backend parity.

Changes

ARM Cortex-M DSP u8x16 Contract Tests

Layer / File(s) Summary
u8x16 contract tests
tests/fl/math/simd.cpp
Adds 232 lines of dedicated test cases validating lane-wise results for sub_sat_u8_16, avg_u8_16, avg_round_u8_16, min_u8_16, max_u8_16, and bitwise ops (and_u8_16, or_u8_16, xor_u8_16, andnot_u8_16); includes add_u16_8 validation through widen/add/narrow round-trip with expected modular/widen-narrow semantics and edge cases for saturation and rounding.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • FastLED/FastLED#2628: The new u8x16 contract tests directly validate the Tier-1 SIMD u8x16 operations and parity coverage for the ARM Cortex-M DSP backend.

Possibly related PRs

  • FastLED/FastLED#2808: Implements the Cortex-M DSP-extension u8x16 operations (sub_sat_u8_16, avg_u8_16/avg_round_u8_16, min_u8_16/max_u8_16, bitwise ops, and add_u16_8) in simd_arm_dsp.hpp that are directly exercised by these contract tests.

Poem

🐰 A rabbit hops through vectors swift,
Eight bytes at once—oh, what a gift!
Saturate, average, minimum too,
Contract tests pass, the DSP shines through!
Cortex-M speed, no morsel gets slow. 🚀

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding parity contract tests for DSP-extension accelerated u8x16 operations, with specific issue reference.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/2628-simd-arm-dsp

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
tests/fl/math/simd.cpp

tests/fl/math/simd.cpp:3:10: fatal error: 'fl/math/simd.h' file not found
3 | #include "fl/math/simd.h"
| ^~~~~~~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/2770d14d8a8003e816bdbd22adc72de05baae9dd-01b34cc93138578e/tmp/clang_command_.tmp.1c781b.txt
++Contents of '/tmp/coderabbit-infer/2770d14d8a8003e816bdbd22adc72de05baae9dd-01b34cc93138578e/tmp/clang_command_.tmp.1c781b.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free"

... [truncated 1052 characters] ...

lang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-fdeprecated-macro" "-ferror-limit" "19" "-fgnuc-version=4.2.1"
"-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/01b34cc93138578e/file.o" "-x" "c++"
"tests/fl/math/simd.cpp" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/fl/math/simd.cpp`:
- Around line 1307-1526: The tests only assert lanes 0..7 so update each u8x16
test (sub_sat_u8_16, avg_u8_16, avg_round_u8_16, min_u8_16, max_u8_16,
and_u8_16, or_u8_16, xor_u8_16, andnot_u8_16) to also validate lanes 8..15 by
extending the FL_REQUIRE checks for dst[8]..dst[15] to expected values, and for
the add_u16_8 round-trip test populate non-zero values in the high half of the
input arrays (so widen_hi_u8_to_u16 / narrow_u16_to_u8 are exercised) and add
corresponding FL_REQUIRE assertions for dst[8]..dst[15]; locate these changes
around the simd::load_u8_16 / simd::store_u8_16 usage and the
add_u16_8/widen_/narrow_ sequence to ensure full 16-lane coverage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b7335adb-6ac1-457f-9d93-80239d67208f

📥 Commits

Reviewing files that changed from the base of the PR and between 163c5f5 and 2770d14.

📒 Files selected for processing (1)
  • tests/fl/math/simd.cpp

Comment thread tests/fl/math/simd.cpp
Comment on lines +1307 to +1526
FL_TEST_CASE("sub_sat_u8_16 subtracts without underflow") {
uint8_t a[16] = {100, 200, 255, 0, 50, 10, 128, 200, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = { 50, 100, 255, 100, 60, 10, 128, 201, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::sub_sat_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 50); // 100 - 50
FL_REQUIRE(dst[1] == 100); // 200 - 100
FL_REQUIRE(dst[2] == 0); // 255 - 255
FL_REQUIRE(dst[3] == 0); // 0 - 100 → clamp 0
FL_REQUIRE(dst[4] == 0); // 50 - 60 → clamp 0
FL_REQUIRE(dst[5] == 0); // 10 - 10
FL_REQUIRE(dst[6] == 0); // 128 - 128
FL_REQUIRE(dst[7] == 0); // 200 - 201 → clamp 0
}

FL_TEST_CASE("avg_u8_16 average (floor / round agreement zone)") {
// Inputs deliberately constrained to even sums so that floor and
// round-half-up semantics agree on every lane. This sidesteps a real
// backend divergence (host SSE uses `_mm_avg_epu8` which rounds while
// the noop and ARM DSP-ext path floor) so the contract this test
// pins down — symmetric average for matched-parity inputs — is the
// intersection the dispatched API must honour. A round-only or
// floor-only behavioural test belongs in a backend-specific suite.
uint8_t a[16] = {0, 100, 200, 254, 255, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
uint8_t b[16] = {0, 100, 200, 254, 255, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::avg_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 100);
FL_REQUIRE(dst[2] == 200);
FL_REQUIRE(dst[3] == 254);
FL_REQUIRE(dst[4] == 255);
FL_REQUIRE(dst[5] == 2); // (1+3)/2 = 2
FL_REQUIRE(dst[6] == 4);
FL_REQUIRE(dst[7] == 6);
}

FL_TEST_CASE("avg_round_u8_16 ceiling average") {
uint8_t a[16] = {0, 0, 100, 200, 254, 255, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0, 255, 100, 201, 254, 255, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::avg_round_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0); // (0 + 0 + 1) / 2 = 0
FL_REQUIRE(dst[1] == 128); // (0 + 255 + 1) / 2 = 128 (ceil; vs 127 floor)
FL_REQUIRE(dst[2] == 100); // (100 + 100 + 1) / 2 = 100
FL_REQUIRE(dst[3] == 201); // (200 + 201 + 1) / 2 = 201
FL_REQUIRE(dst[4] == 254); // (254 + 254 + 1) / 2 = 254
FL_REQUIRE(dst[5] == 255); // (255 + 255 + 1) / 2 = 255 — no overflow
FL_REQUIRE(dst[6] == 2); // (1 + 2 + 1) / 2 = 2
}

FL_TEST_CASE("min_u8_16 elementwise minimum") {
uint8_t a[16] = {0, 100, 200, 255, 0, 128, 1, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {1, 50, 200, 0, 255, 127, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::min_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 50);
FL_REQUIRE(dst[2] == 200); // equal → either
FL_REQUIRE(dst[3] == 0);
FL_REQUIRE(dst[4] == 0);
FL_REQUIRE(dst[5] == 127);
FL_REQUIRE(dst[6] == 1);
FL_REQUIRE(dst[7] == 254); // equal → either
}

FL_TEST_CASE("max_u8_16 elementwise maximum") {
uint8_t a[16] = {0, 100, 200, 255, 0, 128, 1, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {1, 50, 200, 0, 255, 127, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::max_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 1);
FL_REQUIRE(dst[1] == 100);
FL_REQUIRE(dst[2] == 200);
FL_REQUIRE(dst[3] == 255);
FL_REQUIRE(dst[4] == 255);
FL_REQUIRE(dst[5] == 128);
FL_REQUIRE(dst[6] == 2);
FL_REQUIRE(dst[7] == 254);
}

FL_TEST_CASE("and_u8_16 bitwise AND") {
uint8_t a[16] = {0xFF, 0xF0, 0x0F, 0xAA, 0x55, 0x00, 0xC3, 0x3C, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0xFF, 0x0F, 0xF0, 0xFF, 0xAA, 0xFF, 0x81, 0x18, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::and_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0x00);
FL_REQUIRE(dst[2] == 0x00);
FL_REQUIRE(dst[3] == 0xAA);
FL_REQUIRE(dst[4] == 0x00);
FL_REQUIRE(dst[5] == 0x00);
FL_REQUIRE(dst[6] == 0x81);
FL_REQUIRE(dst[7] == 0x18);
}

FL_TEST_CASE("or_u8_16 bitwise OR") {
uint8_t a[16] = {0xFF, 0xF0, 0x0F, 0xAA, 0x55, 0x00, 0xC3, 0x3C, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0x00, 0x0F, 0xF0, 0x55, 0xAA, 0x00, 0x18, 0x81, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::or_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0xFF);
FL_REQUIRE(dst[2] == 0xFF);
FL_REQUIRE(dst[3] == 0xFF);
FL_REQUIRE(dst[4] == 0xFF);
FL_REQUIRE(dst[5] == 0x00);
FL_REQUIRE(dst[6] == 0xDB); // 0xC3 | 0x18 = 0xDB
FL_REQUIRE(dst[7] == 0xBD); // 0x3C | 0x81 = 0xBD
}

FL_TEST_CASE("xor_u8_16 bitwise XOR") {
uint8_t a[16] = {0xFF, 0xF0, 0xAA, 0x55, 0x00, 0xFF, 0x12, 0x34, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0x00, 0xF0, 0xAA, 0xAA, 0x00, 0xFF, 0x34, 0x12, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::xor_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0x00); // a ^ a = 0
FL_REQUIRE(dst[2] == 0x00); // a ^ a = 0
FL_REQUIRE(dst[3] == 0xFF); // 0x55 ^ 0xAA
FL_REQUIRE(dst[4] == 0x00);
FL_REQUIRE(dst[5] == 0x00); // 0xFF ^ 0xFF = 0
FL_REQUIRE(dst[6] == 0x26); // 0x12 ^ 0x34
FL_REQUIRE(dst[7] == 0x26); // 0x34 ^ 0x12
}

FL_TEST_CASE("andnot_u8_16: (~a) & b") {
uint8_t a[16] = {0xFF, 0x00, 0xF0, 0x0F, 0xAA, 0x55, 0x12, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::andnot_u8_16(va, vb);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0x00); // (~0xFF) & 0xAA = 0
FL_REQUIRE(dst[1] == 0xAA); // (~0x00) & 0xAA = 0xAA
FL_REQUIRE(dst[2] == 0x0F); // (~0xF0) & 0xFF = 0x0F
FL_REQUIRE(dst[3] == 0xF0);
FL_REQUIRE(dst[4] == 0x55); // (~0xAA) & 0xFF
FL_REQUIRE(dst[5] == 0xAA);
FL_REQUIRE(dst[6] == 0xED); // (~0x12) & 0xFF = 0xED
FL_REQUIRE(dst[7] == 0x00); // (~0xFF) & 0x55 = 0
}

FL_TEST_CASE("add_u16_8 via widen/narrow round-trip (UADD16 contract)") {
// The public API doesn't expose load_u16_8/store_u16_8 — every backend
// hands `simd_u16x8` back as either a struct, an SSE __m128i, or a
// hardware vector register. Verifying add_u16_8 across backends means
// round-tripping through widen_*_u8_to_u16 + narrow_u16_to_u8.
//
// Inputs are constrained to fit in u8 [0..255] both before AND after the
// halfword add so the saturating narrow at the end doesn't clamp our
// outputs and the test reflects the modular add_u16_8 contract that
// UADD16 implements.
uint8_t a[16] = { 0, 10, 50, 100, 127, 128, 200, 255, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = { 0, 1, 25, 50, 0, 127, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];

auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto la = simd::widen_lo_u8_to_u16(va);
auto lb = simd::widen_lo_u8_to_u16(vb);
auto ha = simd::widen_hi_u8_to_u16(va);
auto hb = simd::widen_hi_u8_to_u16(vb);
auto sum_lo = simd::add_u16_8(la, lb);
auto sum_hi = simd::add_u16_8(ha, hb);
auto vr = simd::narrow_u16_to_u8(sum_lo, sum_hi);
simd::store_u8_16(dst, vr);

FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 11);
FL_REQUIRE(dst[2] == 75);
FL_REQUIRE(dst[3] == 150);
FL_REQUIRE(dst[4] == 127);
FL_REQUIRE(dst[5] == 255); // 128 + 127 = 255 — at boundary, no clamp
FL_REQUIRE(dst[6] == 250);
FL_REQUIRE(dst[7] == 255);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Validate all 16 lanes; current checks miss half the vector contract

The new u8x16 parity tests only assert lanes 0..7. That leaves lanes 8..15 unverified across every new op, and add_u16_8 currently feeds all-zero high-half inputs, so widen_hi_*/upper-half round-trip behavior is effectively untested.

Suggested test-shape fix
 FL_TEST_CASE("sub_sat_u8_16 subtracts without underflow") {
     uint8_t a[16] = {100, 200, 255,   0,  50,  10, 128, 200, 0, 0, 0, 0, 0, 0, 0, 0};
     uint8_t b[16] = { 50, 100, 255, 100,  60,  10, 128, 201, 0, 0, 0, 0, 0, 0, 0, 0};
+    uint8_t expected[16] = {50, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     uint8_t dst[16];
@@
-    FL_REQUIRE(dst[0] == 50);
-    ...
-    FL_REQUIRE(dst[7] == 0);
+    for (int i = 0; i < 16; ++i) {
+        FL_REQUIRE_EQ(dst[i], expected[i]);
+    }
 }
 FL_TEST_CASE("add_u16_8 via widen/narrow round-trip (UADD16 contract)") {
-    uint8_t a[16] = { 0, 10, 50, 100, 127, 128, 200, 255, 0, 0, 0, 0, 0, 0, 0, 0};
-    uint8_t b[16] = { 0,  1, 25,  50,   0, 127,  50,   0, 0, 0, 0, 0, 0, 0, 0, 0};
+    uint8_t a[16] = { 0, 10, 50, 100, 127, 128, 200, 255, 1, 2, 3, 4, 5, 6, 7, 8};
+    uint8_t b[16] = { 0,  1, 25,  50,   0, 127,  50,   0, 8, 7, 6, 5, 4, 3, 2, 1};
+    uint8_t expected[16] = {0, 11, 75, 150, 127, 255, 250, 255, 9, 9, 9, 9, 9, 9, 9, 9};
@@
-    FL_REQUIRE(dst[0] == 0);
-    ...
-    FL_REQUIRE(dst[7] == 255);
+    for (int i = 0; i < 16; ++i) {
+        FL_REQUIRE_EQ(dst[i], expected[i]);
+    }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
FL_TEST_CASE("sub_sat_u8_16 subtracts without underflow") {
uint8_t a[16] = {100, 200, 255, 0, 50, 10, 128, 200, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = { 50, 100, 255, 100, 60, 10, 128, 201, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::sub_sat_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 50); // 100 - 50
FL_REQUIRE(dst[1] == 100); // 200 - 100
FL_REQUIRE(dst[2] == 0); // 255 - 255
FL_REQUIRE(dst[3] == 0); // 0 - 100 → clamp 0
FL_REQUIRE(dst[4] == 0); // 50 - 60 → clamp 0
FL_REQUIRE(dst[5] == 0); // 10 - 10
FL_REQUIRE(dst[6] == 0); // 128 - 128
FL_REQUIRE(dst[7] == 0); // 200 - 201 → clamp 0
}
FL_TEST_CASE("avg_u8_16 average (floor / round agreement zone)") {
// Inputs deliberately constrained to even sums so that floor and
// round-half-up semantics agree on every lane. This sidesteps a real
// backend divergence (host SSE uses `_mm_avg_epu8` which rounds while
// the noop and ARM DSP-ext path floor) so the contract this test
// pins down — symmetric average for matched-parity inputs — is the
// intersection the dispatched API must honour. A round-only or
// floor-only behavioural test belongs in a backend-specific suite.
uint8_t a[16] = {0, 100, 200, 254, 255, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
uint8_t b[16] = {0, 100, 200, 254, 255, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::avg_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 100);
FL_REQUIRE(dst[2] == 200);
FL_REQUIRE(dst[3] == 254);
FL_REQUIRE(dst[4] == 255);
FL_REQUIRE(dst[5] == 2); // (1+3)/2 = 2
FL_REQUIRE(dst[6] == 4);
FL_REQUIRE(dst[7] == 6);
}
FL_TEST_CASE("avg_round_u8_16 ceiling average") {
uint8_t a[16] = {0, 0, 100, 200, 254, 255, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0, 255, 100, 201, 254, 255, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::avg_round_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0); // (0 + 0 + 1) / 2 = 0
FL_REQUIRE(dst[1] == 128); // (0 + 255 + 1) / 2 = 128 (ceil; vs 127 floor)
FL_REQUIRE(dst[2] == 100); // (100 + 100 + 1) / 2 = 100
FL_REQUIRE(dst[3] == 201); // (200 + 201 + 1) / 2 = 201
FL_REQUIRE(dst[4] == 254); // (254 + 254 + 1) / 2 = 254
FL_REQUIRE(dst[5] == 255); // (255 + 255 + 1) / 2 = 255 — no overflow
FL_REQUIRE(dst[6] == 2); // (1 + 2 + 1) / 2 = 2
}
FL_TEST_CASE("min_u8_16 elementwise minimum") {
uint8_t a[16] = {0, 100, 200, 255, 0, 128, 1, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {1, 50, 200, 0, 255, 127, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::min_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 50);
FL_REQUIRE(dst[2] == 200); // equal → either
FL_REQUIRE(dst[3] == 0);
FL_REQUIRE(dst[4] == 0);
FL_REQUIRE(dst[5] == 127);
FL_REQUIRE(dst[6] == 1);
FL_REQUIRE(dst[7] == 254); // equal → either
}
FL_TEST_CASE("max_u8_16 elementwise maximum") {
uint8_t a[16] = {0, 100, 200, 255, 0, 128, 1, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {1, 50, 200, 0, 255, 127, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::max_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 1);
FL_REQUIRE(dst[1] == 100);
FL_REQUIRE(dst[2] == 200);
FL_REQUIRE(dst[3] == 255);
FL_REQUIRE(dst[4] == 255);
FL_REQUIRE(dst[5] == 128);
FL_REQUIRE(dst[6] == 2);
FL_REQUIRE(dst[7] == 254);
}
FL_TEST_CASE("and_u8_16 bitwise AND") {
uint8_t a[16] = {0xFF, 0xF0, 0x0F, 0xAA, 0x55, 0x00, 0xC3, 0x3C, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0xFF, 0x0F, 0xF0, 0xFF, 0xAA, 0xFF, 0x81, 0x18, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::and_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0x00);
FL_REQUIRE(dst[2] == 0x00);
FL_REQUIRE(dst[3] == 0xAA);
FL_REQUIRE(dst[4] == 0x00);
FL_REQUIRE(dst[5] == 0x00);
FL_REQUIRE(dst[6] == 0x81);
FL_REQUIRE(dst[7] == 0x18);
}
FL_TEST_CASE("or_u8_16 bitwise OR") {
uint8_t a[16] = {0xFF, 0xF0, 0x0F, 0xAA, 0x55, 0x00, 0xC3, 0x3C, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0x00, 0x0F, 0xF0, 0x55, 0xAA, 0x00, 0x18, 0x81, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::or_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0xFF);
FL_REQUIRE(dst[2] == 0xFF);
FL_REQUIRE(dst[3] == 0xFF);
FL_REQUIRE(dst[4] == 0xFF);
FL_REQUIRE(dst[5] == 0x00);
FL_REQUIRE(dst[6] == 0xDB); // 0xC3 | 0x18 = 0xDB
FL_REQUIRE(dst[7] == 0xBD); // 0x3C | 0x81 = 0xBD
}
FL_TEST_CASE("xor_u8_16 bitwise XOR") {
uint8_t a[16] = {0xFF, 0xF0, 0xAA, 0x55, 0x00, 0xFF, 0x12, 0x34, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0x00, 0xF0, 0xAA, 0xAA, 0x00, 0xFF, 0x34, 0x12, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::xor_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0xFF);
FL_REQUIRE(dst[1] == 0x00); // a ^ a = 0
FL_REQUIRE(dst[2] == 0x00); // a ^ a = 0
FL_REQUIRE(dst[3] == 0xFF); // 0x55 ^ 0xAA
FL_REQUIRE(dst[4] == 0x00);
FL_REQUIRE(dst[5] == 0x00); // 0xFF ^ 0xFF = 0
FL_REQUIRE(dst[6] == 0x26); // 0x12 ^ 0x34
FL_REQUIRE(dst[7] == 0x26); // 0x34 ^ 0x12
}
FL_TEST_CASE("andnot_u8_16: (~a) & b") {
uint8_t a[16] = {0xFF, 0x00, 0xF0, 0x0F, 0xAA, 0x55, 0x12, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = {0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::andnot_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0x00); // (~0xFF) & 0xAA = 0
FL_REQUIRE(dst[1] == 0xAA); // (~0x00) & 0xAA = 0xAA
FL_REQUIRE(dst[2] == 0x0F); // (~0xF0) & 0xFF = 0x0F
FL_REQUIRE(dst[3] == 0xF0);
FL_REQUIRE(dst[4] == 0x55); // (~0xAA) & 0xFF
FL_REQUIRE(dst[5] == 0xAA);
FL_REQUIRE(dst[6] == 0xED); // (~0x12) & 0xFF = 0xED
FL_REQUIRE(dst[7] == 0x00); // (~0xFF) & 0x55 = 0
}
FL_TEST_CASE("add_u16_8 via widen/narrow round-trip (UADD16 contract)") {
// The public API doesn't expose load_u16_8/store_u16_8 — every backend
// hands `simd_u16x8` back as either a struct, an SSE __m128i, or a
// hardware vector register. Verifying add_u16_8 across backends means
// round-tripping through widen_*_u8_to_u16 + narrow_u16_to_u8.
//
// Inputs are constrained to fit in u8 [0..255] both before AND after the
// halfword add so the saturating narrow at the end doesn't clamp our
// outputs and the test reflects the modular add_u16_8 contract that
// UADD16 implements.
uint8_t a[16] = { 0, 10, 50, 100, 127, 128, 200, 255, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t b[16] = { 0, 1, 25, 50, 0, 127, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto la = simd::widen_lo_u8_to_u16(va);
auto lb = simd::widen_lo_u8_to_u16(vb);
auto ha = simd::widen_hi_u8_to_u16(va);
auto hb = simd::widen_hi_u8_to_u16(vb);
auto sum_lo = simd::add_u16_8(la, lb);
auto sum_hi = simd::add_u16_8(ha, hb);
auto vr = simd::narrow_u16_to_u8(sum_lo, sum_hi);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 11);
FL_REQUIRE(dst[2] == 75);
FL_REQUIRE(dst[3] == 150);
FL_REQUIRE(dst[4] == 127);
FL_REQUIRE(dst[5] == 255); // 128 + 127 = 255 — at boundary, no clamp
FL_REQUIRE(dst[6] == 250);
FL_REQUIRE(dst[7] == 255);
}
FL_TEST_CASE("sub_sat_u8_16 subtracts without underflow") {
uint8_t a[16] = {100, 200, 255, 0, 50, 10, 128, 200, 1, 2, 3, 4, 5, 6, 7, 8};
uint8_t b[16] = { 50, 100, 255, 100, 60, 10, 128, 201, 8, 7, 6, 5, 4, 3, 2, 1};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto vr = simd::sub_sat_u8_16(va, vb);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 50); // 100 - 50
FL_REQUIRE(dst[1] == 100); // 200 - 100
FL_REQUIRE(dst[2] == 0); // 255 - 255
FL_REQUIRE(dst[3] == 0); // 0 - 100 → clamp 0
FL_REQUIRE(dst[4] == 0); // 50 - 60 → clamp 0
FL_REQUIRE(dst[5] == 0); // 10 - 10
FL_REQUIRE(dst[6] == 0); // 128 - 128
FL_REQUIRE(dst[7] == 0); // 200 - 201 → clamp 0
FL_REQUIRE(dst[8] == 0); // 1 - 8 → clamp 0
FL_REQUIRE(dst[9] == 0); // 2 - 7 → clamp 0
FL_REQUIRE(dst[10] == 0); // 3 - 6 → clamp 0
FL_REQUIRE(dst[11] == 0); // 4 - 5 → clamp 0
FL_REQUIRE(dst[12] == 1); // 5 - 4
FL_REQUIRE(dst[13] == 3); // 6 - 3
FL_REQUIRE(dst[14] == 5); // 7 - 2
FL_REQUIRE(dst[15] == 7); // 8 - 1
}
FL_TEST_CASE("add_u16_8 via widen/narrow round-trip (UADD16 contract)") {
uint8_t a[16] = { 0, 10, 50, 100, 127, 128, 200, 255, 1, 2, 3, 4, 5, 6, 7, 8};
uint8_t b[16] = { 0, 1, 25, 50, 0, 127, 50, 0, 8, 7, 6, 5, 4, 3, 2, 1};
uint8_t dst[16];
auto va = simd::load_u8_16(a);
auto vb = simd::load_u8_16(b);
auto la = simd::widen_lo_u8_to_u16(va);
auto lb = simd::widen_lo_u8_to_u16(vb);
auto ha = simd::widen_hi_u8_to_u16(va);
auto hb = simd::widen_hi_u8_to_u16(vb);
auto sum_lo = simd::add_u16_8(la, lb);
auto sum_hi = simd::add_u16_8(ha, hb);
auto vr = simd::narrow_u16_to_u8(sum_lo, sum_hi);
simd::store_u8_16(dst, vr);
FL_REQUIRE(dst[0] == 0);
FL_REQUIRE(dst[1] == 11);
FL_REQUIRE(dst[2] == 75);
FL_REQUIRE(dst[3] == 150);
FL_REQUIRE(dst[4] == 127);
FL_REQUIRE(dst[5] == 255); // 128 + 127 = 255 — at boundary, no clamp
FL_REQUIRE(dst[6] == 250);
FL_REQUIRE(dst[7] == 255);
FL_REQUIRE(dst[8] == 9); // 1 + 8
FL_REQUIRE(dst[9] == 9); // 2 + 7
FL_REQUIRE(dst[10] == 9); // 3 + 6
FL_REQUIRE(dst[11] == 9); // 4 + 5
FL_REQUIRE(dst[12] == 9); // 5 + 4
FL_REQUIRE(dst[13] == 9); // 6 + 3
FL_REQUIRE(dst[14] == 9); // 7 + 2
FL_REQUIRE(dst[15] == 9); // 8 + 1
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/fl/math/simd.cpp` around lines 1307 - 1526, The tests only assert lanes
0..7 so update each u8x16 test (sub_sat_u8_16, avg_u8_16, avg_round_u8_16,
min_u8_16, max_u8_16, and_u8_16, or_u8_16, xor_u8_16, andnot_u8_16) to also
validate lanes 8..15 by extending the FL_REQUIRE checks for dst[8]..dst[15] to
expected values, and for the add_u16_8 round-trip test populate non-zero values
in the high half of the input arrays (so widen_hi_u8_to_u16 / narrow_u16_to_u8
are exercised) and add corresponding FL_REQUIRE assertions for dst[8]..dst[15];
locate these changes around the simd::load_u8_16 / simd::store_u8_16 usage and
the add_u16_8/widen_/narrow_ sequence to ensure full 16-lane coverage.

@zackees
zackees merged commit a1311af into master Jun 6, 2026
4 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 6, 2026
@zackees
zackees deleted the feat/2628-simd-arm-dsp branch June 6, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant