Repository navigation
fix(fixed_point): snap pow base near 1.0 to dodge log2 polynomial residual - #2972
Conversation
…idual (#2969) `pow(base, exp)` is implemented as `exp2_fp(exp * log2_fp(base))`. The `log2_fp` helper uses a 4-term minimax polynomial for `log2(1+t)` on `t in [0, 1)`. At the upper endpoint t->1 the polynomial evaluates to ~0.999557 instead of 1.0 — a 0.000443 error that, after exp2 and scale-to-u16, drops the output by ~50-100 LSB below expected. The existing short-circuits caught exact-1.0 inputs only, but `255 * (1/255)` in fixed-point lands one ULP below 1.0, exposing the bug: pow(0.99999994, 3.2) returned ~0.99902 (raw 65478) instead of 1.0 (raw 65535) Fix: in every `pow` (base class plus the 12 concrete specializations s4x12, s8x8, s8x24, s12x4, s16x16, s24x8, u4x12, u8x8, u8x24, u12x4, u16x16, u24x8) snap `base.mValue` values within 2 ULPs of `SCALE` to exactly-one before invoking the polynomial. Also drops the `Gamma8Impl::mLut[255] = 65535` workaround in `src/fl/math/ease.cpp.hpp` that was added in #2968 — the loop now runs through `[1, 256)` because `s8x24::pow` is correct at the boundary. Tests added in `tests/fl/math/fixed_point.cpp`: - pow boundary - exactly 1.0 - pow boundary - one ULP below 1.0 snaps to 1.0 - pow boundary - two ULPs below 1.0 snaps to 1.0 - pow boundary - far from 1.0 not snapped all parameterised over s16x16, s8x24, u16x16, u8x24. Verified: - bash test fl_math_fixed_point --cpp (passes, including new pow boundary cases) - bash test fl_chipsets_ucs7604 --cpp (passes, gamma=3.2 boundary at i=255 -> 65535) - bash test fl_math_ease --cpp (passes) - bash lint --cpp (clean) Closes #2969
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds near-1.0 snap fast-paths to fixed-point pow implementations, updates Gamma8Impl to compute the final LUT entry via the fixed-point pow path instead of manual assignment, and adds regression tests plus a baseline metric update. ChangesFixed-point pow snap boundary handling
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 Infer (1.2.0)tests/fl/math/fixed_point.cpptests/fl/math/fixed_point.cpp:104:10: fatal error: 'test.h' file not found ... [truncated 1055 characters] ... stall/lib/clang/18/include" 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/fl/math/fixed_point.cpp`:
- Around line 5133-5140: The comment block around log2_fp should be converted to
7-bit ASCII: replace Unicode math glyphs (such as ∈, ≈, →, —) with ASCII
equivalents (e.g. "in", "≈" -> "approx", "->", "--" or plain punctuation) and
ensure phrases like "t ∈ [0, 1)" become "t in [0, 1)" and "t→1" becomes "t ->
1"; update the lines referencing base.mValue, exp2 + scale-to-u16, and
Gamma8Impl::mLut[255] accordingly so the semantics remain the same but only
ASCII characters are used.
- Around line 5170-5175: The test test_pow_two_ulps_below_one_impl currently
only asserts FP::pow(just_below, gamma_2_8).raw() equals one.raw(); extend this
to also assert the same for gamma_3_2 so the 2-ULP edge uses the same exponent
fanout as the 1-ULP test: locate test_pow_two_ulps_below_one_impl, compute
just_below and one as already done, and add a second FL_CHECK_EQ calling
FP::pow(just_below, gamma_3_2).raw() == one.raw() (matching the existing pattern
for gamma_2_8).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: fec64d2a-855b-4337-8586-60c2ecec8c45
📒 Files selected for processing (15)
src/fl/math/ease.cpp.hppsrc/fl/math/fixed_point/base.hsrc/fl/math/fixed_point/s12x4.hsrc/fl/math/fixed_point/s16x16.hsrc/fl/math/fixed_point/s24x8.hsrc/fl/math/fixed_point/s4x12.hsrc/fl/math/fixed_point/s8x24.hsrc/fl/math/fixed_point/s8x8.hsrc/fl/math/fixed_point/u12x4.hsrc/fl/math/fixed_point/u16x16.hsrc/fl/math/fixed_point/u24x8.hsrc/fl/math/fixed_point/u4x12.hsrc/fl/math/fixed_point/u8x24.hsrc/fl/math/fixed_point/u8x8.htests/fl/math/fixed_point.cpp
CodeRabbit findings on tests/fl/math/fixed_point.cpp: - ASCII-fy non-7-bit chars in the pow boundary tests block (- -> --, in [0,1) instead of glyph, t -> 1, approx instead of ≈) so the file stays in the project's ASCII convention. - test_pow_two_ulps_below_one_impl now also asserts the snap collapses pow(just_below, gamma_3_2) to one.raw(), matching the gamma exponent fanout already exercised by test_pow_one_ulp_below_one_impl. Also bump esp32s3 bloat baseline 338247 -> 338372 (+125 B from the snap-to-one short-circuit added across 13 fixed_point::pow specs). The increase is one-time and intentional; the gate's job is to catch ongoing drift.
Closes #2969
Root cause
fl::fixed_point<I,F>::pow(base, exp)is implemented asexp2_fp(exp * log2_fp(base)). Thelog2_fphelper uses a 4-term minimax polynomial forlog2(1+t)ont ∈ [0, 1). At the upper endpointt → 1the polynomial evaluates to ≈0.999557 instead of 1.0 — a 0.000443 residual that, afterexp2and scale-to-u16, drops the output by ~50-100 LSB below the expected value.The existing short-circuits caught
base.mValue == SCALEexactly,base.mValue <= 0, andexp.mValue == 0. But values like255 * (1/255)in fixed-point land one ULP below 1.0 because1/255is not representable exactly — so the input never hit the early-out and went through the lossy polynomial path:Fix
Add a near-1.0 snap before the polynomial dispatch in every
pow: ifbase.mValueis within 2 ULPs ofSCALE, return one directly. Applied uniformly across:src/fl/math/fixed_point/base.h(the templated base-class pow)s4x12,s8x8,s8x24,s12x4,s16x16,s24x8,u4x12,u8x8,u8x24,u12x4,u16x16,u24x8Bonus cleanup
Removed the
Gamma8Impl::mLut[255] = 65535workaround that was added in #2968 to dodge this bug. The constructor now loops through[1, 256)and letss8x24::powproduce the correct boundary value on its own. The explanatory comment block for the workaround is gone;mLut[0] = 0is kept (slightly cheaper than a loop branch and ensures mLut is initialised).Tests
Added 4 new
FL_TEST_CASEgroups intests/fl/math/fixed_point.cpp(4 type subcases each —s16x16,s8x24,u16x16,u8x24):pow boundary - exactly 1.0— base == 1.0 still returns 1.0pow boundary - one ULP below 1.0 snaps to 1.0— the regression casepow boundary - two ULPs below 1.0 snaps to 1.0— upper edge of snap windowpow boundary - far from 1.0 not snapped—pow(0.5, 2)≈ 0.25 (signed) /pow(2, 0.5)≈ 1.414 (unsigned) confirm the snap doesn't over-triggerVerified locally
bash test fl_math_fixed_point --cpp— passes (including 16 new boundary subcases)bash test fl_chipsets_ucs7604 --cpp— passes (originally-failing gamma=3.2 at i=255 → 65535 case)bash test fl_math_ease --cpp— passes (Gamma8Impl rewrite)bash lint --cpp— clean🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests