Skip to content

perf(wave): spread-LUT bit transpose — ~2x transpose / ~1.24x encode on ESP32-P4 - #2534

Merged
zackees merged 1 commit into
masterfrom
perf/spread-lut-transpose
May 29, 2026
Merged

zackees merged 1 commit into
masterfrom
perf/spread-lut-transpose

Conversation

@zackees

@zackees zackees commented May 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Replace the unrolled-naive wave8/wave3 8- and 16-lane bit transposes with a u32 spread-LUT: acc = OR over 8 lanes of spread(laneByte) << lane, where spread() pre-positions a byte's pulse bits into 8 separate bytes via a 16-entry literal table. All native u32, independent ops, no dependency chain / no emulated u64 — the op shape the in-order RV32 schedules best. New shared helper: src/fl/channels/detail/bit_spread_lut.hpp.

Measured on a real ESP32-P4 (RV32), bit-exact vs naive

16-lane 8-lane
isolated transpose 6649 → 3353 µs (1.98×) 3356 → 1764 µs (1.90×)
full encode_us (256 LEDs) 14611 → 11744 µs (1.24×) 7208 → 5879 µs (1.23×)

Correctness: bit-identical to the naive across the host equivalence + known-answer + round-trip tests, and on-device mismatch=0. The naive was the proven-correct production transpose, so the WS2812 output is byte-for-byte unchanged.

Why this beats everything else

Every prior candidate lost to the naive on the in-order P4 — u64 SWAR (0.41×), Hacker's-Delight u32 8×8 (0.71×), two-pass (0.63×), u64-pack template. The spread-LUT is the first to beat the naive because it's independent table-lookup + shift + OR-reduce (no dependency chain). Full investigation + on-device benchmark harness (bash autoresearch --transpose 16) in #2533.

Test plan

  • bash test wave8 / bash test wave3 / bash test parlio_driver (bit-exact vs naive) — pass
  • bash lint --cpp — clean
  • On-device measureParlio encode_us before/after (P4)

Refs #2533

Summary by CodeRabbit

  • Refactor
    • Updated bit-matrix transpose implementations to use lookup table-based helpers for improved code maintainability across channel processing routines.

Review Change Stack

…P32-P4 (#2533)

Replace the unrolled-naive wave8/wave3 8- and 16-lane bit transposes with a u32
"spread LUT": acc = OR over 8 lanes of spread(laneByte) << lane, where spread()
pre-positions a byte's pulse bits into 8 separate bytes via a 16-entry literal
table (C++11-valid, ~64 B, cache-resident). All native u32, independent ops, no
dependency chain / no emulated u64 — the shape the in-order RV32 schedules best.

Measured on ESP32-P4 (RV32), bit-exact vs naive (host equivalence tests +
on-device mismatch=0):
  isolated transpose: 16-lane 6649->3353 us (1.98x), 8-lane 3356->1764 us (1.90x)
  full encode_us:     16-lane 14611->11744 us (1.24x), 8-lane 7208->5879 us (1.23x)

Beats every prior candidate (u64 SWAR, Hacker's-Delight u32, two-pass, u64-pack)
which all lost to the naive; the spread-LUT is the first to beat it. Shared
helper: src/fl/channels/detail/bit_spread_lut.hpp.

Refs #2533
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f6bbe3ff-baf7-4232-a514-1497252f3d85

📥 Commits

Reviewing files that changed from the base of the PR and between 5cc4967 and f8eafce.

📒 Files selected for processing (3)
  • src/fl/channels/detail/bit_spread_lut.hpp
  • src/fl/channels/detail/wave3.hpp
  • src/fl/channels/detail/wave8.hpp

📝 Walkthrough

Walkthrough

This PR introduces a shared LUT-based bit-transpose primitive module and refactors Wave3 and Wave8 channel implementations to use it, replacing previous fully-unrolled bitwise macro implementations with calls to compact spread-and-extract helpers backed by a 16-entry nibble lookup table.

Changes

Bit-matrix transpose refactoring

Layer / File(s) Summary
LUT-based spread and transpose primitives
src/fl/channels/detail/bit_spread_lut.hpp
New bit_spread_lut.hpp header provides a 16-entry kSpreadNibble lookup table and inlined helpers (spreadA, spreadB) that map input byte nibbles to positioned 32-bit pulse values; two force-inlined transpose functions (spread_transpose16_symbol, spread_transpose8_symbol) compute 16 or 8 output bytes from input lane bytes using LUT lookups and 32-bit intermediate shifts.
Wave3 transpose refactoring
src/fl/channels/detail/wave3.hpp
wave3_transpose_8 and wave3_transpose_16 now include bit_spread_lut.hpp and gather per-symbol lane bytes into temporary arrays, delegating bit-spreading and transposition to the new LUT-based helpers instead of fully-unrolled bitwise macros.
Wave8 transpose refactoring
src/fl/channels/detail/wave8.hpp
wave8_transpose_8 and wave8_transpose_16 now include bit_spread_lut.hpp and assemble per-symbol lane bytes into local arrays, invoking LUT-based transpose helpers instead of fully-unrolled bit-by-bit packing logic; removes previous macro-based bit extraction.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

The refactoring touches critical bit-manipulation transpose logic across three files, requires understanding the nibble-to-pulse LUT mapping and the 32-bit shift-based byte extraction, and involves verification that the new compact implementations produce identical output to the previous unrolled versions across multiple lane widths and channels.

Possibly related issues

  • FastLED/FastLED#2526: Introduces nibble-LUT expansion and transpose helpers directly used by Wave8 optimization.
  • FastLED/FastLED#2522: Replaces wave3 transpose logic with LUT-based implementation, directly addressing performance investigation of wave3 encode.
  • FastLED/FastLED#2524: Implements u32-friendly LUT-based transpose as an alternative to SWAR, directly resolving Wave8 transpose performance strategy discussion.
  • FastLED/FastLED#2530: Provides the u32-friendly LUT-based bit transpose optimization for Wave3/Wave8 discussed in the issue.
  • FastLED/FastLED#2533: Targets the same transpose routines benchmarked in PARLIO transpose performance investigation.
  • FastLED/FastLED#2523: Directly relates to generalizing wave3/wave8 transpose and LUT patterns into shared WaveN infrastructure.

Possibly related PRs

  • FastLED/FastLED#2532: Main PR replaces the fully-unrolled naive bit-by-bit transpose that #2532 introduced with optimized LUT-based spread_transpose primitives in the same wave3/wave8 transpose functions.

Poem

🐰 A nibble-to-pulse ballet now plays,
Sixteen table entries light the way—
Spread and shift those bytes with glee,
LUT-fast transposition, crisp and clean!
Wave3 and Wave8 pirouette with pride.

✨ 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 perf/spread-lut-transpose

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.

@zackees
zackees merged commit 5ba7e6b into master May 29, 2026
5 of 7 checks passed
@zackees
zackees deleted the perf/spread-lut-transpose branch August 21, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant