Skip to content

fix(fl): make_unsigned keeps 32-bit width on AVR - #5292

Merged
zackees merged 2 commits into
masterfrom
fix/avr-make-unsigned-width
Nov 19, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/avr-make-unsigned-width

Conversation

@zackees

@zackees zackees commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #5291. Fixes the unresolved CodeRabbit finding there, which was posted after the merge.

Problem

fl::make_unsigned selects by size, and unsigned_by_size<4> was unsigned int. That type is 16 bits on AVR, so make_unsigned<u32/i32/long>::type was 16 bits there. #5291 made fl::popcount / fl::countr_zero convert through make_unsigned, so on AVR popcount(u32(0x011fffff)) still returned 16 and countr_zero(u32(0x00010000)) reached __builtin_ctz(0). fl::format's format_integer truncated 32-bit values the same way.

Fix

  • unsigned_by_size<4> uses unsigned long when unsigned int is not 4 bytes. Every other target keeps unsigned int, so no type changes there.
  • Namespace-scope static_asserts check the width of make_unsigned<short/int/long/long long> on every build, AVR CI included.

Evidence

  • avr-g++ 7.3 (atmega328p): the new asserts fail before the fix. After it, popcount(u32 0x011fffff) folds to 22, countr_zero(u32 0x00010000) to 16 and popcount(i8 -1) to 8 (checked in the generated assembly).
  • bash test --cpp, bash lint, bash compile uno --examples Blink all pass.

Summary by CodeRabbit

  • Bug Fixes
    • Improved portability of integer type mappings across platforms, helping ensure unsigned types retain the same width as their corresponding signed types. This reduces the risk of unexpected behavior in applications that rely on consistent integer sizes.

make_unsigned_detail::unsigned_by_size<4> was `unsigned int`, which is
16-bit on AVR, so make_unsigned<u32/i32/long>::type silently truncated
to 16 bits there. That broke fl::popcount / fl::countr_zero for u32 on
AVR after #5291 (CodeRabbit review on #5291), and fl::format's
format_integer for 32-bit values.

Select `unsigned long` when `unsigned int` is not 4 bytes; every other
target is unchanged. Namespace-scope static_asserts now check the width
of make_unsigned<short/int/long/long long> on every build.

Verified with avr-g++ (atmega328p): the asserts fail before the fix;
after it, popcount(u32 0x011fffff) folds to 22, countr_zero(u32
0x00010000) to 16, popcount(i8 -1) to 8.
@zackees zackees added ci-platform:uno Select FastLED board CI cells ci-test:avr8js Select FastLED board CI cells ci-platform:mega Select FastLED board CI cells labels Nov 19, 2026
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The 4-byte unsigned type selector now chooses a type based on its width. Compile-time checks verify that make_unsigned preserves the widths of long, short, int, and long long.

Changes: Unsigned type width mapping

Layer / File(s) Summary
Width selection and checks
src/fl/stl/type_traits.h
The 4-byte selector uses unsigned int when it is 4 bytes and otherwise uses unsigned long. A compile-time assertion checks the selected width. Additional checks compare the unsigned mappings for four integer types with their source widths.

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

Change: Bug fix

Possibly related PRs

  • FastLED/FastLED#5291: Updates width-appropriate builtin selection for fl::popcount and countr_zero; its behavior for 32-bit arguments on AVR is related to this PR’s make_unsigned width change.
Merge Risk: Minimal · up to d4325

Merge Risk: ⚪ Minimal · up to d4325

This change makes make_unsigned keep 32-bit width on AVR, so popcount, countr_zero and format should behave correctly there. Other targets are unaffected. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(fl): make_unsigned keeps 32-bit width on AVR" matches the main change. The raw summary says unsigned_by_size<4> now selects unsigned long when unsigned int is not 4 bytes, and adds comp…
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.
Full details: Title check

Explanation

The title "fix(fl): make_unsigned keeps 32-bit width on AVR" matches the main change. The raw summary says unsigned_by_size<4> now selects unsigned long when unsigned int is not 4 bytes, and adds compile-time checks that the unsigned mappings keep each source type's width. The PR objectives describe the same AVR width mismatch, where make_unsigned for 32-bit types was 16 bits.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e0ae9a1 and 70ba897.

Files selected for processing: 1

  • src/fl/stl/type_traits.h

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

@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 19, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 19, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, Same is unchanged).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (Same) 136 (Same)
uno 3,600 (Same) 134 (Same)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 19, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 19, 2026
@zackees
zackees enabled auto-merge November 19, 2026 04:59
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 19, 2026
@zackees
zackees added this pull request to the merge queue Nov 19, 2026
Merged via the queue into master with commit e55446f Nov 19, 2026
131 checks passed
@zackees
zackees deleted the fix/avr-make-unsigned-width branch November 19, 2026 06:41
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-platform:mega Select FastLED board CI cells ci-platform:uno Select FastLED board CI cells ci-test:avr8js Select FastLED board CI cells sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant