Skip to content

filters: multiply by the high word alone when biquad coefficients fit - #1254

Merged
dpwe merged 1 commit into
shorepine:mainfrom
rt-rtos:perf/biquad-high-word-multiply
Nov 7, 2026
Merged

dpwe merged 1 commit into
shorepine:mainfrom
rt-rtos:perf/biquad-high-word-multiply

Conversation

@rt-rtos

@rt-rtos rt-rtos commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

When every coefficient of a biquad is below 0.5, its loop now takes one high-word
multiply per product. Six filtered saws render 11 % faster on an ESP32-S3. Output does
not change.

Context

SMUL64R shifts by 23, so each product needs both words of the multiply plus shifts to
join them. A biquad coefficient is fixed for the block, so it can carry the shift:
mulh(c << 9, x) equals (c * x) >> 23 while c << 9 fits, which is |c| < 0.5.

Each block checks its coefficients once and picks one of two copies of the loop. The
loops moved into macros because of that second copy, as with RENDER_LUT_GUTS; the
statements are unchanged apart from the multiply and coef[0..2] becoming locals.
amy.h also builds on MSVC, which rules out a forced-inline function. EQ_MULH now
points at FILT_MULH, the same expression.

This covers LPF and BPF up to about 2.5 kHz at Q 0.7, 4 kHz at Q 2 and 5 kHz at Q 8.
HPF and notch never fit, because b0 stays near 1. They stay on the existing loop, as
does anything without AMY_HAS_MUL64. Only the two kernels the filter path calls
changed; the phaser allpass could follow.

A single loop at a smaller shift would cover every coefficient, but the dropped bits go
through the feedback. With bias correction, an LPF at 100 Hz, Q 0.7 has -48.6 dBFS of
error against a double-precision filter, where main has -95.7.

Verification

ESP32-S3 at 240 MHz on v1.2.200, all loops in one image. Six saws, cycles per 256-sample
block, of a 1,280,000 budget:

Filter, 1 kHz, Q 0.7 Before After Change
LPF 236,755 210,792 -11.0 %
LPF24 260,958 231,918 -11.1 %
HPF (fallback) 236,821 236,860 +0.02 %

Instructions in the per-sample loop when coefficients fit, -O2, old -> new:

Target Biquad LPF24
ESP32-S3 41 -> 21 61 -> 41
ESP32-P4 (RV32) 44 -> 21 61 -> 38
Cortex-M33 38 -> 23 65 -> 44
Cortex-M7 42 -> 24 58 -> 45
x86-64 34 -> 29 46 -> 47

On the ESP32-S3 the twofunctions grow from 184 to 357 and 262 to 447 bytes;
the second is AMY_IRAM_ATTR.

A host test compared filters.c from main and this branch over 614 M samples, including
coefficients around the boundary, with no mismatches.
Deliberately loosening the fit test to |c| < 1, where the shift overflows,
gives 802,018 mismatched blocks of 2.4 M, so the test catches a wrong boundary.

make test: identical to main

The biquad loops spend most of their instructions joining the two words of
each 64-bit product, because SMUL64R's shift by S_FRAC_BITS does not land on
a word boundary. A coefficient is fixed for the whole block, so it can carry
that shift instead: mulh(c << 9, x) equals (c * x) >> 23 for any x, as long
as c << 9 still fits, which is |c| < 0.5.

Each block checks its coefficients once and runs one of two copies of the
loop, written once as a macro. Output does not change. Coefficients at or
above 0.5, which includes b0 of a highpass or notch at any cutoff, stay on
the existing loop, as does anything without AMY_HAS_MUL64.

FILT_MULH is the expression EQ_MULH already used, so the 64-bit EQ_MULH now
points at it.
@dpwe
dpwe merged commit a324d2e into shorepine:main Nov 7, 2026
13 checks passed
@bwhitman

bwhitman commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

⛓️ tulipcc integration PR opened

This merge was pinned into tulipcc for full-system CI: shorepine/tulipcc#1420

Test it there and merge that PR to move tulipcc onto this AMY.

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.

3 participants