Repository navigation
Description
Measurement
testSimdBenchmark on RP2350W 2DCB876B587EA334, 10000 iterations, times in µs (lower is better):
| op | float | s16x16 | u16x16 | s8x8 | s16x16x4 (packed) |
|---|---|---|---|---|---|
| mul | 738 | 984 | 980 | 891 | 11568 |
| sub | 653 | 570 | 568 | 821 | 10434 |
Reproduce with bash autoresearch rp2350w --simd.
The comparison is like-for-like
Every helper in examples/AutoResearch/AutoResearchSimd.h processes four lanes per iteration, so this is not a 1-lane-vs-4-lane artefact:
benchFloat4— four scalarfloatopsbenchS16x16_4— four scalarfl::s16x16opsbenchSimd4— one packedfl::s16x16x4op
Same work, ~15× the time.
Why this is a bug rather than "no SIMD on this chip"
RP2350 is Cortex-M33 without __ARM_FEATURE_DSP, so src/platforms/simd.h falls through to platforms/shared/simd_noop.hpp:
#if x86/x64 -> simd_x86.hpp
#elif FL_IS_ESP32 -> simd_esp32.hpp
#elif __ARM_FEATURE_DSP == 1 -> simd_arm_dsp.hpp
#else -> simd_noop.hpp // "No SIMD support - use scalar fallback"
A scalar fallback should cost approximately what the equivalent scalar code costs. Failing to accelerate is expected and fine. Being 15× slower than the code it falls back to is not — it means reaching for the portable vector type silently costs an order of magnitude on every platform that lands in this branch.
That branch is not RP-specific: it is the default for every ARM part without the DSP extension, and for any platform not otherwise matched.
Hypothesis — not verified
The fallback vector is:
struct FL_ALIGNAS(16) simd_u32x4 { u32 data[4]; };16 bytes. Under AAPCS a 16-byte aggregate is not returned in registers, so each operator likely returns through memory, and a dependent chain like the benchmark's (a = op(a, b); b = a + bump;) would round-trip the stack every iteration.
I have not read the generated assembly, so I am offering this as a plausible mechanism consistent with the measurement, not as a diagnosis. Anyone picking this up should confirm against the disassembly before acting on it.
Severity
Low-to-moderate, and deliberately not inflated:
s16x16x4appears nowhere outsidesrc/fl/math/— no driver or pipeline selects it, so nothing regresses for users who never opt in.- The cost is paid only by code that deliberately reaches for the packed type expecting a win, and instead takes a 15× loss.
So this is a correctness-of-expectation problem in an opt-in API, not a live performance regression in the rendering path.
Suggested direction
Either make the no-op fallback compile to genuinely scalar-equivalent code (so the portable type is safe to use everywhere), or have the packed types degrade to the plain scalar implementations when no vector backend is present, so s16x16x4 is never worse than s16x16.
Found while closing the SIMD compute gate on #3899 — the gate itself passes (85/85 correctness tests).
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Sep 8, 2026
Narrowed considerably. Three candidate causes eliminated, including the one I originally proposed, and a much better-supported explanation found.
Eliminated
1. The ABI/over-alignment theory I filed — refuted by measurement.
I hypothesised that the 16-byte
simd_u32x4aggregate was returned through memory.simd_noop.hppdeclares all four vector typesFL_ALIGNAS(16), which serves no purpose in a scalar fallback and can stop a four-word aggregate living in registers. I removed the alignment and re-measured on RP2350W:No change. Not the cause. I have not shipped that change.
2.
FL_IRAMpreventing inlining — refuted by inspection.Every function in
simd_noop.hppis declaredFASTLED_FORCE_INLINE FL_IRAM, and a section attribute can defeat inlining. ButFL_IRAMis defined only for stm32, esp32 and esp8266;fl/stl/compiler_control.h:521makes it a no-op everywhere else, RP included.3. Different arithmetic — refuted by inspection.
s16x16::operator*has a RISC-V inline-asm fast path, but it is gated on#if defined(__riscv) && (__riscv_xlen == 32). RP2350 is Cortex-M33, so it takes the portable#else:mulhi_i32_4does exactly the same(i64)a * b >> 16per lane. Identical arithmetic, 4 lanes each way. So the gap is not in the math.Leading explanation:
-OsThe RP2350W firmware is built with
-Os(cc_flags/cxx_flagsin.build/pio/rp2350w/build_info.json).FASTLED_FORCE_INLINEstill inlines, but-Osdoes not prioritise the scalar-replacement and copy-elision that would keep the aggregates in registers. Every packed operation passes two 16-byte structs by value, builds a third, and returns it —mulhi_i32_4(raw, b.raw)thenfrom_raw(...). At-Osthat is real stack traffic on each op. The scalar path manipulates four plaini32values, which stay in registers with no optimisation effort at all.That accounts for a ~12x gap on identical arithmetic, and for why removing the alignment changed nothing: the copies are inherent to the by-value API, not to how the type is aligned.
Not yet confirmed by disassembly — that is the obvious next step and would settle it. I am flagging the eliminations so the next person does not repeat them.
Implication
If this is right, the fix is not a tweak to the fallback's internals. Either that code needs to be compiled at a higher optimisation level, or the packed operations need an API that does not pass and return aggregates by value (in-place operations on references) so
-Oshas nothing to elide.Severity is unchanged and still low:
s16x16x4appears nowhere outsidesrc/fl/math/, so no driver or pipeline selects it.zackees commentedon Sep 8, 2026
Fourth hypothesis tested and refuted — including the
-Osexplanation I proposed in my previous comment. Correcting it here.-Osis not the causeI forced
-O2on the benchmark translation unit with#pragma GCC optimize("O2")(the fallback's operations areFASTLED_FORCE_INLINE, so they optimise as part of the enclosing function) and re-measured on RP2350W:-Os-O2Everything got faster, but the packed type improved least. The ratio against scalar
s16x16went from ~11.9x to ~14.1x — it got relatively worse. Raising the optimisation level does not close the gap, so my previous comment's conclusion was wrong.Refined explanation
What survives all four eliminations is the shape of the data, not the arithmetic, the alignment, the section attributes or the optimisation level:
The lanes live in a
u32[4]array member, accessed through an indexed loop. The scalar comparison holds four independent namedi32values that a register allocator places trivially. An array inside a struct is far more likely to be materialised in memory and indexed, so each lane costs a load and a store that the scalar version never pays — and that cost is proportional to the work, which is why more optimisation helps it least.This is consistent with every measurement so far, and it is the reason the alignment change did nothing: the traffic comes from indexing an array, not from how that array is aligned.
Still not disassembly-confirmed. That remains the decisive next step, and I have not done it.
Eliminated so far, so nobody repeats them
FL_IRAMdefeating inlining — it is a no-op outside stm32/esp32/esp8266.__riscv; ARM and the fallback both compute(i64)a * b >> 16per lane.-Os— forcing-O2improves the packed path least of all four representations.Severity unchanged and still low:
s16x16x4appears nowhere outsidesrc/fl/math/, so nothing selects it automatically.zackees commentedon Sep 8, 2026
Settled by disassembly
Disassembled
benchSimd4<OpMul>from the RP2350W build (arm-none-eabi-objdump -d, release object,-Os). The mechanism is now confirmed rather than inferred.The per-lane multiply loop:
Six instructions and a taken branch per lane, of which one is the multiply. The lanes are loaded from and stored back to the stack every operation because they live in a
u32 data[4]array member, and the trip-4 loop is not unrolled.Whole-struct copies between stack slots, immediately after:
r4,r7,ipandlrare four distinct 16-byte stack buffers. That is the by-value aggregate parameter/return traffic, materialised.Frame size and runtime alignment:
72 bytes of stack for what is nominally four
u32s of state, plus runtime alignment fixup on every call.Why each earlier hypothesis failed, in light of this
2e/32) but a small constant, not the 12x — which is why removing it measured no change.-O2: cannot help much, because the loads and stores are semantically required once the lanes live in an array in memory. It improved the packed path 11% against 25-34% for the scalar paths, exactly as this predicts.smullper lane, the same operation the scalar path performs.Fix direction
The problem is the representation, not the operations. A scalar fallback whose lanes are an indexed array cannot stay in registers. Named members (
u32 x, y, z, w) with explicitly unrolled operations would let the register allocator keep all four lanes live, which is what the scalars16x16comparison already does — and it is the reason it is 12x faster while executing the samesmull.That is a contained change to
simd_noop.hppalone, though it touches every operation in that file.Correction log for this issue
I proposed four causes before disassembling; three were wrong and I have corrected each above:
FL_IRAMblocking inlining — refuted, it is a no-op outside stm32/esp32/esp8266.(i64)a * b >> 16.-Os— refuted, forcing-O2helped the packed path least.The disassembly should have come first.
zackees commentedon Sep 8, 2026
Tried the cheap fix. It does not work, and the disassembly says why.
Before proposing the invasive change, I tested whether simply unrolling the trip-4 lane loops would recover the performance — same struct layout, same API, same semantics, only the induction variable removed. That would have been a ~16-function, low-risk diff.
Unrolled all 16
simd_u32x4lane loops insimd_noop.hpp(each lane body scoped in braces, since several declare locals). Measured on RP2350W:Identical. Disassembling the result shows why:
The compiler re-rolled it.
-Osoptimises for size, and rolling four identical bodies back into a loop is smaller. Source-level unrolling cannot survive that, so this avenue is closed — noting it so nobody else spends the attempt.What that leaves
The lanes have to stop living in an indexed array. Named members (
u32 x, y, z, w) are what would let the register allocator keep them live, which is exactly what the scalars16x16comparison does while executing the identicalsmull.That is not a small change:
simd_noop.hppis 591 lines with 66 operations and 80data[...]accesses, essentially all of which would be touched.Why I have not made that change
Weighing it honestly:
s16x16x4appears nowhere outsidesrc/fl/math/, so no driver or pipeline selects it. Nothing regresses today for anyone who does not deliberately opt in.A 591-line rewrite of a shared fallback, verifiable on two of many targets, to speed up an API nothing currently calls, is not a trade I should make unilaterally on an unattended bench. Flagging it as the known fix with the cost stated, for a maintainer to weigh.
If it is wanted, the mechanical part is straightforward and the on-device
--simdbenchmark gives an immediate before/after.zackees commentedon Sep 10, 2026
The reason not to do the rewrite was better than it looked — and #4285 removes it
Your last comment declined the rewrite on this:
Correct, and stronger than stated: the fallback is not tested on the host either.
platforms/simd.hdispatches on architecture macros with no override, so an x86 host compilessimd_x86.hppand never reachessimd_noop.hpp. Proven rather than inferred — an#errorat the top of the file does not fire anywhere in the test build.So the 66 operations shipped to AVR, ESP8266, Cortex-M0/M0+/M3, WASM and RP2350 had no host coverage at all, and a 591-line rewrite of them had nothing to check it.
#4285 gives it one, and the oracle found three bugs on the way
The fallback's namespace is now a macro. That lets a test include it beside the host's real backend and check all 66 operations against each other — which immediately turned up three where x86 answers differently from every other backend:
avg_u8_16(a+b)>>1— truncates_mm_avg_epu8→ roundsnarrow_u16_to_u8narrow_u16x16_to_u8The API has two averages and they are different operations; x86 returned the same intrinsic for both. What settles it is that
simd_x86.hpp's own non-SSE2 path already implemented both the other way — the file gave two different answers depending on whether SSE2 was available.Neither is reachable today, and I am not claiming a live bug:
avg_u8_16has no caller insrc/, andnarrow_u16_to_u8's only caller isblur.cpp.hpp, where the value is at most 255 before it narrows. Contract bugs, fixed, with the differential cases pinning them.What this means for the decision here
It does not make the decision. The two things you weighed are unchanged in kind:
s16x16x4is still selected by nothing outsidesrc/fl/math/;What changed is the third factor, the one that made it a bad unilateral trade: the rewrite is no longer unverifiable. All 80
data[...]accesses now sit behind cases that compare each operation to an independent implementation, so it becomes a mechanical refactor with a check, and the on-device--simdbenchmark still gives the before/after you described.I have not done the rewrite. It remains yours to want.
test(simd): cover the scalar fallback, and fix three x86 divergences …
zackees commentedon Sep 10, 2026
Reproduced unchanged on current master — and division is worse than the packed type
Re-ran
bash autoresearch rp2350w --simdon the same board (2DCB876B587EA334) against master5ff2d8fd21, to check whether anything merged since had moved these numbers.The reported defect is unchanged
Against the figures in the issue body — mul 11568, sub 10434 — these are the same measurement to within noise (11621 and 10430). Nothing in the intervening merges touched it, and
addbehaves identically tomul/sub, which the original table did not include.Division is a separate and larger anomaly
The same run also benchmarks
div, which the issue does not cover:s16x16division costs 31.9x float and 47xs8x8u16x16division costs 22.5x floats8x8division is faster than float (0.7x)So the 8-bit fixed-point type divides faster than
float, while the 16-bit types cost thirty to fifty times as much for the same operation. That is a different shape from the packed-type problem: it is not thesimd_noopfallback path, and it affects the plain scalars16x16/u16x16types that a user would reach for first.Why I am reporting it here rather than opening a new issue
It may share a root cause with the packed case — both are "a portable fixed-point type costs an order of magnitude more than the scalar code it is meant to stand in for" — and whoever picks up the packed rewrite will be in the right code to judge that. If they turn out to be unrelated, this half is worth splitting out; I do not have enough to say which.
Reproduce either with
bash autoresearch rp2350w --simd; the timings arrive in thetestSimdBenchmarkpayload.zackees commentedon Sep 10, 2026
Root cause of the division half: it compiles to a 64-bit software divide
Traced the
divanomaly I reported above to source. It is not thesimd_noopfallback — it is the width of the intermediate.src/fl/math/fixed_point/s16x16.h:88That is an i64 / i32 division. Cortex-M33 has a 32-bit hardware divider (
SDIV/UDIV) and no 64-bit divide, so this lowers to__aeabi_ldivmod— a software routine costing hundreds of cycles.src/fl/math/fixed_point/s12x4.h:61, the 8-bit family, for contrast:i32 / i16 — one
SDIVinstruction.The same shape appears in the shared
base.h:73fallback, so any type whoseintermediate_typeis 64-bit divides in software.It predicts the measurements exactly
s8x8divSDIVfloatdivu16x16divs16x16divSigned costing more than unsigned (57649 vs 40655) is the sign-handling in
__aeabi_ldivmodversus__aeabi_uldivmod, which is what you would expect if this is the mechanism. And it explains the otherwise odd result that the 8-bit fixed-point type divides faster thanfloat: it gets the hardware divider whilefloatgoes through the FPU, and the 16-bit types get neither.Note on what a fix would involve
The general 16.16 case genuinely needs 48 bits of numerator, so a plain 32-bit divide is not a drop-in. But a 64/32→32 division specialised on the hardware divider is far cheaper than the generic
__aeabi_ldivmod, and the common case where the operands are small enough for the result to fit in 32 bits can take a singleSDIV. I have not attempted either — flagging the mechanism so whoever does is not starting from the benchmark.This may or may not share a root cause with the packed
s16x16x4problem in the issue title. This half is a 64-bit intermediate on a chip without a 64-bit divider; the packed half is a 15x overhead onsimd_noop, which is a different mechanism. They may want splitting.perf(simd): stop the 4-lane scalar backends indexing their lanes out …
fix(math): make s16x16x4.h compile as a translation unit's only inclu…
zackees commentedon Sep 10, 2026
Device verification of #4299 on the attached RP2350
#4299 landed while I was measuring, so the before/after here is on real silicon rather than instruction counts. Same board (
2DCB876B587EA334),bash autoresearch rp2350 --simd, 10000 iterations, µs.d7d279ea14)85/85device SIMD tests pass on both sides, so the win is not bought with behaviour. The packed type is now at or below scalar float on add and sub, and within 1.4x on mul — which is the outcome this issue was opened for.Two details worth recording, since I arrived at the same root cause independently before finding #4299 on master:
FL_UNROLL(4)alone is sufficient, and I confirmed that in isolation. Leavingmulhi_i32_4as the only rolled loop while add/sub were unrolled heldmulat 6322 µs while add/sub collapsed to ~530 — which pins the cost to the rolled loop rather than to the arithmetic. #4299'sFL_SIMD_LANE4is the better mechanism (it binds the index as a compile-time constant rather than relying on the pragma being honoured, and it coverssimd_riscv.hpptoo), but the two agree on the cause.The fix makes the firmware smaller. Two otherwise-identical
bash compile rp2350 --examples AutoResearchbuilds across my equivalent patch: text 978536 → 976012, total 1043220 → 1040916, i.e. 2304 bytes smaller. Removing theldmia/stmiastack round-trips for the by-value 16-byte aggregate more than pays for the unrolling, so there is no size/speed trade to weigh here.The division half of this issue is untouched by #4299 and still reproduces:
s16x16div 54456 µs against float 1519 (36x), from thei64 / i32atsrc/fl/math/fixed_point/s16x16.h:88lowering to__aeabi_ldivmod, as described in my earlier comment. That is a separate defect in the fixed-point type, not in the SIMD backends, and it survives this issue's closure — worth its own issue if it is not already tracked.perf(fixed-point): divide Q16.16 without the libgcc call on cores wit…