27template<
typename Derived,
int IntBits,
int FracBits>
57 return Derived(
raw,
typename Derived::RawTag());
69 return Derived::from_raw(
static_cast<raw_type>(
74 return Derived::from_raw(
static_cast<raw_type>(
79 return Derived::from_raw(
static_cast<raw_type>(
84 return Derived::from_raw(
static_cast<raw_type>(
89 return Derived::from_raw(
static_cast<raw_type>(
94 return Derived::from_raw(
mValue >> shift);
100 return Derived::from_raw(
mValue * scalar);
104 return Derived::from_raw(scalar * fp.mValue);
119 return Derived::from_raw(a.mValue % b.mValue);
123 return Derived::from_raw(
x.mValue & ~(
raw_type((Derived::SCALE) - 1)));
127 return Derived::from_raw((
x.mValue & ~(
raw_type((Derived::SCALE) - 1))) +
128 ((
x.mValue &
raw_type((Derived::SCALE) - 1)) ? (Derived::SCALE) : 0));
132 return Derived::from_raw(
x.mValue &
raw_type((Derived::SCALE) - 1));
136 return Derived::from_raw(
x.mValue < 0 ? -
x.mValue :
x.mValue);
140 return x.mValue > 0 ? 1 : (
x.mValue < 0 ? -1 : 0);
144 return a + (b - a) *
t;
148 return x < lo ? lo : (
x > hi ? hi :
x);
152 return x < edge ? Derived() : Derived(1.0f);
156 constexpr Derived zero(0.0f);
157 constexpr Derived one(1.0f);
158 constexpr Derived two(2.0f);
159 constexpr Derived three(3.0f);
160 Derived
t =
clamp((
x - edge0) / (edge1 - edge0), zero, one);
161 return t *
t * (three - two *
t);
167 constexpr Derived one(1.0f);
168 constexpr Derived pi_over_2(1.5707963f);
169 bool neg =
x.mValue < 0;
181 constexpr Derived pi(3.1415926f);
182 constexpr Derived pi_over_2(1.5707963f);
183 if (
x.mValue == 0 &&
y.mValue == 0)
return Derived();
184 if (
x.mValue == 0)
return y.mValue > 0 ? pi_over_2 : -pi_over_2;
185 if (
y.mValue == 0)
return x.mValue > 0 ? Derived() : pi;
194 if (
x.mValue < 0) a = pi - a;
195 if (
y.mValue < 0) a = -a;
200 constexpr Derived one(1.0f);
205 constexpr Derived one(1.0f);
216 return Derived::from_raw(
static_cast<raw_type>(
222 return Derived::from_raw(
static_cast<raw_type>(
229 return sqrt(
x).mValue == 0
235 if (base.mValue <= 0)
return Derived();
236 constexpr Derived one(1.0f);
237 if (
exp.mValue == 0)
return one;
238 if (base == one)
return one;
245 if (base.mValue >=
static_cast<raw_type>(kOneRaw - kNearOneEpsilon) &&
246 base.mValue <= kOneRaw) {
255 return floor(*
static_cast<const Derived*
>(
this));
259 return ceil(*
static_cast<const Derived*
>(
this));
263 return fract(*
static_cast<const Derived*
>(
this));
267 return abs(*
static_cast<const Derived*
>(
this));
271 return sign(*
static_cast<const Derived*
>(
this));
275 return sin(*
static_cast<const Derived*
>(
this));
279 return cos(*
static_cast<const Derived*
>(
this));
283 return atan(*
static_cast<const Derived*
>(
this));
287 return asin(*
static_cast<const Derived*
>(
this));
291 return acos(*
static_cast<const Derived*
>(
this));
295 return sqrt(*
static_cast<const Derived*
>(
this));
299 return rsqrt(*
static_cast<const Derived*
>(
this));
305 return Derived::from_raw(
static_cast<raw_type>(
310 return Derived::from_raw(
static_cast<raw_type>(
336 : (v & 0x00000002u) ? r + 1
363 constexpr poly_type c0 =
static_cast<poly_type
>(1.44179 * (1LL << IFRAC));
364 constexpr poly_type c1 =
static_cast<poly_type
>(-0.69907 * (1LL << IFRAC));
365 constexpr poly_type c2 =
static_cast<poly_type
>(0.36348 * (1LL << IFRAC));
366 constexpr poly_type c3 =
static_cast<poly_type
>(-0.10660 * (1LL << IFRAC));
369 poly_type t_ifrac =
static_cast<poly_type
>(
t) << (IFRAC -
FRAC_BITS);
376 template<
typename PolyType>
378 PolyType c0, PolyType c1, PolyType c2, PolyType c3,
382 acc = c2 +
static_cast<PolyType
>((
static_cast<i64>(acc) * t_ifrac) >> IFRAC);
383 acc = c1 +
static_cast<PolyType
>((
static_cast<i64>(acc) * t_ifrac) >> IFRAC);
384 acc = c0 +
static_cast<PolyType
>((
static_cast<i64>(acc) * t_ifrac) >> IFRAC);
385 PolyType frac_part =
static_cast<PolyType
>((
static_cast<i64>(acc) * t_ifrac) >> IFRAC);
391 template<
typename PolyType>
393 PolyType c0, PolyType c1, PolyType c2, PolyType c3,
397 acc = c2 + ((acc * t_ifrac) >> IFRAC);
398 acc = c1 + ((acc * t_ifrac) >> IFRAC);
399 acc = c0 + ((acc * t_ifrac) >> IFRAC);
400 PolyType frac_part = (acc * t_ifrac) >> IFRAC;
411 Derived fl_val =
floor(
x);
412 Derived fr =
x - fl_val;
427 constexpr poly_type d0 =
static_cast<poly_type
>(0.69316 * (1LL << IFRAC));
428 constexpr poly_type d1 =
static_cast<poly_type
>(0.24071 * (1LL << IFRAC));
429 constexpr poly_type d2 =
static_cast<poly_type
>(0.05336 * (1LL << IFRAC));
430 constexpr poly_type d3 =
static_cast<poly_type
>(0.01276 * (1LL << IFRAC));
433 poly_type fr_ifrac =
static_cast<poly_type
>(fr.mValue) << (IFRAC -
FRAC_BITS);
440 template<
typename PolyType>
442 PolyType d0, PolyType d1, PolyType d2, PolyType d3,
445 constexpr PolyType one_ifrac =
static_cast<PolyType
>(1) << IFRAC;
447 acc = d2 +
static_cast<PolyType
>((
static_cast<i64>(acc) * fr_ifrac) >> IFRAC);
448 acc = d1 +
static_cast<PolyType
>((
static_cast<i64>(acc) * fr_ifrac) >> IFRAC);
449 acc = d0 +
static_cast<PolyType
>((
static_cast<i64>(acc) * fr_ifrac) >> IFRAC);
450 PolyType frac_pow_ifrac = one_ifrac +
static_cast<PolyType
>((
static_cast<i64>(acc) * fr_ifrac) >> IFRAC);
457 template<
typename PolyType>
459 PolyType d0, PolyType d1, PolyType d2, PolyType d3,
462 constexpr PolyType one_ifrac =
static_cast<PolyType
>(1) << IFRAC;
464 acc = d2 + ((acc * fr_ifrac) >> IFRAC);
465 acc = d1 + ((acc * fr_ifrac) >> IFRAC);
466 acc = d0 + ((acc * fr_ifrac) >> IFRAC);
467 PolyType frac_pow_ifrac = one_ifrac + ((acc * fr_ifrac) >> IFRAC);
478 return static_cast<u32
>(
486 constexpr Derived c0(0.9998779297f);
487 constexpr Derived c1(-0.3269348145f);
488 constexpr Derived c2(0.1594085693f);
489 constexpr Derived c3(-0.0472106934f);
491 return t * (c0 + t2 * (c1 + t2 * (c2 + t2 * c3)));
typename traits::unsigned_raw_type unsigned_raw_type
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v)
static constexpr FASTLED_FORCE_INLINE Derived clamp(Derived x, Derived lo, Derived hi)
static FASTLED_FORCE_INLINE Derived log2_horner(raw_type int_part, PolyType t_ifrac, PolyType c0, PolyType c1, PolyType c2, PolyType c3, fl::true_type)
constexpr fixed_point_base() FL_NOEXCEPT=default
constexpr bool operator!=(Derived b) const
static FASTLED_FORCE_INLINE void sincos(Derived angle, Derived &out_sin, Derived &out_cos)
static constexpr FASTLED_FORCE_INLINE Derived rsqrt(Derived x)
FASTLED_FORCE_INLINE Derived atan() const
constexpr FASTLED_FORCE_INLINE Derived operator*(Derived b) const
typename traits::intermediate_type intermediate_type
constexpr FASTLED_FORCE_INLINE Derived sqrt() const
static constexpr FASTLED_FORCE_INLINE Derived sqrt(Derived x)
static FASTLED_FORCE_INLINE Derived smoothstep(Derived edge0, Derived edge1, Derived x)
static constexpr FASTLED_FORCE_INLINE Derived lerp(Derived a, Derived b, Derived t)
constexpr fixed_point_base(raw_type raw, RawTag)
fixed_point_traits< IntBits, FracBits > traits
static constexpr int FRAC_BITS
constexpr float to_float() const
typename traits::raw_type raw_type
static FASTLED_FORCE_INLINE Derived atan2(Derived y, Derived x)
constexpr FASTLED_FORCE_INLINE Derived fract() const
constexpr FASTLED_FORCE_INLINE Derived operator-(Derived b) const
constexpr bool operator<(Derived b) const
constexpr FASTLED_FORCE_INLINE Derived ceil() const
static constexpr FASTLED_FORCE_INLINE Derived sqrt_impl(Derived x, fl::false_type)
static FASTLED_FORCE_INLINE Derived sin(Derived angle)
static constexpr FASTLED_FORCE_INLINE Derived fract(Derived x)
FASTLED_FORCE_INLINE Derived asin() const
constexpr FASTLED_FORCE_INLINE Derived operator/(Derived b) const
static constexpr FASTLED_FORCE_INLINE Derived ceil(Derived x)
constexpr FASTLED_FORCE_INLINE Derived operator-() const
static constexpr int _highest_bit_step(u32 v, int r)
constexpr FASTLED_FORCE_INLINE Derived floor() const
constexpr FASTLED_FORCE_INLINE Derived rsqrt() const
static FASTLED_FORCE_INLINE Derived acos(Derived x)
static FASTLED_FORCE_INLINE Derived log2_fp(Derived x)
static constexpr FASTLED_FORCE_INLINE Derived from_raw(raw_type raw)
constexpr bool operator>=(Derived b) const
static constexpr i32 RAD_TO_24
constexpr raw_type raw() const
static FASTLED_FORCE_INLINE Derived atan_unit(Derived t)
static constexpr FASTLED_FORCE_INLINE int sign(Derived x)
typename traits::unsigned_intermediate_type unsigned_intermediate_type
static constexpr FASTLED_FORCE_INLINE u32 angle_to_a24(Derived angle)
typename traits::poly_intermediate_type poly_intermediate_type
static FASTLED_FORCE_INLINE Derived exp2_horner(raw_type int_pow, PolyType fr_ifrac, PolyType d0, PolyType d1, PolyType d2, PolyType d3, fl::false_type)
static FASTLED_FORCE_INLINE Derived exp2_horner(raw_type int_pow, PolyType fr_ifrac, PolyType d0, PolyType d1, PolyType d2, PolyType d3, fl::true_type)
constexpr bool operator>(Derived b) const
constexpr bool operator==(Derived b) const
static constexpr FASTLED_FORCE_INLINE Derived abs(Derived x)
FASTLED_FORCE_INLINE Derived cos() const
constexpr raw_type to_int() const
static constexpr FASTLED_FORCE_INLINE Derived step(Derived edge, Derived x)
constexpr FASTLED_FORCE_INLINE int sign() const
static FASTLED_FORCE_INLINE Derived pow(Derived base, Derived exp)
static FASTLED_FORCE_INLINE Derived log2_horner(raw_type int_part, PolyType t_ifrac, PolyType c0, PolyType c1, PolyType c2, PolyType c3, fl::false_type)
static constexpr FASTLED_FORCE_INLINE Derived sqrt_impl(Derived x, fl::true_type)
constexpr FASTLED_FORCE_INLINE Derived abs() const
constexpr FASTLED_FORCE_INLINE Derived operator>>(int shift) const
static constexpr FASTLED_FORCE_INLINE Derived mod(Derived a, Derived b)
FASTLED_FORCE_INLINE Derived sin() const
constexpr FASTLED_FORCE_INLINE Derived operator+(Derived b) const
static FASTLED_FORCE_INLINE Derived cos(Derived angle)
static FASTLED_FORCE_INLINE Derived exp2_fp(Derived x)
constexpr FASTLED_FORCE_INLINE Derived operator*(raw_type scalar) const
static constexpr FASTLED_FORCE_INLINE Derived floor(Derived x)
FASTLED_FORCE_INLINE Derived acos() const
static constexpr int INT_BITS
static FASTLED_FORCE_INLINE Derived asin(Derived x)
static FASTLED_FORCE_INLINE Derived atan(Derived x)
friend constexpr Derived operator*(raw_type scalar, Derived fp)
constexpr bool operator<=(Derived b) const
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
integral_constant< bool, B > bool_constant
integral_constant< bool, true > true_type
integral_constant< bool, false > false_type
FASTLED_FORCE_INLINE i32 cos32(u32 angle) FL_NOEXCEPT
FASTLED_FORCE_INLINE i32 sin32(u32 angle) FL_NOEXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)
FASTLED_FORCE_INLINE SinCos32 sincos32(u32 angle) FL_NOEXCEPT
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
FL_OPTIMIZE_FUNCTION constexpr u16 isqrt32(u32 x) FL_NOEXCEPT
FL_OPTIMIZE_FUNCTION constexpr u32 isqrt64(u64 x) FL_NOEXCEPT
Base definition for an LED controller.
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FASTLED_FORCE_INLINE
#define FL_OPTIMIZATION_LEVEL_O3_END
fl::conditional_t<(TOTAL_BITS<=16), i16, i32 > raw_type
static constexpr int SIN_COS_SHIFT
fl::conditional_t<(IFRAC<=16), i32, i64 > poly_intermediate_type
static constexpr auto MAX_OVERFLOW
fl::conditional_t<(TOTAL_BITS<=16), u16, u32 > unsigned_raw_type
static constexpr int IFRAC
fl::conditional_t<(TOTAL_BITS<=16), i32, i64 > intermediate_type
fl::conditional_t<(TOTAL_BITS<=16), u32, u64 > unsigned_intermediate_type