Repository navigation
perf(wave): fold half-duplex zero-clamp into main update loop (closes #3093) - #3101
Conversation
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
|
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)
📝 WalkthroughWalkthroughIn ChangesHalf-duplex clamp folded into wave update loops
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
…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
Implements #3093 (§3 from meta tracker #3098).
Change
WaveSimulation{1D,2D}_Real::update()previously ran a separateif (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 singlefl::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 withfl::clamp, which most compilers lower branchlessly on Cortex-M4+. This overlaps with §4 (#3094)'s broader "branchless clamp" goal, but #3094 also hoistsj * strideand 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