Repository navigation
fix(power): drop 64-bit math and unused RGBW conversion from the AVR limiter - #4734
Conversation
…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]>
…oduct Co-Authored-By: Claude <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (8)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe 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. ChangesPower Management
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to 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)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 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 |
This fixes the AVR flash/RAM regression in the reporter's Nano sketch, which uses
setMaxPowerInVoltsAndMilliampswith one NEOPIXEL strip. The power limiter gives the same results as before.Sizes (fbuild, nanoatmega328 / uno / attiny85)
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 becausem <= 256. This removes__muldi3,__lshrdi3and__umulsidi3from the build.power_ratio_of_255): an 8-step restoring division that gives exactlymin(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 anyden.fl::sizeis 16 bits. A static assert proves the worst case fits.CLEDController::applyRgbw(the pathsetRgbw/setFixedRgbwuse) and by Channel'sapplyWhiteCfg. Sketches that only drive RGB no longer linkrgb_2_rgbwinto the limiter (about 1 KB). The limiter also reads the storedRgbwwithout copying it, soshared_ptr<DiodeProfile>acquire/reset is gone from this path.PowerModelRGB: a constant-initialized function-local static instead of aSingleton(−140 B flash, −10 B RAM).Equivalence evidence
tests/power_mgt.cppadds three tests that compare the new code against the old 64-bit formulas:scale32by8: every scale × edge values × 4000 random values each.denup to0xFFFFFFFF.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-1correction) makes two of them fail.Validation (local)
bash test --cpp: 318/318 unit tests and 95/95 examples pass.bash lintis clean.What is still above 3.10.3
These were left in deliberately:
ChannelOptionsin the controller object (+65 B RAM). It holds the white-channel variant, gamma and the colour-profile binding, which are features on this tier.fl::Pinindicator path (about 160 B). It includesSelectablevirtuals. Removing it would changePinsemantics.Known limitation: a third-party
CLEDControllersubclass that writesmSettings.mWhiteCfgdirectly, without going throughsetRgbw, must callfl::detail::enable_rgbw_power_estimate(). This is documented onrgbwConfig().Summary by CodeRabbit