29constexpr q31_t ONE_Q31{2147483647};
30constexpr float ONE_Q31f{2147483647.0f};
31constexpr q31_t ONE_Q15{65536};
32constexpr q31_t NEGATIVE_ONE_Q31{-2147483648};
33constexpr q31_t ONE_OVER_SQRT2_Q31{1518500250};
36static inline q31_t toPositive(q31_t a) __attribute__((always_inline, unused));
37static inline q31_t toPositive(q31_t a) {
38 return ((a / 2) + (1073741824));
41[[gnu::always_inline]]
constexpr int32_t clz(uint32_t input) {
42 return std::countl_zero(input);
50static inline q31_t multiply_32x32_rshift32(q31_t a, q31_t b) __attribute__((always_inline, unused));
51static inline q31_t multiply_32x32_rshift32(q31_t a, q31_t b) {
53 asm(
"smmul %0, %1, %2" :
"=r"(out) :
"r"(a),
"r"(b));
58static inline q31_t multiply_32x32_rshift32_rounded(q31_t a, q31_t b) __attribute__((always_inline, unused));
59static inline q31_t multiply_32x32_rshift32_rounded(q31_t a, q31_t b) {
61 asm(
"smmulr %0, %1, %2" :
"=r"(out) :
"r"(a),
"r"(b));
67static inline q31_t q31_mult(q31_t a, q31_t b) __attribute__((always_inline, unused));
68static inline q31_t q31_mult(q31_t a, q31_t b) {
70 asm(
"smmul %0, %1, %2" :
"=r"(out) :
"r"(a),
"r"(b));
75static inline q31_t q31tRescale(q31_t a, uint32_t proportion) __attribute__((always_inline, unused));
76static inline q31_t q31tRescale(q31_t a, uint32_t proportion) {
78 asm(
"smmul %0, %1, %2" :
"=r"(out) :
"r"(a),
"r"(proportion));
83static inline q31_t multiply_accumulate_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b)
84 __attribute__((always_inline, unused));
85static inline q31_t multiply_accumulate_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b) {
87 asm(
"smmlar %0, %1, %2, %3" :
"=r"(out) :
"r"(a),
"r"(b),
"r"(sum));
92static inline q31_t multiply_accumulate_32x32_rshift32(q31_t sum, q31_t a, q31_t b)
93 __attribute__((always_inline, unused));
94static inline q31_t multiply_accumulate_32x32_rshift32(q31_t sum, q31_t a, q31_t b) {
96 asm(
"smmla %0, %1, %2, %3" :
"=r"(out) :
"r"(a),
"r"(b),
"r"(sum));
101static inline q31_t multiply_subtract_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b)
102 __attribute__((always_inline, unused));
103static inline q31_t multiply_subtract_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b) {
105 asm(
"smmlsr %0, %1, %2, %3" :
"=r"(out) :
"r"(a),
"r"(b),
"r"(sum));
110template <u
int8_t bits>
111static inline int32_t signed_saturate(int32_t val) __attribute__((always_inline, unused));
112template <u
int8_t bits>
113static inline int32_t signed_saturate(int32_t val) {
114 return __ssat(val, bits);
117static inline int32_t add_saturate(int32_t a, int32_t b) __attribute__((always_inline, unused));
118static inline int32_t add_saturate(int32_t a, int32_t b) {
122static inline int32_t subtract_saturate(int32_t a, int32_t b) __attribute__((always_inline, unused));
123static inline int32_t subtract_saturate(int32_t a, int32_t b) {
129static inline q31_t q31_from_float(
float value) {
130 asm(
"vcvt.s32.f32 %0, %0, #31" :
"+t"(value));
131 return std::bit_cast<q31_t>(value);
136static inline float q31_to_float(q31_t value) {
137 asm(
"vcvt.f32.s32 %0, %0, #31" :
"+t"(value));
138 return std::bit_cast<float>(value);
143static inline q31_t multiply_32x32_rshift32(q31_t a, q31_t b) {
144 return (int32_t)(((int64_t)a * b) >> 32);
148static inline q31_t multiply_32x32_rshift32_rounded(q31_t a, q31_t b) {
149 return (int32_t)(((int64_t)a * b + 0x80000000) >> 32);
154static inline q31_t q31_mult(q31_t a, q31_t b) {
155 return multiply_32x32_rshift32(a, b) * 2;
160static inline q31_t q31tRescale(q31_t a, uint32_t proportion) {
161 return multiply_32x32_rshift32(a, (q31_t)proportion);
169static inline q31_t multiply_accumulate_32x32_rshift32(q31_t sum, q31_t a, q31_t b) {
170 return (q31_t)(sum + (((int64_t)a * b) >> 32));
174static inline q31_t multiply_accumulate_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b) {
175 return (q31_t)(sum + (((int64_t)a * b + 0x80000000) >> 32));
179static inline q31_t multiply_subtract_32x32_rshift32(q31_t sum, q31_t a, q31_t b) {
180 return (q31_t)(sum + ((-((int64_t)a * b)) >> 32));
184static inline q31_t multiply_subtract_32x32_rshift32_rounded(q31_t sum, q31_t a, q31_t b) {
185 return (q31_t)(sum + ((0x80000000LL - (int64_t)a * b) >> 32));
191template <u
int8_t bits>
192static inline int32_t signed_saturate(int32_t val) {
193 constexpr int32_t hi =
static_cast<int32_t
>((
static_cast<uint32_t
>(1) << (bits - 1)) - 1);
194 constexpr int32_t lo = -hi - 1;
195 return val > hi ? hi : (val < lo ? lo : val);
200static inline int32_t add_saturate(int32_t a, int32_t b) __attribute__((always_inline, unused));
201static inline int32_t add_saturate(int32_t a, int32_t b) {
202 int64_t r =
static_cast<int64_t
>(a) + b;
203 return r > INT32_MAX ? INT32_MAX : (r < INT32_MIN ? INT32_MIN : static_cast<int32_t>(r));
207static inline int32_t subtract_saturate(int32_t a, int32_t b) __attribute__((always_inline, unused));
208static inline int32_t subtract_saturate(int32_t a, int32_t b) {
209 int64_t r =
static_cast<int64_t
>(a) - b;
210 return r > INT32_MAX ? INT32_MAX : (r < INT32_MIN ? INT32_MIN : static_cast<int32_t>(r));
213[[gnu::always_inline]]
constexpr q31_t q31_from_float(
float value) {
217 auto bits = std::bit_cast<uint32_t>(value);
220 bool negative = bits & 0x80000000;
223 int32_t exponent =
static_cast<int32_t
>((bits >> 23) & 0xFF) - 127;
227 return negative ? std::numeric_limits<q31_t>::min() : std::numeric_limits<q31_t>::max();
232 int32_t shift = -exponent;
238 uint32_t mantissa = (bits << 8) | 0x80000000;
239 q31_t output_value =
static_cast<q31_t
>(mantissa >> shift);
240 return (negative) ? -output_value : output_value;
243static inline float q31_to_float(q31_t value) {
244 return static_cast<float>(value) / 2147483648.0f;
253template <std::
size_t FractionalBits,
bool Rounded = false,
bool FastApproximation = true>
255 static_assert(FractionalBits > 0,
"FractionalBits must be greater than 0");
256 static_assert(FractionalBits < 32,
"FractionalBits must be less than 32");
258 using BaseType = int32_t;
259 using IntermediateType = int64_t;
263 if constexpr (Rounded) {
264 return multiply_accumulate_32x32_rshift32_rounded(a, b, c);
267 return multiply_accumulate_32x32_rshift32(a, b, c);
272 [[gnu::always_inline]]
static int32_t signed_most_significant_word_multiply(int32_t a, int32_t b) {
273 if constexpr (Rounded) {
274 return multiply_32x32_rshift32_rounded(a, b);
277 return multiply_32x32_rshift32(a, b);
281 static constexpr BaseType one() noexcept {
282 if constexpr (fractional_bits == 31) {
283 return std::numeric_limits<BaseType>::max();
286 return 1 << fractional_bits;
291 constexpr static std::size_t fractional_bits = FractionalBits;
292 constexpr static std::size_t integral_bits = 32 - FractionalBits;
293 constexpr static bool rounded = Rounded;
294 constexpr static bool fast_approximation = FastApproximation;
307 static constexpr double vfp_scale() noexcept {
return static_cast<double>(uint64_t{1} << fractional_bits); }
313 if (scaled >=
static_cast<double>(std::numeric_limits<BaseType>::max()) + 1.0) {
314 return std::numeric_limits<BaseType>::max();
316 if (scaled <=
static_cast<double>(std::numeric_limits<BaseType>::min()) - 1.0) {
317 return std::numeric_limits<BaseType>::min();
319 return static_cast<BaseType
>(scaled);
324 if (value != value) {
332 return static_cast<float>(
static_cast<double>(
raw) / vfp_scale());
336 static constexpr double raw_to_double(BaseType
raw)
noexcept {
return static_cast<double>(
raw) / vfp_scale(); }
343 template <std::
size_t OtherFractionalBits>
345 if constexpr (FractionalBits == OtherFractionalBits) {
348 else if constexpr (FractionalBits > OtherFractionalBits) {
350 constexpr int32_t shift = FractionalBits - OtherFractionalBits;
351 value_ = signed_saturate<32 - shift>(value_);
352 value_ = (value_ << shift) + (value_ % 2);
354 else if constexpr (rounded) {
356 constexpr int32_t shift = OtherFractionalBits - FractionalBits;
357 value_ >>= shift + ((1 << shift) - 1);
361 value_ >>= (OtherFractionalBits - FractionalBits);
367 template <std::
integral T>
368 constexpr explicit FixedPoint(T value) noexcept : value_(
static_cast<BaseType
>(value) << fractional_bits) {}
377 if (!std::is_constant_evaluated()) {
378 asm(
"vcvt.s32.f32 %0, %0, %1" :
"+t"(value) :
"I"(fractional_bits));
379 value_ = std::bit_cast<int32_t>(value);
388 constexpr explicit operator float() const noexcept {
390 if (!std::is_constant_evaluated()) {
391 int32_t output = value_;
392 asm(
"vcvt.f32.s32 %0, %0, %1" :
"+t"(output) :
"I"(fractional_bits));
393 return std::bit_cast<float>(output);
403 if (!std::is_constant_evaluated()) {
404 auto output = std::bit_cast<int64_t>(value);
408 asm(
"vcvt.s32.f64 %P0, %P0, %1" :
"+w"(output) :
"I"(fractional_bits));
409 value_ =
static_cast<BaseType
>(output);
418 constexpr explicit operator double() const noexcept {
420 if (!std::is_constant_evaluated()) {
421 auto output = std::bit_cast<double>((int64_t)value_);
422 asm(
"vcvt.f64.s32 %P0, %P0, %1" :
"+w"(output) :
"I"(fractional_bits));
430 template <std::
size_t OutputFractionalBits>
436 template <std::
integral T>
437 explicit operator T() const noexcept {
438 return static_cast<T
>(value_ >> fractional_bits);
441 explicit operator bool() const noexcept {
return value_ != 0; }
447 [[nodiscard]]
constexpr BaseType
raw() const noexcept {
return value_; }
463 value_ = add_saturate(value_, rhs.value_);
470 return from_raw(subtract_saturate(value_, rhs.value_));
476 value_ = subtract_saturate(value_, rhs.value_);
483 if constexpr (fast_approximation && fractional_bits > 16) {
485 constexpr int32_t shift = fractional_bits - ((fractional_bits * 2) - 32);
486 return from_raw(signed_most_significant_word_multiply(value_, rhs.value_) << shift);
489 if constexpr (rounded) {
490 IntermediateType value = (
static_cast<IntermediateType
>(value_) * rhs.value_) >> (fractional_bits - 1);
491 return from_raw(
static_cast<BaseType
>((value >> 1) + (value % 2)));
494 IntermediateType value = (
static_cast<IntermediateType
>(value_) * rhs.value_) >> fractional_bits;
495 return from_raw(
static_cast<BaseType
>(value));
507 template <std::size_t OutputFractionalBits = FractionalBits, std::size_t OtherFractionalBits,
bool OtherRounded,
508 bool OtherApproximating>
509 requires(OtherRounded == Rounded && OtherApproximating == FastApproximation)
512 if constexpr (fast_approximation) {
513 constexpr int32_t l_shift = OutputFractionalBits - ((FractionalBits + OtherFractionalBits) - 32);
514 static_assert(l_shift < 32 && l_shift > -32);
515 BaseType value = signed_most_significant_word_multiply(value_, rhs.
raw());
516 return from_raw(l_shift > 0 ? value << l_shift : value >> -l_shift);
519 constexpr int32_t r_shift = (FractionalBits + OtherFractionalBits) - OutputFractionalBits;
520 if constexpr (rounded) {
521 IntermediateType value = (
static_cast<IntermediateType
>(value_) * rhs.
raw())
523 return from_raw(
static_cast<BaseType
>((value / 2) + (value % 2)));
526 IntermediateType value = (
static_cast<IntermediateType
>(value_) * rhs.
raw()) >> r_shift;
527 return from_raw(
static_cast<BaseType
>(value));
532 template <std::
size_t OtherFractionalBits>
541 if (rhs.value_ == 0) {
542 return from_raw(std::numeric_limits<BaseType>::max());
544 if constexpr (rounded) {
545 IntermediateType value = (
static_cast<IntermediateType
>(value_) << (fractional_bits + 1)) / rhs.value_;
546 return from_raw(
static_cast<BaseType
>((value / 2) + (value % 2)));
549 IntermediateType value = (
static_cast<IntermediateType
>(value_) << fractional_bits) / rhs.value_;
550 return from_raw(
static_cast<BaseType
>(value));
561 template <std::
size_t OtherFractionalBitsA, std::
size_t OtherFractionalBitsB>
566 if constexpr (fast_approximation && (OtherFractionalBitsA + OtherFractionalBitsB) - 32 == FractionalBits) {
569 return *
this +
static_cast<FixedPoint>(a * b);
574 if constexpr (fast_approximation && (((FractionalBits * 2) - 32) == (FractionalBits - 1))) {
575 return from_raw(
static_cast<FixedPoint<FractionalBits - 1
>>(*this).MultiplyAdd(a, b).raw() << 1);
577 return *
this + (a * b);
587 template <std::
size_t OtherFractionalBits>
589 int integral_value = value_ >> fractional_bits;
590 int other_integral_value = rhs.
raw() >> OtherFractionalBits;
591 int fractional_value = value_ & ((1 << fractional_bits) - 1);
592 int other_fractional_value = rhs.
raw() & ((1 << OtherFractionalBits) - 1);
595 if (fractional_bits > OtherFractionalBits) {
596 fractional_value >>= fractional_bits - OtherFractionalBits;
599 other_fractional_value >>= OtherFractionalBits - fractional_bits;
602 return integral_value == other_integral_value && fractional_value == other_fractional_value;
606 template <std::
size_t OtherFractionalBits>
609 int integral_value = value_ >> fractional_bits;
610 int other_integral_value = rhs.
raw() >> OtherFractionalBits;
611 int fractional_value = value_ & ((1 << fractional_bits) - 1);
612 int other_fractional_value = rhs.
raw() & ((1 << OtherFractionalBits) - 1);
615 if (fractional_bits > OtherFractionalBits) {
616 fractional_value >>= fractional_bits - OtherFractionalBits;
619 other_fractional_value >>= OtherFractionalBits - fractional_bits;
622 if (integral_value < other_integral_value) {
623 return std::strong_ordering::less;
625 if (integral_value > other_integral_value) {
626 return std::strong_ordering::greater;
628 if (fractional_value < other_fractional_value) {
629 return std::strong_ordering::less;
631 if (fractional_value > other_fractional_value) {
632 return std::strong_ordering::greater;
634 return std::strong_ordering::equal;
638 template <
typename T>
639 requires std::integral<T> || std::floating_point<T>
Fixed point number with a configurable number of fractional bits.
Definition fixedpoint.h:254
constexpr bool operator==(const FixedPoint &rhs) const noexcept
Equality operator.
Definition fixedpoint.h:581
constexpr FixedPoint operator*=(const FixedPoint< OtherFractionalBits > &rhs)
Multiplication operator Multiply two fixed point numbers with different number of fractional bits.
Definition fixedpoint.h:533
constexpr FixedPoint(float value) noexcept
Convert from a float to a fixed point number.
Definition fixedpoint.h:372
constexpr std::strong_ordering operator<=>(const FixedPoint &rhs) const noexcept
Three-way comparison operator.
Definition fixedpoint.h:584
static constexpr BaseType saturate_to_raw(double scaled) noexcept
Round toward zero and saturate a scaled real value to int32, as VFP's f->fixed convert does.
Definition fixedpoint.h:310
static constexpr float raw_to_float(BaseType raw) noexcept
vcvt.f32.s32: fixed point -> float (a single round-to-nearest, as the hardware does).
Definition fixedpoint.h:331
constexpr FixedPoint operator/(const FixedPoint &rhs) const
Division operator Divide two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:540
constexpr FixedPoint(T value) noexcept
Convert an integer to a fixed point number.
Definition fixedpoint.h:368
constexpr FixedPoint< OutputFractionalBits > as() const
Convert to a fixed point number with a different number of fractional bits.
Definition fixedpoint.h:431
constexpr FixedPoint MultiplyAdd(const FixedPoint< OtherFractionalBitsA > &a, const FixedPoint< OtherFractionalBitsB > &b) const
Fused multiply-add operation for fixed point numbers with a different number of fractional bits.
Definition fixedpoint.h:562
constexpr FixedPoint operator/=(const FixedPoint &rhs)
Division operator Divide two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:555
static int32_t signed_most_significant_word_multiply_add(int32_t a, int32_t b, int32_t c)
a + b * c
Definition fixedpoint.h:262
constexpr bool operator==(const FixedPoint< OtherFractionalBits > &rhs) const noexcept
Equality operator for fixed point numbers with different number of fractional bits.
Definition fixedpoint.h:588
constexpr FixedPoint(FixedPoint< OtherFractionalBits > other) noexcept
Construct a fixed point number from another fixed point number.
Definition fixedpoint.h:344
constexpr FixedPoint operator+(const FixedPoint &rhs) const
Addition operator Add two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:458
static constexpr BaseType float_to_raw(double value) noexcept
vcvt.s32.f32 / vcvt.s32.f64: floating point -> fixed point. (float promotes to double exactly....
Definition fixedpoint.h:323
constexpr FixedPoint operator-() const
Negation operator.
Definition fixedpoint.h:444
constexpr FixedPoint operator*(const FixedPoint &rhs) const
Multiplication operator Multiply two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:482
constexpr BaseType raw() const noexcept
Get the internal value.
Definition fixedpoint.h:447
constexpr std::strong_ordering operator<=>(const FixedPoint< OtherFractionalBits > &rhs) const noexcept
Three-way comparison operator for fixed point numbers with different number of fractional bits.
Definition fixedpoint.h:607
static constexpr FixedPoint from_raw(BaseType raw) noexcept
Construct from a raw value.
Definition fixedpoint.h:450
constexpr FixedPoint operator-(const FixedPoint &rhs) const
Subtraction operator Subtract two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:469
constexpr FixedPoint operator*=(const FixedPoint &rhs)
Multiplication operator Multiply two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:500
constexpr FixedPoint(double value) noexcept
Convert from a double to a fixed point number.
Definition fixedpoint.h:401
constexpr FixedPoint operator+=(const FixedPoint &rhs)
Addition operator Add two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:462
constexpr FixedPoint MultiplyAdd(const FixedPoint &a, const FixedPoint &b) const
Fused multiply-add operation for fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:573
constexpr FixedPoint operator-=(const FixedPoint &rhs)
Subtraction operator Subtract two fixed point numbers with the same number of fractional bits.
Definition fixedpoint.h:475
static constexpr double raw_to_double(BaseType raw) noexcept
vcvt.f64.s32: fixed point -> double (exact).
Definition fixedpoint.h:336
constexpr FixedPoint()=default
Default constructor.
constexpr bool operator==(const T &rhs) const noexcept
Equality operator for integers and floating point numbers.
Definition fixedpoint.h:640