FastLED 3.9.15
Loading...
Searching...
No Matches
ieee754_string.h
Go to the documentation of this file.
1#pragma once
2
4// Integer-only string <-> IEEE 754 single-precision codec.
5//
6// Motivation: on no-FPU ARM (Cortex-M0+ / LPC845 etc.) the libgcc soft-FP
7// helpers (__aeabi_d* / __aeabi_f* and their ~7 KB transitive closure) get
8// pulled into any link that runs decimal arithmetic through `float` or
9// `double` operators. FastLED #3022 (and the gap-tracking issue #3029) ask
10// the library to never structurally reach those helpers from the JSON
11// parser / serializer or the variant's float-vector storage path.
12//
13// This codec satisfies that constraint:
14// * Parse: digits feed an integer mantissa, scaled by a precomputed table
15// of `10^k` in 64-bit-mantissa + binary-exponent form. The widening
16// multiply that follows uses only 32x32->64 native ops (no `__aeabi_d*`).
17// * Format: mantissa / exponent are unpacked from the IEEE-754 bit
18// pattern via shifts and masks; decimal digits emerge from integer
19// scaling against the same `10^k` table.
20//
21// API surface deliberately works on `fl::u32` bit-patterns (not `float`).
22// The caller can `fl::bit_cast<float>(bits)` at the API boundary -- that is
23// a `memcpy` and never anchors libgcc. Doing the conversion *inside* this
24// codec would defeat the point.
25//
26// Precision target: ~6 significant decimal digits with at-most +/-1 ULP
27// drift from `strtof` / `snprintf("%g")` at the edges. Sufficient for
28// FastLED's JSON-RPC use case (brightness, ratios, coordinates). Callers
29// that need bit-exact round-trip on hosts should use the platform `strtof`
30// / `snprintf` directly -- those are *intentionally* not wired through this
31// header.
32//
33// See FastLED #3022 (parent), #3029 (gap), #3027 (superseded gate PR).
35
36#include "fl/stl/stdint.h"
37#include "fl/stl/cstddef.h"
38#include "fl/stl/noexcept.h"
39
40namespace fl {
41
42// Forward declaration to avoid pulling fl/stl/string.h here.
43class string;
44
66u32 ieee754_parse_decimal(const char* s, fl::size len, fl::size* consumed = nullptr) FL_NOEXCEPT;
67
81fl::string ieee754_format_decimal(u32 bits, int precision = 6) FL_NOEXCEPT;
82
83} // namespace fl
fl::string ieee754_format_decimal(u32 bits, int precision) FL_NOEXCEPT
Format IEEE 754 single-precision bits as decimal text.
u32 ieee754_parse_decimal(const char *s, fl::size len, fl::size *consumed) FL_NOEXCEPT
Parse a decimal floating-point number into IEEE 754 single-precision bits.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT