FastLED 3.10.10
Loading...
Searching...
No Matches

◆ polyLineT28()

fl::i32 fl::gfx::detail::polyLineT28 ( fl::i32 B,
fl::i32 D,
fl::i64 u,
fl::i32 ta,
fl::i32 tb )
inline

Exact left-of-line coverage of one pixel for one polygon edge.

Line: x(tau) = x_q + (B/D) * tau with tau in Q16.16 relative to the row center (the caller's anchor; drawPolygonCore always anchors at the row center, which is what the floored-Q16.16 edge crossing stores); u = D * (x_q - px * 65536) at the anchor. Returns T = round(2^28 * cov) where cov = int_{ta}^{tb} clamp(x(tau) - px_left, 0, 1) dtau/65536, i.e. the fraction of the pixel lying to the LEFT of the line over the row sub-interval [ta, tb]. Pixel coverage between span lines L (left) and R (right) is then T_left(R) - T_left(L).

In s = B * tau space the clamp breakpoints are integers (s = -u and s = 2*D*32768 - u), so the integral is a sum of at most two trapezoid/rectangle pieces: M = (hi-lo) * ((u+lo)+(u+hi)) + (s_hi-lo2) * (4*D*32768) T = round(M / (32*D*|B|)) M < 2^98, den = 32*D*S < 2^67; the exact quotient is < 2^31 and the result is clamped to 2^28 = kPolyOne, the physical full-pixel bound.

The quotient is the exact round-half-up M / den for every den (#5286). For den < 2^63 (every edge with dx*dy < 2^26 px^2, i.e. all normal polygons) it is the committed 457b21d1b shift-subtract, which is exact in that range; huge edges (den >= 2^63) divide M >> 4 by den >> 4 instead, see the comment at the den setup. In polyDivRoundT28 M is left-aligned so its highest relevant bit sits at the top of the 128-bit window, and the numerator is fed top-down by left-shifting the window in place — constant-width shifts only, no FPU, no __int128, and on AVR the whole loop stays in registers (no libgcc __*di3 helper calls). An M == 0 numerator (line does not cross the pixel within [ta, tb]) returns 0 immediately. Result: [0, kPolyOne].

Definition at line 541 of file primitives.h.

542 {
543 if (B == 0) {
544 // Vertical line: x is constant, cov = clamp(u0, 0, 1) * (tb-ta)/65536
545 // with u0 = u/D exact (u = D * u0).
546 fl::i64 w = u / D;
547 if (w < 0) w = 0;
548 else if (w > 65536) w = 65536;
549 return (fl::i32)(((w * (tb - ta)) + 8) >> 4);
550 }
551 const fl::i64 S = (B < 0) ? -(fl::i64)B : (fl::i64)B;
552 const fl::i64 sa = (fl::i64)B * ta;
553 const fl::i64 sb = (fl::i64)B * tb;
554 // s = B*tau runs over [sa, sb] as tau runs [ta, tb]; for B < 0 the
555 // interval is reversed — use min/max (the area is orientation-free).
556 const fl::i64 s_lo = (sa < sb) ? sa : sb;
557 const fl::i64 s_hi = (sa < sb) ? sb : sa;
558 const fl::i64 c0 = -u;
559 const fl::i64 c1 = (fl::i64)2 * D * 32768 - u;
560 fl::u64 mhi = 0, mlo = 0;
561 const fl::i64 lo = (s_lo > c0) ? s_lo : c0;
562 const fl::i64 hi = (s_hi < c1) ? s_hi : c1;
563 if (hi > lo) {
564 // (u+lo) and (u+hi) each lie in [0, 2*D*32768] (lo >= c0, hi <= c1),
565 // so both factors are non-negative and < 2^48.
566 polyMul128((fl::u64)(hi - lo),
567 (fl::u64)((u + lo) + (u + hi)), mhi, mlo);
568 }
569 const fl::i64 lo2 = (s_lo > c1) ? s_lo : c1;
570 if (s_hi > lo2) {
571 fl::u64 phi = 0, plo = 0;
572 polyMul128((fl::u64)(s_hi - lo2),
573 (fl::u64)((fl::i64)4 * D * 32768), phi, plo);
574 polyAdd128(mhi, mlo, phi, plo);
575 }
576 if (mhi == 0 && mlo == 0) return 0; // line misses the pixel in [ta, tb]
577 // den = 32 * D * S as a u64 pair (D*S < 2^62, so den_hi = DS >> 59 and
578 // den < 2^67).
579 const fl::u64 DS = (fl::u64)S * (fl::u64)D;
580 const fl::u64 dhi = DS >> 59;
581 const fl::u64 dlo = DS << 5;
582 // The loop below is a 64-bit remainder state machine
583 // s <- 2s + bit, s -= dv when s >= dv. It is an exact long division
584 // only while dv < 2^63 (s < dv keeps 2s + bit below 2^64). The
585 // committed implementation always fed den itself, so for den >= 2^63
586 // the carry out of the remainder's MSB was dropped (den in
587 // [2^63, 2^64)) or the quotient collapsed to 0/1 (den >= 2^64), giving
588 // wrong coverage for edges of dx*dy >= 2^26 px^2 (#5286). Those huge
589 // edges instead divide M' = M >> 4 by dv = den >> 4 = 2*DS < 2^63:
590 // floor(M' / dv) == floor(M / den) (nested floors collapse), and
591 // with R = 16*r' + (M mod 16), 2R >= den <=> 2r' + bit3(M) >= dv
592 // <=> 2r' >= dv, because 2r' and dv are both even.
593 // So quotient AND round-half-up are exact with the unchanged loop and
594 // rounding in polyDivRoundT28; only the huge-edge setup is new.
595 if (dhi != 0 || (dlo >> 63) != 0) {
596 const fl::u64 nlo = (mlo >> 4) | (mhi << 60); // M < 2^98, so
597 return polyDivRoundT28(mhi >> 4, nlo, DS << 1); // M >> 4 < 2^94
598 }
599 // Fast path: if mhi == 0 the numerator is < 2^64, so the loop's q
600 // accumulation (at most 64 steps) cannot wrap its u64 and the whole
601 // loop reduces to one exact 64/64 division plus the same round-half-up
602 // and physical clamp. On AVR the division lowers to __udivdi3 (already
603 // linked for the sort keys). dlo != 0 keeps this total for degenerate
604 // direct calls (the committed loop never divided; drawPolygon always
605 // feeds den > 0).
606 if (dlo != 0 && mhi == 0) {
607 fl::u64 q = mlo / dlo;
608 const fl::u64 r = mlo - q * dlo; // mlo % dlo, one division
609 if ((r >> 63) != 0 || (r << 1) >= dlo) ++q;
610 if (q > (fl::u64)kPolyOne) q = (fl::u64)kPolyOne;
611 return (fl::i32)q;
612 }
613 return polyDivRoundT28(mhi, mlo, dlo);
614}
FL_NO_INLINE fl::i32 polyDivRoundT28(fl::u64 mhi, fl::u64 mlo, fl::u64 dv) FL_NO_EXCEPT
T = round_half_up(M / dv) clamped to kPolyOne, for M = (mhi, mlo) and 0 < dv < 2^63 (polyLineT28's qu...
Definition primitives.h:436
void polyMul128(fl::u64 a, fl::u64 b, fl::u64 &hi, fl::u64 &lo) FL_NO_EXCEPT
Multiply two unsigned values < 2^48, returning the 128-bit product as a (hi, lo) u64 pair.
Definition primitives.h:344
void polyAdd128(fl::u64 &hi, fl::u64 &lo, fl::u64 ahi, fl::u64 alo) FL_NO_EXCEPT
128-bit add: (hi, lo) += (ahi, alo).
Definition primitives.h:355
static const fl::i32 kPolyOne
Full-pixel coverage in T units (see polyLineT28).
Definition primitives.h:340
fl::i64 i64
Definition s16x16x4.h:222
fl::u64 u64
Definition s16x16x4.h:221

References fl::B, fl::FL_NO_EXCEPT, kPolyOne, polyAdd128(), polyDivRoundT28(), polyLineT28(), and polyMul128().

Referenced by drawPolygonCore(), and polyLineT28().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: