Repository navigation
Expand file tree
/
Copy pathfloat24.h
More file actions
334 lines (268 loc) · 7.69 KB
/
Copy pathfloat24.h
File metadata and controls
334 lines (268 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* float24.h, Copyright (c) 2020 Yuriy Yakimenko
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
#pragma once
#include <stdint.h> // unit8_t
#include <math.h> // ldexp
#include <string.h> // memset
// FP24 impl. unsigned 24-bit float. Range: 0 to 65535 (when FP24_EXP_BITS set to 5).
// Epsilon value with MANTISSA bits equal 19 is 0.000002 (2^-19).
#define FP24_EXP_BITS 5
#define FP24_MANT_BITS (24 - FP24_EXP_BITS) // 19: 24 bits in 3 bytes minus exp. bits.
#define FP24_EXP_SHIFT ((1 << (FP24_EXP_BITS - 1)) - 1) // 15
#define FP24_MAX_EXP ((1 << FP24_EXP_BITS) - 1) // 31
typedef union {
float f;
struct
{
// Order is important.
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
} IEEE_float;
typedef union {
unsigned char buffer[4];
struct
{
unsigned int mantissa : FP24_MANT_BITS;
unsigned int exponent : FP24_EXP_BITS;
unsigned int unused : 8;
} raw;
} float24_cast; // supports only non-negative values.
static void set24bit (float24_cast & value, float input);
static float get24bit (const float24_cast value);
struct float24_u
{
float24_cast data;
float24_u ()
{
set (0);
}
float24_u (float input)
{
set (input);
}
void set(float input)
{
set24bit(data, input);
}
float get() const
{
return get24bit(data);
}
operator float() const
{
return get();
}
static float huge() // largest possible value
{
float24_u largest;
memset (largest.data.buffer,0xFF, 3);
return largest.get();
}
static float tiny() // smallest possible value greater than zero
{
float24_u smallest;
memset (smallest.data.buffer,0, 3);
smallest.data.raw.mantissa = 1;
return smallest.get();
}
};
static bool isFloatBigEndian()
{
float floatNumber = -1.0;
uint8_t* numPtr = (uint8_t*)&floatNumber;
return (numPtr[0] != 0);
}
static float ReverseFloat(const float inFloat)
{
float retVal;
char* floatToConvert = (char*)&inFloat;
char* returnFloat = (char*)&retVal;
// swap the bytes into a temporary buffer
returnFloat[0] = floatToConvert[3];
returnFloat[1] = floatToConvert[2];
returnFloat[2] = floatToConvert[1];
returnFloat[3] = floatToConvert[0];
return retVal;
}
static void set24bit (float24_cast & value, float input)
{
if (isFloatBigEndian())
{
input = ReverseFloat(input);
}
IEEE_float temp;
temp.f = input;
if (input <= 0)
{
memset(value.buffer, 0, 3);
return;
}
int exp = (int)temp.raw.exponent - 127 + FP24_EXP_SHIFT + 1;
if (exp <= 0) // number betwen 0 and 1/(2^15)
{
value.raw.unused = 0;
input = ldexp(input, FP24_EXP_SHIFT + FP24_MANT_BITS); // input* pow(2, 15 + 19); // result number between 0 and 2^19-1
value.raw.mantissa = (int)round(input);
value.raw.exponent = 0;
return;
}
else if (exp > FP24_MAX_EXP)
{
value.raw.unused = 0;
// value.raw.exponent = 31;
// value.raw.mantissa = 0x7ffff;
memset(value.buffer, 0xFF, 3);
return;
}
value.raw.exponent = exp;
value.raw.mantissa = temp.raw.mantissa >> (23 - FP24_MANT_BITS);
value.raw.unused = 0;
}
static float get24bit (const float24_cast value)
{
IEEE_float temp;
if (value.raw.exponent == 0 && value.raw.mantissa == 0) return 0;
if (value.raw.exponent == 0) // values between 0 and 1/(2^15)
{
double ret = ldexp(value.raw.mantissa, -(FP24_EXP_SHIFT + FP24_MANT_BITS)); // *pow(2, -34); // 34 = 15 + 19
return (float)ret;
}
temp.raw.sign = 0;
temp.raw.exponent = value.raw.exponent - FP24_EXP_SHIFT + 127 - 1;
temp.raw.mantissa = value.raw.mantissa << (23 - FP24_MANT_BITS);
if (isFloatBigEndian())
{
return ReverseFloat(temp.f);
}
return temp.f;
}
// FP24 impl. signed 24-bit float. Range from -65535 to 65535 (when FP24_EXP_BITS set to 5).
// Epsilon value with MANTISSA bits equal 18 is 0.000004 (2^-18).
////////////////////////////////////////////////////////////////////////////////////////////////////
#define FP24S_EXP_BITS 5
#define FP24S_MANT_BITS (24 - FP24S_EXP_BITS - 1) // 18: 24 bits in 3 bytes minus exp. bits minus sign bit
#define FP24S_EXP_SHIFT ((1 << (FP24S_EXP_BITS - 1)) - 1) // 15
#define FP24S_MAX_EXP ((1 << FP24S_EXP_BITS) - 1) // 31
typedef union {
unsigned char buffer[4];
struct
{
unsigned int mantissa : FP24S_MANT_BITS;
unsigned int exponent : FP24S_EXP_BITS;
unsigned int sign : 1;
unsigned int unused : 8;
} raw;
} float24s_cast; // supports negative and positive values
static void set24sbit (float24s_cast & value, float input);
static float get24sbit (const float24s_cast value);
struct float24_s
{
float24s_cast data;
float24_s ()
{
set (0);
}
float24_s (float input)
{
set (input);
}
void set(float input)
{
set24sbit(data, input);
}
float get() const
{
return get24sbit(data);
}
operator float() const
{
return get();
}
static float huge() // largest possible value
{
float24_s largest;
memset (largest.data.buffer, 0xFF, 3);
largest.data.raw.sign = 0;
return largest.get();
}
static float tiny() // smallest possible value greater than zero
{
float24_s smallest;
memset (smallest.data.buffer, 0, 3);
smallest.data.raw.sign = 0;
smallest.data.raw.mantissa = 1;
return smallest.get();
}
};
static void set24sbit (float24s_cast & value, float input)
{
if (isFloatBigEndian())
{
input = ReverseFloat(input);
}
if (input == 0)
{
memset(value.buffer, 0, 3);
return;
}
value.raw.sign = 0;
if (input < 0)
{
value.raw.sign = 1;
input = -input;
}
IEEE_float temp;
temp.f = input;
int exp = (int)temp.raw.exponent - 127 + FP24S_EXP_SHIFT + 1;
if (exp <= 0) // number betwen 0 and 1/(2^15)
{
value.raw.unused = 0;
input = ldexp(input, FP24S_EXP_SHIFT + FP24S_MANT_BITS); // input* pow(2, 15 + 18); // result number between 0 and 2^18-1
value.raw.mantissa = (int)round(input);
value.raw.exponent = 0;
return;
}
else if (exp > FP24S_MAX_EXP)
{
value.raw.unused = 0;
//value.raw.exponent = 31;
//value.raw.mantissa = 0x7ffff;
int sign = value.raw.exponent;
memset(value.buffer, 0xFF, 3);
value.raw.exponent = sign;
return;
}
value.raw.exponent = exp;
value.raw.mantissa = temp.raw.mantissa >> (23 - FP24S_MANT_BITS);
value.raw.unused = 0;
}
static float get24sbit (const float24s_cast value)
{
IEEE_float temp;
if (value.raw.exponent == 0 && value.raw.mantissa == 0) return 0;
if (value.raw.exponent == 0) // values between 0 and 1/(2^15)
{
double ret = ldexp(value.raw.mantissa, -(FP24S_EXP_SHIFT + FP24S_MANT_BITS)); // *pow(2, -34); // 34 = 15 + 19
if (value.raw.sign != 0)
ret = -ret;
return (float)ret;
}
temp.raw.sign = value.raw.sign;
temp.raw.exponent = value.raw.exponent - FP24S_EXP_SHIFT + 127 - 1;
temp.raw.mantissa = value.raw.mantissa << (23 - FP24S_MANT_BITS);
if (isFloatBigEndian())
{
return ReverseFloat(temp.f);
}
return temp.f;
}