FastLED 3.9.15
Loading...
Searching...
No Matches
u16x16.h
Go to the documentation of this file.
1#pragma once
2
3// Unsigned 16.16 fixed-point arithmetic.
4// All operations are integer-only in the hot path.
5
6#include "fl/stl/stdint.h"
10#include "fl/stl/noexcept.h"
11#include "fl/stl/undef.h" // Undefine abs/min/max macros from Arduino.h
12
14
15namespace fl {
16
17// Unsigned 16.16 fixed-point value type.
18class u16x16 {
19 public:
20 static constexpr int INT_BITS = 16;
21 static constexpr int FRAC_BITS = 16;
22 static constexpr i32 SCALE = static_cast<i32>(1) << FRAC_BITS;
23
24 // ---- Construction ------------------------------------------------------
25
26 constexpr u16x16() FL_NOEXCEPT = default;
27
28 explicit constexpr u16x16(float f) FL_NOEXCEPT
29 : mValue(static_cast<u32>(f * (SCALE))) {}
30
31 // Integer constructor — any integer width (portable: AVR 16-bit int, ARM/x86 32-bit).
32 // Compile error if constexpr value exceeds INT_BITS range.
33 template <typename IntT, detail::enable_if_integer_t<IntT> = 0>
36
37 // Auto-promotion from other fixed-point types
38 // Enabled only when both INT_BITS and FRAC_BITS can be promoted (no demotion)
39 template <typename OtherFP>
40 constexpr u16x16(const OtherFP& other,
41 typename fl::enable_if<
42 (OtherFP::INT_BITS <= INT_BITS) &&
43 (OtherFP::FRAC_BITS <= FRAC_BITS) &&
44 (OtherFP::INT_BITS != INT_BITS || OtherFP::FRAC_BITS != FRAC_BITS),
45 int>::type = 0)
46 FL_NOEXCEPT : mValue(static_cast<u32>(
47 static_cast<u64>(other.raw()) << (FRAC_BITS - OtherFP::FRAC_BITS))) {}
48
49 // Raw constructor for C++11 constexpr from_raw
50 struct RawTag {};
51 constexpr explicit u16x16(u32 raw, RawTag) FL_NOEXCEPT : mValue(raw) {}
52
54 return u16x16(raw, RawTag());
55 }
56
57 // ---- Access ------------------------------------------------------------
58
59 constexpr u32 raw() const FL_NOEXCEPT { return mValue; }
60 constexpr u32 to_int() const FL_NOEXCEPT { return mValue >> FRAC_BITS; }
61 constexpr float to_float() const FL_NOEXCEPT { return static_cast<float>(mValue) / (SCALE); }
62
63 // ---- Fixed-point arithmetic --------------------------------------------
64
65#if defined(__riscv) && (__riscv_xlen == 32)
66 // RV32: Use mul+mulhu inline asm to avoid expensive 64-bit widening multiply.
67 // Extracts bits [47:16] of the 64-bit product for Q16.16 unsigned fixed-point.
69 u32 lo, hi;
70 asm("mul %0, %2, %3\n\t"
71 "mulhu %1, %2, %3"
72 : "=&r"(lo), "=&r"(hi)
73 : "r"(mValue), "r"(b.mValue));
74 return from_raw((lo >> 16) | (hi << 16));
75 }
76#else
78 return from_raw(static_cast<u32>(
79 (static_cast<u64>(mValue) * b.mValue) >> FRAC_BITS));
80 }
81#endif
82
84 return from_raw(static_cast<u32>(
85 (static_cast<u64>(mValue) * (static_cast<u64>(1) << FRAC_BITS)) / b.mValue));
86 }
87
89 return from_raw(mValue + b.mValue);
90 }
91
93 return from_raw(mValue - b.mValue);
94 }
95
97 return from_raw(mValue >> shift);
98 }
99
101 return from_raw(mValue << shift);
102 }
103
104 // ---- Scalar multiply (no fixed-point shift) ----------------------------
105
106 constexpr FASTLED_FORCE_INLINE u16x16 operator*(u32 scalar) const FL_NOEXCEPT {
107 return from_raw(mValue * scalar);
108 }
109
110 friend constexpr u16x16 operator*(u32 scalar, u16x16 fp) FL_NOEXCEPT {
111 return u16x16::from_raw(scalar * fp.mValue);
112 }
113
114 // ---- Comparisons -------------------------------------------------------
115
116 constexpr bool operator<(u16x16 b) const FL_NOEXCEPT { return mValue < b.mValue; }
117 constexpr bool operator>(u16x16 b) const FL_NOEXCEPT { return mValue > b.mValue; }
118 constexpr bool operator<=(u16x16 b) const FL_NOEXCEPT { return mValue <= b.mValue; }
119 constexpr bool operator>=(u16x16 b) const FL_NOEXCEPT { return mValue >= b.mValue; }
120 constexpr bool operator==(u16x16 b) const FL_NOEXCEPT { return mValue == b.mValue; }
121 constexpr bool operator!=(u16x16 b) const FL_NOEXCEPT { return mValue != b.mValue; }
122
123 // ---- Math ---------------------------------------------------------------
124
126 return from_raw(a.mValue % b.mValue);
127 }
128
130 return from_raw(x.mValue & ~(u32((SCALE) - 1)));
131 }
132
134 return from_raw((x.mValue & ~(u32((SCALE) - 1))) +
135 ((x.mValue & u32((SCALE) - 1)) ? (SCALE) : 0));
136 }
137
139 return from_raw(x.mValue & u32((SCALE) - 1));
140 }
141
143 return a < b ? a : b;
144 }
145
147 return a > b ? a : b;
148 }
149
151 return x < lo ? lo : (x > hi ? hi : x);
152 }
153
154#if defined(__riscv) && (__riscv_xlen == 32)
156#else
158#endif
159 return a + (b - a) * t;
160 }
161
163 return x < edge ? u16x16() : u16x16(1.0f);
164 }
165
167 constexpr u16x16 zero(0.0f);
168 constexpr u16x16 one(1.0f);
169 constexpr u16x16 two(2.0f);
170 constexpr u16x16 three(3.0f);
171 u16x16 t = clamp((x - edge0) / (edge1 - edge0), zero, one);
172 return t * t * (three - two * t);
173 }
174
176 return x.mValue == 0 ? u16x16() : from_raw(static_cast<u32>(
177 fl::isqrt64(static_cast<u64>(x.mValue) << FRAC_BITS)));
178 }
179
181 return sqrt(x).mValue == 0
182 ? u16x16()
183 : from_raw(static_cast<u32>(1) << FRAC_BITS) / sqrt(x);
184 }
185
187 constexpr u16x16 one(1.0f);
188 if (exp.mValue == 0) return one;
189 if (base == one) return one;
190 if (base.mValue == 0) return u16x16();
191 // Snap base values within ~2 ULPs of 1.0 to exactly 1.0 to dodge the
192 // log2(1+t) minimax polynomial's upper-endpoint residual (#2969).
193 constexpr u32 kOneRaw = static_cast<u32>(SCALE);
194 if (base.mValue >= (kOneRaw - 2u) && base.mValue <= kOneRaw) {
195 return one;
196 }
197 return exp2_fp(exp * log2_fp(base));
198 }
199
200 private:
201 u32 mValue = 0;
202
203 // Returns 0-based position of highest set bit, or -1 if v==0.
204 static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT {
205 return v == 0 ? -1 : _highest_bit_step(v, 0);
206 }
207
208 static constexpr int _highest_bit_step(u32 v, int r) FL_NOEXCEPT {
209 return (v & 0xFFFF0000u) ? _highest_bit_step(v >> 16, r + 16)
210 : (v & 0x0000FF00u) ? _highest_bit_step(v >> 8, r + 8)
211 : (v & 0x000000F0u) ? _highest_bit_step(v >> 4, r + 4)
212 : (v & 0x0000000Cu) ? _highest_bit_step(v >> 2, r + 2)
213 : (v & 0x00000002u) ? r + 1
214 : r;
215 }
216
217 // Fixed-point log base 2 for positive values.
218 // Uses 2-term polynomial for log2(1+t), t in [0,1) (simplified for unsigned).
219 // Horner evaluation uses u64 intermediates (24 frac bits) to minimize
220 // rounding error, then converts back to 16 frac bits.
222 u32 val = x.mValue;
223 int msb = highest_bit(val);
224 if (msb < 0) return u16x16(); // log2(0) = undefined, return 0
225
226 u32 int_part = msb - FRAC_BITS;
227 u32 t;
228 if (msb >= FRAC_BITS) {
229 t = (val >> (msb - FRAC_BITS)) - (SCALE);
230 } else {
231 t = (val << (FRAC_BITS - msb)) - (SCALE);
232 }
233
234 // 2-term polynomial coefficients for log2(1+t), t in [0,1).
235 // Stored as u64 with 24 fractional bits.
236 constexpr int IFRAC = 24;
237 constexpr u64 c0 = 24189248ULL; // 1.44179 * 2^24
238 constexpr u64 c1 = 5049984ULL; // 0.30093 * 2^24
239
240 // Extend t from 16 to 24 frac bits.
241 u64 t24 = static_cast<u64>(t) << (IFRAC - FRAC_BITS);
242
243 // Simplified Horner: c0 + c1*t, then multiply by t
244 u64 acc = c0 + ((c1 * t24) >> IFRAC);
245 u64 frac_part = (acc * t24) >> IFRAC;
246
247 // Convert from 24 frac bits back to 16.
248 u32 frac16 = static_cast<u32>(frac_part >> (IFRAC - FRAC_BITS));
249 return from_raw((int_part << FRAC_BITS) + frac16);
250 }
251
252 // Fixed-point 2^x. Uses 4-term minimax polynomial for 2^t, t in [0,1).
253 // Horner evaluation uses u64 intermediates (24 frac bits) to minimize
254 // rounding error, then converts back to 16 frac bits.
256 u16x16 fl_val = floor(x);
257 u16x16 fr = x - fl_val;
258 u32 n = fl_val.mValue >> FRAC_BITS;
259
260 if (n >= INT_BITS) return from_raw(0xFFFFFFFF); // overflow
261
262 u32 int_pow = (SCALE) << n;
263
264 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
265 // Stored as u64 with 24 fractional bits.
266 constexpr int IFRAC = 24;
267 constexpr u64 d0 = 11629376ULL; // 0.69316 * 2^24
268 constexpr u64 d1 = 4038400ULL; // 0.24071 * 2^24
269 constexpr u64 d2 = 895232ULL; // 0.05336 * 2^24
270 constexpr u64 d3 = 214016ULL; // 0.01276 * 2^24
271
272 // Extend fr from 16 to 24 frac bits.
273 u64 fr24 = static_cast<u64>(fr.mValue) << (IFRAC - FRAC_BITS);
274
275 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
276 u64 acc = d3;
277 acc = d2 + ((acc * fr24) >> IFRAC);
278 acc = d1 + ((acc * fr24) >> IFRAC);
279 acc = d0 + ((acc * fr24) >> IFRAC);
280 constexpr u64 one24 = 1ULL << IFRAC;
281 u64 frac_pow24 = one24 + ((acc * fr24) >> IFRAC);
282
283 // Convert from 24 frac bits to 16 frac bits, then scale by int_pow.
284 u32 frac_pow16 = static_cast<u32>(frac_pow24 >> (IFRAC - FRAC_BITS));
285 u64 result = (static_cast<u64>(int_pow) * frac_pow16) >> FRAC_BITS;
286 return from_raw(static_cast<u32>(result));
287 }
288};
289
290} // namespace fl
291
static constexpr FASTLED_FORCE_INLINE u16x16 mod(u16x16 a, u16x16 b) FL_NOEXCEPT
Definition u16x16.h:125
static FASTLED_FORCE_INLINE u16x16 smoothstep(u16x16 edge0, u16x16 edge1, u16x16 x) FL_NOEXCEPT
Definition u16x16.h:166
constexpr u16x16(u32 raw, RawTag) FL_NOEXCEPT
Definition u16x16.h:51
constexpr FASTLED_FORCE_INLINE u16x16 operator/(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:83
static constexpr FASTLED_FORCE_INLINE u16x16 sqrt(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:175
static constexpr FASTLED_FORCE_INLINE u16x16 floor(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:129
static constexpr int INT_BITS
Definition u16x16.h:20
constexpr u32 raw() const FL_NOEXCEPT
Definition u16x16.h:59
constexpr bool operator==(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:120
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition u16x16.h:204
static constexpr FASTLED_FORCE_INLINE u16x16 max(u16x16 a, u16x16 b) FL_NOEXCEPT
Definition u16x16.h:146
constexpr bool operator<=(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:118
u32 mValue
Definition u16x16.h:201
static constexpr int _highest_bit_step(u32 v, int r) FL_NOEXCEPT
Definition u16x16.h:208
constexpr FASTLED_FORCE_INLINE u16x16 operator-(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:92
constexpr u16x16(IntT n) FL_NOEXCEPT
Definition u16x16.h:34
constexpr FASTLED_FORCE_INLINE u16x16 operator*(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:77
static FASTLED_FORCE_INLINE u16x16 exp2_fp(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:255
static constexpr FASTLED_FORCE_INLINE u16x16 step(u16x16 edge, u16x16 x) FL_NOEXCEPT
Definition u16x16.h:162
constexpr FASTLED_FORCE_INLINE u16x16 operator<<(int shift) const FL_NOEXCEPT
Definition u16x16.h:100
constexpr bool operator<(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:116
static constexpr FASTLED_FORCE_INLINE u16x16 ceil(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:133
constexpr bool operator>=(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:119
static constexpr FASTLED_FORCE_INLINE u16x16 min(u16x16 a, u16x16 b) FL_NOEXCEPT
Definition u16x16.h:142
friend constexpr u16x16 operator*(u32 scalar, u16x16 fp) FL_NOEXCEPT
Definition u16x16.h:110
static FASTLED_FORCE_INLINE u16x16 pow(u16x16 base, u16x16 exp) FL_NOEXCEPT
Definition u16x16.h:186
constexpr FASTLED_FORCE_INLINE u16x16 operator+(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:88
static constexpr FASTLED_FORCE_INLINE u16x16 lerp(u16x16 a, u16x16 b, u16x16 t) FL_NOEXCEPT
Definition u16x16.h:157
constexpr u16x16(const OtherFP &other, typename fl::enable_if<(OtherFP::INT_BITS<=INT_BITS) &&(OtherFP::FRAC_BITS<=FRAC_BITS) &&(OtherFP::INT_BITS !=INT_BITS||OtherFP::FRAC_BITS !=FRAC_BITS), int >::type=0) FL_NOEXCEPT
Definition u16x16.h:40
constexpr float to_float() const FL_NOEXCEPT
Definition u16x16.h:61
static constexpr FASTLED_FORCE_INLINE u16x16 fract(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:138
constexpr FASTLED_FORCE_INLINE u16x16 operator*(u32 scalar) const FL_NOEXCEPT
Definition u16x16.h:106
static FASTLED_FORCE_INLINE u16x16 log2_fp(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:221
constexpr u16x16() FL_NOEXCEPT=default
constexpr u32 to_int() const FL_NOEXCEPT
Definition u16x16.h:60
constexpr FASTLED_FORCE_INLINE u16x16 operator>>(int shift) const FL_NOEXCEPT
Definition u16x16.h:96
constexpr bool operator>(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:117
static constexpr FASTLED_FORCE_INLINE u16x16 from_raw(u32 raw) FL_NOEXCEPT
Definition u16x16.h:53
static constexpr i32 SCALE
Definition u16x16.h:22
constexpr bool operator!=(u16x16 b) const FL_NOEXCEPT
Definition u16x16.h:121
static constexpr int FRAC_BITS
Definition u16x16.h:21
static constexpr FASTLED_FORCE_INLINE u16x16 clamp(u16x16 x, u16x16 lo, u16x16 hi) FL_NOEXCEPT
Definition u16x16.h:150
static constexpr FASTLED_FORCE_INLINE u16x16 rsqrt(u16x16 x) FL_NOEXCEPT
Definition u16x16.h:180
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
fl::u64 u64
Definition s16x16x4.h:221
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
FL_OPTIMIZE_FUNCTION constexpr u32 isqrt64(u64 x) FL_NOEXCEPT
Definition isqrt.h:58
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FASTLED_FORCE_INLINE
#define FL_OPTIMIZATION_LEVEL_O3_END
#define FL_NOEXCEPT