Repository navigation
perf(wave): replace damping division by arithmetic shift (closes #3092) - #3099
Conversation
…rnels `f - (f / (1 << mDampening))` is equivalent to `f - (f >> mDampening)` for the power-of-two damping factor used by WaveSimulation1D_Real and WaveSimulation2D_Real, modulo a 1-LSB rounding asymmetry on negative values (signed `/` truncates toward zero; arithmetic `>>` rounds toward -infinity). Cycle cost on the hottest line in update(): - AVR (no HW divider): ~150-200 cycles software signed div → ~32 cycles shift - Cortex-M0 / RP2040 / Teensy-LC: similar - Cortex-M4/M7 / ESP32 / ESP32-S3: ~2-12 cycles HW div → ~1 cycle shift The 1-LSB negative-rounding asymmetry is invisible at visual Q15 amplitudes — the default `setHalfDuplex(true)` mode clamps negatives to zero, and the wave settles toward steady-state from many small contributions where one LSB doesn't matter. Dead `i32 dampening_factor = 1 << mDampening;` assignments removed from both 1D and 2D update() functions. Validated by the existing tests/fl/math/wave/wave_simulation_real.cpp suite (5/5 cases pass) which is bounds-based, not exact-value. Closes #3092 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 ChangesWave Damping Optimization (1D and 2D)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
Replaces the per-cell `f -= (f >> mDampening)` arithmetic-shift form shipped in #3099 with a precomputed Q15 multiplier: i16 mDampDecayQ15; // = (1 - 1/2^damp) in Q15, computed once at // setDampening time // Inner loop: f = static_cast<i32>((static_cast<i64>(f) * mDampDecayQ15) >> 15); Functionally equivalent for power-of-two damping (modulo 1-LSB rounding) but generalizes cleanly to non-power-of-two damping if the public API is ever extended to accept float. The public int-only API stays exactly as it was — `setDampening(int)` and `getDampenening()/getDampening()` return the same int values; the Q15 multiplier is purely an internal implementation detail. Per-cycle cost: - Cortex-M4+: single smmlsr/smulwb instruction (~1 cycle) - ESP32 / ESP32-S3: 32-bit multiply (~2 cycles) - AVR (no HW 32-bit mul): ~30 cycles for the i16 * i32 multiply — same ballpark as the shift it replaces, but the inner loop is simpler. Implementation: - New `wave_detail::compute_damp_decay_q15(int damp)` helper computes the Q15 decay factor from a power-of-two exponent. damp <= 0 means no decay. - Both `WaveSimulation1D_Real` and `WaveSimulation2D_Real` store the precomputed value as `i16 mDampDecayQ15` alongside the existing `mDampening`/`mDampenening` int. - Recomputed in setDampening() and at construction. No new public API; tests/fl/math/wave/wave_simulation_real.cpp 8/8 pass (bounds-based tests tolerate the ~1 LSB rounding difference vs the shift form). Closes #3096 Parent meta: #3098 Supersedes the shift form shipped in #3099 §2.
Implements #3092 (§2 from meta tracker #3098).
Change
f = f - (f / dampening_factor)→f -= (f >> mDampening)in both 1D and 2D update kernels. Removes the deaddampening_factor = 1 << ...assignment since the shifted variant uses the exponent directly.Why
dampening_factoris always a power of two (1 << mDampening), so the signed division is equivalent to arithmetic right-shift modulo a 1-LSB rounding asymmetry on negative values.Cycle cost on the hottest line in
update():The 1-LSB negative-rounding asymmetry is invisible at visual Q15 amplitudes; the default
setHalfDuplex(true)clamps negatives to zero anyway.Validation
bash test fl_math_wave_wave_simulation_real --cpp— 5/5 cases pass (bounds-based suite, not exact-value, tolerates the LSB asymmetry).bash lint --cpp— clean.Closes #3092
Refs #3098 (parent meta)
Summary by CodeRabbit