Skip to content

fix(filters): accumulate on fxp and move to caller-owned storage - #6

Merged
aajll merged 1 commit into
masterfrom
5-fix-filters-fxp
Oct 14, 2026
Merged

aajll merged 1 commit into
masterfrom
5-fix-filters-fxp

Conversation

@aajll

@aajll aajll commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Refs #5

Layer 1 of 4. Ports the FIR and biquad filters onto fxp and moves them to caller-owned storage.

Why

Both filters summed int64_t products unchecked and narrowed with acc >> 16. Two INT32_MIN operands overflow that sum (undefined behaviour), and the shift of a negative sum is implementation-defined and floors. Accumulating with fxp_acc_mac_chk and narrowing with fxp_acc_narrow_chk makes overflow saturate and report FPC_STATUS_SATURATED, and narrowing follows the build-wide FXP_ROUNDING.

Caller-owned structs replace the pool so the filters keep no global state and need no capacity configuration; this matches rampg and debounce.

Changes

  • subprojects/fxp.wrap pins fxp at 24315b4, which includes fix: take uint_least8_t in fxp_rescale for CHAR_BIT == 16 targets fxp#3 (fxp_rescale takes uint_least8_t, so fxp.h compiles where CHAR_BIT == 16); fpc_dep carries fxp_dep because fxp_t is in the public API.
  • A c2000 CI job builds the library and its subprojects with -Dwerror=true on TI C2000 CGT 22.6.2 and 25.11.1, under --abi=coffabi and --abi=eabi. Installers are SHA-256 pinned and the toolchain cached.
  • include/fpc_filters.h: public struct fpc_fir and struct fpc_biquad; FIR history is a caller-provided buffer of order elements. init, reset, set_config and process; no deinit.
  • Biquad a1/a2 == FXP_MIN is rejected so that their negation is exact. A zero biquad is a valid all-zero filter; a zero FIR is uninitialised.
  • CI cppcheck fetches the fxp wrap and suppresses findings inside subprojects/.
  • tests/test_filters.c: 11 cases with exact expectations.

Acceptance criteria evidence

  • INT32_MIN saturates — FIR and biquad cases with FXP_MIN coefficients and samples return FPC_STATUS_SATURATED and the saturated output; clean under ASan+UBSan with -fno-sanitize-recover=all. An unchecked-sum mutant fails under UBSan.
  • Narrowing follows FXP_ROUNDING — no >> or << remains in src/filters.c; negative round-to-nearest-even cases (−5 → −2, −1 → 0) are asserted. A floor-shift mutant fails at the 1000 expectation.
  • Re-derived expectations — the three-tap average (coefficients 21845) of 1000, 2000, 2000 is 333, 1000, 1667 (was 333, 999, 1666; exact 333.328, 999.985, 1666.641), and the same filter fed 900 after a reset gives 300 (was 299; exact 299.995). Round-to-nearest-even replaces the floor; each exact value is a comment beside its assert.

Checks

  • gcc ASan+UBSan -Werror: pass. clang -Werror: pass.
  • src/filters.c coverage: 100% lines, 100% branches.
  • cppcheck with the CI flags: clean. Valgrind memcheck: 0 errors.
  • TI C2000 CGT 22.6.2 and 25.11.1, COFF and EABI, -Dwerror=true (the CI step run locally on a clean export): 0 diagnostics.

Stack

  1. filters — FIR and biquad on fxp, caller-owned, saturating; TI C2000 CI gate
  2. pid — PID on fxp: wide integral, dt per term, caller-owned
  3. build — drop pool-allocator, its options and headers; coverage gate 100/100
  4. docs — README, CONTRIBUTING, AGENTS and CHANGELOG for 0.4.0

Review bottom first. Each layer builds and passes the full suite on its own. The whole-change narrative and limitations are in the top pull request (layer 4).

FIR and biquad outputs are accumulated with fxp_acc_mac_chk and narrowed once through the fxp rounding policy, so INT32_MIN operands saturate and return FPC_STATUS_SATURATED instead of overflowing the int64 accumulator, and negative results round to nearest instead of flooring through a right shift. Filters become caller-owned structs, and the FIR borrows caller-sized coefficient and history arrays.

fxp is pinned by commit (24315b4, which compiles where CHAR_BIT == 16) until it is tagged, and CI now builds the library with TI C2000 CGT 22.6.2 and 25.11.1 under COFF and EABI. Expected FIR outputs move by one LSB where the old floor shift truncated (999 to 1000, 1666 to 1667, 299 to 300).

Refs #5
@aajll
aajll force-pushed the 5-fix-filters-fxp branch from 7ca487c to 43e95a0 Compare October 14, 2026 16:56
@aajll
aajll merged commit f4f35ea into master Oct 14, 2026
10 checks passed
@aajll
aajll deleted the 5-fix-filters-fxp branch October 14, 2026 17:00
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.

Port the arithmetic onto fxp and move to caller-owned storage

1 participant