Repository navigation
refactor(math): move JSON soft-FP bit-codec into fl/math/ + fl::number scaffold - #3294
Conversation
…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]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughAdds two new math headers: ChangesSoft-float integer codec and JSON float conversion wiring
fl::number 64-bit packed tagged numeric type
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Poem
✨ 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 |
Summary
Phase-1 step toward closing #3022 / #3002. Moves the integer IEEE-754 bit-codec out of
fl/stl/json/types_impl.hintofl/math/soft_float.hso 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 rewiresfloat_conversion_visitorso the residualstatic_cast<double>(float)/static_cast<float>(double)onas_float()/as_double()extraction can no longer anchor__aeabi_f2d/__aeabi_d2fon no-FPU targets. Also lands thefl::number64-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 -- whenas_double()extracts a value stored asfloat-- still went through a plain C++ cast, which on no-FPU targets pulls__aeabi_f2dand transitively the soft-double cascade. Even withas_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:
All four use only shift / mask / add. Handle subnormals, NaN canonicalization, +/-inf bit-exactly. Symmetric round-trip:
src/fl/math/number.h(new)64-bit packed tagged numeric value -- the #3022 design's storage shape. Lays down:
fl::numberclass withraw : fl::u64slotfl::number::tag3-bit enum (SIGNED_INT,UNSIGNED_INT,FLOAT_60,RESERVED_3..7)number::from_signed(i64),number::from_unsigned(u64)(saturate on overflow)get_tag(),payload(),as_signed_payload()The
FLOAT_60tag 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 newfl::soft_*API. Zero behavior change for existing JSON callers.src/fl/stl/json/types.h(refactor)float_conversion_visitor's float<->double cast paths (thestatic_castsites flagged in the issue) route throughdetail::json_double_to_float/detail::json_float_to_double, which delegate to the newfl::soft_*helpers.Verification
bash test --cpp-- 327 / 327 pass (incl. 7 newsoft_floattests, 5 newfl::numbertests).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 upstreamArduinoCore-LPC8xxsource 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
doubleinto json ->as_float()extractjson_double_to_float)fl::soft_double_to_float)floatinto json ->as_double()extractstatic_cast<double>->__aeabi_f2dfl::soft_float_to_double)floatinto json ->as_float<float>()extractdoubleinto json ->as_float<float>()extractstatic_cast<float>->__aeabi_d2ffl::soft_double_to_float)#include "fl/stl/json/types_impl.h"(awkward)#include "fl/math/soft_float.h"(proper home)Test plan
bash test --cpp327/327bash lint --cppcleansoft_double_to_float(soft_float_to_double(f)) == f) covers 13 sample bit-patterns spanning normal, subnormal, FLT_MIN/MAX, +/-zero, +/-inffl::numbersaturation + sign-extension test covers payload-boundary values🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
fl::number— a compact 64-bit numeric type for efficient storage of signed integers, unsigned integers, and floats.Tests