Repository navigation
test(simd): add parity contract tests for DSP-ext accelerated u8x16 ops (refs #2628) - #2870
Conversation
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]>
📝 WalkthroughWalkthroughThis 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. ChangesARM Cortex-M DSP u8x16 Contract Tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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.cpptests/fl/math/simd.cpp:3:10: fatal error: 'fl/math/simd.h' file not found ... [truncated 1052 characters] ... lang/install/lib/clang/18/include" 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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
tests/fl/math/simd.cpp
| 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); | ||
| } |
There was a problem hiding this comment.
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.
| 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.
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 intests/fl/math/simd.cpp. That leftsub_sat_u8_16,avg_u8_16,avg_round_u8_16,min/max_u8_16, the four bitwise*_u8_16ops, andadd_u16_8running 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_16via SMUAD/PKHBT,min/max_u8_16via USUB8+SEL,narrow_u16_to_u8via 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 throughwiden_lo/hi_u8_to_u16+narrow_u16_to_u8rather than touchingsimd_u16x8fields directly, because the public API leaves the type opaque (__m128ion SSE, struct on noop / DSP-ext, hardware vector on Xtensa). Inputs clamped to u8 range so the narrow's saturation doesn't obscure the modularadd_u16_8semantics.Tests covered
sub_sat_u8_16avg_u8_16avg_round_u8_16min_u8_16max_u8_16and_u8_16or_u8_16xor_u8_16andnot_u8_16add_u16_8Test 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)
scale_u8_16(SMUAD-based),blend_u8_16(weighted blend),min/max_u8_16(USUB8 + SEL with GE flags), andnarrow_u16_to_u8(USAT16). These remain TODO markers insimd_arm_dsp.hpp; the tests added here will validate them automatically once the intrinsics land.avg_u8_16floor/round divergence between x86 and noop/DSP-ext.🤖 Generated with Claude Code
Summary by CodeRabbit