Skip to content

fl::fixed_point::pow boundary handling: x=0 and x≈1 produce wrong results #2969

Closed

Description

@zackees

Summary

fl::s8x24::pow (and other fl::fixed_point<I,F>::pow specializations) give wrong / lossy results at the input boundaries x = 0 and x ≈ 1. Callers currently have to special-case these manually.

Repro

Found while building Gamma8Impl LUT (PR #2968, src/fl/math/ease.cpp.hpp):

const fl::s8x24 gamma_fp(3.2f);
constexpr fl::s8x24 inv_255_fp(1.0f / 255.0f);

// i = 0: x = 0 — pow should return 0 (mathematically pow(0, any) = 0
// for positive exponents). Implementation returns 0 OK but only because
// of an early-out check in the wrapper, not because the algorithm
// handles it.

// i = 255: x = 255 * (1/255) — but 1/255 in s8x24 is not exact, so
// x lands at raw 0x00FFFFFF = 0.99999994 (one LSB below 1.0).
const fl::s8x24 x   = static_cast<i32>(255) * inv_255_fp;
const fl::s8x24 r   = fl::s8x24::pow(x, gamma_fp);
// r ≈ 0.99902 instead of expected 1.0 — off by ~57 LSB when scaled to u16
// (returns 65478 instead of 65535).

Root cause

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 ∈ [0, 1). At the upper endpoint t→1:

  • True value: log2(2) = 1.0
  • Polynomial: c0 + c1 + c2 + c3 = 0.999557 — off by 0.000443

When the input is one LSB below 1.0, the polynomial path is taken instead of an exact-1.0 short-circuit, and the 0.000443 error propagates through exp2_fp, then scales up to ~50-100 LSB when mapped to u16.

Suggested fix

Add explicit short-circuits in pow BEFORE invoking exp2_fp(exp * log2_fp(...)):

static FASTLED_FORCE_INLINE Derived pow(Derived base, Derived exp) FL_NOEXCEPT {
    if (base.mValue <= 0) return Derived();           // already present
    constexpr Derived one(1.0f);
    if (exp.mValue == 0) return one;                  // already present
    if (base == one) return one;                      // already present
    // NEW: snap base values within 1 ULP of 1.0 to exactly 1.0 to
    // dodge the log2(1+t) polynomial's upper-endpoint residual.
    if (base.mValue >= Derived::SCALE - SOME_TOLERANCE && base.mValue <= Derived::SCALE) {
        return one;
    }
    return exp2_fp(exp * log2_fp(base));
}

Or more rigorously: extend the log2(1+t) polynomial fit to be exact at t=1 (one-sided constraint in the minimax solver).

Affected types

All fl::fixed_point<I,F>::pow specializations that route through exp2_fp(exp * log2_fp(base)):

  • s4x12, s8x8, s8x24, s12x4, s16x16, s24x8
  • u4x12, u8x8, u8x24, u12x4, u16x16, u24x8

Workaround (current)

Gamma8Impl in src/fl/math/ease.cpp.hpp manually sets mLut[0] = 0 and mLut[255] = 65535 to bypass the boundary issue. Should be removable once pow is fixed.

Refs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions