Skip to content

feat(simd/arm-dsp): implement Cortex-M DSP-ext SIMD backend for Teensy 3.x/4.x (per-op checklist) #3425

Closed

Description

@zackees

Background

Spun out from #2628 with concrete per-op scope. FastLED's fl::simd layer (src/platforms/shared/simd_noop.hpp) falls through to scalar on all ARM Cortex-M targets. The ARMv7E-M DSP extension (Cortex-M4/M4F on Teensy 3.x, Cortex-M7 on Teensy 4.x) provides 4-lane 8-bit and 2-lane 16-bit packed-integer ops in a single 32-bit GPR.

Plan: create src/platforms/shared/simd_arm_dsp.hpp and route __ARM_FEATURE_DSP to it from the trampoline at src/platforms/simd.h. Implement one op at a time and verify each via the autoresearch SIMD test path before moving to the next.

Implementation flow (mandatory per op)

For each unchecked item below:

  1. Implement the op as one inline function in src/platforms/shared/simd_arm_dsp.hpp using ARM intrinsics from <arm_acle.h> (e.g. __UQADD8, __UHADD8, __UXTB16, __SMUAD) — NOT inline asm, intrinsics are cleaner.
  2. Wire the trampoline in src/platforms/simd.h to route just THIS op to the new backend (leave the rest falling through to simd_noop.hpp).
  3. Compile for teensy40 and verify no regression: bash compile teensy40 --examples AutoResearch.
  4. Host unit test: add or extend the existing fl::simd tests so the new op's behavior is bit-exact equivalent to the noop fallback on the host (catches semantic regressions before they reach hardware).
  5. Hardware validate: bash autoresearch teensy40 --simd --skip-lint (single run is fine for SIMD — it's a deterministic compute test, not a hardware-timing test). The device runs the SIMD self-test suite and reports per-op pass/fail via JSON-RPC.
  6. If the device crashes (RPC timeout, watchdog reset, or device disappears from serial): the issue is almost certainly an intrinsic mis-use (operand alignment, register class, or undefined behavior on the M-class core). Capture the crash log (`[FastLED.watchdog] SRC_SRSR = ...` from the next boot), bisect the new function, fix, re-run.
  7. Tick the checkbox + link the PR.

Why one-at-a-time: ARM DSP intrinsics are sharp — a misclassified operand or wrong intrinsic family for the lane width can cause a HardFault that takes down the whole sketch. Bisecting one op at a time keeps each crash localized.

Op checklist

🟢 Big wins — implement these first (highest ROI)

These are 1 DSP opcode per 4 bytes / 2 halfwords, vs ~16+ scalar ops with branches.

  • add_sat_u8_16 — 4× __UQADD8. Hot path for every brightness step, fade, blend. Probably the single highest-impact op in FastLED.
  • sub_sat_u8_16 — 4× __UQSUB8. Same shape, used in fade-down and subtract-blend.
  • avg_u8_16 (truncating) — 4× __UHADD8. Used in averaging filters.
  • avg_round_u8_16 — 4× __UHADD8 + correction term. Used in 2D blur (fl/fx/2d/blend.cpp.hpp).
  • avg_round_u8_32 — 8× __UHADD8. u8x32 sibling for wider buffers.
  • add_u16_8 — 4× __UADD16. 2-halfword adds.
  • widen_lo_u8_to_u16 — 2× __UXTB16. Zero-extends 2 bytes to 2 halfwords in one cycle — perfect DSP fit.
  • widen_hi_u8_to_u16 — ROR #8 + 2× __UXTB16. Same pattern, upper-half source.
  • narrow_u16_to_u8 — __PKHBT / __PKHTB + __USAT8 to pack halfwords back to bytes.
  • widen_lo_u8x32_to_u16 + widen_hi_u8x32_to_u16 — u8x32 versions of the widen ops.

🟡 Possible wins — implement only if profiling shows them hot

Multi-opcode but still beats scalar. Skip unless a measurable benchmark says they matter.

  • min_u8_16 — __UQSUB8 + __SEL byte-mask. ~3 ops per 4 bytes.
  • max_u8_16 — Same shape, swapped operands.
  • scale_u8_16 — __UXTB16 × 2 → __SMULBB × 2 → recombine via __PKHBT. 4-byte scaled-mul in ~6 cycles.
  • blend_u8_16 — Same shape as scale, with two sources interpolated by an 8-bit amount.
  • mullo_u16_8 — __SMULBB / __SMULBT / __SMULTB / __SMULTT quartet to do 2 mullo per 32-bit DSP op + low-bit mask.

🔴 Explicitly NOT in scope (and why)

DON'T waste time on these — they have no DSP-ext acceleration path:

  • All u32x4 ops — DSP regs are 32-bit; u32x4 is inherently one-word-per-register. Scalar IS the SIMD on this ISA.
  • All f32x4 ops — DSP-ext is integer-only. Cortex-M4F/M7 has VFP (scalar float) but no vector float. NEON isn't on M-class.
  • and_u8_16 / or_u8_16 / xor_u8_16 / andnot_u8_16 — Compiler already emits 4× 32-bit AND/ORR/EOR/BIC; DSP-ext adds nothing on byte-bitwise.
  • load_u8_16 / store_u8_16 / load_u8_32 / store_u8_32 — LDM/STM bursts already, no widening possible.
  • srli_u16_8, srl_u32_4, sll_u32_4, sra_i32_4 — Scalar shifts are 1 cycle; no SIMD shift in DSP-ext.
  • All extract_* / unpack* / set1_* / set_* — Lane manipulation; no DSP-ext win.

Verification & meta

  • Final acceptance: bash autoresearch teensy40 --simd --skip-lint reports all 🟢 ops as ENABLED (not falling through to noop) and bit-exact equivalent to the noop fallback per the host unit tests.
  • Cross-target check: bash compile teensy41 --examples AutoResearch ✓ (T4.1 same M7 core, same DSP-ext).
  • Teensy 3.x build sanity (compile-only, no hardware): bash compile teensy31 --examples Blink and bash compile teensy36 --examples Blink -- Cortex-M4/M4F also has DSP-ext, so the same backend should compile and link there.
  • No regression on non-DSP targets: bash compile uno --examples Blink (AVR), bash compile esp32dev --examples Blink (Xtensa) -- ensure the trampoline gates on __ARM_FEATURE_DSP correctly and these still fall through to their own backends.

References

  • Closed parent: feat(simd): add Cortex-M DSP-extension SIMD backend for Teensy 3.x / 4.x (close the SIMD-dispatch gap) #2628 (per-op scope folded into this issue)
  • Existing SIMD layout: src/platforms/shared/simd_noop.hpp (scalar fallback to mirror), src/platforms/shared/simd_x86.hpp (intrinsic-style precedent), src/platforms/esp/32/simd_xtensa.hpp (ESP32-S3 PIE precedent), src/platforms/esp/32/simd_riscv.hpp (ESP32-P4 RISC-V precedent), src/platforms/simd.h (trampoline).
  • ARM ARMv7-M Architecture Reference Manual §A4.4 (SIMD instructions).
  • GCC <arm_acle.h> — the intrinsics header.

🤖 Generated with Claude Code

Activity

  1. moved this from Triage to Done in FastLED Trackeron Jun 27, 2026
  2. added a commit that references this issue on Jun 27, 2026
    cab1619
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions