Skip to content

audiograph: round a sample with two adds instead of a branchy helper - #359

Merged
bdbarnett merged 1 commit into
mainfrom
perf/graph-p4
Oct 31, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
perf/graph-p4

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

Every sample every graph node writes is rounded by sat16. Since #314 that went through round_even, a dozen instructions and two branches per sample. This rounds a clamped sample by adding and subtracting 1.5 × 2^23, which gives the same answer.

Same bytes: all 2^32 float bit patterns give the same int16 through the old and new sat16; a ties-away version planted in that check differs on 32,770 of them. Every graph probe (graph_parity, graph_nodes, graph_source, graph_carry, graph_probe) and both ladder probes print identical output on CPython before and after. On an ESP32-P4, renders of five instruments (Poly106, Poly6, Mono101, Poly8, PolyA6) hash the same before and after.

Faster: on the ESP32-P4 at 128-frame blocks, Poly106 with six notes held spends 23.12 ms per 1024 frames in graph.run, down from 24.65. Per block: a two-input mix 19.2 µs (was 23.7), noise 14.4 (19.6), the VCA 13.2 (17.3), the LFO 29.5 (34.3).

A target that evaluates floats wider than float (FLT_EVAL_METHOD other than 0, as x87 does) keeps round_even.

Every sample every node writes goes through sat16, and since #314 its
rounding was round_even: a dozen instructions and two branches, there so
RP2350 CircuitPython links without lrintf. Adding and subtracting 1.5 * 2^23
rounds a clamped sample to nearest, ties to even, exactly as round_even does:
all 2^32 float bit patterns give the same int16 either way, and a
ties-away version planted in the same check differs on 32,770 of them.

On the P4 DEV-KIT (128-frame blocks, Poly106 with six notes held), graph.run
goes from 24.65 to 23.12 ms per 1024 frames; a two-input mix from 23.7 to
19.2 us a block, noise from 19.6 to 14.4, the VCA from 17.3 to 13.2. The
board's renders of Poly106, Poly6, Mono101, Poly8 and PolyA6 hash the same
before and after, and every graph probe prints the same bytes on CPython.

A target that evaluates float wider than float (x87) keeps round_even.
@bdbarnett
bdbarnett merged commit 95fc8e8 into main Oct 31, 2026
31 checks passed
@bdbarnett
bdbarnett deleted the perf/graph-p4 branch October 31, 2026 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant