Skip to content

fix(power): drop 64-bit math and unused RGBW conversion from the AVR limiter - #4734

Merged
zackees merged 2 commits into
masterfrom
fix/avr-nano-regression
Oct 7, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/avr-nano-regression

Conversation

@zackees

@zackees zackees commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

This fixes the AVR flash/RAM regression in the reporter's Nano sketch, which uses setMaxPowerInVoltsAndMilliamps with one NEOPIXEL strip. The power limiter gives the same results as before.

Sizes (fbuild, nanoatmega328 / uno / attiny85)

sketch 3.10.3 master this PR
reporter's Nano sketch, flash 5096 8874 6570
reporter's Nano sketch, RAM 163 317 300
uno Blink / ColorPalette / Apa102 flash 5158 / 5782 / 3682 unchanged
attiny85 Blink flash 2632 unchanged

Changes

  • scale32by8 (AVR, ATtiny and shared versions): computes (i>>8)*m + ((i&255)*m>>8) instead of a 64-bit product. Both give the same result for every input, and the sum cannot overflow because m <= 256. This removes __muldi3, __lshrdi3 and __umulsidi3 from the build.
  • Limiter ratio (power_ratio_of_255): an 8-step restoring division that gives exactly min(255, 255*num/den) using only 32-bit math. It replaces the 64-bit divide (__udivmod64). The overflow safety added in fix(power): stop scaling the part of the draw brightness cannot reach (#4156 R4) #4268 is kept: the doubling step is written as a comparison, so it cannot overflow for any den.
  • Dither reserve: uses a 32-bit accumulator when fl::size is 16 bits. A static assert proves the worst case fits.
  • RGBW estimate: reached through a pointer that is set by CLEDController::applyRgbw (the path setRgbw/setFixedRgbw use) and by Channel's applyWhiteCfg. Sketches that only drive RGB no longer link rgb_2_rgbw into the limiter (about 1 KB). The limiter also reads the stored Rgbw without copying it, so shared_ptr<DiodeProfile> acquire/reset is gone from this path.
  • PowerModelRGB: a constant-initialized function-local static instead of a Singleton (−140 B flash, −10 B RAM).

Equivalence evidence

tests/power_mgt.cpp adds three tests that compare the new code against the old 64-bit formulas:

  • scale32by8: every scale × edge values × 4000 random values each.
  • The ratio: an edge-value cross product plus 400k mixed random pairs, with den up to 0xFFFFFFFF.
  • The whole calculate_max_brightness_for_power_mW: an LED-count sweep from 0 to 65535 with 4 colour patterns, 20 budgets (0, baseline±1, demand±1, 0xFFFFFFFF, random) and 10 targets.

All three report 0 mismatches. A deliberately broken ratio (dropping the q-1 correction) makes two of them fail.

Validation (local)

  • bash test --cpp: 318/318 unit tests and 95/95 examples pass.
  • bash lint is clean.
  • Blink compiles for uno, attiny85, nano_every, esp32dev and teensy41.

What is still above 3.10.3

These were left in deliberately:

Known limitation: a third-party CLEDController subclass that writes mSettings.mWhiteCfg directly, without going through setRgbw, must call fl::detail::enable_rgbw_power_estimate(). This is documented on rgbwConfig().

Summary by CodeRabbit

  • Bug Fixes
    • RGBW power estimation is now enabled when RGBW channel configuration is applied, including on fixed-white chipsets that reject RGBW setup. This helps power-based brightness limiting account for RGBW LEDs more consistently.
    • Power calculations for brightness scaling now preserve exact results across supported scaling modes, improving consistency in power-limited output.

zackees and others added 2 commits October 6, 2026 21:43
…limiter

The nanoatmega328 sketch from the regression report drops from
8874 to 6622 B flash and from 317 to 300 B RAM. Limiter output
does not change.

- scale32by8 (AVR, ATtiny, shared): exact 32-bit split form
  instead of a 64-bit product.
- Limiter ratio: power_ratio_of_255, an 8-step restoring
  division that gives exactly min(255, 255*num/den) in 32 bits.
- Dither reserve: 32-bit accumulator where fl::size is 16 bits,
  with a static assert on the bound.
- RGBW estimate: reached through a pointer that applyRgbw and
  Channel set, so RGB-only sketches do not link rgb_2_rgbw.
  Reads the stored Rgbw without copying its shared_ptr.
- PowerModelRGB: constant-initialized static instead of a
  Singleton.

Host tests compare the 32-bit forms and the whole limiter against
the 64-bit reference over sweeps and edge values.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: FastLED/FastLED/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a864c888-3887-4fd5-8bba-f5f9f9de4f72
📥 Commits

Reviewing files that changed from the base of the PR and between d84bf17 and 291c353.

📒 Files selected for processing (8)
  • src/fl/channels/channel.cpp.hpp
  • src/fl/channels/cled_controller.h
  • src/platforms/avr/atmega/common/scale8_avr.h
  • src/platforms/avr/attiny/math/scale8_attiny.h
  • src/platforms/shared/scale8.h
  • src/power_mgt.cpp.hpp
  • src/power_mgt.h
  • tests/power_mgt.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The change updates RGBW power-estimator activation and dispatch, replaces selected 64-bit scaling and brightness-ratio arithmetic with 32-bit calculations, and selects dither-reserve accumulator types by memory tier. Equivalence tests compare the new calculations and limiter results with 64-bit references.

Changes

Power Management

Layer / File(s) Summary
RGBW estimator activation and dispatch
src/fl/channels/cled_controller.h, src/fl/channels/channel.cpp.hpp, src/power_mgt.cpp.hpp
RGBW configuration access and estimator activation are connected to controller setup and power estimation.
Exact 32-bit scaling
src/platforms/shared/scale8.h, src/platforms/avr/atmega/common/scale8_avr.h, src/platforms/avr/attiny/math/scale8_attiny.h, tests/power_mgt.cpp
scale32by8 uses split 32-bit arithmetic in shared and AVR implementations. Tests compare results with a 64-bit reference.
Brightness budget ratio
src/power_mgt.h, src/power_mgt.cpp.hpp, tests/power_mgt.cpp
The brightness ratio helper uses 32-bit division steps and is used by the limiter. Tests compare its results with a 64-bit reference.
Limiter state and dither reserve
src/power_mgt.cpp.hpp, tests/power_mgt.cpp
The power model uses a function-local static. Dither-reserve accumulation selects its integer type by memory tier. Limiter tests compare results with a 64-bit reference.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 291c3

No actionable merge-blocking issue remains in the supplied evidence. The change is ready for normal merge checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.52% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 31 functions across 8 files. 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 summarizes the main changes: replacing 64-bit math and avoiding unused RGBW conversion in the AVR power limiter.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@zackees
zackees merged commit 0c2e9f7 into master Oct 7, 2026
120 checks passed
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.

1 participant