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

◆ polyDivRoundT28()

FL_NO_INLINE fl::i32 fl::gfx::detail::polyDivRoundT28 ( fl::u64 mhi,
fl::u64 mlo,
fl::u64 dv )
inline

T = round_half_up(M / dv) clamped to kPolyOne, for M = (mhi, mlo) and 0 < dv < 2^63 (polyLineT28's quotient step).

Exact: the 64-bit remainder state s of the shift-subtract below satisfies s < dv at the start of every step, so s <- 2s + bit never reaches 2^64 and the loop computes q = floor(M/dv), r = M mod dv. Requires dv > 0: polyLineT28 never passes 0 (D == 0 makes M == 0, which returns before the call). Kept out of line so polyLineT28's two call sites (normal and huge edges) share one copy: on AVR that measured smaller AND faster than one inlined loop with a runtime-selected divisor (the register allocator spills inside it). External-linkage inline (not static), like the polyClz32/polyTopBit128 helpers it calls, so the inline polyLineT28 refers to the same entity in every TU (ODR); kPolyOne is only read as a constant (no odr-use).

Definition at line 436 of file primitives.h.

437 {
438 fl::u32 m3 = (fl::u32)(mhi >> 32), m2 = (fl::u32)mhi;
439 fl::u32 m1 = (fl::u32)(mlo >> 32), m0 = (fl::u32)mlo;
440 const int top = polyTopBit128(m3, m2, m1, m0);
441 // The committed loop feeds bits 93..0 only; M bits above 93 are
442 // never read, so cap the window top at 93. In-range inputs never
443 // reach it (quotient < 2^29 and dv < 2^63 give M < 2^92).
444 const int t = (top < 94) ? top : 93;
445 // Left-align M by (127 - t) so its highest relevant bit reaches bit
446 // 127 of the window: the loop then feeds numerator bits with the
447 // fixed (w1 >> 63) extraction (constant-width shifts only — no
448 // libgcc __*di3 helpers on AVR).
449 const int k = 127 - t;
450 const int kl = k >> 5;
451 const int kb = k & 31;
452 {
453 fl::u32 n3, n2, n1, n0;
454 if (kl >= 3) { n3 = m0; n2 = 0; n1 = 0; n0 = 0; }
455 else if (kl == 2) { n3 = m1; n2 = m0; n1 = 0; n0 = 0; }
456 else if (kl == 1) { n3 = m2; n2 = m1; n1 = m0; n0 = 0; }
457 else { n3 = m3; n2 = m2; n1 = m1; n0 = m0; }
458 if (kb) {
459 const fl::u32 h3 = n2 >> (32 - kb);
460 const fl::u32 h2 = n1 >> (32 - kb);
461 const fl::u32 h1 = n0 >> (32 - kb);
462 n3 = (n3 << kb) | h3;
463 n2 = (n2 << kb) | h2;
464 n1 = (n1 << kb) | h1;
465 n0 = n0 << kb;
466 }
467 m3 = n3; m2 = n2; m1 = n1; m0 = n0;
468 }
469 // The shift-subtract, expressed in 32-bit lanes: each lane op is
470 // the corresponding half of the u64 op it replaces (same carries,
471 // same mod-2^64 wraps of the remainder and quotient states), but
472 // avr-gcc inlines 32-bit shifts, compares and subtracts instead
473 // of emitting __lshrdi3/__ashldi3/__cmpdi2/__subdi3 helper calls
474 // per iteration — the dominant cost of this loop on AVR.
475 const fl::u32 d1 = (fl::u32)(dv >> 32), d0 = (fl::u32)dv;
476 fl::u32 s1 = 0, s0 = 0; // remainder (high, low), always < dv
477 fl::u32 q1 = 0, q0 = 0; // (q<<1)|ge each step; keeps the <64 bits
478 for (int i = t; i >= 0; --i) {
479 const fl::u32 bit = m3 >> 31;
480 m3 = (m3 << 1) | (m2 >> 31);
481 m2 = (m2 << 1) | (m1 >> 31);
482 m1 = (m1 << 1) | (m0 >> 31);
483 m0 = m0 << 1;
484 const fl::u32 sh = s1 << 1, sl = s0 << 1;
485 s1 = sh | (s0 >> 31);
486 s0 = sl | bit;
487 const fl::u32 ge = (s1 > d1) || (s1 == d1 && s0 >= d0);
488 if (ge) {
489 const fl::u32 sb = s0 - d0;
490 s1 -= d1 + (fl::u32)(sb > s0);
491 s0 = sb;
492 }
493 const fl::u32 qh = q1 << 1, ql = q0 << 1;
494 q1 = qh | (q0 >> 31);
495 q0 = ql | ge;
496 }
497 // Round half-up: if 2r >= dv, q++ (r = (s1, s0)). The carry into
498 // r1 comes out of s0's MSB; s1's MSB is the 2r overflow bit.
499 const fl::u32 r1 = (s1 << 1) | (s0 >> 31), r0 = s0 << 1;
500 if ((s1 >> 31) != 0 || r1 > d1 || (r1 == d1 && r0 >= d0)) {
501 const fl::u32 qb = q0 + 1;
502 q1 += (fl::u32)(qb < q0); // carry: q0 wrapped past 0xFFFFFFFF
503 q0 = qb;
504 }
505 fl::u64 q = ((fl::u64)q1 << 32) | q0;
506 if (q > (fl::u64)kPolyOne) q = (fl::u64)kPolyOne; // physical bound
507 return (fl::i32)q;
508}
int polyTopBit128(fl::u32 m3, fl::u32 m2, fl::u32 m1, fl::u32 m0) FL_NO_EXCEPT
Index (0..127) of the highest set bit of a 128-bit value held in four 32-bit lanes (m3 most significa...
Definition primitives.h:414
static const fl::i32 kPolyOne
Full-pixel coverage in T units (see polyLineT28).
Definition primitives.h:340
fl::u64 u64
Definition s16x16x4.h:221

References fl::FL_NO_EXCEPT, FL_NO_INLINE, kPolyOne, polyDivRoundT28(), polyTopBit128(), and fl::t.

Referenced by polyDivRoundT28(), and polyLineT28().

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