Skip to content

fix(avr/math): #undef Arduino F() macro before fl::math polynomial templates (unbreak master Uno build) - #3086

Merged
zackees merged 1 commit into
masterfrom
fix/avr-math-F-macro-collision
Jun 15, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/avr-math-F-macro-collision

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Master's Uno (AVR) build has been red since c0da8bd35d ("perf(fl/math): gate libm behind SKETCH_HAS_LARGE_MEMORY; polynomial impls on Low-memory"). 5 consecutive master CI failures, none related to feature work. This PR unbreaks it.

Root cause

Arduino's `WString.h` (transitively included via the Arduino core on AVR and other Arduino-framework targets) defines:

```cpp
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
```

That collides with the `template ` parameter used by 9 polynomial / Newton-iteration helpers in `src/fl/math/math.cpp.hpp` (sqrt_newton_, sin_poly_quarterturn_, cos_poly_quarterturn_, sin_reduce_, cos_reduce_, log_natural_, atan_poly_unit_, atan_full_, atan2_full_, ldexp_loop_). The preprocessor rewrites every `F(value)` constructor cast in those templates into `WString::F(value)`, which fails with:

```
WString.h:38:74: error: initializer fails to determine size of '__c'
note: in expansion of macro 'F'
```

Fix

`#undef F` at the top of `math.cpp.hpp` after the includes that pull in WString.h. Local to this translation unit; `fl::math` itself never uses the macro; only end-user sketches do (where the macro stays defined).

No behavior change on any platform. Just unbreaks the AVR translation unit.

Discovery context

Surfaced while CI-checking PRs #3084 and #3085 (against meta #3079 — LPC845 64 KB flash bloat hunt). Those PRs were green on every other platform but blocked by this pre-existing master breakage on AVR.

Test plan

  • Code review of `grep -n F(` in math.cpp.hpp — confirms all uses are template constructor casts (`F(value)`), none are intended PROGMEM strings.
  • CI: full matrix, especially Uno (AVR).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a macro naming conflict that was preventing successful compilation in certain build scenarios.

…emplates

Master Uno (AVR) build has been red since c0da8bd landed the Low-memory
polynomial fallbacks. Root cause: Arduino's WString.h (transitively included
via the Arduino core on AVR) defines `F()` as a PROGMEM-string macro. The
preprocessor rewrites every `F(value)` constructor cast in fl::math's
polynomial helpers (e.g. `F(1.0 / 6.0)`, `F(0.5)`) into `WString::F(1.0 / 6.0)`,
which fails with "initializer fails to determine size of '__c'".

Undefining the macro at the top of math.cpp.hpp is local to this translation
unit. fl::math itself never uses the macro; only end-user sketches do (where
the macro stays defined). No behavior change on any platform.

Pre-existing master breakage; surfaced while CI-checking PRs #3084 / #3085
for meta #3079.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b5badcac-958f-40cf-8335-e927305ca0b0

📥 Commits

Reviewing files that changed from the base of the PR and between 4a514d4 and 66c24b1.

📒 Files selected for processing (1)
  • src/fl/math/math.cpp.hpp

📝 Walkthrough

Walkthrough

In src/fl/math/math.cpp.hpp, an #ifdef F / #undef F guard is added to locally remove Arduino's F() flash-string macro within this translation unit, preventing it from macro-substituting the F template parameter name used in polynomial and Newton helper templates.

Changes

Arduino F() Macro Collision Fix

Layer / File(s) Summary
Guard and undef Arduino F() macro
src/fl/math/math.cpp.hpp
Adds an #ifdef F / #undef F block (11 lines) so Arduino's global F() macro does not rewrite F(…) call sites inside template <typename F> polynomial/Newton helpers in this translation unit.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐇 A macro named F caused quite a fright,
Rewriting my templates left and right.
With #undef in place, the conflict is done,
Templates compile clean under Arduino's sun.
Hop hop hooray, the math helpers run!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the specific fix applied—undefining Arduino's F() macro to resolve a collision with template parameter names in polynomial templates, and explicitly mentions restoring the broken Uno build.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/avr-math-F-macro-collision

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 and usage tips.

@zackees
zackees merged commit a767970 into master Jun 15, 2026
9 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 15, 2026
zackees added a commit that referenced this pull request Jun 18, 2026
…er (closes #3235 Tier 1B) (#3238)

Replaces the previous 5-alternative `fl::variant` storage (heap-fallback +
free-fn + inlined-lambda + non-const member + const member) with a single
small-buffer-optimized storage plus two function pointers:

  - `Invoker`: per-Func; knows how to call the stored callable with
    (Args...).
  - `Manager`: per-Func (or shared `trivial_manager<sizeof(Func)>` for
    trivially-copyable callables); multiplexes copy / move / destroy
    through an Op enum.

The variant's per-(T, op) thunk explosion -- 5 alternatives × 4 ops × N
signatures = ~25 small functions per Sig at ~80-120 B each -- collapses
to one invoker + one manager pointer per Func instance. Trivially-copyable
captures (the common case) share a per-size `memcpy` manager so the
linker collapses many F's into one body.

## Member-function support preserved

The `function(R(C::*)(Args...), C*)` and const variant constructors
remain in the public API. They route through thin `NonConstMemberWrapper<C>`
and `ConstMemberWrapper<C>` structs stored in the SBO -- the legacy
`MemberCallableBase` / `NonConstMemberCallable` / `ConstMemberCallable`
class hierarchy and its virtual dispatch are gone.

## Heap fallback retained

Lambdas larger than `FASTLED_INLINE_LAMBDA_SIZE` (default 64 B) still
compile -- they get wrapped in a `HeapHolder<Func>` struct that stores
`fl::shared_ptr<Func>` in the SBO. This preserves the legacy "big
lambda lives on the heap" semantics without re-introducing the variant.

Tradeoff investigation tracked separately at #3237 (HeapHolder's
shared_ptr vs unique_ptr-deep-copy vs single-threaded-refcount options).

## Verification

LPC845-BRK / AutoResearch (Low-memory):
    flash 49,396 B -> 48,260 B  (-1,136 B)
    RAM   9,984 B  ->  9,984 B  (unchanged)

Teensy 4.0 / AutoResearch (Large-memory):
    flash 357,376 B -> 348,160 B  (-9,216 B saved, -2.6%)
    RAM   112,576 B -> 110,528 B  (-2,048 B saved)

`fl::function`-related symbol mass on LPC845 dropped from 3,164 B (post-
#3084) to 2,302 B (-862 B specifically from the variant collapse, the
rest is downstream DCE).

The Low-memory `FL_FUNCTION_FULL_VARIANT` macro is no longer needed --
both tiers share the same Option B implementation. The Low-memory
"no-op heap-fallback" branch from #3077/#3084 is replaced by the real
`HeapHolder` path; behavior is now uniform across all targets.

## Rename: F -> Func template parameter

Renamed the template parameter throughout to `Func` because Arduino's
`Arduino.h` (LPC8xx core, AVR core) defines `F()` as a PROGMEM-string
preprocessor macro that collides with `F` used as a template parameter
name. Same root cause as #3086's m0clockless_c.h fix.

Refs #3235 Tier 1B, #3237
@zackees
zackees deleted the fix/avr-math-F-macro-collision 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.

1 participant