FastLED 3.9.15
Loading...
Searching...
No Matches
s16x16x4.h
Go to the documentation of this file.
1#pragma once
2
6#include "fl/math/simd.h"
12namespace fl {
14// Forward declaration for cross-type operations
15struct s0x32x4;
17/// 4-wide s16x16 vector (general fixed-point)
18/// Backed by 128-bit SIMD register (4× i32 in Q16 format)
19struct s16x16x4 {
20 simd::simd_u32x4 raw; // 4× i32 values in Q16 format
22 // ---- Construction ------------------------------------------------------
30 // Load 4 s16x16 values from memory (unaligned access supported)
32 return from_raw(simd::platforms::load_u32_4(reinterpret_cast<const u32*>(ptr))); // ok reinterpret cast
33 }
35 // Store 4 s16x16 values to memory (unaligned access supported)
37 simd::platforms::store_u32_4(reinterpret_cast<u32*>(ptr), raw); // ok reinterpret cast
38 }
40 // Broadcast single s16x16 value to all 4 lanes
42 return from_raw(simd::platforms::set1_u32_4(static_cast<u32>(value.raw())));
43 }
45 // ---- SIMD arithmetic (s16x16x4 OP s16x16x4 → s16x16x4) -----------------
48 return from_raw(simd::add_i32_4(raw, b.raw));
49 }
52 return from_raw(simd::sub_i32_4(raw, b.raw));
53 }
54
56 // Q16 × Q16 = Q32 → shift right 16 → Q16
57 return from_raw(simd::mulhi_i32_4(raw, b.raw));
58 }
59
61 // Unary negation: -x = 0 - x
62 auto zero = simd::set1_u32_4(0);
63 return from_raw(simd::sub_i32_4(zero, raw));
64 }
65
67 return from_raw(simd::sra_i32_4(raw, shift));
68 }
71 return from_raw(simd::sll_u32_4(raw, shift));
72 }
74 // Cross-type multiply: s16x16x4 × s0x32x4 → s16x16x4 (commutative)
75 // Implemented after s0x32x4 is defined
78 // ---- Math functions -------------------------------------------------------
79
80
82 // mask = -1 if negative (sign extended), 0 if positive
83 auto mask = simd::sra_i32_4(raw, 31);
84 // flip bits if negative, then add 1 (two's complement)
85 auto flipped = simd::xor_u32_4(raw, mask);
86 return from_raw(simd::sub_i32_4(flipped, mask));
87 }
91 return from_raw(simd::min_i32_4(raw, b.raw));
92 }
96 return from_raw(simd::max_i32_4(raw, b.raw));
97 }
98
99
99 /// Clamp to [lo, hi]
101 return max(lo).min(hi);
103
104
107 auto t_vec = s16x16x4::set1(t);
108 auto diff = b - (*this);
109 return (*this) + (diff * t_vec);
113 /// Results written to out_sin and out_cos
114 FASTLED_FORCE_INLINE void sincos(s16x16x4& out_sin, s16x16x4& out_cos) const {
115 // Convert radians to 24-bit angle units (same as scalar s16x16)
116 // RAD_TO_24 = 2^24 / (2π) in Q16
117 static constexpr i32 RAD_TO_24 = 2670177; // from s16x16.h
119 // Convert 4 angles: mulhi_i32_4 does (i64*i64) >> 16
120 auto angles_u32 = simd::mulhi_su32_4(raw, simd::set1_u32_4(static_cast<u32>(RAD_TO_24)));
122 // Call vectorized sincos
123 auto sc = sincos32_simd(angles_u32);
125 // Shift results right by 15 to convert from raw sin32 output to Q16.16
126 out_sin = from_raw(simd::sra_i32_4(sc.sin_vals, 15));
127 out_cos = from_raw(simd::sra_i32_4(sc.cos_vals, 15));
132 s16x16x4 sin_out, cos_out;
133 sincos(sin_out, cos_out);
134 return sin_out;
139 s16x16x4 sin_out, cos_out;
140 sincos(sin_out, cos_out);
141 return cos_out;
143};
145// Include simd_ops.h to implement cross-type operations
146// Must come after all types are defined
147#include "fl/math/fixed_point/simd_ops.h" // allow-include-after-namespace
149} // namespace fl
Alignment macros and utilities for FastLED.
constexpr i32 raw() const FL_NOEXCEPT
Definition s16x16x4.h:72
platforms::simd_u32x4 simd_u32x4
Definition types.h:26
constexpr int type_rank< T >::value
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
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_FORCE_INLINE
Umbrella header for SIMD subsystem.
Cross-type SIMD fixed-point operations (implemented after all types are defined)
4-wide s0x32 vector (normalized values [-1, 1]) Backed by 128-bit SIMD register (4× i32 in Q31 format...
Definition s16x16x4.h:17
FASTLED_FORCE_INLINE s16x16x4 min(s16x16x4 b) const
Element-wise minimum.
Definition s16x16x4.h:90
FASTLED_FORCE_INLINE s16x16x4 clamp(s16x16x4 lo, s16x16x4 hi) const
Clamp to [lo, hi].
Definition s16x16x4.h:100
FASTLED_FORCE_INLINE s16x16x4 operator-(s16x16x4 b) const
Definition s16x16x4.h:51
FASTLED_FORCE_INLINE s16x16x4 cos() const
Compute cosine of 4 angles (in radians)
Definition s16x16x4.h:138
FASTLED_FORCE_INLINE s16x16x4 operator-() const
Definition s16x16x4.h:60
FASTLED_FORCE_INLINE s16x16x4 operator*(s16x16x4 b) const
Definition s16x16x4.h:55
FASTLED_FORCE_INLINE void store(s16x16 *ptr) const
Definition s16x16x4.h:36
FASTLED_FORCE_INLINE void sincos(s16x16x4 &out_sin, s16x16x4 &out_cos) const
Compute sin and cos of 4 angles (in radians) Results written to out_sin and out_cos.
Definition s16x16x4.h:114
FASTLED_FORCE_INLINE s16x16x4 sin() const
Compute sine of 4 angles (in radians)
Definition s16x16x4.h:131
FASTLED_FORCE_INLINE s16x16x4 abs() const
Absolute value: branchless via mask and xor.
Definition s16x16x4.h:81
FASTLED_FORCE_INLINE s16x16x4 operator>>(int shift) const
Definition s16x16x4.h:66
static FASTLED_FORCE_INLINE s16x16x4 from_raw(simd::simd_u32x4 r)
Definition s16x16x4.h:24
FASTLED_FORCE_INLINE s16x16x4 operator<<(int shift) const
Definition s16x16x4.h:70
simd::simd_u32x4 raw
Definition s16x16x4.h:20
FASTLED_FORCE_INLINE s16x16x4 max(s16x16x4 b) const
Element-wise maximum.
Definition s16x16x4.h:95
static FASTLED_FORCE_INLINE s16x16x4 load(const s16x16 *ptr)
Definition s16x16x4.h:31
static FASTLED_FORCE_INLINE s16x16x4 set1(s16x16 value)
Definition s16x16x4.h:41
FASTLED_FORCE_INLINE s16x16x4 lerp(s16x16x4 b, s16x16 t) const
Linear interpolation: a + (b - a) * t (using Q16 multiply) t: s16x16 interpolation factor in [0,...
Definition s16x16x4.h:106
FASTLED_FORCE_INLINE s16x16x4 operator+(s16x16x4 b) const
Definition s16x16x4.h:47
4-wide s16x16 vector (general fixed-point) Backed by 128-bit SIMD register (4× i32 in Q16 format)
Definition s16x16x4.h:19