Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
fixedpoint.h
1/*
2 * Copyright © 2014-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <bit>
20#include <cmath>
21#include <compare>
22#include <cstdint>
23#include <limits>
24#include <type_traits>
25
26// signed 31 fractional bits (e.g. one would be 1<<31 but can't be represented)
27using q31_t = int32_t;
28
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};
34
35// This converts the range -2^31 to 2^31 to the range 0-2^31
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));
39}
40
41[[gnu::always_inline]] constexpr int32_t clz(uint32_t input) {
42 return std::countl_zero(input);
43}
44
45// this is only defined for 32 bit arm
46#if defined(__arm__)
47#include <arm_acle.h>
48// This multiplies two numbers in signed Q31 fixed point as if they were q32, so the return value is half what it should
49// be. Use this when several corrective shifts can be accumulated and then combined
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) {
52 q31_t out;
53 asm("smmul %0, %1, %2" : "=r"(out) : "r"(a), "r"(b));
54 return out;
55}
56
57// This multiplies two numbers in signed Q31 fixed point and rounds the result
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) {
60 q31_t out;
61 asm("smmulr %0, %1, %2" : "=r"(out) : "r"(a), "r"(b));
62 return out;
63}
64
65// This multiplies two numbers in signed Q31 fixed point, returning the result in q31. This is more useful for readable
66// multiplies
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) {
69 q31_t out;
70 asm("smmul %0, %1, %2" : "=r"(out) : "r"(a), "r"(b));
71 return out * 2;
72}
73
74// This multiplies a number in q31 by a number in q32 (e.g. unsigned, 2^32 representing one), returning a scaled value a
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) {
77 q31_t out;
78 asm("smmul %0, %1, %2" : "=r"(out) : "r"(a), "r"(proportion));
79 return out;
80}
81
82// Multiplies A and B, adds to sum, and returns output
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) {
86 q31_t out;
87 asm("smmlar %0, %1, %2, %3" : "=r"(out) : "r"(a), "r"(b), "r"(sum));
88 return out;
89}
90
91// Multiplies A and B, adds to sum, and returns output
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) {
95 q31_t out;
96 asm("smmla %0, %1, %2, %3" : "=r"(out) : "r"(a), "r"(b), "r"(sum));
97 return out;
98}
99
100// Multiplies A and B, subtracts from sum, and returns output
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) {
104 q31_t out;
105 asm("smmlsr %0, %1, %2, %3" : "=r"(out) : "r"(a), "r"(b), "r"(sum));
106 return out;
107}
108
109// computes limit((val >> rshift), 2**bits)
110template <uint8_t bits>
111static inline int32_t signed_saturate(int32_t val) __attribute__((always_inline, unused));
112template <uint8_t bits>
113static inline int32_t signed_saturate(int32_t val) {
114 return __ssat(val, bits);
115}
116
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) {
119 return __qadd(a, b);
120}
121
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) {
124 return __qsub(a, b);
125}
126
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);
132}
133
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);
139}
140
141#else
142
143static inline q31_t multiply_32x32_rshift32(q31_t a, q31_t b) {
144 return (int32_t)(((int64_t)a * b) >> 32);
145}
146
147// This multiplies two numbers in signed Q31 fixed point and rounds the result
148static inline q31_t multiply_32x32_rshift32_rounded(q31_t a, q31_t b) {
149 return (int32_t)(((int64_t)a * b + 0x80000000) >> 32);
150}
151
152// This multiplies two numbers in signed Q31 fixed point, returning the result in q31. Matches the ARM
153// `smmul`-then-`* 2` of the hardware path (smmul = multiply_32x32_rshift32).
154static inline q31_t q31_mult(q31_t a, q31_t b) {
155 return multiply_32x32_rshift32(a, b) * 2;
156}
157
158// This multiplies a number in q31 by a number in q32, returning a scaled value. The ARM path is `smmul`
159// (a signed multiply) even though proportion is unsigned, so match that by treating proportion as signed.
160static inline q31_t q31tRescale(q31_t a, uint32_t proportion) {
161 return multiply_32x32_rshift32(a, (q31_t)proportion);
162}
163
164// Multiplies A and B, adds to sum, and returns output.
165// NB: keep the 64-bit product term separate and add `sum` last. The old form shifted sum<<32 first
166// (~2^63) then added a*b (~2^62), overflowing int64 (UB) for Q31-scale operands — it diverged from the
167// ARM smmla (which wraps in 32-bit) and, at -O2, the UB corrupted neighbouring code. Mathematically the
168// non-overflowing forms below are identical to the originals: floor((sum<<32 ± a*b)/2^32) = sum ± (a*b>>32).
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));
171}
172
173// Multiplies A and B, adds to sum, and returns output, rounding
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));
176}
177
178// Multiplies A and B, subtracts from sum, and returns output
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));
181}
182
183// Multiplies A and B, subtracts from sum, and returns output, rounding
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));
186}
187
188// Matches ARM `ssat %0, %1, #bits`: clamp to the signed range representable in `bits` bits,
189// i.e. [-2^(bits-1), 2^(bits-1)-1]. (The old host stub `std::min(val, 1<<bits)` clamped only the
190// upper bound, to the wrong value, with no lower bound — silently wrong saturation in the DSP.)
191template <uint8_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);
196}
197
198// Matches ARM `qadd`: saturating add, clamped to the signed 32-bit range (the old host stub
199// `a + b` wrapped on overflow — a ramp past INT32_MAX flipped to negative instead of pinning).
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));
204}
205
206// Matches ARM `qsub`: saturating subtract, clamped to the signed 32-bit range.
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));
211}
212
213[[gnu::always_inline]] constexpr q31_t q31_from_float(float value) {
214 // A float is represented as 32 bits:
215 // 1-bit sign, 8-bit exponent, 24-bit mantissa
216
217 auto bits = std::bit_cast<uint32_t>(value);
218
219 // Sign bit being 1 indicates negative value
220 bool negative = bits & 0x80000000;
221
222 // Extract exponent and adjust for bias (IEEE 754)
223 int32_t exponent = static_cast<int32_t>((bits >> 23) & 0xFF) - 127;
224
225 // saturate if |value| >= 1.f (vcvt.s32.f32 #31 saturates to INT32_MIN / INT32_MAX)
226 if (exponent >= 0) {
227 return negative ? std::numeric_limits<q31_t>::min() : std::numeric_limits<q31_t>::max();
228 }
229
230 // |value| < 2^-31 truncates to 0. (Was `mantissa >> -exponent` — for these tiny values -exponent
231 // is >= 32, i.e. a shift >= the operand width, which is UB and at -O2 corrupted neighbouring code.)
232 int32_t shift = -exponent;
233 if (shift >= 32) {
234 return 0;
235 }
236
237 // extract mantissa (truncating toward zero, matching VCVT's rounding mode)
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;
241}
243static inline float q31_to_float(q31_t value) {
244 return static_cast<float>(value) / 2147483648.0f;
245}
246#endif
247
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");
257
258 using BaseType = int32_t;
259 using IntermediateType = int64_t;
260
262 [[gnu::always_inline]] static int32_t signed_most_significant_word_multiply_add(int32_t a, int32_t b, int32_t c) {
263 if constexpr (Rounded) {
264 return multiply_accumulate_32x32_rshift32_rounded(a, b, c);
265 }
266 else {
267 return multiply_accumulate_32x32_rshift32(a, b, c);
268 }
269 }
270
271 // a * b
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);
275 }
276 else {
277 return multiply_32x32_rshift32(a, b);
278 }
279 }
280
281 static constexpr BaseType one() noexcept {
282 if constexpr (fractional_bits == 31) {
283 return std::numeric_limits<BaseType>::max();
284 }
285 else {
286 return 1 << fractional_bits;
287 }
288 }
289
290public:
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;
295
296 // --- Portable emulation of the ARM VFP fixed-point conversion instructions ---
297 //
298 // vcvt.{s32.f32,f32.s32,s32.f64,f64.s32} back the float/double conversions on ARM. Off-target
299 // (no VFP) and during constant evaluation we must reproduce them *bit-exactly*, or the host-sim
300 // diverges from the firmware. The hardware behaviour is: scale by exactly 2^fractional_bits,
301 // round toward zero, saturate to the int32 range, and map NaN to 0. Verified lane-for-lane
302 // against qemu-arm in tests/qemu/spec/fixedpoint_vfp_spec.cpp.
303 //
304 // Note the scale is 2^fractional_bits, NOT one() — one() is INT32_MAX (not 2^31) when
305 // fractional_bits == 31, whereas VFP always uses the power of two. 2^fractional_bits is exact
306 // in double for every valid fractional_bits, so the scaling never rounds.
307 static constexpr double vfp_scale() noexcept { return static_cast<double>(uint64_t{1} << fractional_bits); }
308
310 static constexpr BaseType saturate_to_raw(double scaled) noexcept {
311 // INT32_MAX + 1 == 2^31 and INT32_MIN - 1 == -(2^31)-1, both exact in double. +inf/-inf
312 // compare past these bounds and saturate, matching the hardware.
313 if (scaled >= static_cast<double>(std::numeric_limits<BaseType>::max()) + 1.0) {
314 return std::numeric_limits<BaseType>::max();
315 }
316 if (scaled <= static_cast<double>(std::numeric_limits<BaseType>::min()) - 1.0) {
317 return std::numeric_limits<BaseType>::min();
318 }
319 return static_cast<BaseType>(scaled); // double->int truncates toward zero; guaranteed in range here
320 }
321
323 static constexpr BaseType float_to_raw(double value) noexcept {
324 if (value != value) { // NaN -> 0
325 return 0;
326 }
327 return saturate_to_raw(value * vfp_scale());
328 }
329
331 static constexpr float raw_to_float(BaseType raw) noexcept {
332 return static_cast<float>(static_cast<double>(raw) / vfp_scale());
333 }
334
336 static constexpr double raw_to_double(BaseType raw) noexcept { return static_cast<double>(raw) / vfp_scale(); }
337
339 constexpr FixedPoint() = default;
340
343 template <std::size_t OtherFractionalBits>
344 constexpr explicit FixedPoint(FixedPoint<OtherFractionalBits> other) noexcept : value_(other.raw()) {
345 if constexpr (FractionalBits == OtherFractionalBits) {
346 return;
347 }
348 else if constexpr (FractionalBits > OtherFractionalBits) {
349 // saturate
350 constexpr int32_t shift = FractionalBits - OtherFractionalBits;
351 value_ = signed_saturate<32 - shift>(value_);
352 value_ = (value_ << shift) + (value_ % 2);
353 }
354 else if constexpr (rounded) {
355 // round
356 constexpr int32_t shift = OtherFractionalBits - FractionalBits;
357 value_ >>= shift + ((1 << shift) - 1);
358 }
359 else {
360 // truncate
361 value_ >>= (OtherFractionalBits - FractionalBits);
362 }
363 }
364
367 template <std::integral T>
368 constexpr explicit FixedPoint(T value) noexcept : value_(static_cast<BaseType>(value) << fractional_bits) {}
369
372 constexpr explicit FixedPoint(float value) noexcept {
373#if defined(__arm__)
374 // Note: a plain (not `if constexpr`) runtime check — `if constexpr (std::is_constant_evaluated())`
375 // is always true, which would discard the VFP path entirely. On host there is no VFP, so the asm
376 // branch is compiled out and the portable path below is always taken.
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); // NOLINT
380 return;
381 }
382#endif
383 value_ = float_to_raw(value);
384 }
385
388 constexpr explicit operator float() const noexcept {
389#if defined(__arm__)
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);
394 }
395#endif
396 return raw_to_float(value_);
397 }
398
401 constexpr explicit FixedPoint(double value) noexcept {
402#if defined(__arm__)
403 if (!std::is_constant_evaluated()) {
404 auto output = std::bit_cast<int64_t>(value);
405 // %P selects the 64-bit d-register name (the fixed-point vcvt.*.f64 operate on a d-reg; a
406 // bare %0 prints the single-precision s-name). The instruction is in-place — operands must
407 // be the same register — so use a single read-write (+w) operand rather than tied =w/w.
408 asm("vcvt.s32.f64 %P0, %P0, %1" : "+w"(output) : "I"(fractional_bits));
409 value_ = static_cast<BaseType>(output);
410 return;
411 }
412#endif
413 value_ = float_to_raw(value);
414 }
415
418 constexpr explicit operator double() const noexcept {
419#if defined(__arm__)
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));
423 return output;
424 }
425#endif
426 return raw_to_double(value_);
427 }
428
430 template <std::size_t OutputFractionalBits>
432 return static_cast<FixedPoint<OutputFractionalBits>>(*this);
433 }
434
436 template <std::integral T>
437 explicit operator T() const noexcept {
438 return static_cast<T>(value_ >> fractional_bits);
439 }
440
441 explicit operator bool() const noexcept { return value_ != 0; }
442
444 constexpr FixedPoint operator-() const { return FixedPoint::from_raw(-value_); }
445
447 [[nodiscard]] constexpr BaseType raw() const noexcept { return value_; }
448
450 static constexpr FixedPoint from_raw(BaseType raw) noexcept {
451 FixedPoint result{};
452 result.value_ = raw;
453 return result;
454 }
455
458 constexpr FixedPoint operator+(const FixedPoint& rhs) const { return from_raw(add_saturate(value_, rhs.value_)); }
459
462 constexpr FixedPoint operator+=(const FixedPoint& rhs) {
463 value_ = add_saturate(value_, rhs.value_);
464 return *this;
465 }
466
469 constexpr FixedPoint operator-(const FixedPoint& rhs) const {
470 return from_raw(subtract_saturate(value_, rhs.value_));
471 }
472
475 constexpr FixedPoint operator-=(const FixedPoint& rhs) {
476 value_ = subtract_saturate(value_, rhs.value_);
477 return *this;
478 }
479
482 constexpr FixedPoint operator*(const FixedPoint& rhs) const {
483 if constexpr (fast_approximation && fractional_bits > 16) {
484 // less than 16 would mean no fractional bits remain after right shift by 32
485 constexpr int32_t shift = fractional_bits - ((fractional_bits * 2) - 32);
486 return from_raw(signed_most_significant_word_multiply(value_, rhs.value_) << shift);
487 }
488
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)));
492 }
493
494 IntermediateType value = (static_cast<IntermediateType>(value_) * rhs.value_) >> fractional_bits;
495 return from_raw(static_cast<BaseType>(value));
496 }
497
500 constexpr FixedPoint operator*=(const FixedPoint& rhs) {
501 value_ = this->operator*(rhs).value_;
502 return *this;
503 }
504
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);
517 }
518
519 constexpr int32_t r_shift = (FractionalBits + OtherFractionalBits) - OutputFractionalBits;
520 if constexpr (rounded) {
521 IntermediateType value = (static_cast<IntermediateType>(value_) * rhs.raw())
522 >> (r_shift - 1); // At this point Q is FractionalBits + OtherFractionalBits
523 return from_raw(static_cast<BaseType>((value / 2) + (value % 2)));
524 }
525
526 IntermediateType value = (static_cast<IntermediateType>(value_) * rhs.raw()) >> r_shift;
527 return from_raw(static_cast<BaseType>(value));
528 }
529
532 template <std::size_t OtherFractionalBits>
534 value_ = this->operator*(rhs).value_;
535 return *this;
536 }
537
540 constexpr FixedPoint operator/(const FixedPoint& rhs) const {
541 if (rhs.value_ == 0) {
542 return from_raw(std::numeric_limits<BaseType>::max());
543 }
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)));
547 }
548
549 IntermediateType value = (static_cast<IntermediateType>(value_) << fractional_bits) / rhs.value_;
550 return from_raw(static_cast<BaseType>(value));
551 }
552
555 constexpr FixedPoint operator/=(const FixedPoint& rhs) {
556 value_ = this->operator/(rhs).value_;
557 return *this;
558 }
559
561 template <std::size_t OtherFractionalBitsA, std::size_t OtherFractionalBitsB>
563 const FixedPoint<OtherFractionalBitsB>& b) const {
564 // ensure that the number of fractional bits in the addend is equal to the sum of the number of fractional bits
565 // in multiplicands, minus 32 (due to right shift) before using smmla/smmlar
566 if constexpr (fast_approximation && (OtherFractionalBitsA + OtherFractionalBitsB) - 32 == FractionalBits) {
568 }
569 return *this + static_cast<FixedPoint>(a * b);
570 }
571
573 constexpr FixedPoint MultiplyAdd(const FixedPoint& a, const FixedPoint& b) const {
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);
576 }
577 return *this + (a * b);
578 }
579
581 constexpr bool operator==(const FixedPoint& rhs) const noexcept { return value_ == rhs.value_; }
582
584 constexpr std::strong_ordering operator<=>(const FixedPoint& rhs) const noexcept { return value_ <=> rhs.value_; }
585
587 template <std::size_t OtherFractionalBits>
588 constexpr bool operator==(const FixedPoint<OtherFractionalBits>& rhs) const noexcept {
589 int integral_value = value_ >> fractional_bits;
590 int other_integral_value = rhs.raw() >> OtherFractionalBits;
591 int fractional_value = value_ & ((1 << fractional_bits) - 1); // Mask out the integral part
592 int other_fractional_value = rhs.raw() & ((1 << OtherFractionalBits) - 1); // Mask out the integral parts
593
594 // Shift the fractional part if the number of fractional bits is different
595 if (fractional_bits > OtherFractionalBits) {
596 fractional_value >>= fractional_bits - OtherFractionalBits;
597 }
598 else {
599 other_fractional_value >>= OtherFractionalBits - fractional_bits;
600 }
601
602 return integral_value == other_integral_value && fractional_value == other_fractional_value;
603 }
604
606 template <std::size_t OtherFractionalBits>
607 constexpr std::strong_ordering operator<=>(const FixedPoint<OtherFractionalBits>& rhs) const noexcept {
608 // Compare integral and fractional components separately
609 int integral_value = value_ >> fractional_bits;
610 int other_integral_value = rhs.raw() >> OtherFractionalBits;
611 int fractional_value = value_ & ((1 << fractional_bits) - 1); // Mask out the integral part
612 int other_fractional_value = rhs.raw() & ((1 << OtherFractionalBits) - 1); // Mask out the integral part
613
614 // Shift the fractional part if the number of fractional bits is different
615 if (fractional_bits > OtherFractionalBits) {
616 fractional_value >>= fractional_bits - OtherFractionalBits;
617 }
618 else {
619 other_fractional_value >>= OtherFractionalBits - fractional_bits;
620 }
621
622 if (integral_value < other_integral_value) {
623 return std::strong_ordering::less;
624 }
625 if (integral_value > other_integral_value) {
626 return std::strong_ordering::greater;
627 }
628 if (fractional_value < other_fractional_value) {
629 return std::strong_ordering::less;
630 }
631 if (fractional_value > other_fractional_value) {
632 return std::strong_ordering::greater;
633 }
634 return std::strong_ordering::equal;
635 }
636
638 template <typename T>
639 requires std::integral<T> || std::floating_point<T>
640 constexpr bool operator==(const T& rhs) const noexcept {
641 return value_ == FixedPoint(rhs).value_;
642 }
643
644private:
645 BaseType value_{0};
646};
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