Repository navigation
feat(simd): minimal Cortex-M DSP-ext backend for Teensy 3.x/4.x (refs #2628) - #2808
Conversation
…2628) Adds the missing ARM Cortex-M DSP-extension dispatch slot in `fl::simd`. Previously every ARM target fell through to `simd_noop.hpp` even though Teensy 3.x (Cortex-M4/M4F) and Teensy 4.x (Cortex-M7) ship the ARMv7E-M DSP extension — the same ISA family that makes CMSIS-DSP fast. What landed: - `src/platforms/arm/teensy/simd_arm_dsp.hpp` — new backend. 128-bit fl::simd vectors land as 4 packed 32-bit chunks; each Tier-1 byte op becomes a chain of 4 DSP-ext ops (one per chunk). GCC inline assembly rather than CMSIS intrinsics so the header is self-contained and does not depend on the Teensy core pulling in `<cmsis_gcc.h>` first. Accelerated this round (minimum useful subset): - `add_sat_u8_16` (UQADD8 x4) - `sub_sat_u8_16` (UQSUB8 x4) - `avg_u8_16` (UHADD8 x4 — floor average) - `and_u8_16` / `or_u8_16` / `xor_u8_16` / `andnot_u8_16` (plain 32-bit bitwise ops are already optimal; mirrored for API parity) - `add_u16_8` (UADD16 x4) Everything else (scale, blend, narrow/widen, u32 ops, f32 ops) falls through to scalar reference implementations identical to the noop backend. Follow-up work tracked in #2628. - `src/platforms/simd.h` — dispatcher: routes to `simd_arm_dsp.hpp` when `__ARM_FEATURE_DSP == 1`, otherwise keeps the existing `simd_noop.hpp` fallback. Cortex-M0/M0+/M3 keep the noop path. - `tests/fl/math/simd.cpp` — extends the existing SIMD test with one test case that compile-smokes the ARM-DSP dispatch under a mocked Teensy gate, plus a regression case that confirms the noop fallback still produces byte-equal output to the scalar reference. What is NOT in scope here (called out as #2628 follow-up): - UQADD16/UQSUB16 saturating-halfword variants - SMUAD-based `scale_u8_16` / mul-acc paths - USAT16-based `narrow_u16_to_u8` - SEL+USUB8-based `min_u8` / `max_u8` - Hardware byte-equality validation of DSP-ext output vs scalar reference on a real Teensy 4.x board (this PR is wiring + minimal ops; hardware parity tests are deferred to the next iteration). Worktree was interrupted mid-stream by a stop-hook lock contention. The file deltas above are the agent's pre-interruption work product, verified identical between the worktree and the main checkout it accidentally leaked into. Main checkout has been reverted to its pre-loop state; the authoritative copy lives on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds an ARMv7E‑M DSP‑extension SIMD backend header with 128/256-bit vector types and ops (DSP inline‑asm for 8‑bit saturating ops, scalar fallbacks for others), integrates it into the SIMD platform dispatcher, and adds a dispatch sanity test. ChangesARM Cortex-M DSP SIMD Backend Implementation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related issues
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)
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
🧹 Nitpick comments (3)
src/platforms/arm/teensy/simd_arm_dsp.hpp (3)
124-126: 💤 Low valueSection title "Atomic" may cause confusion.
The section header says "Atomic Load/Store Operations" but these operations are not atomic in the memory-ordering or thread-safety sense. Consider renaming to "Load/Store Operations" or "Memory Access Operations" to avoid confusion with
std::atomicsemantics.📝 Suggested rename
//============================================================================== -// Atomic Load/Store Operations +// Load/Store Operations //==============================================================================🤖 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 `@src/platforms/arm/teensy/simd_arm_dsp.hpp` around lines 124 - 126, The section header comment currently reads "Atomic Load/Store Operations" which is misleading because the functions under this section are not std::atomic/thread-safe; change that comment to a non‑atomic wording such as "Load/Store Operations" or "Memory Access Operations" (replace the string "Atomic Load/Store Operations" in the comment block above the related load/store functions in simd_arm_dsp.hpp) so readers won't confuse it with atomic memory-order semantics.
2-2: 💤 Low valueConsider using FastLED compiler control macro for include guard.
Per coding guidelines, prefer FastLED compiler control macros from
fl/stl/compiler_control.hinstead of raw#pragmadirectives. If no equivalent exists for#pragma once, this can remain as-is.As per coding guidelines: "Use FastLED compiler control macros from fl/stl/compiler_control.h instead of raw
#pragmadirectives"🤖 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 `@src/platforms/arm/teensy/simd_arm_dsp.hpp` at line 2, Replace the raw "`#pragma` once" with the FastLED compiler control macros: include "fl/stl/compiler_control.h" at the top of src/platforms/arm/teensy/simd_arm_dsp.hpp and wrap the header with the library's header-guard macros (use the begin/ end header guard macros provided by that file) instead of the pragma so the file follows the project's compiler control conventions.
444-468: 💤 Low valueClarify naming distinction between
mulhi_*_4(>>16) andmulhi32_i32_4(>>32).
mulhi_i32_4andmulhi_u32_4shift by 16 bits, whilemulhi32_i32_4(line 523) shifts by 32 bits. The naming could be clearer:
- "mulhi" typically implies the high half of a product (>>32 for 32-bit inputs)
- The >>16 variants might better be named
mul_shr16_*or similarIf this naming convention is intentional to match another SIMD API, consider adding a brief comment explaining the distinction.
🤖 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 `@src/platforms/arm/teensy/simd_arm_dsp.hpp` around lines 444 - 468, The names mulhi_i32_4 and mulhi_u32_4 currently perform a >>16 reduction while mulhi32_i32_4 performs >>32, which is confusing; either rename the >>16 variants to a clearer name (e.g., mul_shr16_i32_4 / mul_shr16_u32_4) and update all callsites, or keep the names and add a concise comment above mulhi_i32_4/mulhi_u32_4 explaining that in this file "mulhi" has been implemented as a >>16 high-half semantics (and that mulhi32_* are the true >>32 high-half variants), referencing the functions mulhi_i32_4, mulhi_u32_4 and mulhi32_i32_4 so readers know the distinction.
🤖 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 `@src/platforms/arm/teensy/simd_arm_dsp.hpp`:
- Around line 25-29: Fix the mismatched closing bracket characters in the doc
comment lines for the SIMD intrinsics: replace the incorrect trailing ] with )
for entries and_u8_16, or_u8_16, xor_u8_16, andnot_u8_16, and add_u16_8 so each
line reads e.g. "and_u8_16 (AND x4 — plain 32-bit AND is already optimal)"
instead of using ].
---
Nitpick comments:
In `@src/platforms/arm/teensy/simd_arm_dsp.hpp`:
- Around line 124-126: The section header comment currently reads "Atomic
Load/Store Operations" which is misleading because the functions under this
section are not std::atomic/thread-safe; change that comment to a non‑atomic
wording such as "Load/Store Operations" or "Memory Access Operations" (replace
the string "Atomic Load/Store Operations" in the comment block above the related
load/store functions in simd_arm_dsp.hpp) so readers won't confuse it with
atomic memory-order semantics.
- Line 2: Replace the raw "`#pragma` once" with the FastLED compiler control
macros: include "fl/stl/compiler_control.h" at the top of
src/platforms/arm/teensy/simd_arm_dsp.hpp and wrap the header with the library's
header-guard macros (use the begin/ end header guard macros provided by that
file) instead of the pragma so the file follows the project's compiler control
conventions.
- Around line 444-468: The names mulhi_i32_4 and mulhi_u32_4 currently perform a
>>16 reduction while mulhi32_i32_4 performs >>32, which is confusing; either
rename the >>16 variants to a clearer name (e.g., mul_shr16_i32_4 /
mul_shr16_u32_4) and update all callsites, or keep the names and add a concise
comment above mulhi_i32_4/mulhi_u32_4 explaining that in this file "mulhi" has
been implemented as a >>16 high-half semantics (and that mulhi32_* are the true
>>32 high-half variants), referencing the functions mulhi_i32_4, mulhi_u32_4 and
mulhi32_i32_4 so readers know the distinction.
🪄 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: cc949fa1-da7c-4ed0-90ca-4f167d0f0501
📒 Files selected for processing (3)
src/platforms/arm/teensy/simd_arm_dsp.hppsrc/platforms/simd.htests/fl/math/simd.cpp
Comment-only refresh of src/platforms/arm/teensy/simd_arm_dsp.hpp:
- Fix mismatched ] -> ) in the file-level accelerated-ops doc list
(and_u8_16, or_u8_16, xor_u8_16, andnot_u8_16, add_u16_8). [CR actionable]
- Rename the "Atomic Load/Store Operations" section header to make clear
these are plain word-granular memory accesses with no std::atomic /
thread-safety / memory-order semantics. [CR nit]
- Add a NOTE block above mulhi_*_4 explaining the cross-backend FastLED
SIMD naming convention: mulhi_{i,u}32_4 = (a*b)>>16 (PMULHW-style),
mulhi32_{i,u}32_4 = (a*b)>>32 (SMMUL/UMMUL-style). The names are
load-bearing across simd_noop/x86/arm_neon/xtensa/riscv, so we keep
them and document the distinction instead of renaming. [CR nit]
CR nit "replace #pragma once with FastLED header-guard macros" is
declined: fl/stl/compiler_control.h has no header-guard macros, and all
33 other headers under src/platforms/arm/teensy use #pragma once. CR
itself called this out as conditional ("If no equivalent exists for
#pragma once, this can remain as-is.").
No behavioral change. The header is #error-gated on __ARM_FEATURE_DSP
and is not included on host builds.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
@coderabbitai — CR review triage for 87285b8 ( Actionable (lines 25-29) — fixed. Replaced the stray Nit (lines 124-126) — fixed. Renamed the section header to Nit (lines 444-468) — clarification accepted, rename declined. Added a NOTE block above
These names are load-bearing across Nit (line 2, No behavioral change in this commit — only doc-comment edits inside a header that is |
|
Tip For best results, initiate chat on the files or code changes.
No further action needed from my side on these four threads. ✏️ Learnings added
🧠 Learnings usedIf you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
Refs FastLED#2628. PR FastLED#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 FastLED#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]>
Summary
Closes the SIMD-dispatch gap for ARMv7E-M cores. Teensy 3.x (Cortex-M4/M4F) and Teensy 4.x (Cortex-M7) all ship the ARMv7E-M DSP extension, but FastLED's `fl::simd` dispatcher routed every ARM target to `simd_noop.hpp`. This PR adds the minimum useful subset so byte-saturating arithmetic actually uses the hardware ops.
Refs #2628. Hardware byte-equality validation is deferred (call-out below).
What's accelerated
Everything else (scale, blend, narrow/widen, u32 ops, f32 ops) keeps the scalar reference path. Tracked in #2628 follow-ups:
What's changed
Test plan
Process note
The agent that drafted this PR was interrupted mid-flight by an unrelated stop-hook lock contention. The worktree state at `feat/2628-arm-dsp-simd` is its complete pre-interruption work product. Files identical between worktree and main checkout were verified before commit; main checkout has been reverted, so the authoritative copy lives on this branch.
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests