Repository navigation
perf(wave): precompute damping decay as a Q15 multiplier (closes #3096) - #3105
Conversation
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.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughReplaces the per-sample arithmetic right-shift damping ( ChangesQ15 Precomputed Damping Multiplier
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Possibly related PRs
Poem
✨ 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 |
Implements #3096 (§6 from meta tracker #3098). Supersedes the arithmetic-shift form shipped in #3099 §2.
Change
Replaces the per-cell
f -= (f >> mDampening)form with a precomputed Q15 multiplier:API
No public API change.
setDampening(int)and the getters still operate in int. The Q15 multiplier is an internal implementation detail.Why
Generalizes cleanly to non-power-of-two damping if the public API is ever extended (e.g.
setDampening(float)). Functionally equivalent today modulo 1-LSB rounding.Cycle cost
smmlsr/smulwb(~1 cycle).Validation
bash test fl_math_wave_wave_simulation_real --cpp— 8/8 cases pass (bounds-based suite tolerates the 1-LSB difference).bash lint --cpp— clean.Closes #3096
Refs #3098 (parent meta), #3099 §2 (superseded)
Summary by CodeRabbit