Skip to content

fix(arm): correct apply_scale shift in m0clockless_c.h (#3504) - #3737

Merged
zackees merged 2 commits into
masterfrom
fix/3504-m0clockless-apply-scale
Jul 27, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/3504-m0clockless-apply-scale

Conversation

@zackees

@zackees zackees commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #3504 — apply_scale() in src/platforms/arm/common/m0clockless_c.h always returned 0, blanking every pixel on the C++ M0 clockless path.

Root cause. The function computed (pixel * scale_factor) >> 16. But M0ClocklessData.s[] holds plain 8-bit color-correction values (pixels.mColorAdjustment.premixed[i], 0..255 — or 1..256 after the FASTLED_SCALE8_FIXED pre-increment). The product tops out at 255 * 256 == 65280 < 65536, so the shift discarded everything.

Why the translation went wrong. The C++ file was derived from m0clockless_asm.h, where the scaling is split across two macros. scale4 leaves the raw product in bn; swapbbn1 then does lsl b, bn, #16, which places product bit 15 into b bit 31 — exactly the bit qlo4 shifts into the carry first. So the byte the assembly clocks out is product bits 15:8, i.e. plain scale8 semantics. The C++ port folded swapbbn1 into a no-op (prepare_byte_for_output) but kept a >> 16 that only made sense for a 16.16 fixed-point scale that this driver never used.

Fix: shift by 8, and document the scale4/swapbbn1 pairing so the two implementations no longer read as if they disagree.

Testing

  • bash lint — green
  • bash compile lpc845 --examples Blink — succeeds (LPC845 is the platform that selects m0clockless_c.h via led_sysdefs_arm_lpc.h); flash 17.68KB, RAM 5.10KB
  • bash test --cpp — green

No host unit test: the header is ARM-only (inline MRS/cpsid asm, CMSIS SysTick) and cannot be included from the host test build. The fix is verified against the assembly it mirrors, as traced above.

Note

The issue also asks whether this generated-from-asm C++ variant should exist at all, given the hand-written drivers under arm/k66 etc. That is a larger design question and is deliberately out of scope here — this PR only fixes the reported blanking bug.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Corrected brightness and color-correction scaling on ARM M0 platforms for more accurate LED output.
  • Chores

    • Standardized builds to use the stable Rust toolchain, helping prevent toolchain and target mismatches.

zackees and others added 2 commits July 27, 2026 10:03
Without a repo-root pin, soldr resolves rustc from PATH. On hosts where a
non-rustup Rust shadows rustup (e.g. chocolatey's x86_64-pc-windows-gnu
build), soldr still canonicalizes the build target to
x86_64-pc-windows-msvc and every crate fails with E0463 (can't find crate
for `std`). Pinning the toolchain routes resolution through rustup, which
always carries the host target's std.

Co-Authored-By: Claude <[email protected]>
apply_scale() returned `(pixel * scale) >> 16`, but the scale factors in
M0ClocklessData.s[] are plain 8-bit values (0..255, or 1..256 under
FASTLED_SCALE8_FIXED). The product therefore never reached 65536, so the
function returned 0 for every pixel and the C++ M0 driver output all black.

The assembly it was translated from is scale4 + swapbbn1 taken together:
scale4 leaves the raw product in bn, then swapbbn1's `lsl b, bn, #16` puts
product bit 15 in b bit 31 -- the first bit qlo4 shifts into the carry. The
byte the assembly clocks out is product bits 15:8, i.e. scale8 semantics.

Shift by 8 instead, and document the scale4/swapbbn1 pairing so the two
implementations no longer look like they disagree.

Fixes #3504

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

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR pins Rust toolchain resolution to stable and changes ARM M0 clockless scaling to interpret stored factors as 8-bit values, extracting scaled output with result >> 8.

Changes

Rust toolchain pin

Layer / File(s) Summary
Stable toolchain configuration
rust-toolchain.toml
Adds a [toolchain] configuration selecting the stable Rust channel and documenting toolchain resolution behavior.

ARM scaling correction

Layer / File(s) Summary
M0 scale8 arithmetic
src/platforms/arm/common/m0clockless_c.h
Updates apply_scale to use 8-bit scale-factor semantics and extract the scaled result with result >> 8.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The rust-toolchain.toml pin is unrelated to fixing apply_scale and adds scope beyond #3504. Split the toolchain pin into a separate PR or document it as required for this issue's fix.
✅ 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 states the ARM apply_scale shift fix in m0clockless_c.h.
Linked Issues check ✅ Passed The shift correction in apply_scale addresses the broken zero-output bug reported in #3504.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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/3504-m0clockless-apply-scale

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
rust-toolchain.toml (1)

12-12: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Pin an exact Rust release if reproducibility is required.

channel = "stable" selects the moving stable channel rather than a specific compiler version, so lint results can change as Rust releases advance. Rustup supports exact version channels; consider pinning the version used by CI and updating it deliberately. (rust-lang.github.io)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust-toolchain.toml` at line 12, Update the rust-toolchain.toml channel
setting from the moving “stable” channel to the exact Rust release used by CI,
and maintain that pinned version through deliberate updates.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/platforms/arm/common/m0clockless_c.h`:
- Around line 337-340: Move the apply_scale function definition out of
m0clockless_c.h into its corresponding .cpp.hpp or .impl.hpp implementation
file, preserving its FL_FORCE_INLINE qualifier and current scale8 behavior. Keep
only the necessary declaration or usage-facing interface in the header.

---

Nitpick comments:
In `@rust-toolchain.toml`:
- Line 12: Update the rust-toolchain.toml channel setting from the moving
“stable” channel to the exact Rust release used by CI, and maintain that pinned
version through deliberate updates.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 51f4d3d0-5116-4167-9fdc-0dcc12716ff4

📥 Commits

Reviewing files that changed from the base of the PR and between 5ec41c1 and 7103f6c.

📒 Files selected for processing (2)
  • rust-toolchain.toml
  • src/platforms/arm/common/m0clockless_c.h

Comment on lines 337 to +340
FL_FORCE_INLINE fl::u8 apply_scale(fl::u8 pixel, fl::u32 scale_factor) FL_NO_EXCEPT {
fl::u32 result = (fl::u32)pixel * scale_factor;
// Extract high byte (bits 23:16) as the scaled result
return (fl::u8)(result >> 16);
// Extract bits 15:8 as the scaled result (scale8 semantics)
return (fl::u8)(result >> 8);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift

Move this function definition out of the .h file.

apply_scale is defined directly in src/platforms/arm/common/m0clockless_c.h. Move the implementation to the corresponding .cpp.hpp or .impl.hpp file while preserving FL_FORCE_INLINE; the updated shift itself matches the intended scale8 behavior.

As per path instructions, headers under src/**/*.h must not contain function definitions; implementations belong in .cpp.hpp or .impl.hpp.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/platforms/arm/common/m0clockless_c.h` around lines 337 - 340, Move the
apply_scale function definition out of m0clockless_c.h into its corresponding
.cpp.hpp or .impl.hpp implementation file, preserving its FL_FORCE_INLINE
qualifier and current scale8 behavior. Keep only the necessary declaration or
usage-facing interface in the header.

Source: Path instructions

@zackees
zackees merged commit bf06e79 into master Jul 27, 2026
11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jul 27, 2026
@zackees
zackees deleted the fix/3504-m0clockless-apply-scale branch August 21, 2026 19:15
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.

m0clockless_c.h apply_scale() is broken, always resulting in 0 value output

1 participant