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

◆ divide64By32()

FL_FIXED_POINT_DIVIDE_INLINE u32 fl::divide64By32 ( u32 hi,
u32 lo,
u32 divisor )

(hi:lo) / divisor, for a quotient that fits 32 bits.

Knuth's Algorithm D over two base-2^16 digits, which is what lets a 64-bit numerator be divided using the 32-bit divider twice. The two correction loops run at most twice each and usually not at all; they are what make the base-2^16 estimate exact rather than approximate.

Defined, not undefined, on the two inputs the wide path leaves to the compiler: a zero divisor and a quotient too large for 32 bits both return 0xFFFFFFFF. That is a deliberate difference and is not something a caller may rely on – the wide path wraps instead – so operator/'s contract is unchanged and the divergence is pinned by a test rather than left to be discovered.

Kept out of #if FL_FIXED_POINT_NARROW_DIVIDE on purpose. Gating the definition would leave it compiled by no host and tested by nothing, which is how simd_noop.hpp went two rounds of tuning against a build that excluded it (FastLED#4216).

Definition at line 125 of file wide_divide.h.

126 {
127 constexpr u32 kBase = 65536u;
128 if (divisor == 0u || hi >= divisor) {
129 return 0xFFFFFFFFu;
130 }
131
132 // Normalising the divisor is what makes the digit estimate below good to
133 // within one, which is the whole reason two corrections suffice.
134 const int shift = detail::fixedPointLeadingZeros(divisor);
135 const u32 d = divisor << shift;
136 const u32 d_high = d >> 16;
137 const u32 d_low = d & 0xFFFFu;
138
139 // A shift of 32 is undefined, so the unshifted case is spelled out rather
140 // than folded in with a mask.
141 const u32 n_high = shift == 0 ? hi : ((hi << shift) | (lo >> (32 - shift)));
142 const u32 n_low = lo << shift;
143 const u32 n_low_high = n_low >> 16;
144 const u32 n_low_low = n_low & 0xFFFFu;
145
146 u32 quotient_high = n_high / d_high;
147 u32 remainder = n_high - quotient_high * d_high;
148 while (quotient_high >= kBase ||
149 static_cast<u64>(quotient_high) * d_low >
150 static_cast<u64>(kBase) * remainder + n_low_high) {
151 --quotient_high;
152 remainder += d_high;
153 if (remainder >= kBase) {
154 break;
155 }
156 }
157
158 // Deliberate 32-bit wraparound: the true value needs 33 bits and the
159 // low 32 are the ones the next digit is taken from.
160 const u32 partial =
161 static_cast<u32>(n_high * kBase + n_low_high - quotient_high * d);
162
163 u32 quotient_low = partial / d_high;
164 remainder = partial - quotient_low * d_high;
165 while (quotient_low >= kBase ||
166 static_cast<u64>(quotient_low) * d_low >
167 static_cast<u64>(kBase) * remainder + n_low_low) {
168 --quotient_low;
169 remainder += d_high;
170 if (remainder >= kBase) {
171 break;
172 }
173 }
174
175 return quotient_high * kBase + quotient_low;
176}
fl::u64 u64
Definition s16x16x4.h:221
FL_FIXED_POINT_DIVIDE_INLINE int fixedPointLeadingZeros(u32 value) FL_NO_EXCEPT
Leading zeros of a non-zero 32-bit value; 32 for zero.
Definition wide_divide.h:86
fl::u64 u64
Definition s16x16x4.h:221

References fl::detail::fixedPointLeadingZeros(), FL_FIXED_POINT_DIVIDE_INLINE, and FL_NO_EXCEPT.

Referenced by fl::s16x16::operator/(), and fl::u16x16::operator/().

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