Repository navigation
fix(math): make s16x16x4.h compile as a translation unit's only include - #4300
Conversation
`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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review. 📝 WalkthroughWalkthroughThe change moves ChangesFixed-point header compilation
Estimated code review effort: 2 (Simple) | ~10 minutes Severity of issue fixed: Low Merge Risk: ⚪ Minimal · up to 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)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
…#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]>
The defect
s16x16x4.hreachess0x32x4.hthroughsimd_ops.h, and that include sits insidenamespace fl— deliberately:simd_ops.hsays so in its own header comment, becauseit defines cross-type operators needing both types complete, and the include carries an
// allow-include-after-namespacemarker.On the usual path that is harmless. Something else has already included
s0x32x4.h, so#pragma oncemakes it a no-op and the nesting never happens. In a TU that reachess16x16x4.hfirst it does happen: the whole ofs0x32x4.h,s0x32.h, andfl/stl/type_traits.hunder it get processed one namespace deeper, sofl::enable_ifresolves as
fl::fl::enable_if.The fix
Include
s0x32x4.hat file scope ins16x16x4.h. No cycle —s0x32x4.honlyforward-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.hrather thanat 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.his deliberately excluded: failing standalone is its documentedcontract, 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 leavesthe other three passing.
303/303 unit + 84/84 examples,
bash lintclean.Found while building a cross-compile harness for #4216.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9
Summary by CodeRabbit
Bug Fixes
Tests