arduino-audio-tools
Loading...
Searching...
No Matches
float16.h
Go to the documentation of this file.
1#pragma once
2
3namespace audio_tools {
4
26class float16 {
27 public:
28 float16() = default;
29 float16(float in) { this->value = float16::float_to_half(in); }
30 float16(const float16 &value16) { this->value = value16.value; }
31 inline operator float() { return half_to_float(value); }
32 explicit inline operator double() {
33 return (double)float16::half_to_float(value);
34 }
35 explicit inline operator int() { return (int)float16::half_to_float(value); }
36 explicit inline operator int() const {
37 return (int)float16::half_to_float(value);
38 }
39 inline bool operator<(float16 other) const { return float() < (float)other; }
40 inline bool operator<=(float16 other) const {
41 return float() <= (float)other;
42 }
43 inline bool operator>(float16 other) const { return float() > (float)other; }
44 inline bool operator>=(float16 other) const {
45 return float() >= (float)other;
46 }
47 inline bool operator==(float16 other) const {
48 return float() == (float)other;
49 }
50 inline bool operator!=(float16 other) const {
51 return float() != (float)other;
52 }
53
54 protected:
55 uint16_t value = 0;
56
59 static uint32_t as_uint(const float x) { return *(uint *)&x; }
62 static float as_float(const uint32_t x) { return *(float *)&x; }
63
66 static float half_to_float(
67 const uint16_t x) { // IEEE-754 16-bit floating-point format (without
68 // infinity): 1-5-10, exp-15, +-131008.0,
69 // +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
70 const uint32_t e = (x & 0x7C00) >> 10; // exponent
71 const uint32_t m = (x & 0x03FF) << 13; // mantissa
72 const uint32_t v =
73 as_uint((float)m) >>
74 23; // evil log2 bit hack to count leading zeros in denormalized format
75 return as_float((x & 0x8000) << 16 | (e != 0) * ((e + 112) << 23 | m) |
76 ((e == 0) & (m != 0)) *
77 ((v - 37) << 23 |
78 ((m << (150 - v)) &
79 0x007FE000))); // sign : normalized : denormalized
80 }
83 static uint16_t float_to_half(
84 const float x) { // IEEE-754 16-bit floating-point format (without
85 // infinity): 1-5-10, exp-15, +-131008.0,
86 // +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
87 const uint32_t b =
88 as_uint(x) + 0x00001000; // round-to-nearest-even: add last bit after
89 // truncated mantissa
90 const uint32_t e = (b & 0x7F800000) >> 23; // exponent
91 const uint32_t m = b & 0x007FFFFF; // mantissa; in line below: 0x007FF000 =
92 // 0x00800000-0x00001000 = decimal
93 // indicator flag - initial rounding
94 return (b & 0x80000000) >> 16 |
95 (e > 112) * ((((e - 112) << 10) & 0x7C00) | m >> 13) |
96 ((e < 113) & (e > 101)) *
97 ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) |
98 (e > 143) * 0x7FFF; // sign : normalized : denormalized : saturate
99 }
100};
101
102inline float operator+(float16 one, float16 two) {
103 return (float)one + (float)two;
104}
105inline float operator-(float16 one, float16 two) {
106 return (float)one - (float)two;
107}
108inline float operator*(float16 one, float16 two) {
109 return (float)one * (float)two;
110}
111inline float operator/(float16 one, float16 two) {
112 return (float)one / (float)two;
113}
114inline float operator+(float16 one, float two) { return (float)one + two; }
115inline float operator-(float16 one, float two) { return (float)one - two; }
116inline float operator*(float16 one, float two) { return (float)one * two; }
117inline float operator/(float16 one, float two) { return (float)one / two; }
118inline float operator+(float one, float16 two) { return two + float(one); }
119inline float operator-(float one, float16 two) { return two - float(one); }
120inline float operator*(float one, float16 two) { return two * float(one); }
121inline float operator/(float one, float16 two) { return two / float(one); }
122
123} // namespace audio_tools
124
125namespace std {
126
127inline float floor(float16 arg) { return std::floor((float)arg); }
128inline float fabs(float16 arg) { return std::fabs((float)arg); }
129
130} // namespace std
Half-precision IEEE-754 binary16 float compressed into 2 bytes (1 sign + 5 exponent + 10 mantissa bit...
Definition float16.h:26
static float half_to_float(const uint16_t x)
Definition float16.h:66
bool operator==(float16 other) const
Definition float16.h:47
float16(float in)
Definition float16.h:29
bool operator>(float16 other) const
Definition float16.h:43
bool operator!=(float16 other) const
Definition float16.h:50
bool operator<=(float16 other) const
Definition float16.h:40
uint16_t value
Definition float16.h:55
static uint32_t as_uint(const float x)
Definition float16.h:59
bool operator>=(float16 other) const
Definition float16.h:44
static float as_float(const uint32_t x)
Definition float16.h:62
bool operator<(float16 other) const
Definition float16.h:39
static uint16_t float_to_half(const float x)
Definition float16.h:83
float16(const float16 &value16)
Definition float16.h:30
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
float operator+(float16 one, float16 two)
Definition float16.h:102
float operator/(float16 one, float16 two)
Definition float16.h:111
float operator-(float16 one, float16 two)
Definition float16.h:105
float operator*(float16 one, float16 two)
Definition float16.h:108
Definition InitializerList.h:7
float floor(float16 arg)
Definition float16.h:127
float fabs(float16 arg)
Definition float16.h:128