FastLED 3.9.15
Loading...
Searching...
No Matches

◆ abs()

static constexpr FASTLED_FORCE_INLINE s0x32 fl::fl::s0x32::abs ( s0x32 x)
inlinestaticconstexpr

Definition at line 152 of file s16x16x4.h.

163 {
164
165// Forward declaration for cross-type operations
166struct s0x32x4;
167
170struct s16x16x4 {
171 simd::simd_u32x4 raw; // 4× i32 values in Q16 format
172
173 // ---- Construction ------------------------------------------------------
174
175 static FASTLED_FORCE_INLINE s16x16x4 from_raw(simd::simd_u32x4 r) {
176 s16x16x4 result;
177 result.raw = r;
178 return result;
179 }
180
181 // Load 4 s16x16 values from memory (unaligned access supported)
182 static FASTLED_FORCE_INLINE s16x16x4 load(const s16x16* ptr) {
183 return from_raw(simd::platforms::load_u32_4(reinterpret_cast<const u32*>(ptr))); // ok reinterpret cast
184 }
185
186 // Store 4 s16x16 values to memory (unaligned access supported)
187 FASTLED_FORCE_INLINE void store(s16x16* ptr) const {
188 simd::platforms::store_u32_4(reinterpret_cast<u32*>(ptr), raw); // ok reinterpret cast
189 }
190
191 // Broadcast single s16x16 value to all 4 lanes
192 static FASTLED_FORCE_INLINE s16x16x4 set1(s16x16 value) {
193 return from_raw(simd::platforms::set1_u32_4(static_cast<u32>(value.raw())));
194 }
195
196 // ---- SIMD arithmetic (s16x16x4 OP s16x16x4 → s16x16x4) -----------------
197
198 FASTLED_FORCE_INLINE s16x16x4 operator+(s16x16x4 b) const {
199 return from_raw(simd::add_i32_4(raw, b.raw));
200 }
201
202 FASTLED_FORCE_INLINE s16x16x4 operator-(s16x16x4 b) const {
203 return from_raw(simd::sub_i32_4(raw, b.raw));
204 }
205
206 FASTLED_FORCE_INLINE s16x16x4 operator*(s16x16x4 b) const {
207 // Q16 × Q16 = Q32 → shift right 16 → Q16
208 return from_raw(simd::mulhi_i32_4(raw, b.raw));
209 }
210
211 FASTLED_FORCE_INLINE s16x16x4 operator-() const {
212 // Unary negation: -x = 0 - x
213 auto zero = simd::set1_u32_4(0);
214 return from_raw(simd::sub_i32_4(zero, raw));
215 }
216
217 FASTLED_FORCE_INLINE s16x16x4 operator>>(int shift) const {
218 return from_raw(simd::sra_i32_4(raw, shift));
219 }
220
221 FASTLED_FORCE_INLINE s16x16x4 operator<<(int shift) const {
222 return from_raw(simd::sll_u32_4(raw, shift));
223 }
224
225 // Cross-type multiply: s16x16x4 × s0x32x4 → s16x16x4 (commutative)
226 // Implemented after s0x32x4 is defined
227 FASTLED_FORCE_INLINE s16x16x4 operator*(s0x32x4 b) const;
228
229 // ---- Math functions -------------------------------------------------------
230
232 FASTLED_FORCE_INLINE s16x16x4 abs() const {
233 // mask = -1 if negative (sign extended), 0 if positive
234 auto mask = simd::sra_i32_4(raw, 31);
235 // flip bits if negative, then add 1 (two's complement)
236 auto flipped = simd::xor_u32_4(raw, mask);
237 return from_raw(simd::sub_i32_4(flipped, mask));
238 }
239
241 FASTLED_FORCE_INLINE s16x16x4 min(s16x16x4 b) const {
242 return from_raw(simd::min_i32_4(raw, b.raw));
243 }
244
246 FASTLED_FORCE_INLINE s16x16x4 max(s16x16x4 b) const {
247 return from_raw(simd::max_i32_4(raw, b.raw));
248 }
249
251 FASTLED_FORCE_INLINE s16x16x4 clamp(s16x16x4 lo, s16x16x4 hi) const {
252 return max(lo).min(hi);
253 }
254
257 FASTLED_FORCE_INLINE s16x16x4 lerp(s16x16x4 b, s16x16 t) const {
258 auto t_vec = s16x16x4::set1(t);
259 auto diff = b - (*this);
260 return (*this) + (diff * t_vec);
261 }
262
265 FASTLED_FORCE_INLINE void sincos(s16x16x4& out_sin, s16x16x4& out_cos) const {
266 // Convert radians to 24-bit angle units (same as scalar s16x16)
267 // RAD_TO_24 = 2^24 / (2π) in Q16
268 static constexpr i32 RAD_TO_24 = 2670177; // from s16x16.h
269
270 // Convert 4 angles: mulhi_i32_4 does (i64*i64) >> 16
271 auto angles_u32 = simd::mulhi_su32_4(raw, simd::set1_u32_4(static_cast<u32>(RAD_TO_24)));
272
273 // Call vectorized sincos
274 auto sc = sincos32_simd(angles_u32);
275
276 // Shift results right by 15 to convert from raw sin32 output to Q16.16
277 out_sin = from_raw(simd::sra_i32_4(sc.sin_vals, 15));
278 out_cos = from_raw(simd::sra_i32_4(sc.cos_vals, 15));
279 }
280
282 FASTLED_FORCE_INLINE s16x16x4 sin() const {
283 s16x16x4 sin_out, cos_out;
284 sincos(sin_out, cos_out);
285 return sin_out;
286 }
287
289 FASTLED_FORCE_INLINE s16x16x4 cos() const {
290 s16x16x4 sin_out, cos_out;
291 sincos(sin_out, cos_out);
292 return cos_out;
293 }
294};
295
296// Include simd_ops.h to implement cross-type operations
297// Must come after all types are defined
298#include "fl/math/fixed_point/simd_ops.h" // allow-include-after-namespace
299
300} // namespace fl
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Memory functions are available in fl:: namespace via fl/stl/cstring.h Using declarations cannot work ...
Definition math.h:71
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT
static uint32_t t
Definition Luminova.h:55
FASTLED_FORCE_INLINE CRGB * operator+(const CRGBSet &pixels, int offset)
Retrieve a pointer to a CRGB array, using a CRGBSet and an LED offset.
Definition pixelset.h:488
ostream & operator<<(ostream &os, const hex_t &) FL_NOEXCEPT
FASTLED_FORCE_INLINE SinCos32_simd sincos32_simd(simd::simd_u32x4 angles) FL_NOEXCEPT
Process 4 angles simultaneously, returning vectorized sin/cos values SIMD-optimized: vectorized angle...
Definition sin32.h:145
enable_if< is_fixed_point< T >::value, void >::type sincos(T angle, T &out_sin, T &out_cos) FL_NOEXCEPT
FASTLED_FORCE_INLINE CRGB operator*(const CRGB &p1, u8 d) FL_NOEXCEPT
Multiply each of the channels by a constant, saturating each channel at 0xFF.
Definition crgb.hpp:198
FASTLED_FORCE_INLINE CRGB operator-(const CRGB &p1, const CRGB &p2) FL_NOEXCEPT
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:190
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
enable_if< is_fixed_point< T >::value, T >::type cos(T angle) FL_NOEXCEPT
enable_if< is_fixed_point< T >::value, T >::type sin(T angle) FL_NOEXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NOEXCEPT
FASTLED_FORCE_INLINE float lerp(float t, float a, float b)
#define FASTLED_FORCE_INLINE
Cross-type SIMD fixed-point operations (implemented after all types are defined)