Repository navigation
perf(fl/math): gate libm behind SKETCH_HAS_LARGE_MEMORY; polynomial impls on Low-memory (closes most of #3002) - #3012
Conversation
…mpls on Low-memory Closes the dominant share of FastLED #3002 — the libm/soft-FP bloat that made `bash autoresearch lpc845brk` fail to link by ~3.4 KB on the LPC845 JSON-RPC bring-up sketch. Root cause traced via the LPC845-BRK firmware.map: fl.math+_5ad9.o (symbol from plugin) (ldexpf) fl.math+_5ad9.o (symbol from plugin) (atan2) `fl::math`'s `*_impl_*` wrappers thinly forwarded to libm (`::sqrtf`, `::sinf`, `::atan2`, `::ldexpf`, ...). On no-FPU MCUs that anchors libm's internal soft-FP chain (`libm_a-s_scalbn.o` → `__aeabi_dadd`, `libm_a-w_fmod.o` → `__aeabi_ddiv`, etc.), pulling libgcc's full soft- double helper set into every sketch that linked `fl::math` — ~6 KB on the 64 KB-flash LPC845. The sketch never actually computed an arctangent or a square root. Changes ------- 1. **src/fl/math/math.cpp.hpp** — every `*_impl_*` function is now gated by `FL_MATH_USE_LIBM`, which derives from `SKETCH_HAS_LARGE_MEMORY`: * Large-memory targets (`SKETCH_HAS_LARGE_MEMORY=1` — host, ESP32, Teensy 4.x, RP2040, SAMD51, STM32F4+) use libm unchanged for full IEEE-754 precision. Existing tests' `epsilon(1e-6)`/`epsilon(1e-10)` guarantees are preserved. * Low-memory targets (`SKETCH_HAS_LARGE_MEMORY=0` — AVR, LPC8xx, Teensy LC/3.x, STM32F1, ESP8266, ...) use hand-rolled polynomial / Newton-Raphson approximations in a `detail::` namespace: - `sqrt_newton_<F>` — bounded Newton iteration - `sin_reduce_<F>` / `cos_reduce_<F>` — range-reduced degree-7 Taylor - `log_natural_<F>` — fraction extraction + Taylor on [1,2) - `atan_full_<F>` / `atan2_full_<F>` — minimax degree-11 polynomial - `ldexp_loop_<F>` — bounded multiply-by-2 loop - `pow`, `fmod`, `lround`, `round`, `asin`, `acos`, `tan`, `hypot` derived from the primitives above Sketches on Low-memory targets that need full IEEE-754 precision can `#include <cmath>` directly; that anchors libm only at the call site that opted in, not from every `fl::sqrt(x)`. 2. **src/fl/system/sketch_macros.h** — add `FL_IS_ARM_LPC` to the `SKETCH_HAS_LARGE_MEMORY=0` list. NXP LPC8xx parts (LPC845: 16 KB SRAM / 64 KB flash, LPC804: 4 KB SRAM / 32 KB flash) are in the same tier as Teensy LC / STM32F1 — not the Mega-class "high" tier. 3. **src/platforms/arm/compile_test.hpp** — extend the Low-memory ARM group to include `FL_IS_ARM_LPC` so the platform self-check passes with the corrected `SKETCH_HAS_LARGE_MEMORY=0`. Verification ------------ * Host: `bash test fl/math/math.cpp --cpp` — passes (libm path unchanged, all existing `doctest::Approx` precision guarantees hold). * LPC845-BRK firmware map (after rebuild): before: fl.math+ → ldexp, ldexpf, atan2, acos, asin, fmod, hypot, log, log10, pow, sqrt, atan, cos, sin, tan after: fl.math+ → (no libm references) Net flash savings: 2664 bytes on the bring-up sketch (3424 B over → 760 B over). A small residual 760 B is from double-precision compare helpers (`__aeabi_dcmpeq` and friends, ~424 B from `__ledf2`/`__gedf2`) anchored by a path that still uses `double` somewhere in the linked output. That is the remaining work toward closing `bash autoresearch lpc845brk` end-to-end — tracked as the natural follow-up. Related ------- * FastLED #3002 — original "fl::json fixed-point variant" issue. The investigation under this PR moved the implementation target from "json refactor" to "libm-free fl::math primitives" once the firmware.map showed `fl.math+` was the actual libm anchor; fl::json already uses `float` (not `double`) per the comment at `src/fl/stl/json/types.h:652`. * FastLED #3009 — LPC845 AutoResearch harness PR (merged on master, this PR's verification target). Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesConditional math library selection
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 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 |
Add `BareLibmChecker` (PR #3012, issue #3002): bans direct `::sqrtf`, `::atan2`, `::powf`, `::ldexpf`, etc. in `src/`, forcing new code to go through `fl::sqrt(x)` / `fl::atan2(y, x)` / ... — which on Low-memory targets route to hand-rolled polynomial / Newton-Raphson approximations rather than dragging libm + libgcc soft-FP into the link. Exemptions: - `fl/math/math.cpp.hpp` / `math.h` — the compat shim itself. - `src/third_party/` — upstream code we don't control. - Pre-existing heavy users staged for migration (fl/audio, fl/fx, fl/gfx, fl/math/{fixed_point,line_simplification,screenmap,transform}, platforms/esp, platforms/wasm). The discipline this checker enforces is "no NEW code that imports libm directly"; the existing subsystems (audio FFT in particular needs full IEEE 754) will be ported through fl:: over a series of focused refactors. Suppression: `// ok libm` at end of line to whitelist a specific call. Fix `src/power_mgt.cpp.hpp:87` — the actual LPC845-BRK blocker. The old `lroundf(powf(normalized, exponent) * 255.0f)` anchored libm's full soft-FP chain on no-FPU targets even though everything else already routes through fl::math. Now uses `fl::powf` / `fl::lroundf` (the float overloads — fl::pow takes double, so passing floats would auto-promote and re-anchor double-precision soft-FP). Co-Authored-By: Claude Opus 4.7 <[email protected]>
Add `BareLibmChecker` (PR #3012, issue #3002): bans direct `::sqrtf`, `::atan2`, `::powf`, `::ldexpf`, etc. in `src/`, forcing new code to go through `fl::sqrt(x)` / `fl::atan2(y, x)` / ... — which on Low-memory targets route to hand-rolled polynomial / Newton-Raphson approximations rather than dragging libm + libgcc soft-FP into the link. Exemptions: - `fl/math/math.cpp.hpp` / `math.h` — the compat shim itself. - `src/third_party/` — upstream code we don't control. - Pre-existing heavy users staged for migration (fl/audio, fl/fx, fl/gfx, fl/math/{fixed_point,line_simplification,screenmap,transform}, platforms/esp, platforms/wasm). The discipline this checker enforces is "no NEW code that imports libm directly"; the existing subsystems (audio FFT in particular needs full IEEE 754) will be ported through fl:: over a series of focused refactors. Suppression: `// ok libm` at end of line to whitelist a specific call. Fix `src/power_mgt.cpp.hpp:87` — the actual LPC845-BRK blocker. The old `lroundf(powf(normalized, exponent) * 255.0f)` anchored libm's full soft-FP chain on no-FPU targets even though everything else already routes through fl::math. Now uses `fl::powf` / `fl::lroundf` (the float overloads — fl::pow takes double, so passing floats would auto-promote and re-anchor double-precision soft-FP). Co-Authored-By: Claude Opus 4.7 <[email protected]>
…link Last 692 B of the LPC845-BRK flash deficit (FastLED #3002) came from a chain that started innocently and ended in libgcc's full soft-double set: int_conversion_visitor<i64>::operator()(const float& value) result = static_cast<i64>(value); // <-- __aeabi_f2lz // ↓ _fixsfdi.o → __aeabi_f2ulz // ↓ _fixunssfdi.o → __aeabi_dmul, __aeabi_dsub // ~6 KB of soft-double The cast is reached through `json_value::as_int()`, which `fl::Remote` calls on every incoming RPC to read the request `id` and `timestamp`. Even though the variant only holds `float` (not `double` — see types.h:652), one tag in the variant is enough to instantiate the visitor's float overload and pull in the chain. Fix: route (i64)(float) through (i32)(float) when the value is in range (saturates otherwise). The variant only ever holds float and JSON integers from any sane RPC payload fit in int32, so this is a no-behavior-change refactor for the realistic input set, but it lowers libgcc's chosen helper from `__aeabi_f2lz` (which uses double) to `__aeabi_f2iz` (which doesn't). Also strip the gratuitous `static_cast<double>(...)` casts from the JsonToIntegerVisitor / JsonToBoolVisitor / JsonToFloatVisitor / JsonToStringVisitor warning-formatting paths — they each pulled in `__aeabi_dcmpeq` / `__ledf2` / `__gedf2` / `__aeabi_d2f` for no diagnostic benefit (float has more than enough precision for a "truncated to int" warning string). After: ❯ arm-none-eabi-size firmware.elf text data bss 63088 372 8336 (~2 KB flash headroom on a 64 KB part) Refs: #3002, #3012, #3013. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Summary
Closes the dominant share of #3002 — the libm/soft-FP bloat that made
bash autoresearch lpc845brkfail to link by ~3.4 KB on the LPC845 JSON-RPC bring-up sketch.Root cause (LPC845-BRK firmware.map)
fl::math's*_impl_*wrappers thinly forwarded to libm (::sqrtf,::sinf,::atan2,::ldexpf, ...). On no-FPU MCUs that anchors libm's internal soft-FP chain (libm_a-s_scalbn.o→__aeabi_dadd,libm_a-w_fmod.o→__aeabi_ddiv, etc.), pulling libgcc's full soft-double helper set into every sketch that linkedfl::math— ~6 KB on the 64 KB-flash LPC845. The sketch never actually computed an arctangent or a square root.Note: the original #3002 hypothesis was that
fl::json'sdoublerepresentation was the libm anchor. Investigation showedfl::jsonalready usesfloatpersrc/fl/stl/json/types.h:652("changed from double to float"). The actual anchor wasfl::math's thin libm wrappers —fl.math+_5ad9.o (symbol from plugin) (ldexpf)is unambiguous in the firmware.map.Changes
src/fl/math/math.cpp.hpp— every*_impl_*function is gated byFL_MATH_USE_LIBM, which derives fromSKETCH_HAS_LARGE_MEMORY:Large-memory (
SKETCH_HAS_LARGE_MEMORY=1— host, ESP32, Teensy 4.x, RP2040, SAMD51, STM32F4+): use libm unchanged for full IEEE-754 precision. Existing tests'epsilon(1e-6)/epsilon(1e-10)guarantees are preserved.Low-memory (
SKETCH_HAS_LARGE_MEMORY=0— AVR, LPC8xx, Teensy LC/3.x, STM32F1, ESP8266, ...): hand-rolled polynomial / Newton-Raphson approximations in adetail::namespace:Sketches on Low-memory targets that need full IEEE-754 precision can `#include ` directly; that anchors libm only at the call site that opted in, not from every `fl::sqrt(x)`.
`src/fl/system/sketch_macros.h` — add `FL_IS_ARM_LPC` to the `SKETCH_HAS_LARGE_MEMORY=0` list. NXP LPC8xx parts (LPC845: 16 KB SRAM / 64 KB flash, LPC804: 4 KB SRAM / 32 KB flash) are in the same tier as Teensy LC / STM32F1 — not the Mega-class "high" tier.
`src/platforms/arm/compile_test.hpp` — extend the Low-memory ARM group to include `FL_IS_ARM_LPC` so the platform self-check passes with the corrected `SKETCH_HAS_LARGE_MEMORY=0`.
Verification
Host: `bash test fl/math/math.cpp --cpp` — passes. Libm path is unchanged, all existing `doctest::Approx` precision guarantees hold.
LPC845-BRK firmware.map (after rebuild):
Net flash savings on the bring-up sketch: 2664 bytes (3424 B over → 760 B over).
Remaining work
A small residual 760 B is from double-precision compare helpers (`__aeabi_dcmpeq` and friends, ~424 B from `__ledf2` / `__gedf2`) anchored by a path that still uses `double` somewhere in the linked output (likely a remaining downstream consumer of `fl::pow` or similar with a `double` argument). Closing that residual is the natural follow-up needed for `bash autoresearch lpc845brk` to link cleanly end-to-end.
Related
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Chores