Skip to content

perf(wave): replace damping division by arithmetic shift (closes #3092) - #3099

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

zackees merged 1 commit into
masterfrom
fix/3092-wave-damp-shift

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

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 dead dampening_factor = 1 << ... assignment since the shifted variant uses the exponent directly.

Why

dampening_factor is 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():

  • AVR (no HW divider): ~150-200 cycles 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) 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

  • Refactor
    • Optimized wave simulation dampening calculation by replacing division with more efficient arithmetic shift operations, improving computational performance without changing external behavior.

…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
@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: bc53b319-e738-4064-bbc5-89e503b7c5e1

📥 Commits

Reviewing files that changed from the base of the PR and between 8d7af6d and 094c1e4.

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

📝 Walkthrough

Walkthrough

In wave_simulation_real.cpp.hpp, the damping step in both WaveSimulation1D_Real::update() and WaveSimulation2D_Real::update() is changed from computing an explicit dampening_factor = 1 << mDampening and dividing (f / dampening_factor) to a direct arithmetic right-shift subtraction (f -= (f >> mDampening)). The intermediate variables are removed and inline comments are updated.

Changes

Wave Damping Optimization (1D and 2D)

Layer / File(s) Summary
1D update: remove dampening_factor, apply shift-based damping
src/fl/math/wave/wave_simulation_real.cpp.hpp
Removes the dampening_factor integer variable and replaces f / dampening_factor with f -= (f >> mDampenening) in the 1D inner loop; updates surrounding comments to describe shift behavior and rounding.
2D update: remove dampening_factor, apply shift-based damping
src/fl/math/wave/wave_simulation_real.cpp.hpp
Same substitution applied to the 2D inner loop: drops dampening_factor, uses f -= (f >> mDampening), and adds comments referencing the 1D change for rationale.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related issues

  • #3092 — This PR directly implements the optimization described in that issue: replacing f / (1 << mDampening) with f -= (f >> mDampening) in the 1D and 2D wave update inner loops.
  • #3073 — This PR implements the §2 optimization from that meta-issue, converting the wave damping division to an arithmetic right shift in both update paths.
  • #3098 — Tracks the same arithmetic right-shift damping optimization in the same functions and file.
  • #3093 — Complementary inner-loop optimization targeting the same update() methods; the shift-based damping change in this PR pairs with the half-duplex zero-clamp folding described there.

Poem

🐇 A shift to the right, no division in sight,
The dampening factor has fled overnight!
Where once we divided by two to the power,
A single >> shift now rules every hour.
Cycles saved, the wave rolls on bright! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: replacing damping division with arithmetic shift in the wave simulation, and references the closed issue.
Linked Issues check ✅ Passed The PR directly implements the coding requirements from #3092: replacing division with arithmetic right-shift in both 1D and 2D update kernels, removing intermediate dampening_factor variables, and existing tests pass.
Out of Scope Changes check ✅ Passed All changes are scoped to the wave damping optimization specified in #3092: modifications to update() functions and removal of redundant dampening_factor assignments, with no unrelated alterations.

✏️ 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/3092-wave-damp-shift

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.

@zackees
zackees merged commit eba4478 into master Jun 15, 2026
9 checks passed
@zackees
zackees deleted the fix/3092-wave-damp-shift branch June 15, 2026 20:04
@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
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.
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): replace damping division by power-of-two with arithmetic shift (refs #3073 §2)

1 participant