FastLED 3.10.10
Loading...
Searching...
No Matches
wide_divide.h
Go to the documentation of this file.
1#pragma once
2
25
26#include "fl/stl/int.h"
28#include "fl/stl/noexcept.h"
29
55#ifndef FL_FIXED_POINT_NARROW_DIVIDE
56#if __cplusplus < 201402L
57#define FL_FIXED_POINT_NARROW_DIVIDE 0
58#elif defined(__arm__) && defined(__ARM_FEATURE_IDIV) && \
59 (__ARM_FEATURE_IDIV + 0) == 1
60#define FL_FIXED_POINT_NARROW_DIVIDE 1
61#elif defined(__riscv_div) && defined(__riscv_xlen) && (__riscv_xlen + 0) == 32
62#define FL_FIXED_POINT_NARROW_DIVIDE 1
63#else
64#define FL_FIXED_POINT_NARROW_DIVIDE 0
65#endif
66#endif
67
75#if __cplusplus >= 201402L
76#define FL_FIXED_POINT_DIVIDE_INLINE constexpr FASTLED_FORCE_INLINE
77#else
78#define FL_FIXED_POINT_DIVIDE_INLINE FASTLED_FORCE_INLINE
79#endif
80
81namespace fl {
82
83namespace detail {
84
87#if defined(__GNUC__) || defined(__clang__)
88 // One CLZ instruction on every core this file's fast path is enabled for.
89 // __builtin_clz takes `unsigned int`, which is 16-bit on AVR; there the
90 // 32-bit `unsigned long` builtin is the one that sees every bit.
91 return value == 0 ? 32
92 : sizeof(unsigned) >= sizeof(u32)
93 ? __builtin_clz(static_cast<unsigned>(value))
94 : __builtin_clzl(static_cast<unsigned long>(value));
95#else
96 int count = 0;
97 while (count < 32 && (value & 0x80000000u) == 0u) {
98 value <<= 1;
99 ++count;
100 }
101 return count;
102#endif
103}
104
105} // namespace detail
106
126 u32 divisor) FL_NO_EXCEPT {
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}
177
178} // namespace fl
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_FIXED_POINT_DIVIDE_INLINE u32 divide64By32(u32 hi, u32 lo, u32 divisor) FL_NO_EXCEPT
(hi:lo) / divisor, for a quotient that fits 32 bits.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
fl::u64 u64
Definition s16x16x4.h:221
Base definition for an LED controller.
#define FL_FIXED_POINT_DIVIDE_INLINE
constexpr where the language allows it, always force-inlined.
Definition wide_divide.h:78