Skip to content

upstream request: minimp3-fixed point arithmetic - #153

Open
zackees wants to merge 5 commits into
lieff:masterfrom
zackees:master
Open

zackees wants to merge 5 commits into
lieff:masterfrom
zackees:master

Conversation

@zackees

@zackees zackees commented Sep 2, 2026 •

Copy link
Copy Markdown

A fixed-point fork of minimp3, forked at
ea99364 and rewritten to decode MPEG Layer III in 32-bit integer arithmetic
with no FPU, no dynamic allocation, and a bounded stack — while holding
accuracy far above the ISO floor.

benchmark-accuracy

+14 dB accuracy vs LibHelix, +13% performance penalty

It is the decoder that ships in FastLED,
and this repository is where it gets optimised. FastLED vendors five files from
here verbatim; tools/check_vendor_sync.py proves they have not drifted.

…pent

Forked from lieff/minimp3 at ea99364 and rewritten to decode MPEG Layer III
in 32-bit integer arithmetic -- no FPU, no allocation, bounded stack. On an
ESP32-C6 at 160 MHz the decode went from 142,647 us to 39,385 us, which is
-72%, and the gap to Helix from 3.99x to 1.13x. Accuracy did not move: 123.24
dB throughout, against an ISO floor of 60 and Helix's own 102.87.

The two arithmetic wins come from one observation. On a 32-bit target the high
half of a product is a single instruction -- mulh on RISC-V, smull on ARM --
so any rounding expressible up there is free. DCT-32's coefficients are
compile-time constants, so its rounding moves to bit 32 exactly; the general
case cannot do that, but folding the rounding into the low half
(hi << (32-S)) + (((lo >> (S-1)) + 1) >> 1) makes the carry structurally
impossible and costs seven instructions where the obvious spelling costs nine.
Both identities are written out in port/platforms/int_asm.h and pinned by a
codegen test, because the idiom is only fast while the compiler recognises it
and when recognition lapses it lapses silently -- 2 instructions became 40,
with the right answer every time.

What this repository adds beyond the decoder:

- tools/conformance.py runs the 83 upstream vectors that came with the fork.
  73 pass and 0 fail on x86-64, riscv32 and arm, with an identical PCM
  checksum on all three. Exceptions are declared with exact bounds rather than
  by name, because a decoder can emit a correct prefix and stop and a PSNR
  over the shared prefix will not notice. Every bound is identical on the
  float build, which is the evidence it belongs to the vector and not to the
  fixed-point port.
- QEMU cross-checking, so bit-exactness on another ISA needs no hardware. It
  is a correctness instrument and not a stopwatch: wall clock under qemu-user
  measures the JIT.
- tools/decode.cpp strips ID3v2, ID3v1 and APE containers, which is why four
  vectors that decode to nothing at all in a bare codec now score 105.24 dB.
- tools/codesize.py for riscv32 and arm .text with a baseline diff.
- tools/check_vendor_sync.py, which proves the five shared files are
  byte-identical to FastLED's copies. That drift would be silent and would
  make every measurement here meaningless for the thing that ships.
- helix/, the benchmark reference, under its own separate and non-permissive
  RealNetworks licence and clearly marked as such. A number quoted against a
  decoder nobody can run is not a measurement.

The README carries the measurement doctrine, which is the hardest-won part:
static size bounds a win but cannot rank changes, and a 64-bit host cannot
rank changes for a 32-bit target. Four recorded cases where it got the sign or
the magnitude wrong are tabulated there, including a polyphase restructure the
host called a 3.2% regression and the device measured at -9.7%.

Upstream's C test harness and scripts no longer build: the decoder is C++ now
and templates and inline attributes are load-bearing. They are kept for
reference and tools/conformance.py is the entry point.
Adds the measurement and publication path this repository was missing: a
number you can watch move, on a page, without owning the hardware.

The metric that did not exist before is an executed instruction count. A
QEMU TCG plugin (tools/qemu-plugin/opcount.c) accumulates each translation
block's instruction count at translation time, so the executed total is
exact while costing one add per block. Decoding l3-hecommon is 23,918,711
riscv32 instructions, and it is 23,918,711 every time, on any machine.

That exactness is the point. Static .text bounds a win but cannot rank
changes, the host's Callgrind counts x86-64 instructions for a decoder that
ships to 32-bit targets and has been wrong about both sign and magnitude,
and wall clock under qemu-user measures the JIT. An instruction count on the
target ISA has none of those problems and no run-to-run variance either, so
a 0.5% move is a real 0.5% rather than something to average away over
fifteen repetitions. What it cannot do is predict hardware time -- it knows
nothing about cache, pipeline or multiplier latency -- so it ranks
candidates and real silicon stays the timing authority. Both READMEs say so.

Publication follows the shape mimalloc-pprof uses: a benchmark-stats branch
served as GitHub Pages at the root, carrying .nojekyll, index.html, the
SVGs, an append-only history.jsonl, latest.json for the most recent run, and
manifest.json with a sha256 and a role for every artifact so a chart can be
tied back to the envelope that produced it. The README embeds the SVGs from
that branch through raw.githubusercontent.com, so master carries no
generated artifacts and the images track the latest run on their own.

The charts are written by hand rather than by a plotting library. CI then
needs no dependency beyond the standard library, and -- more usefully -- a
hand-written SVG can carry its own prefers-color-scheme stylesheet, so the
same file is legible on GitHub in light and dark mode. A library-generated
chart bakes in one background and looks broken in the other.

The workflow runs the whole conformance suite, on the host and under QEMU
for both ISAs, before it publishes anything. A benchmark for a decoder that
stopped decoding correctly is worse than no benchmark.
A flat series and a halved one look identical when the axis auto-fits, so
the line carried no magnitude at all. Labels the axis bounds and the latest
value.
The static assert lived in the riscv32 branch only, but the portable form
is just as undefined outside [1, 31] -- with Shift == 0, 1 << (Shift - 1)
is a negative shift count. Mirrors FastLED, which is the contract
tools/check_vendor_sync.py enforces.
@zackees zackees changed the title upstream stream request: minimp3-fixed point arithmetic upstream request: minimp3-fixed point arithmetic Sep 3, 2026
#1)

Every workflow now carries a top-level concurrency group so a new push to a
branch or PR cancels the superseded run instead of queuing behind it in the
shared account-wide runner pool. Runs on master get a unique group (run_id) so
they are never cancelled and never coalesced; release/publish workflows are
likewise never cancelled. Reusable (workflow_call) workflows are untouched
because the caller's group governs them; constant cross-ref groups are left
as deliberate serialization.

1 workflow files changed.


Claude-Session: https://claude.ai/code/session_01Vf5U1eUfawCVRTVnJSF2He

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
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