Skip to content

fix(math): make s16x16x4.h compile as a translation unit's only include - #4300

Merged
zackees merged 1 commit into
masterfrom
fix/s16x16x4-self-contained
Sep 10, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/s16x16x4-self-contained

Conversation

@zackees

@zackees zackees commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

The defect

$ echo '#include "fl/math/fixed_point/s16x16x4.h"' > only.cpp
$ g++ -fsyntax-only only.cpp -Isrc -std=gnu++17
src/fl/math/fixed_point/s0x32.h:53:34: error: 'enable_if' in namespace 'fl::fl' does not name a template type
src/fl/math/fixed_point/simd_ops.h:16:62: error: invalid use of incomplete type 'struct fl::s0x32x4'

s16x16x4.h reaches s0x32x4.h through simd_ops.h, and that include sits inside
namespace fl — deliberately: simd_ops.h says so in its own header comment, because
it defines cross-type operators needing both types complete, and the include carries an
// allow-include-after-namespace marker.

On the usual path that is harmless. Something else has already included s0x32x4.h, so
#pragma once makes it a no-op and the nesting never happens. In a TU that reaches
s16x16x4.h first it does happen: the whole of s0x32x4.h, s0x32.h, and
fl/stl/type_traits.h under it get processed one namespace deeper, so fl::enable_if
resolves as fl::fl::enable_if.

The fix

Include s0x32x4.h at file scope in s16x16x4.h. No cycle — s0x32x4.h only
forward-declares s16x16x4.

Why a test for something nothing hits

Nothing in the tree uses that include order today, so this is latent rather than a live
break. That is the argument for pinning it: it appears the first time someone adds an
include that gets there first, and the diagnostic points at type_traits.h rather than
at the ordering, so the next person spends the time I did working out that a header was
being processed inside a namespace.

The test checks the four 4-wide/scalar fixed-point headers compile as a TU's only
include. simd_ops.h is deliberately excluded: failing standalone is its documented
contract, not a defect.

Mutation-checked — reverting the one-line fix fails
test_header_compiles_as_the_only_include[fl/math/fixed_point/s16x16x4.h] and leaves
the other three passing.

303/303 unit + 84/84 examples, bash lint clean.

Found while building a cross-compile harness for #4216.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

Summary by CodeRabbit

  • Bug Fixes

    • Fixed compilation when including the 4-wide fixed-point headers independently.
    • Corrected namespace handling for fixed-point header dependencies.
  • Tests

    • Added automated checks confirming each supported 4-wide fixed-point header compiles as a standalone include.

`s16x16x4.h` reaches `s0x32x4.h` through `simd_ops.h`, and that include sits
inside `namespace fl` -- deliberately, since `simd_ops.h` defines the
cross-type operators that need both types complete and so does not open a
namespace of its own.

On the usual path that is harmless: something else has already included
`s0x32x4.h`, so `#pragma once` makes it a no-op. In a translation unit that
reaches `s16x16x4.h` first it is not. The whole of `s0x32x4.h` -- and
`s0x32.h`, and `fl/stl/type_traits.h` under it -- is then processed one
namespace deeper, `fl::enable_if` resolves as `fl::fl::enable_if`, and the
header does not compile:

    src/fl/math/fixed_point/s0x32.h:53:34: error: 'enable_if' in namespace
    'fl::fl' does not name a template type

Pulling `s0x32x4.h` in at file scope fixes it. There is no cycle to worry
about: `s0x32x4.h` only forward-declares `s16x16x4`.

Nothing in the tree hits that include order today, so this is latent rather
than a live break -- which is the argument for the test, not against it. It
appears the first time someone adds an include that gets there first, and the
diagnostic points at `type_traits.h` rather than at the ordering.

`simd_ops.h` is left out of the test: it documents that it must be included
inside an open `fl` namespace, so failing standalone is its contract.

Found while building a cross-compile harness for #4216.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9
@coderabbitai

coderabbitai Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 3725d30d-4541-4100-a24b-4de189ab3239

📥 Commits

Reviewing files that changed from the base of the PR and between 5ff2d8f and db4aba3.

📒 Files selected for processing (2)
  • ci/tests/test_fixed_point_headers_are_self_contained.py
  • src/fl/math/fixed_point/s16x16x4.h

Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review.


📝 Walkthrough

Walkthrough

The change moves s0x32x4.h outside the namespace fl block in s16x16x4.h. A new test compiles each of four fixed-point headers as the only include in a C++ translation unit.

Changes

Fixed-point header compilation

Layer / File(s) Summary
Header include and standalone compilation validation
src/fl/math/fixed_point/s16x16x4.h, ci/tests/test_fixed_point_headers_are_self_contained.py
s16x16x4.h includes s0x32x4.h at file scope. The new parametrized test checks standalone compilation for four fixed-point headers with a discovered C++ compiler.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to db4ab

This change makes the fixed-point header self-contained and adds regression coverage for standalone inclusion. Reported tests, examples, and lint checks pass, so it is ready to merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: fixing s16x16x4.h so it compiles as the only included header in a translation unit.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/s16x16x4-self-contained

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 2df9d91 into master Sep 10, 2026
27 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Sep 10, 2026
zackees added a commit that referenced this pull request Sep 10, 2026
…#4043) (#4305)

