Skip to content

perf(wave): replace damping division by power-of-two with arithmetic shift (refs #3073 §2) #3092

Closed

Description

@zackees

Scope from #3073 §2.

Current code

src/fl/math/wave/wave_simulation_real.cpp.hpp:

  • 1D update line ~135: f = f - (f / dampening_factor);
  • 2D update line ~285: same pattern.

dampening_factor = 1 << mDampening is always a power of two, so the division is equivalent to f >> mDampening modulo a 1-LSB asymmetry on negative values (signed / truncates toward zero; arithmetic >> rounds toward -infinity).

Cost (per cell, per step)

Target / cost >> cost
AVR (no HW divider) ~150–200 cycles software div ~32 cycles
Cortex-M0 / RP2040 / Teensy-LC similar — software div ~32 cycles
Cortex-M4/M7 / ESP32 / ESP32-S3 ~2–12 cycles HW div ~1 cycle

This is the hottest line in the inner update loop on the Low-memory targets.

Change

f -= (f >> mDampening);

The 1-LSB negative-rounding asymmetry is invisible visually — the default setHalfDuplex(true) mode clamps negatives to zero anyway, and the wave settles toward steady-state from many small contributions where one LSB doesn't matter.

If exact backwards rounding is required (some test guards against bit-exact values), use the standard shift-with-round-to-zero idiom:

f -= (f < 0 ? -((-f) >> mDampening) : (f >> mDampening));

Acceptance

  • bash test fl_math_wave_wave_simulation_real --cpp continues to pass (the existing suite is bounds-based, not exact-value).
  • Optional: microbench on AVR/uno to confirm cycle reduction.

Relation

Alternative path: see §6 (#TBD — precomputed Q15 damping multiplier), which generalizes this to non-power-of-two damping factors.

Refs: #3073 §2.

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Parent meta: #3098. Alternative path: #3096 (§6 precomputed Q15 damping multiplier).

  2. added a commit that references this issue on Jun 15, 2026
    eba4478
  3. moved this from Triage to Done in FastLED Trackeron Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions