Skip to content

refactor(math): move JSON soft-FP bit-codec into fl/math/ + fl::number scaffold - #3294

Merged
zackees merged 1 commit into
masterfrom
feat/3022-json-soft-fp-completion
Jun 19, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/3022-json-soft-fp-completion

Conversation

@zackees

@zackees zackees commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Phase-1 step toward closing #3022 / #3002. Moves the integer IEEE-754 bit-codec out of fl/stl/json/types_impl.h into fl/math/soft_float.h so non-JSON callers can use it directly. Adds the widening companion (soft_float_to_double) -- previously the JSON layer only had the narrowing path -- and rewires float_conversion_visitor so the residual static_cast<double>(float) / static_cast<float>(double) on as_float() / as_double() extraction can no longer anchor __aeabi_f2d / __aeabi_d2f on no-FPU targets. Also lands the fl::number 64-bit packed tagged-value scaffold (#3022's design) so phase 2 can build on it.

Refs #3022, #3002, #3208.

Background

The pre-#3038 JSON layer already used integer bit-codec for the narrowing path (json_double_to_float). The widening path -- when as_double() extracts a value stored as float -- still went through a plain C++ cast, which on no-FPU targets pulls __aeabi_f2d and transitively the soft-double cascade. Even with as_double() dead-code-eliminated on LowMemory builds today, the cast remained a footgun: a future caller keeping the path alive would re-drag soft-FP. This PR closes that gap structurally.

Files

src/fl/math/soft_float.h (new)

Public API:

fl::soft_float_to_double(float) -> double           // NEW widening
fl::soft_double_to_float(double) -> float           // moved from JSON, existing
fl::soft_double_bits_from_float_bits(u32) -> u64    // NEW widening bits
fl::soft_float_bits_from_double_bits(u64) -> u32    // moved from JSON, existing

All four use only shift / mask / add. Handle subnormals, NaN canonicalization, +/-inf bit-exactly. Symmetric round-trip:

soft_double_to_float(soft_float_to_double(f)) == f   // bit-exact for finite + inf

src/fl/math/number.h (new)

64-bit packed tagged numeric value -- the #3022 design's storage shape. Lays down:

  • fl::number class with raw : fl::u64 slot
  • fl::number::tag 3-bit enum (SIGNED_INT, UNSIGNED_INT, FLOAT_60, RESERVED_3..7)
  • Factory: number::from_signed(i64), number::from_unsigned(u64) (saturate on overflow)
  • Accessors: get_tag(), payload(), as_signed_payload()

The FLOAT_60 tag is reserved (no parser/serializer yet -- those follow in phase 2 alongside JSON adoption). Footprint is exactly 64 bits so the type can slot into a JSON value variant cell without padding overhead.

src/fl/stl/json/types_impl.h (refactor)

Now a thin shim that maps the old json_* names to the new fl::soft_* API. Zero behavior change for existing JSON callers.

src/fl/stl/json/types.h (refactor)

float_conversion_visitor's float<->double cast paths (the static_cast sites flagged in the issue) route through detail::json_double_to_float / detail::json_float_to_double, which delegate to the new fl::soft_* helpers.

Verification

  • ✅ bash test --cpp -- 327 / 327 pass (incl. 7 new soft_float tests, 5 new fl::number tests).
  • ✅ bash lint --cpp -- clean (FL_STATIC_ASSERT per banned-macro checker; only pre-existing warn-only PlatformIO-internal-usage findings remain).
  • ⚠ bash compile lpc845brk -- could not verify locally due to a zccache-daemon flake (fbuild#627 / soldr#710 -- Windows named-pipe race against a stale daemon). Failure is in upstream ArduinoCore-LPC8xx source files (wiring.cpp, WInterrupts.c), not in anything this PR touches. CI on Linux runners will catch any real LPC regression. No source change in this PR touches LPC-specific paths.

Behavior matrix

Path Pre-PR Post-PR
Store double into json -> as_float() extract integer bit-codec (json_double_to_float) same (renamed fl::soft_double_to_float)
Store float into json -> as_double() extract static_cast<double> -> __aeabi_f2d integer bit-codec (fl::soft_float_to_double)
Store float into json -> as_float<float>() extract direct copy same
Store double into json -> as_float<float>() extract static_cast<float> -> __aeabi_d2f integer bit-codec (fl::soft_double_to_float)
Non-JSON code wanting soft-FP widen/narrow #include "fl/stl/json/types_impl.h" (awkward) #include "fl/math/soft_float.h" (proper home)

Test plan

  • bash test --cpp 327/327
  • bash lint --cpp clean
  • Round-trip identity test (soft_double_to_float(soft_float_to_double(f)) == f) covers 13 sample bit-patterns spanning normal, subnormal, FLT_MIN/MAX, +/-zero, +/-inf
  • fl::number saturation + sign-extension test covers payload-boundary values
  • CI green on full platform matrix (waiting on this PR's CI)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Introduced fl::number — a compact 64-bit numeric type for efficient storage of signed integers, unsigned integers, and floats.
    • Added IEEE-754 compliant float-to-double and double-to-float conversion utilities without external dependencies.
    • Enhanced JSON numeric conversion handling with improved IEEE-754 compliance.
  • Tests

    • Added comprehensive tests for the new numeric type and conversion utilities.

…r scaffold

Phase 1 toward closing #3022 / #3002. Two changes:

1. NEW `src/fl/math/soft_float.h` -- integer-only IEEE-754 float<->double
   bit-codec. Moved out of `fl/stl/json/types_impl.h` (which now reads as
   a thin shim around the new home) so non-JSON callers (audio timing,
   animation curves, anyone else who needs FP on a no-FPU target without
   the libgcc soft-FP tax) can use it directly. Public API:

     fl::soft_float_to_double(float) -> double         (NEW widening path)
     fl::soft_double_to_float(double) -> float         (existing, renamed)
     fl::soft_double_bits_from_float_bits(u32) -> u64  (NEW widening bits)
     fl::soft_float_bits_from_double_bits(u64) -> u32  (existing, renamed)

   The widening path is the new piece. The narrowing path moved unchanged
   from `json_double_to_float` (#3038). Both paths use only integer
   shift/mask/add, emit zero soft-FP symbols on no-FPU targets, and
   handle subnormals, NaN canonicalization, and +/-inf bit-exactly.

   Symmetric round-trip: `soft_double_to_float(soft_float_to_double(f))`
   is bit-exact identity for any finite float (and inf/qNaN/zero).

2. NEW `src/fl/math/number.h` -- 64-bit packed tagged numeric value
   (scaffold for the #3022 design). Lays down the type, the 8-tag enum
   (SIGNED_INT, UNSIGNED_INT, FLOAT_60, RESERVED_3..7), and basic
   factory/accessor methods for the integer tags. The FLOAT_60 tag is
   reserved (no parser/serializer yet -- those follow in a phase-2 PR
   alongside JSON adoption). Storage footprint is exactly 64 bits so it
   slots into the JSON value variant without padding overhead.

3. JSON wiring: `float_conversion_visitor` (`as_float` / `as_double`
   visitor used on extraction) routes the residual float<->double
   `static_cast` paths through the new soft_float codec. Was the last
   path that could anchor `__aeabi_f2d` / `__aeabi_d2f` on no-FPU
   targets even if `as_double()` survived DCE. Now structurally
   impossible -- the soft_float helpers compile to integer-only ops.

Verification:
- bash test --cpp -- 327 / 327 pass (incl. 7 new soft_float tests and
  5 new fl::number tests).
- bash lint --cpp -- clean (FL_STATIC_ASSERT used per banned-macro
  checker; only warn-only PlatformIO-internal-usage findings remain).
- bash compile lpc845brk -- could not verify locally due to zccache
  daemon flake (fbuild infrastructure issue, see soldr#710); CI will
  catch any LPC regression. No source change touches LPC-specific paths.

Refs #3022, #3002, #3208 (LPC burn-down meta).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 02cd1cfa-9f66-4d15-9a6a-7327a51f0d91

📥 Commits

Reviewing files that changed from the base of the PR and between cb82d68 and 6626205.

📒 Files selected for processing (6)
  • src/fl/math/number.h
  • src/fl/math/soft_float.h
  • src/fl/stl/json/types.h
  • src/fl/stl/json/types_impl.h
  • tests/fl/math/number.cpp
  • tests/fl/math/soft_float.cpp

📝 Walkthrough

Walkthrough

Adds two new math headers: fl::soft_float (integer-only IEEE-754 float↔double bit-exact codec without libgcc soft-FP) and fl::number (64-bit packed tagged numeric type with 3-bit tag and 61-bit saturating payload). Refactors src/fl/stl/json/types_impl.h to delegate its inline bit-manipulation to soft_float.h, and updates json/types.h float conversion visitors to use the codec. Unit tests cover both new types.

Changes

Soft-float integer codec and JSON float conversion wiring

Layer / File(s) Summary
soft_float integer codec: helpers, narrowing, widening, convenience API
src/fl/math/soft_float.h
Defines soft_bit_copy, round-to-nearest-even right-shift, floor-log2, soft_float_bits_from_scaled_u64, soft_float_bits_from_double_bits, soft_double_bits_from_float_bits, and soft_double_to_float/soft_float_to_double convenience wrappers using only integer arithmetic.
soft_float unit tests: widening, narrowing, round-trip
tests/fl/math/soft_float.cpp
Tests float→double widening for zero, normals, subnormals, infinities, NaN canonicalization, and boundary values; tests double→float narrowing; asserts round-trip correctness over representative float bit patterns.
JSON float conversion refactored to use soft_float codec
src/fl/stl/json/types_impl.h, src/fl/stl/json/types.h
types_impl.h replaces inline IEEE-754 arithmetic with thin wrappers delegating to soft_float.h; types.h declares json_ieee754_double_bits_from_float_bits/json_float_to_double and updates float_conversion_visitor overloads to route through the codec instead of direct casts.

fl::number 64-bit packed tagged numeric type

Layer / File(s) Summary
fl::number type definition, constructors, factories, accessors
src/fl/math/number.h
Defines fl::number with a 3-bit tag enum (SIGNED_INT, UNSIGNED_INT, FLOAT_60), 61-bit payload, payload mask/shift constants, default and (tag, payload) constructors, from_signed/from_unsigned saturating factories, get_tag/payload accessors, as_signed_payload() sign-extension, and a compile-time sizeof assertion.
fl::number unit tests: default, round-trip, saturation, sizeof
tests/fl/math/number.cpp
Tests default construction (zero, SIGNED_INT tag), from_signed round-trip and 61-bit signed boundary saturation, from_unsigned round-trip and saturation at 61-bit unsigned max, and static sizeof equality with u64.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • FastLED/FastLED#3211: Modifies src/fl/stl/json/types.h IEEE-754 float/double handling via integer/bit-level conversion paths, directly overlapping with the float_conversion_visitor changes in this PR.
  • FastLED/FastLED#3226: Modifies float_conversion_visitor in src/fl/stl/json/types.h with platform-gated float parsing/formatting overrides, directly adjacent to the codec-routing changes made here.

Poem

🐰 Bits and tags in neat arrays,
A rabbit packs them sixty-one ways.
No libgcc, no soft-FP fuss,
Just integers riding the data bus.
Round-to-even, subnormal, NaN—
All exact, by the integer plan! ✨

✨ 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 feat/3022-json-soft-fp-completion

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.

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