arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Float16.h
1#pragma once
2
3namespace audio_tools {
4
11class float16 {
12 public:
13 float16() = default;
14 float16(float in) { this->value = float16::float_to_half(in); }
15 float16(const float16 &value16) { this->value = value16.value; }
16 inline operator float() { return half_to_float(value); }
17 explicit inline operator double() {
18 return (double)float16::half_to_float(value);
19 }
20 explicit inline operator int() { return (int)float16::half_to_float(value); }
21 explicit inline operator int() const {
22 return (int)float16::half_to_float(value);
23 }
24 inline bool operator<(float16 other) const { return float() < (float)other; }
25 inline bool operator<=(float16 other) const {
26 return float() <= (float)other;
27 }
28 inline bool operator>(float16 other) const { return float() > (float)other; }
29 inline bool operator>=(float16 other) const {
30 return float() >= (float)other;
31 }
32 inline bool operator==(float16 other) const {
33 return float() == (float)other;
34 }
35 inline bool operator!=(float16 other) const {
36 return float() != (float)other;
37 }
38
39 protected:
40 uint16_t value = 0;
41
44 static uint32_t as_uint(const float x) { return *(uint *)&x; }
47 static float as_float(const uint32_t x) { return *(float *)&x; }
48
51 static float half_to_float(
52 const uint16_t x) { // IEEE-754 16-bit floating-point format (without
53 // infinity): 1-5-10, exp-15, +-131008.0,
54 // +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
55 const uint32_t e = (x & 0x7C00) >> 10; // exponent
56 const uint32_t m = (x & 0x03FF) << 13; // mantissa
57 const uint32_t v =
58 as_uint((float)m) >>
59 23; // evil log2 bit hack to count leading zeros in denormalized format
60 return as_float((x & 0x8000) << 16 | (e != 0) * ((e + 112) << 23 | m) |
61 ((e == 0) & (m != 0)) *
62 ((v - 37) << 23 |
63 ((m << (150 - v)) &
64 0x007FE000))); // sign : normalized : denormalized
65 }
68 static uint16_t float_to_half(
69 const float x) { // IEEE-754 16-bit floating-point format (without
70 // infinity): 1-5-10, exp-15, +-131008.0,
71 // +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
72 const uint32_t b =
73 as_uint(x) + 0x00001000; // round-to-nearest-even: add last bit after
74 // truncated mantissa
75 const uint32_t e = (b & 0x7F800000) >> 23; // exponent
76 const uint32_t m = b & 0x007FFFFF; // mantissa; in line below: 0x007FF000 =
77 // 0x00800000-0x00001000 = decimal
78 // indicator flag - initial rounding
79 return (b & 0x80000000) >> 16 |
80 (e > 112) * ((((e - 112) << 10) & 0x7C00) | m >> 13) |
81 ((e < 113) & (e > 101)) *
82 ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) |
83 (e > 143) * 0x7FFF; // sign : normalized : denormalized : saturate
84 }
85};
86
87inline float operator+(float16 one, float16 two) {
88 return (float)one + (float)two;
89}
90inline float operator-(float16 one, float16 two) {
91 return (float)one - (float)two;
92}
93inline float operator*(float16 one, float16 two) {
94 return (float)one * (float)two;
95}
96inline float operator/(float16 one, float16 two) {
97 return (float)one / (float)two;
98}
99inline float operator+(float16 one, float two) { return (float)one + two; }
100inline float operator-(float16 one, float two) { return (float)one - two; }
101inline float operator*(float16 one, float two) { return (float)one * two; }
102inline float operator/(float16 one, float two) { return (float)one / two; }
103inline float operator+(float one, float16 two) { return two + float(one); }
104inline float operator-(float one, float16 two) { return two - float(one); }
105inline float operator*(float one, float16 two) { return two * float(one); }
106inline float operator/(float one, float16 two) { return two / float(one); }
107
108} // namespace audio_tools
109
110namespace std {
111
112inline float floor(float16 arg) { return std::floor((float)arg); }
113inline float fabs(float16 arg) { return std::fabs((float)arg); }
114
115} // namespace std
Stores float values with 2 bytes.
Definition Float16.h:11
static float half_to_float(const uint16_t x)
Definition Float16.h:51
static uint32_t as_uint(const float x)
Definition Float16.h:44
static float as_float(const uint32_t x)
Definition Float16.h:47
static uint16_t float_to_half(const float x)
Definition Float16.h:68
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10