Skip to content

feat(radio): eliminate double-precision math from firmware#7257

Open
raphaelcoeffic wants to merge 2 commits into
mainfrom
float-conversion
Open

feat(radio): eliminate double-precision math from firmware#7257
raphaelcoeffic wants to merge 2 commits into
mainfrom
float-conversion

Conversation

@raphaelcoeffic

@raphaelcoeffic raphaelcoeffic commented Apr 4, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace newlib's double-precision float conversion chain (strtod, _dtoa_r, mprec Bignum) and printf float formatting with lightweight float-only alternatives
  • Lua is configured with LUA_32BITS (single-precision float) and STM32 MCUs only have single-precision FPU — double was purely parasitic overhead
  • Replace sscanf in theme_manager.cpp with strtol parsing — on non-nano (color) targets, sscanf pulled in the full svfscanf_strtod_r chain even for integer-only formats

Flash savings (text section)

Target MCU Before After Savings
X7 STM32F2 (Cortex-M3, no FPU, nano libc) 505,192 492,708 -12.2 KB
X9D+2019 STM32F4 (Cortex-M4, HW FPU, nano libc) 523,776 512,324 -11.2 KB
TX16S STM32F4 (Cortex-M4, HW FPU, full libc) 1,605,552 1,547,308 -56.9 KB

Changes

  • eyalroz/printf v6.3.0 with PRINTF_USE_DOUBLE_INTERNALLY=0 replaces newlib's snprintf/sprintf/vsnprintf via strong symbol override
  • Custom float-only strtof replaces newlib's strtof (which internally called strtod + d2f) via --wrap linker option
  • Lua luaconf.h: removed unconditional strtod override (restore strtof for LUA_FLOAT_FLOAT), disabled lua_number2strx macro so Lua's native hex float formatter is used instead of snprintf %a
  • Lua math.random: rewritten with bit-manipulation float for [0,1) and integer-only scaling with rejection sampling for [low,up] — eliminates last double-precision ops from Lua
  • -Wdouble-promotion added to catch future regressions
  • Fixed double-promotion in 14 source files: f-suffix literals, sinf/powf/fmodf/fabsf/sqrtf variants, explicit casts for variadic functions
  • 6 new Lua float behavior regression tests: tonumber, tostring, string.format, math.random, string.pack, arithmetic round-trips

Test plan

  • All 12 Lua tests pass (6 existing + 6 new float behavior tests)
  • X7, X9D+2019, TX16S firmware builds succeed
  • Verify Lua scripts behave identically on radio hardware
  • Verify string.format output matches expectations for common patterns (%f, %g, %e, %a)
  • Test math.random() distribution on radio

raphaelcoeffic and others added 2 commits April 4, 2026 14:32
Replace newlib's double-precision float conversion chain (strtod, _dtoa_r,
mprec Bignum) and printf float formatting with lightweight float-only
alternatives. Lua is configured with LUA_32BITS (single-precision float)
and STM32 MCUs only have single-precision FPU, so double was purely
parasitic overhead.

Changes:
- Add eyalroz/printf v6.3.0 with PRINTF_USE_DOUBLE_INTERNALLY=0
- Add custom float-only strtof (replaces newlib's strtod-based strtof)
- Remove -u _printf_float / -u _scanf_float linker flags
- Remove luaconf.h strtod override (restore strtof for LUA_32BITS)
- Use Lua's native hex float formatter instead of snprintf %a
- Rewrite math.random: bit-manipulation float for [0,1), integer
  scaling with rejection sampling for [low,up]
- Fix double-promotion warnings: f-suffix literals, sinf/powf/fmodf/
  fabsf/sqrtf, explicit casts for variadic functions
- Add -Wdouble-promotion to catch future regressions
- Add 6 Lua float behavior regression tests

X7 (Cortex-M3): 507112 -> 492708 bytes text (-14404 bytes)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…chain

theme_manager.cpp was the sole sscanf user in firmware. On non-nano targets
(color LCD), sscanf pulls in full svfscanf → _strtod_r → strtod.o → mprec,
dragging ~47KB of double-precision code even though only %i (integer) format
was used.

Replace with strtol-based parsing for "RGB(%i,%i,%i)" format.

X10: 1599084 -> 1551804 bytes text (-47280 bytes)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@raphaelcoeffic raphaelcoeffic changed the title Eliminate double-precision math from firmware feat(radio): eliminate double-precision math from firmware Apr 4, 2026
@raphaelcoeffic raphaelcoeffic added this to the 3.0 milestone Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant