Skip to content

perf(wave): precompute damping decay as a Q15 multiplier (closes #3096) - #3105

Merged
zackees merged 1 commit into
masterfrom
fix/3096-wave-precomputed-damp
Jun 15, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/3096-wave-precomputed-damp

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

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:

// at setDampening:
mDampDecayQ15 = (1 - 1/2^damp) in Q15
// inner loop:
f = static_cast<i32>((static_cast<i64>(f) * mDampDecayQ15) >> 15);

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

  • Cortex-M4+: single smmlsr/smulwb (~1 cycle).
  • ESP32/S3: 32-bit multiply (~2 cycles).
  • AVR: ~30 cycles for the i16 × i32 multiply — same ballpark as the shift it replaces.

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

  • Refactor
    • Optimized damping calculations in 1D and 2D wave simulations for improved performance through precomputed decay values.

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.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

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: 3756e53e-be10-490c-b1af-8c689ece5f28

📥 Commits

Reviewing files that changed from the base of the PR and between 527216b and 0d38b65.

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

📝 Walkthrough

Walkthrough

Replaces the per-sample arithmetic right-shift damping (f -= f >> mDampenening) in the 1D and 2D wave simulation update loops with a precomputed Q15 fixed-point multiply. A new compute_damp_decay_q15 helper and mDampDecayQ15 private member are added to both simulation classes; constructors and setDampening initialize and recompute this cached value.

Changes

Q15 Precomputed Damping Multiplier

Layer / File(s) Summary
Q15 helper declaration, definition, and new private members
src/fl/math/wave/wave_simulation_real.h, src/fl/math/wave/wave_simulation_real.cpp.hpp
Declares and defines wave_detail::compute_damp_decay_q15(int damp) FL_NOEXCEPT, which clamps damp ≤ 0 to no-decay and converts 1.0f - 1.0f / (1 << damp) to Q15 via existing float↔fixed helpers. Adds the mDampDecayQ15 private i16 member to both WaveSimulation1D_Real and WaveSimulation2D_Real.
Constructor init, setDampening recompute, and update loop replacement
src/fl/math/wave/wave_simulation_real.h, src/fl/math/wave/wave_simulation_real.cpp.hpp
Both constructors now initialize mDampDecayQ15 from the provided exponent. setDampening(int) is marked FL_NOEXCEPT and extended to recompute the cached multiplier. In both 1D and 2D update inner loops, f -= (f >> mDampenening) is replaced by f = static_cast<i32>((static_cast<i64>(f) * mDampDecayQ15) >> 15), with post-damping clamping unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3099: Both PRs modify the 1D/2D wave update() damping logic in wave_simulation_real.cpp.hpp; this PR replaces the shift/subtraction damping approach introduced in #3099 with a cached Q15 multiply-based decay multiplier.
  • FastLED/FastLED#3103: Both PRs modify WaveSimulation2D_Real::update() in wave_simulation_real.cpp.hpp; #3103 refactors inner-loop indexing while this PR changes the damping math to use the cached Q15 multiplier.

Poem

🐇 Hop hop, no more shifting right each beat,
A Q15 multiplier makes damping neat!
mDampDecayQ15 cached with care,
One smul and shift, smooth as a hare.
The waves now fade with fixed-point grace,
While I nibble carrots at a leisurely pace. 🥕

✨ 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/3096-wave-precomputed-damp

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.

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): precompute damping as a Q15 multiplier (refs #3073 §6, alt to §2)

1 participant