Skip to content

fl::json: FastLED-internal float callers + missing manual fixed-point extractor API leak soft-FP into Low-memory link (gap from #3022 phase 1 / #3027) #3029

Closed

Description

@zackees

Context

#3027 (phase 1 of #3022) lands the structural gate that drops float / fl::vector<float> from fl::json_value::variant_t on FL_JSON_HAS_FLOAT=0. That eliminates the soft-FP cascade from JsonTokenizer::parse, JsonBuilder::on_token, parseFloat, and all variant::visit_fn<float, ...> instantiations — verified on lpc845brk AutoResearchLpc.ino: zero __aeabi_d* / __aeabi_f* symbols, flash 64.20 KB → 50.44 KB (~13.6 KB saved).

But during PR review the principle was tightened: the FastLED library must never pull in floating-point ops on Low-memory targets, regardless of which sketch is linked. The current PR violates that in two ways:

  1. json(float) / json(double) / operator=(float) / set(key, float) truncate-to-i64 fallback in src/fl/stl/json.h invokes static_cast<i64>(f) → pulls __aeabi_f2iz / __aeabi_d2iz (~64 B each) into the link at every call site, even when the user only ever wanted integer storage. The library is doing the float conversion itself; the user did not opt in.

  2. FastLED-internal UI controls call json::set(key, float) from library code — that is, the soft-FP pull is anchored inside the library, not inside the user's sketch. The library hits the FP path even when the user's sketch is pure integer JSON-RPC, as long as the link pulls one of the offending TUs in.

The mandate per the design discussion: "If the user uses float that's their problem, FastLED lib should not pull in floating ops when memory is low. If the sketch does then that's beyond our control."

Concrete gap

A. FastLED-internal callers that pass float into fl::json

File Line(s) Pattern
src/platforms/shared/ui/json/slider.h 20–21, 27–31, 40 JsonSliderImpl API takes float for value/min/max/step.
src/platforms/shared/ui/json/slider.cpp.hpp 18–21, 41–46, 52, 62–65 Stores float mMin/mMax/mValue/mStep; calls json.set("value", mValue) etc. and consumes `json
src/platforms/shared/ui/json/number_field.h 20, 26–27, 35, 37–40 Same pattern as slider: float in API + storage.
src/platforms/shared/ui/json/number_field.cpp.hpp 30–32 json.set("value"/"min"/"max", float).
src/platforms/shared/ui/json/json_console.cpp.hpp 190–199, 216, 263 strtod + float storage + doc.set(idStr, value) with a float.
src/fl/math/screenmap.cpp.hpp 209, 290–304 as_float() for diameter; fl::json(static_cast<double>(coord)) per element. Already gated in #3027 behind !FL_JSON_HAS_FLOAT — works as a model for the other consumers.

B. Missing manual fixed-point ↔ json API

fl::fixed_point<IntBits, FracBits, Sign> already exists at src/fl/math/fixed_point/ (32-bit Q-format types: s0x32, s16x16, u24x8, etc.) but has no fl::json integration. Today there is no way to round-trip a fractional JSON number on a Low-memory target other than through as_float() (which dispatches the variant's i64 arm via static_cast<float>(value) and pulls __aeabi_l2f at the call site).

To honor the mandate, fl::json needs these manual extractor / setter overloads:

// In fl::json_value (extractors — pure-integer, no FP):
template<int IntBits, int FracBits, typename Sign>
fl::optional<fl::fixed_point<IntBits, FracBits, Sign>> as_fixed_point() const FL_NOEXCEPT;

// Q-format pair-style accessors per the #3022 design body:
fl::pair<fl::i32, fl::u32> to_q30_31() const FL_NOEXCEPT;
fl::pair<fl::i32, fl::u32> to_q15_46() const FL_NOEXCEPT;
fl::pair<fl::u32, fl::u32> to_uq32_29() const FL_NOEXCEPT;

// In fl::json (setter overloads that store as packed i64, no FP):
template<int IntBits, int FracBits, typename Sign>
json& operator=(fl::fixed_point<IntBits, FracBits, Sign> v) FL_NOEXCEPT;

template<int IntBits, int FracBits, typename Sign>
void set(const fl::string& key, fl::fixed_point<IntBits, FracBits, Sign> v) FL_NOEXCEPT;

These should land alongside the fl::json_number packed-tagged-union design from the parent issue (#3022), since the storage layout is the same (61-bit payload + 3-bit tag in one i64 variant slot).

C. Phase-1 design defect: json(float) / json(double) truncation fallback in #3027

The fallback added in #3027 src/fl/stl/json.h:

#else  // FL_JSON_HAS_FLOAT == 0
    json(float f) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(f))) {}
    json(double d) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(d))) {}
#endif

does the conversion inside the library. The user did not ask for truncation; the library decided. __aeabi_f2iz / __aeabi_d2iz get pulled in at every call site. Per the mandate this should be a hard compile error directing the caller to the manual fixed-point setter — or, equivalently, the overload should not exist.

Same applies to:

  • json& operator=(float) / json& operator=(double) in json.h
  • void set(const fl::string& key, float) / void set(..., double) in json.h

Proposal — "phase 1.5" PR (closes this issue)

  1. Remove the truncation fallback in json.h. Gate json(float/double), operator=(float/double), set(key, float/double) behind #if FL_JSON_HAS_FLOAT only. No #else branch. Caller code that tries to pass a float on a Low-memory build hits a compile error that directs them to fl::fixed_point<> + the new set(key, fixed_point) overload (B above). The error message should name the migration path.

  2. Gate FastLED-internal UI-control .cpp.hpp files behind #if FL_JSON_HAS_FLOAT. slider.cpp.hpp, number_field.cpp.hpp, the float branch of json_console.cpp.hpp. On FL_JSON_HAS_FLOAT=0 the entire UI-slider TU drops out of the library archive — sketches that link nothing else from platforms/shared/ui/json/ see zero impact, sketches that try to use a slider fail at link with undefined reference, mirroring the existing FASTLED_NO_JSON ergonomics. ScreenMap already follows this pattern in feat(json): gate float out of variant on FL_JSON_HAS_FLOAT=0 (#3022 phase 1) #3027.

    Future / phase 2: add fixed-point variants — JsonSliderImplFixed<Q16x16> etc. — so on FL_JSON_HAS_FLOAT=0 users still have a working slider, just one whose value type is fl::s16x16 instead of float. Tracked separately under fl::json: custom 64-bit packed tagged-number type (replaces variant<i64, float, double> in #3002) #3022 once the json_number Q-format extractors land.

  3. Land the json↔fixed_point extractor / setter API (B above) as a sibling PR or bundled into the phase-2 fl::json_number PR for fl::json: custom 64-bit packed tagged-number type (replaces variant<i64, float, double> in #3002) #3022. The extractor / setter design is independent of the packed-tag union; it can land first and the packed-tag union promotes to use it later.

Verification (for the closing PR)

  • bash compile lpc845brk --examples AutoResearchLpc succeeds with FL_JSON_HAS_FLOAT=0 (same as today).
  • arm-none-eabi-nm -C firmware.elf | grep -E '(aeabi_d|aeabi_f|f2iz|d2iz|__l2f|__floatdisf)' stays empty (currently passes the aeabi_d / aeabi_f check but __aeabi_f2iz / __aeabi_d2iz are reachable from the truncation fallback when any caller links).
  • A pure-integer LPC sketch that does not use UI controls or ScreenMap continues to compile (this is the AutoResearchLpc.ino baseline).
  • A pure-integer LPC sketch that does try to write json["x"] = 3.14f fails at compile time with a clear error message pointing to fl::s16x16 + the manual setter.

Related

🤖 Filed via Claude Code as the gap-tracking issue spun out of #3027 review.

Activity

  1. added a commit that references this issue on Jun 14, 2026
    cfe8b91
  2. zackees commented on Jun 14, 2026

    @zackees
    MemberAuthor

    Phase 1.5 work landed as commit cfe8b9156d on PR #3027's branch (feat/3022-json-no-float), per the issue's "fold into the in-flight PR" guidance. The commit:

    1. src/fl/stl/json.h — drops the #else truncation fallback for json(float/double), operator=(float/double), and set(key, float/double). On FL_JSON_HAS_FLOAT=0 these are now SFINAE-templated FL_STATIC_ASSERT overloads that name the migration path (fl::fixed_point<>, e.g. fl::s16x16 from src/fl/math/fixed_point/). The library no longer takes ownership of float→i64 conversion.

    2. src/fl/stl/json.h — adds the fl::json ↔ fl::fixed_point setter / extractor API (item B from the issue body), but minimally: templated json(FP), operator=(FP), set(key, FP), and as_fixed_point<FP>() keyed on a SFINAE trait that probes INT_BITS / FRAC_BITS / raw() / from_raw(). Storage is the existing i64 variant slot (since "our number format is itself a super fixed point" — the Q-format is encoded in the C++ type at the call site, not as a JSON tag). The Q-format pair accessors and full json_number packed-tag union remain deferred to fl::json: custom 64-bit packed tagged-number type (replaces variant<i64, float, double> in #3002) #3022 phase 2.

    3. src/platforms/shared/ui/json/slider.cpp.hpp, .../number_field.cpp.hpp — wrap whole-TU bodies in #if FL_JSON_HAS_FLOAT. Sketches that don't instantiate these widgets see no impact; sketches that try fail at link with undefined reference, matching the FASTLED_NO_JSON / ScreenMap ergonomics.

    4. src/platforms/shared/ui/json/json_console.cpp.hpp — tightens the existing SKETCH_HAS_LARGE_MEMORY gate to also require FL_JSON_HAS_FLOAT, so a user opt-out via -DFL_JSON_HAS_FLOAT=0 on a Large-memory part drops the TU too.

    CI on PR #3027 will exercise the LPC845 verification matrix from the issue body. The fixed-point widget variants (JsonSliderImplFixed<Q16x16>) remain phase-2 work.

    🤖 via Claude Code.

  3. moved this from Triage to Done in FastLED Trackeron Jun 18, 2026
  4. zackees commented on Jun 18, 2026

    @zackees
    MemberAuthor

    Fixed by merged PR #3211: #3211

    What changed:

    • Low-memory builds keep the existing fl::json(float), fl::json(double), and fixed-point JSON entry points.
    • double and fixed-point values now convert into JSON float storage through integer IEEE-754 bit construction instead of relying on target floating-point conversion helpers.
    • Oversized scaled fixed-point values saturate to infinity, and double NaN/overflow cases are covered.
    • ScreenMap::toJson avoids unnecessary float -> double promotion.

    Upgrade guidance: no source changes are expected for users. Update to a FastLED version containing PR #3211; existing JSON float/double/fixed-point callers should continue to compile, including low-memory LPC builds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions