Skip to content

perf(wave): fold half-duplex zero-clamp into main update loop (closes #3093) - #3101

Merged
zackees merged 1 commit into
masterfrom
fix/3093-wave-halfduplex-fold
Jun 15, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/3093-wave-halfduplex-fold

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Implements #3093 (§3 from meta tracker #3098).

Change

WaveSimulation{1D,2D}_Real::update() previously ran a separate if (mHalfDuplex) second pass over the entire grid after the main update loop, walking every cell to clamp negative values to zero.

This PR hoists the half-duplex lower bound into a single const i32 q15_min = mHalfDuplex ? 0 : -32768; before the update loop and folds it into a single fl::clamp(f, q15_min, 32767) per cell. The dedicated post-update pass is removed.

Side effect

The existing branchy if (f > 32767) ... else if (f < -32768) ... Q15 saturation clamp is replaced with fl::clamp, which most compilers lower branchlessly on Cortex-M4+. This overlaps with §4 (#3094)'s broader "branchless clamp" goal, but #3094 also hoists j * stride and adds __restrict__ — this PR is just the half-duplex fold-in.

Why

On PSRAM-backed 2D grids (vector_psram<i16>) the second pass doubled memory traffic for no benefit. The single-pass version is equivalent and cheaper.

Validation

  • bash test fl_math_wave_wave_simulation_real --cpp — 5/5 cases pass, including the saturation tests that exercise the half-duplex floor.
  • bash lint --cpp — clean.

Closes #3093
Refs #3098 (parent meta), #3099 (sibling §2 merged)

Summary by CodeRabbit

Release Notes

  • Performance Improvements
    • Optimized wave simulation update process to reduce computational overhead by consolidating clamp operations into a single pass per update step, resulting in faster simulation performance.

Before: WaveSimulation{1D,2D}_Real::update() ran a separate `if
(mHalfDuplex)` second pass over the entire grid after the update loop,
walking every cell to clamp negative values to zero. On
PSRAM-backed 2D grids (vector_psram<i16>) this doubled the memory
traffic for no reason.

After: the half-duplex lower bound is hoisted into a single
`const i32 q15_min = mHalfDuplex ? 0 : -32768;` declaration before the
update loop, and folded into a single fl::clamp(f, q15_min, 32767) per
cell. The dedicated post-update pass is removed.

Side effect: the existing branchy if/else Q15 saturation clamp is
replaced with fl::clamp, which most compilers lower branchlessly on
Cortex-M4+ (this is §4's broader goal — but §4 is a separate sub-issue
that also hoists row indexing and adds __restrict__; this PR is just
the half-duplex fold-in).

Validated by tests/fl/math/wave/wave_simulation_real.cpp — 5/5 pass,
including the saturation tests which exercise the half-duplex floor.

Closes #3093
Parent meta: #3098
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b1fa2e48-8dfa-4f37-87dc-e618ac574c24

📥 Commits

Reviewing files that changed from the base of the PR and between 6594bb7 and 9da518e.

📒 Files selected for processing (1)
  • src/fl/math/wave/wave_simulation_real.cpp.hpp

📝 Walkthrough

Walkthrough

In wave_simulation_real.cpp.hpp, both WaveSimulation1D_Real::update() and WaveSimulation2D_Real::update() now hoist a q15_min constant computed from mHalfDuplex and apply it as the lower bound of a single fl::clamp per cell, eliminating the separate post-update pass that zeroed negative values when half-duplex mode was active.

Changes

Half-duplex clamp folded into wave update loops

Layer / File(s) Summary
1D update: hoist q15_min and single-clamp per cell
src/fl/math/wave/wave_simulation_real.cpp.hpp
Introduces q15_min = mHalfDuplex ? 0 : -32768 before the inner loop in WaveSimulation1D_Real::update(), then replaces the prior full Q15 clamp plus a separate half-duplex negative-zeroing pass with one fl::clamp(value, q15_min, 32767) per cell.
2D update: hoist q15_min and single-clamp per cell
src/fl/math/wave/wave_simulation_real.cpp.hpp
Applies the same pattern in WaveSimulation2D_Real::update(): q15_min is hoisted outside the nested loop, and the two-step saturation (Q15 clamp then negative-zeroing pass) is collapsed into a single fl::clamp(value, q15_min, 32767) per cell.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐇 Two passes once crawled the grid so wide,
Zeroing negatives on the second ride.
Now q15_min is hoisted up high,
One clamp per cell — the extra loop must die!
Fewer traversals, the waveform sings,
Half-duplex folded on branchless wings. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main performance optimization: folding half-duplex zero-clamp into the main update loop, and references the closed issue #3093.
Linked Issues check ✅ Passed The PR implements all acceptance criteria from #3093: folds half-duplex clamping into the update loop via fl::clamp, test suite passes, and grid is touched once instead of twice.
Out of Scope Changes check ✅ Passed All changes are strictly scoped to the stated optimization: combining half-duplex zero-clamp with Q15 saturation clamp in the update loop, with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 fix/3093-wave-halfduplex-fold

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.

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 15, 2026
@zackees
zackees merged commit 9d5442e into master Jun 15, 2026
8 of 9 checks passed
@zackees
zackees deleted the fix/3093-wave-halfduplex-fold branch June 15, 2026 20:52
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 15, 2026
zackees added a commit that referenced this pull request Jun 15, 2026
…er loop (#3103)

The 2D update inner loop previously recomputed `int index = j * stride + i;`
on every iteration and accessed curr[index+1], curr[index-1],
curr[index+stride], curr[index-stride] without an alias guarantee. The
optimizer *should* hoist `j * stride` and prove non-aliasing across the
curr/next pointers, but with `fl::size` (size_t) iteration and
non-restricted pointers it often doesn't.

Spell it out: lift `row = j * stride` out of the inner loop, set up
`row_curr / row_above / row_below / row_next` pointers, and qualify them
with `FL_RESTRICT_PARAM` (the codebase's portable restrict-keyword macro
from fl/stl/compiler_control.h — __restrict__ on gcc/clang, __restrict on
MSVC, plain `restrict` on C99). The inner loop now indexes by `i` only.

Small side benefit: `c = row_curr[i]` is fetched once and reused in both
the Laplacian (`-(c << 2)`) and the new-value computation (`(c << 1)`),
saving a load on platforms without aggressive CSE.

Branchless-clamp portion of the original §4 scope was already done as a
side effect of #3101 (§3 half-duplex fold-in: `fl::clamp` replaced the
branchy if/else). This PR is the row-indexing portion only.

1D update is unchanged — no `j * stride` to hoist.

Validated by tests/fl/math/wave/wave_simulation_real.cpp (5/5 pass).

Closes #3094
Parent meta: #3098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

perf(wave): fold half-duplex zero-clamp into main update loop (refs #3073 §3)

1 participant