Skip to content

Commit cfe8b91

Browse files
zackeesclaude
andcommitted
feat(json): remove float truncation fallback + add fixed_point setter (closes #3029)
Phase 1.5 of #3022: tighten the FL_JSON_HAS_FLOAT=0 contract so the FastLED library never reaches an IEEE-754 helper on Low-memory targets, regardless of which sketch is linked. src/fl/stl/json.h: * Drop the #else truncation fallback in json(float/double), operator=(float/double), and set(key, float/double). On FL_JSON_HAS_FLOAT=0 these are now SFINAE-templated static_assert overloads that name the migration path (fl::fixed_point<>) — a silent static_cast<i64>(f) inside the library was pulling __aeabi_f2iz / __aeabi_d2iz (~64B each) into the link at every call site, even when the user only ever wanted integer storage. * Add fl::json ↔ fl::fixed_point setter / extractor API. Our number format is itself a super fixed point — the i64 variant slot is the natural carrier for any Q-format value, so the new templated json(FP), operator=(FP), set(key, FP), and as_fixed_point<FP>() overloads pack/unpack the raw integer representation directly. SFINAE detects fixed_point-shaped types via INT_BITS / FRAC_BITS / raw() / from_raw() member probes (works for s16x16, s0x32, u24x8, s12x4, and so on — see src/fl/math/fixed_point/). src/platforms/shared/ui/json/{slider,number_field}.cpp.hpp: * Wrap whole-TU bodies in #if FL_JSON_HAS_FLOAT. JsonSliderImpl and JsonNumberFieldImpl carry intrinsically-float value/min/max/step storage and serialize via json.set(key, float); gating the TU keeps the soft-FP cascade out of the library archive on Low-memory builds. Sketches that don't instantiate these widgets see zero impact; sketches that try to fail at link with `undefined reference`, mirroring the existing FASTLED_NO_JSON ergonomics in ScreenMap. Phase-2 plan tracked under #3022: add fixed-point widget variants (JsonSliderImplFixed<Q16x16>) so Low-memory sketches still have working sliders. src/platforms/shared/ui/json/json_console.cpp.hpp: * Tighten the existing SKETCH_HAS_LARGE_MEMORY gate to SKETCH_HAS_LARGE_MEMORY && FL_JSON_HAS_FLOAT. JsonConsole parses serial input through strtod and routes float values into json::set(key, float); the secondary gate makes the TU drop out under user opt-out via -DFL_JSON_HAS_FLOAT=0 on a Large-memory part too. Note on local verification: lint passes; the worktree's `bash test` runner is blocked by a Windows-worktree meson_native.txt lifecycle race against ninja regeneration (file gets purged between meson setup and the first build action) — relying on PR CI for the test/compile matrix. Closes #3029. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 2463111 commit cfe8b91

4 files changed

Lines changed: 135 additions & 31 deletions

File tree

‎src/fl/stl/json.h‎

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,34 @@
124124
// Implementation details - users should not rely on these directly
125125
#include "fl/stl/json/types.h"
126126
#include "fl/stl/noexcept.h"
127+
#include "fl/stl/static_assert.h"
127128

128129
namespace fl {
129130

131+
namespace detail {
132+
133+
// SFINAE trait — true for fl::fixed_point<>-shaped types (s16x16, s0x32,
134+
// u24x8, ...). Used by fl::json setter / extractor templates so a
135+
// fixed_point value flows through the integer storage path and never
136+
// pulls IEEE-754 helpers into the link (FastLED #3022 / #3029). The
137+
// detection probes the public members every fixed_point type exposes:
138+
// - `INT_BITS` / `FRAC_BITS` — static layout constants
139+
// - `raw()` — read the underlying integer repr
140+
// - `from_raw(raw_type)` — rebuild the value from that repr
141+
template <typename T, typename = void>
142+
struct is_fl_fixed_point : fl::false_type {};
143+
144+
template <typename T>
145+
struct is_fl_fixed_point<T, decltype(
146+
static_cast<void>(T::INT_BITS),
147+
static_cast<void>(T::FRAC_BITS),
148+
static_cast<void>(fl::declval<const T&>().raw()),
149+
static_cast<void>(T::from_raw(fl::declval<const T&>().raw())),
150+
void()
151+
)> : fl::true_type {};
152+
153+
} // namespace detail
154+
130155
class json {
131156
private:
132157
fl::shared_ptr<json_value> mValue;
@@ -142,20 +167,37 @@ class json {
142167
json(float f) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(f)) {}
143168
json(double d) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<float>(d))) {}
144169
#else
145-
// FL_JSON_HAS_FLOAT==0 (FastLED #3022): the variant carries no float
146-
// alternative, but the user-facing constructor / setter API still
147-
// accepts `float` and `double` so caller code (UI widgets, ScreenMap,
148-
// diagnostics, etc.) keeps compiling unchanged. The library takes
149-
// over the conversion here — values are truncated to `i64` storage
150-
// via `static_cast`. Cost at the call site is a single
151-
// `__aeabi_f2iz` / `__aeabi_d2iz` helper (~64B, no chained
152-
// soft-double cascade); the linker drops it under `--gc-sections`
153-
// for sketches that never invoke these constructors.
154-
// TODO(#3022 phase 2): replace truncation with `json_number` Q-format
155-
// tags so the fractional part is preserved.
156-
json(float f) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(f))) {}
157-
json(double d) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(d))) {}
170+
// FL_JSON_HAS_FLOAT==0 (FastLED #3029): the library refuses to perform
171+
// a silent float→i64 truncation here. Pulling `__aeabi_f2iz` /
172+
// `__aeabi_d2iz` into the link is the user's call to make at the call
173+
// site, not the library's; the mandate from #3022 is that the FastLED
174+
// library code path is structurally free of soft-FP. Callers that need
175+
// a fractional value must convert through `fl::fixed_point<>` (see
176+
// `src/fl/math/fixed_point/`, e.g. `fl::s16x16`) — the templated
177+
// fixed-point constructor below packs the raw integer representation
178+
// and never touches IEEE-754. Callers that genuinely want integer
179+
// truncation should `static_cast<i64>(v)` at the call site.
180+
template <typename T, typename fl::enable_if<fl::is_floating_point<T>::value, int>::type = 0>
181+
json(T) FL_NOEXCEPT {
182+
FL_STATIC_ASSERT(sizeof(T) == 0,
183+
"fl::json: FL_JSON_HAS_FLOAT=0 — float/double values cannot be "
184+
"stored on this build (FastLED #3022 / #3029). Convert to "
185+
"fl::fixed_point<> (e.g. fl::s16x16 from fl/math/fixed_point/) "
186+
"and pass that, or static_cast<i64>(v) for explicit integer "
187+
"truncation at the call site.");
188+
}
158189
#endif // FL_JSON_HAS_FLOAT
190+
// fl::fixed_point<> integration (FastLED #3029, phase 1 of #3022).
191+
// Our number format is a "super fixed point" — fl::json's i64 storage
192+
// is also the natural carrier for any Q-format fixed-point value, so
193+
// this overload packs the raw integer representation directly into
194+
// the i64 variant slot with zero IEEE-754 conversion. The Q-format
195+
// (IntBits / FracBits / signedness) is encoded in the C++ type at
196+
// the extraction site, not in the JSON value — the round-trip is
197+
// lossless as long as the reader and writer agree on the type.
198+
// Use as_fixed_point<FP>() below to read it back.
199+
template <typename FP, typename fl::enable_if<detail::is_fl_fixed_point<FP>::value, int>::type = 0>
200+
json(FP v) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(static_cast<i64>(v.raw()))) {}
159201
json(const fl::string& s) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(s)) {}
160202
json(const char* s) FL_NOEXCEPT : json(fl::string(s)) {}
161203
json(json_array a) FL_NOEXCEPT : mValue(fl::make_shared<json_value>(fl::move(a))) {}
@@ -233,19 +275,23 @@ class json {
233275
return *this;
234276
}
235277
#else
236-
// FL_JSON_HAS_FLOAT==0: library-owned float→i64 truncation, see
237-
// json(float)/json(double) constructors above. Same semantics here.
238-
json& operator=(float value) FL_NOEXCEPT {
239-
mValue = fl::make_shared<json_value>(static_cast<i64>(value));
278+
// FL_JSON_HAS_FLOAT==0: see json(float)/json(double) constructors above.
279+
// Refuses silent narrowing; routes callers to fl::fixed_point<>.
280+
template <typename T, typename fl::enable_if<fl::is_floating_point<T>::value, int>::type = 0>
281+
json& operator=(T) FL_NOEXCEPT {
282+
// FL_JSON_HAS_FLOAT=0: float/double assignment refused on Low-memory builds (FastLED #3022/#3029). Convert to fl::fixed_point<> (e.g. fl::s16x16), or static_cast<i64>(v).
283+
FL_STATIC_ASSERT(sizeof(T) == 0, "fl::json: FL_JSON_HAS_FLOAT=0 forbids float/double assignment; see comment above");
240284
return *this;
241285
}
286+
#endif // FL_JSON_HAS_FLOAT
242287

243-
json& operator=(double value) FL_NOEXCEPT {
244-
mValue = fl::make_shared<json_value>(static_cast<i64>(value));
288+
// fl::fixed_point<> assignment — see matching constructor above.
289+
template <typename FP, typename fl::enable_if<detail::is_fl_fixed_point<FP>::value, int>::type = 0>
290+
json& operator=(FP v) FL_NOEXCEPT {
291+
mValue = fl::make_shared<json_value>(static_cast<i64>(v.raw()));
245292
return *this;
246293
}
247-
#endif // FL_JSON_HAS_FLOAT
248-
294+
249295
json& operator=(const fl::string& value) FL_NOEXCEPT {
250296
mValue = fl::make_shared<json_value>(value);
251297
return *this;
@@ -312,9 +358,22 @@ class json {
312358
template<typename FloatType>
313359
fl::optional<FloatType> as_float() const FL_NOEXCEPT {
314360
if (!mValue) return fl::nullopt;
315-
return mValue->template as_float<FloatType>();
361+
return mValue->template as_float<FloatType>();
316362
}
317-
363+
364+
// fl::fixed_point<> extractor — reads the i64 storage back and
365+
// rebuilds the typed value via FP::from_raw. Pure-integer path; no
366+
// IEEE-754 helper is reachable from here. See the matching setter
367+
// template `json(FP)` above and FastLED #3022 / #3029.
368+
template <typename FP>
369+
typename fl::enable_if<detail::is_fl_fixed_point<FP>::value, fl::optional<FP>>::type
370+
as_fixed_point() const FL_NOEXCEPT {
371+
if (!mValue) return fl::nullopt;
372+
auto i = mValue->as_int();
373+
if (!i) return fl::nullopt;
374+
return FP::from_raw(static_cast<decltype(FP().raw())>(*i));
375+
}
376+
318377
fl::optional<fl::string> as_string() const FL_NOEXCEPT {
319378
if (!mValue) return fl::nullopt;
320379
return mValue->as_string();
@@ -757,11 +816,27 @@ class json {
757816
void set(const fl::string& key, bool value) FL_NOEXCEPT { set(key, json(value)); }
758817
void set(const fl::string& key, int value) FL_NOEXCEPT { set(key, json(value)); }
759818
void set(const fl::string& key, i64 value) FL_NOEXCEPT { set(key, json(value)); }
760-
// float / double setters: on FL_JSON_HAS_FLOAT==0 the `json(value)`
761-
// constructors above truncate to i64 storage so this API keeps
762-
// compiling and serializing for caller code (UI widgets, etc.).
819+
#if FL_JSON_HAS_FLOAT
763820
void set(const fl::string& key, float value) FL_NOEXCEPT { set(key, json(value)); }
764821
void set(const fl::string& key, double value) FL_NOEXCEPT { set(key, json(value)); }
822+
#else
823+
// FL_JSON_HAS_FLOAT==0: see json(float)/json(double) constructors above.
824+
// Refuses silent narrowing; routes callers to fl::fixed_point<>.
825+
template <typename T, typename fl::enable_if<fl::is_floating_point<T>::value, int>::type = 0>
826+
void set(const fl::string&, T) FL_NOEXCEPT {
827+
FL_STATIC_ASSERT(sizeof(T) == 0,
828+
"fl::json: FL_JSON_HAS_FLOAT=0 — json::set(key, float|double) "
829+
"is refused on Low-memory builds (FastLED #3022 / #3029). "
830+
"Convert to fl::fixed_point<> (e.g. fl::s16x16) and call "
831+
"set(key, fixed_point_value), or static_cast<i64>(v) for "
832+
"explicit integer truncation.");
833+
}
834+
#endif // FL_JSON_HAS_FLOAT
835+
836+
// fl::fixed_point<> setter — packs the raw integer representation
837+
// into the i64 variant slot. See json(FP) constructor above.
838+
template <typename FP, typename fl::enable_if<detail::is_fl_fixed_point<FP>::value, int>::type = 0>
839+
void set(const fl::string& key, FP value) FL_NOEXCEPT { set(key, json(value)); }
765840
void set(const fl::string& key, const fl::string& value) FL_NOEXCEPT { set(key, json(value)); }
766841
void set(const fl::string& key, const char* value) FL_NOEXCEPT { set(key, json(value)); }
767842
template<typename T, typename = fl::enable_if_t<fl::is_same<T, char>::value>>

‎src/platforms/shared/ui/json/json_console.cpp.hpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
// IWYU pragma: private
22

33
#include "fl/system/sketch_macros.h"
4+
#include "fl/stl/json/types.h" // FL_JSON_HAS_FLOAT (FastLED #3022 / #3029)
45

5-
#if SKETCH_HAS_LARGE_MEMORY
6+
// JsonConsole parses serial input through `strtod` and routes the
7+
// resulting `float` into `json::set(key, float)` (see parseCommand /
8+
// setSliderValue below). On FL_JSON_HAS_FLOAT==0 those setters are
9+
// refused (see fl/stl/json.h), and the parse path itself is soft-FP, so
10+
// the entire TU drops out on Low-memory builds — the existing
11+
// SKETCH_HAS_LARGE_MEMORY gate already covers the default case, and
12+
// adding FL_JSON_HAS_FLOAT makes the gate hold under user opt-out via
13+
// `-DFL_JSON_HAS_FLOAT=0` on a Large-memory part too.
14+
#if SKETCH_HAS_LARGE_MEMORY && FL_JSON_HAS_FLOAT
615

716

817
#include "platforms/shared/ui/json/json_console.h"
918
#include "fl/log/log.h"
1019
#include "fl/stl/json.h"
11-
#include "fl/stl/json.h"
1220
#include "fl/stl/algorithm.h"
1321
#include "fl/stl/stdint.h"
1422
#include "platforms/shared/ui/json/ui.h"
@@ -349,4 +357,4 @@ void JsonConsole::dump(fl::sstream& out) FL_NOEXCEPT {
349357

350358
} // namespace fl
351359

352-
#endif // SKETCH_HAS_LARGE_MEMORY
360+
#endif // SKETCH_HAS_LARGE_MEMORY && FL_JSON_HAS_FLOAT

‎src/platforms/shared/ui/json/number_field.cpp.hpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
// IWYU pragma: private
22

3-
#include "fl/stl/json.h"
43
#include "fl/stl/json.h"
54
#include "fl/math/math.h"
65
#include "platforms/shared/ui/json/number_field.h"
76
#include "platforms/shared/ui/json/ui_internal.h"
87
#include "platforms/shared/ui/json/ui.h"
98
#include "fl/stl/noexcept.h"
9+
10+
// FL_JSON_HAS_FLOAT==0 (FastLED #3029): JsonNumberFieldImpl carries
11+
// `float mValue/mMin/mMax` storage and serializes via
12+
// `json.set(key, float)`. Gating the whole TU keeps soft-FP out of the
13+
// library archive on Low-memory builds. See slider.cpp.hpp for the same
14+
// pattern and the phase-2 fixed-point variant tracked under #3022.
15+
#if FL_JSON_HAS_FLOAT
16+
1017
namespace fl {
1118

1219
// Definition of the internal class that was previously in number_field_internal.h
@@ -131,3 +138,5 @@ bool JsonNumberFieldImpl::operator!=(float v) const FL_NOEXCEPT { return !fl::al
131138
bool JsonNumberFieldImpl::operator!=(int v) const FL_NOEXCEPT { return !fl::almost_equal(value(), static_cast<float>(v)); }
132139

133140
} // namespace fl
141+
142+
#endif // FL_JSON_HAS_FLOAT

‎src/platforms/shared/ui/json/slider.cpp.hpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
#include "platforms/shared/ui/json/slider.h"
44
#include "fl/stl/json.h"
5-
#include "fl/stl/json.h"
65
#include "fl/math/math.h"
76
#include "platforms/shared/ui/json/ui.h"
87

98
#include "fl/stl/compiler_control.h"
109
#include "fl/stl/noexcept.h"
1110

11+
// FL_JSON_HAS_FLOAT==0 (FastLED #3029): the slider widget's value/min/max/
12+
// step are intrinsically `float`. Gating the whole TU keeps the soft-FP
13+
// cascade out of the library archive on Low-memory builds — a sketch
14+
// that does not use a slider sees zero impact; a sketch that *does* try
15+
// to instantiate JsonSliderImpl fails at link with `undefined reference`,
16+
// mirroring the FASTLED_NO_JSON ergonomics in ScreenMap. Phase-2 plan
17+
// (per #3022 / #3029): add a JsonSliderImplFixed<Q16x16>-style variant
18+
// so Low-memory sketches still have a working slider, just one whose
19+
// value type is fl::s16x16.
20+
#if FL_JSON_HAS_FLOAT
21+
1222
FL_DISABLE_WARNING(deprecated-declarations)
1323
namespace fl {
1424

@@ -150,3 +160,5 @@ int JsonSliderImpl::id() const FL_NOEXCEPT {
150160
}
151161

152162
} // namespace fl
163+
164+
#endif // FL_JSON_HAS_FLOAT

0 commit comments

Comments
 (0)