* feat(gfx): float-free s16.16 3x3 inverse, the first half of P9 item 2 (#4043)

P9 asks for a colorimetric build and solve that no float symbol reaches.
`buildRgbSolveMatrixQ16` inverts in float and quantises afterwards, so the
inverse was the piece missing. `ci/color_fixed_inverse_study.py` priced it at
0.0064 dE2000 and left the arithmetic to be written; this writes it.

The obstacle the study names: the inverse is `(cofactor << 32) / determinant`,
the cofactor is already Q32, and 32 more bits puts the numerator past i64 for
any matrix that matters. The study computes it with Python's unbounded
integers and says a C++ version needs "a staged or 128-bit divide". AVR has
no `__int128`, so it is staged: each of the 32 doublings is spent on the
numerator while it still fits and on the denominator otherwise. Both raise the
quotient equally, and the determinant carries far more bits than a s16.16
answer needs.

Validated against the exact divide before any C++ was written -- the study's
four-profile corpus, six degrees of primary collapse, and 20,000 random
primary sets: **zero** ULP disagreement, with 72% of coefficients needing at
least one denominator halving (up to 11), so the staged path is the one under
test rather than a branch nothing reaches.

Input is not bounded, it is checked. A fixed limit safe enough to keep the
determinant inside i64 would sit near 16.0, and a deep-blue emitter at
xy = (0.14, 0.03) already has a Z column near 28. Every cofactor, the
determinant's products and its sums are overflow-checked by division, which
is exact everywhere and needs no builtin MSVC lacks.

Float-freeness is checked where it is checkable. On the host, x86 hardware
float leaves no call to find, so `ci/tests/test_q16_inverse_is_float_free.py`
compiles the translation unit for a Cortex-M0+ and reads the disassembly.
Measured at -Os on GCC 16.1:

| function | instructions | float-runtime calls |
|---|---:|---|
| `invert3x3Q16` | 383 | **none** (`__aeabi_ldivmod`, `__aeabi_lmul` are integer) |
| `buildRgbSolveMatrixQ16` | 503 | 11 distinct `__aeabi_f*` / `__aeabi_d*` |

The second row is a positive control, not decoration: without it a mistyped
symbol would make the first pass by disassembling nothing. Mutation-checked
both ways -- a float divide inside `invert3x3Q16` fails the assertion, and
renaming the control's symbol fails the control.

Also, because it blocked the cross-compile: `device_solve.h` spelled
`EmitterProfile` unqualified, which only resolves once some other header has
pulled in the `using` from `fl/channels/color_profile.h`, so the header did
not compile standalone on host or cross. Qualified, and added to the
self-containment test #4300 landed.

Six C++ cases: exact agreement with the study's inverse for all four
profiles, a vacuity guard that three of the four cofactors really do exceed
the 31 bits a naive shift leaves, an identity round-trip no transcription
error could satisfy (worst 7 raw units on the diagonal, 8 off it), singular
refusal by two routes, i64-overflow refusal, and that it rounds rather than
truncates.

Not yet done, and the rest of item 2: `buildRgbSolveMatrixQ16` still builds
its forward matrix in float, and `EmitterProfile` still stores float
chromaticities. #4292 measured that half at 0.1085 dE2000 worst against A1's
0.5, so it is feasible; it is cross-cutting with P2 and wants its own change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* fix(gfx): refuse a determinant of exactly i64's minimum

Both review findings on #4305, and the first is real undefined behaviour with
a reachable input.

`sumFitsI64` allowed the determinant to reach exactly -2^63.
`divideCoefficientQ16` and `roundedDivI64` both negate it, and negating i64's
minimum is undefined. UBSan on the witness below reports at two sites:

    device_solve.cpp.hpp:126: negation of -9223372036854775808 cannot be
    represented in type 'i64'
    device_solve.cpp.hpp:93: (the same, in the rounding helper)

Reachable rather than theoretical, though it takes construction. A single
term cannot be the minimum -- `productFitsI64` rejects any product that large
-- so it needs two terms summing to exactly -2^63. 2^63 - 1 factors as
7 * 73 * 127 * 337 * 92737 * 649657, which gives a term of exactly -(2^63 - 1)
from i32 entries, and a second term supplies the remaining -1:

    {{-92737, 64896, 1}, {0, 64897, 1}, {1, 0, 1532540863}}

Every entry is a valid i32, every cofactor is a valid difference of two i32
products, and every product fits an i64 on its own. Only the sum does not.

The bound now stops one short of the minimum, which is what makes both
negations safe, and the invariant is stated at the negation site rather than
left for a reader to reconstruct. A case carries the matrix.

Second finding: `_arm_tools` returned `None` when `arm-none-eabi-g++` was on
`PATH` without a matching `arm-none-eabi-objdump`, instead of falling through
to the PlatformIO and fbuild caches -- so a machine with a usable toolchain
would skip the float-freeness test rather than run it. Verified both ways by
putting only the compiler on `PATH`: before, both cases skipped; after, both
run.

303/303 unit + 84/84 examples, 15/15 in `fl_gfx_device_solve` under
ASAN/UBSAN with no runtime errors, `bash lint` clean.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* fix(gfx): correct the factorisation of 2^63-1, and make the test prove it

Review on #4305: I wrote 2^63 - 1 as 7 * 73 * 127 * 337 * 92737 * 649657 in
two comments. That product is 1317624576693539401 -- exactly a seventh of
2^63 - 1, which is 7^2 * 73 * 127 * 337 * 92737 * 649657. Corrected in both.

The witness matrix itself was never wrong; only the arithmetic the comment
gave for how it was built. But a comment describing a matrix is the kind of
thing that drifts away from the matrix, so the case now computes the
determinant the way `invert3x3Q16` computes it and checks it really is i64's
minimum, rather than leaving that to a sentence.

Mutation-checked: changing one entry by one takes the determinant to
-9223372030836422719, the equality fails, and the refusal check fails with
it -- so the case is now anchored to the property rather than to a matrix
someone might edit.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
@zackees
zackees deleted the fix/s16x16x4-self-contained branch November 5, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant