|
| 1 | +#pragma once |
| 2 | +#include "AudioConfig.h" |
| 3 | + |
| 4 | +namespace audio_tools { |
| 5 | + |
| 6 | +/** |
| 7 | + * @brief Stores float values as uint32_t so that we can use memory allocated with MALLOC_CAP_32BIT |
| 8 | + * @author Phil Schatzmann |
| 9 | + * @copyright GPLv3 |
| 10 | + */ |
| 11 | + |
| 12 | +class float32 { |
| 13 | + public: |
| 14 | + float32() = default; |
| 15 | + float32(float value){ |
| 16 | + this->value = float32::as_uint(value); |
| 17 | + } |
| 18 | + float32(const float32 &value){ |
| 19 | + this->value = value.value; |
| 20 | + } |
| 21 | + inline operator float() { |
| 22 | + return as_float(value); |
| 23 | + } |
| 24 | + explicit inline operator double() { |
| 25 | + return (double) float32::as_float(value); |
| 26 | + } |
| 27 | + explicit inline operator int() { |
| 28 | + return (int) float32::as_float(value); |
| 29 | + } |
| 30 | + explicit inline operator int() const { |
| 31 | + return (int) float32::as_float(value); |
| 32 | + } |
| 33 | + inline bool operator< (float32 other) const{ |
| 34 | + return float() < (float)other; |
| 35 | + } |
| 36 | + inline bool operator<= (float32 other) const{ |
| 37 | + return float() <= (float)other; |
| 38 | + } |
| 39 | + inline bool operator> (float32 other) const{ |
| 40 | + return float() > (float)other; |
| 41 | + } |
| 42 | + inline bool operator>= (float32 other) const{ |
| 43 | + return float() >= (float)other; |
| 44 | + } |
| 45 | + inline bool operator== (float32 other) const{ |
| 46 | + return float() == (float)other; |
| 47 | + } |
| 48 | + inline bool operator!= (float32 other) const{ |
| 49 | + return float() != (float)other; |
| 50 | + } |
| 51 | + |
| 52 | + protected: |
| 53 | + uint32_t value=0; |
| 54 | + |
| 55 | + static uint32_t as_uint(float x) { |
| 56 | + return *(uint32_t*)&x; |
| 57 | + } |
| 58 | + static float as_float(uint32_t x) { |
| 59 | + return *(float*)&x; |
| 60 | + } |
| 61 | + |
| 62 | +}; |
| 63 | + |
| 64 | +inline float operator+ (float32 one, float32 two) |
| 65 | +{ |
| 66 | + return (float)one + (float)two; |
| 67 | +} |
| 68 | +inline float operator- (float32 one, float32 two) |
| 69 | +{ |
| 70 | + return (float)one - (float)two; |
| 71 | +} |
| 72 | +inline float operator* (float32 one, float32 two) |
| 73 | +{ |
| 74 | + return (float)one * (float)two; |
| 75 | +} |
| 76 | +inline float operator/ (float32 one, float32 two) |
| 77 | +{ |
| 78 | + return (float)one / (float)two; |
| 79 | +} |
| 80 | +inline float operator+ (float32 one, float two) |
| 81 | +{ |
| 82 | + return (float)one + two; |
| 83 | +} |
| 84 | +inline float operator- (float32 one, float two) |
| 85 | +{ |
| 86 | + return (float)one - two; |
| 87 | +} |
| 88 | +inline float operator* (float32 one, float two) |
| 89 | +{ |
| 90 | + return (float)one * two; |
| 91 | +} |
| 92 | +inline float operator/ (float32 one, float two) |
| 93 | +{ |
| 94 | + return (float)one / two; |
| 95 | +} |
| 96 | +inline float operator+ (float one, float32 two) |
| 97 | +{ |
| 98 | + return two + float(one); |
| 99 | +} |
| 100 | +inline float operator- (float one, float32 two) |
| 101 | +{ |
| 102 | + return two - float(one); |
| 103 | +} |
| 104 | +inline float operator* (float one, float32 two) |
| 105 | +{ |
| 106 | + return two * float(one); |
| 107 | +} |
| 108 | +inline float operator/ (float one, float32 two) |
| 109 | +{ |
| 110 | + return two / float(one); |
| 111 | +} |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +namespace std { |
| 116 | + |
| 117 | +inline float floor ( float32 arg ) { return std::floor((float)arg);} |
| 118 | +inline float fabs ( float32 arg ) { return std::fabs((float)arg);} |
| 119 | + |
| 120 | +} |
0 commit comments