Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
fp16.hpp
1
9#ifndef QLIBS_FP16
10#define QLIBS_FP16
11
12#include <include/qlibs_types.hpp>
13#if !defined( ARDUINO_PLATFORM )
14 #include <iostream>
15#endif
16
20namespace qlibs {
27 using fp16Raw_t = int32_t;
31 class fp16 {
32 struct fp16Hidden {
33 fp16Raw_t x;
34 };
35 private:
36 fp16Raw_t value{ overflow };
37 static fp16Raw_t Min; // skipcq: CXX-W2009
38 static fp16Raw_t Max; // skipcq: CXX-W2009
39 static bool flag_rounding; // skipcq: CXX-W2009
40 static bool saturation; // skipcq: CXX-W2009
41
42 static const fp16Raw_t exp_max;
43 static const fp16Raw_t f2;
44 static const uint32_t overflow_mask;
45 static const uint32_t fraction_mask;
46 static const uint32_t integer_mask;
47 static const fp16Raw_t f_pi_2;
48 static const fp16Raw_t overflow;
49 static const fp16Raw_t one;
50 static const fp16Raw_t one_half;
51
52 static uint32_t overflowCheck( uint32_t res,
53 const uint32_t x,
54 const uint32_t y ) noexcept;
55 static fp16Raw_t saturate( const fp16Raw_t nsInput,
56 const fp16Raw_t x,
57 const fp16Raw_t y ) noexcept;
58 static fp16Raw_t fromInt( const int x ) noexcept;
59 static fp16Raw_t fromFloat( const float x ) noexcept;
60 static fp16Raw_t fromDouble( const double x ) noexcept;
61 static fp16Raw_t add( const fp16Raw_t X,
62 const fp16Raw_t Y ) noexcept;
63 static fp16Raw_t sub( const fp16Raw_t X,
64 const fp16Raw_t Y ) noexcept;
65 static fp16Raw_t mul( const fp16Raw_t x,
66 const fp16Raw_t y ) noexcept;
67 static fp16Raw_t div( const fp16Raw_t x,
68 const fp16Raw_t y ) noexcept;
69 static fp16Raw_t absolute( fp16Raw_t x ) noexcept;
70 static fp16Raw_t ceil( fp16Raw_t x ) noexcept;
71 static fp16Raw_t sqrt( fp16Raw_t x ) noexcept;
72 static fp16Raw_t exp( fp16Raw_t x ) noexcept;
73 static fp16Raw_t log( fp16Raw_t x ) noexcept;
74 static fp16Raw_t log2( fp16Raw_t x ) noexcept;
75 static fp16Raw_t radToDeg( const fp16Raw_t x );
76 static fp16Raw_t degToRad( const fp16Raw_t x );
77 static fp16Raw_t wrapToPi( fp16Raw_t x ) noexcept;
78 static fp16Raw_t wrapTo180( fp16Raw_t x ) noexcept;
79 static fp16Raw_t sin( fp16Raw_t x ) noexcept;
80 static fp16Raw_t cos( fp16Raw_t x ) noexcept;
81 static fp16Raw_t tan( fp16Raw_t x ) noexcept;
82 static fp16Raw_t atan2( fp16Raw_t y,
83 fp16Raw_t x ) noexcept;
84 static fp16Raw_t atan( fp16Raw_t x ) noexcept;
85 static fp16Raw_t asin( fp16Raw_t x ) noexcept;
86 static fp16Raw_t acos( fp16Raw_t x ) noexcept;
87 static fp16Raw_t cosh( fp16Raw_t x ) noexcept;
88 static fp16Raw_t sinh( fp16Raw_t x ) noexcept;
89 static fp16Raw_t tanh( fp16Raw_t x ) noexcept;
90 static fp16Raw_t powi( fp16Raw_t x,
91 fp16Raw_t y ) noexcept;
92 static fp16Raw_t pow( fp16Raw_t x,
93 fp16Raw_t y ) noexcept;
94 static char* itoa( char *buf,
95 uint32_t scale,
96 uint32_t val,
97 uint8_t skip ) noexcept;
98 static char* toASCII( const fp16Raw_t num,
99 char *str,
100 int decimals ) noexcept;
101 static fp16Raw_t rs( fp16Raw_t x ) noexcept;
102 static fp16Raw_t log2i( fp16Raw_t x ) noexcept;
103
104 constexpr fp16( fp16Hidden val ) : value( val.x ) {}
105 friend constexpr fp16 operator"" _fp( long double val );
106 friend constexpr fp16 operator"" _fp( unsigned long long val );
107 public:
108 constexpr fp16() : value( 0 ) {}
109 fp16( const fp16& other) : value( other.value ) {}
110
115 inline bool isOverflow( void ) const noexcept
116 {
117 return overflow == value;
118 }
119
126 inline bool isExpMax( void ) const noexcept
127 {
128 return ( exp_max == value ) || ( -exp_max == value );
129 }
130
135 inline fp16Raw_t raw( void ) const noexcept
136 {
137 return value;
138 }
139
141 inline fp16 operator+( const fp16 &other ) noexcept
142 {
143 return fp16( { add( value, other.value ) } );
144 }
145 inline fp16& operator+=( const fp16 &other ) noexcept
146 {
147 value = add( value, other.value );
148 return *this;
149 }
150 inline fp16 operator-() const noexcept
151 {
152 return fp16( { -value } );
153 }
154 inline fp16& operator-=( const fp16 &other ) noexcept
155 {
156 value = sub( value, other.value );
157 return *this;
158 }
159 inline fp16 operator-( const fp16 &other ) noexcept
160 {
161 return fp16( { sub( value, other.value ) } );
162 }
163 inline fp16 operator*( const fp16 &other ) noexcept
164 {
165 return fp16( { mul( value, other.value ) } );
166 }
167 inline fp16& operator*=( const fp16 &other ) noexcept
168 {
169 value = mul( value, other.value );
170 return *this;
171 }
172 inline fp16 operator/( const fp16 &other ) noexcept
173 {
174 return fp16( { div( value, other.value ) } );
175 }
176 inline fp16& operator/=( const fp16 &other ) noexcept
177 {
178 value = div( value, other.value );
179 return *this;
180 }
181 inline fp16& operator++() noexcept
182 {
183 value = add( value, one );
184 return *this;
185 }
186 inline fp16 operator++(int) noexcept
187 {
188 fp16 temp = *this;
189 value = add( value, one );;
190 return temp;
191 }
192 inline fp16& operator--() noexcept
193 {
194 value = sub( value, one );
195 return *this;
196 }
197 inline fp16 operator--(int) noexcept
198 {
199 fp16 temp = *this;
200 value = sub( value, one );;
201 return temp;
202 }
203 inline bool operator>(const fp16 &other) const noexcept
204 {
205 return value > other.value;
206 }
207 inline bool operator>=(const fp16 &other) const noexcept
208 {
209 return value >= other.value;
210 }
211 inline bool operator<(const fp16 &other) const noexcept
212 {
213 return value < other.value;
214 }
215 inline bool operator<=(const fp16 &other) const noexcept
216 {
217 return value <= other.value;
218 }
219 inline bool operator==(const fp16 &other) const noexcept
220 {
221 return value == other.value;
222 }
223 inline bool operator!=(const fp16 &other) const noexcept
224 {
225 return value != other.value;
226 }
227 inline fp16& operator=( int x ) noexcept
228 {
229 value = fromInt( x );
230 return *this;
231 }
232 inline fp16& operator=( float x ) noexcept
233 {
234 value = fromFloat( x );
235 return *this;
236 }
237 inline fp16& operator=( double x ) noexcept
238 {
239 value = fromDouble( x );
240 return *this;
241 }
242 inline fp16& operator=( const fp16 &other ) noexcept
243 {
244 value = other.value;
245 return *this;
246 }
254 static int toInt( const fp16 &x ) noexcept;
255
261 static float toFloat( const fp16 &x ) noexcept;
262
268 static double toDouble( const fp16 &x ) noexcept;
269
275 static inline fp16 from( const int x ) noexcept
276 {
277 return fp16( { fromInt( x ) } );
278 }
279
285 static inline fp16 from( const float x ) noexcept
286 {
287 return fp16( { fromFloat( x ) } );
288 }
289
295 static inline fp16 from( const double x ) noexcept
296 {
297 return fp16( { fromDouble( x ) } );
298 }
299
306 static inline fp16 floor( const fp16 &x ) noexcept
307 {
308 /*cstat -MISRAC++2008-5-0-9*/
309 return fp16( { static_cast<fp16Raw_t>( static_cast<uint32_t>( x.raw() ) & integer_mask ) } );
310 /*cstat +MISRAC++2008-5-0-9*/
311 }
312
313 static inline fp16 ceil( const fp16 &x ) noexcept
314 {
315 return fp16( { ceil( x.raw() ) } );
316 }
317
323 static inline fp16 rounding( const fp16 &x ) noexcept
324 {
325 return fp16( { x.raw() + one } );
326 }
327
333 static inline fp16 absolute( const fp16 &x ) noexcept
334 {
335 return fp16( { absolute( x.raw() ) } );
336 }
337
344 static inline fp16 sqrt( const fp16 &x ) noexcept
345 {
346 return fp16( { sqrt( x.raw() ) } );
347 }
348
355 static inline fp16 exp( const fp16 &x ) noexcept
356 {
357 return fp16( { exp( x.raw() ) } );
358 }
359
366 static inline fp16 log( const fp16 &x ) noexcept
367 {
368 return fp16( { log( x.raw() ) } );
369 }
370
377 static inline fp16 log2( const fp16 &x ) noexcept
378 {
379 return fp16( { log2( x.raw() ) } );
380 }
381
388 static inline fp16 radToDeg( const fp16 &x ) noexcept
389 {
390 return fp16( { radToDeg( x.raw() ) } );
391 }
392
399 static inline fp16 degToRad( const fp16 &x ) noexcept
400 {
401 return fp16( { degToRad( x.raw() ) } );
402 }
403
411 static inline fp16 wrapToPi( const fp16 &x ) noexcept
412 {
413 return fp16( { wrapToPi( x.raw() ) } );
414 }
415
423 static inline fp16 wrapTo180( const fp16 &x ) noexcept
424 {
425 return fp16( { wrapTo180( x.raw() ) } );
426 }
427
434 static inline fp16 sin( const fp16 &x ) noexcept
435 {
436 return fp16( { sin( x.raw() ) } );
437 }
438
445 static inline fp16 cos( const fp16 &x ) noexcept
446 {
447 return fp16( { cos( x.raw() ) } );
448 }
449
456 static inline fp16 tan( const fp16 &x ) noexcept
457 {
458 return fp16( { tan( x.raw() ) } );
459 }
460
469 static inline fp16 atan2( const fp16 &y,
470 const fp16 &x ) noexcept
471 {
472 /*cstat -CERT-EXP30-C_b*/
473 return fp16( { atan2( y.raw(), x.raw() ) } );
474 /*cstat +CERT-EXP30-C_b*/
475 }
476
483 static inline fp16 atan( const fp16 &x ) noexcept
484 {
485 return fp16( { atan( x.raw() ) } );
486 }
487
494 static inline fp16 asin( const fp16 &x ) noexcept
495 {
496 return fp16( { asin( x.raw() ) } );
497 }
498
505 static inline fp16 acos( const fp16 &x ) noexcept
506 {
507 return fp16( { acos( x.raw() ) } );
508 }
509
517 static inline fp16 cosh( const fp16 &x ) noexcept
518 {
519 return fp16( { cosh( x.raw() ) } );
520 }
521
529 static inline fp16 sinh( const fp16 &x ) noexcept
530 {
531 return fp16( { sinh( x.raw() ) } );
532 }
533
541 static inline fp16 tanh( const fp16 &x ) noexcept
542 {
543 return fp16( { tanh( x.raw() ) } );
544 }
545
553 static inline fp16 pow( const fp16 &x,
554 const fp16 &y ) noexcept
555 {
556 /*cstat -CERT-EXP30-C_b*/
557 return fp16( { pow( x.raw(), y.raw() ) } );
558 /*cstat +CERT-EXP30-C_b*/
559 }
560
571 static inline char* toASCII( const fp16 &x,
572 char *str,
573 int decimals ) noexcept
574 {
575 return toASCII( x.raw(), str, decimals );
576 }
577
578 };
579
581 #if !defined( ARDUINO_PLATFORM )
582 inline std::ostream& operator<<( std::ostream& os,
583 const fp16& obj )
584 {
585 char buff[ 64 ] = { 0 };
586 os << fp16::toASCII( obj, buff, static_cast<int>( os.precision() ) );
587 return os;
588 }
589 #endif
590 /*cstat -MISRAC++2008-5-0-7 -CERT-FLP34-C -MISRAC++2008-5-0-9*/
591 constexpr fp16 operator"" _fp( long double val )
592 {
593 return { { static_cast<fp16Raw_t>( ( ( static_cast<double>( val )*65536.0 ) >= 0.0 ) ? ( static_cast<double>( val )*65536.0 ) + 0.5 : ( static_cast<double>( val )*65536.0 ) - 0.5 ) } };
594 }
595 constexpr fp16 operator"" _fp(unsigned long long val)
596 {
597 return { { static_cast<fp16Raw_t>( static_cast<uint32_t>( val ) << 16 ) } };
598 }
599 /*cstat +MISRAC++2008-5-0-7 -CERT-FLP34-C +MISRAC++2008-5-0-9*/
603 extern const fp16 FP_E;
605 extern const fp16 FP_LOG2E;
607 extern const fp16 FP_LOG10E;
609 extern const fp16 FP_LN2;
611 extern const fp16 FP_LN10;
613 extern const fp16 FP_PI;
615 extern const fp16 FP_PI_2;
617 extern const fp16 FP_PI_4;
619 extern const fp16 FP_1_PI;
621 extern const fp16 FP_2_PI;
623 extern const fp16 FP_2_SQRTPI;
625 extern const fp16 FP_SQRT2;
627 extern const fp16 FP_SQRT1_2;
628
630}
631
632#endif /*QLIBS_FP16*/
Fixed-point Q16.16 type with width of exactly 32 bits.
Definition fp16.hpp:31
bool isOverflow(void) const noexcept
Check for (q16.16) fixed-point overflow.
Definition fp16.hpp:115
static fp16 sin(const fp16 &x) noexcept
Computes the fixed-point sine of the radian angle x.
Definition fp16.hpp:434
static fp16 absolute(const fp16 &x) noexcept
Returns the absolute value of x.
Definition fp16.hpp:333
static fp16 sqrt(const fp16 &x) noexcept
Returns the fixed-point square root of x.
Definition fp16.hpp:344
static fp16 cos(const fp16 &x) noexcept
Computes the fixed-point cosine of the radian angle x.
Definition fp16.hpp:445
static fp16 log2(const fp16 &x) noexcept
Returns the fixed-point log base 2 of x.
Definition fp16.hpp:377
static fp16 acos(const fp16 &x) noexcept
Computes the fixed-point arc cosine of x in radians.
Definition fp16.hpp:505
static fp16 degToRad(const fp16 &x) noexcept
Converts angle units from degrees to radians.
Definition fp16.hpp:399
static fp16 log(const fp16 &x) noexcept
Returns the fixed-point natural logarithm (base-e logarithm) of x.
Definition fp16.hpp:366
fp16Raw_t raw(void) const noexcept
Get the (q16.16) raw integer value from x.
Definition fp16.hpp:135
static fp16 exp(const fp16 &x) noexcept
Returns the fixed-point value of e raised to the xth power.
Definition fp16.hpp:355
static float toFloat(const fp16 &x) noexcept
Returns the fixed-point value x converted to float.
Definition fp16.cpp:58
static fp16 wrapTo180(const fp16 &x) noexcept
Wrap the fixed-point angle in degrees to [−180 180].
Definition fp16.hpp:423
static fp16 rounding(const fp16 &x) noexcept
Returns the nearest integer value of the fixed-point argument x.
Definition fp16.hpp:323
static fp16 atan(const fp16 &x) noexcept
Computes the fixed-point arc tangent of x in radians.
Definition fp16.hpp:483
static fp16 atan2(const fp16 &y, const fp16 &x) noexcept
Computes the fixed-point arc tangent in radians of y / x based on the signs of both values to determi...
Definition fp16.hpp:469
static fp16 from(const double x) noexcept
Returns the double value x converted to fixed-point q16.16.
Definition fp16.hpp:295
static fp16 tanh(const fp16 &x) noexcept
Computes the fixed-point hyperbolic tangent of x.
Definition fp16.hpp:541
static int toInt(const fp16 &x) noexcept
Returns the fixed-point value x converted to int.
Definition fp16.cpp:39
static fp16 radToDeg(const fp16 &x) noexcept
Converts angle units from radians to degrees.
Definition fp16.hpp:388
static fp16 tan(const fp16 &x) noexcept
Computes the fixed-point tangent of the radian angle x.
Definition fp16.hpp:456
static fp16 wrapToPi(const fp16 &x) noexcept
Wrap the fixed-point angle in radians to [−pi pi].
Definition fp16.hpp:411
static fp16 pow(const fp16 &x, const fp16 &y) noexcept
Returns x raised to the power of y. (x^y)
Definition fp16.hpp:553
static fp16 cosh(const fp16 &x) noexcept
Computes the fixed-point hyperbolic cosine of x.
Definition fp16.hpp:517
static fp16 ceil(const fp16 &x) noexcept
Definition fp16.hpp:313
static double toDouble(const fp16 &x) noexcept
Returns the fixed-point value x converted to double.
Definition fp16.cpp:66
static fp16 asin(const fp16 &x) noexcept
Computes the fixed-point arc sine of x in radians.
Definition fp16.hpp:494
static char * toASCII(const fp16 &x, char *str, int decimals) noexcept
Converts the fixed-point value to a formatted string.
Definition fp16.hpp:571
static fp16 from(const int x) noexcept
Returns the int value x converted to fixed-point q16.16.
Definition fp16.hpp:275
static fp16 sinh(const fp16 &x) noexcept
Computes the fixed-point hyperbolic sine of x.
Definition fp16.hpp:529
static fp16 from(const float x) noexcept
Returns the float value x converted to fixed-point q16.16.
Definition fp16.hpp:285
constexpr fp16()
Definition fp16.hpp:108
bool isExpMax(void) const noexcept
Check for (q16.16) fixed-point fp16::exp() operation reaches the EXP_MAX value.
Definition fp16.hpp:126
static fp16 floor(const fp16 &x) noexcept
Returns the largest integer value less than or equal to x.
Definition fp16.hpp:306
fp16(const fp16 &other)
Definition fp16.hpp:109
const fp16 FP_LOG10E
log10(e) The base 10 logarithm of e as a Fixed-point Q16.16 value.
Definition fp16.cpp:7
const fp16 FP_SQRT1_2
1/sqrt(2) The inverse of the square root of 2 as a Fixed-point Q16.16 value.
Definition fp16.cpp:17
const fp16 FP_E
e The base of natural logarithms as a Fixed-point Q16.16 value.
Definition fp16.cpp:5
const fp16 FP_2_PI
2/pi Twice the inverse of pi as a Fixed-point Q16.16 value.
Definition fp16.cpp:14
const fp16 FP_2_SQRTPI
2/sqrt(pi) The inverse of the square root of pi as a Fixed-point Q16.16 value.
Definition fp16.cpp:15
const fp16 FP_SQRT2
sqrt(2) The square root of 2 as a Fixed-point Q16.16 value.
Definition fp16.cpp:16
const fp16 FP_LOG2E
log2(e) The base 2 logarithm of e as a Fixed-point Q16.16 value.
Definition fp16.cpp:6
const fp16 FP_PI_2
pi/2 Half of pi as a Fixed-point Q16.16 value.
Definition fp16.cpp:11
const fp16 FP_1_PI
1/pi The inverse of pi as a Fixed-point Q16.16 value.
Definition fp16.cpp:13
const fp16 FP_LN2
ln(2) The natural logarithm of 2 as a Fixed-point Q16.16 value.
Definition fp16.cpp:8
const fp16 FP_LN10
ln(10) The natural logarithm of 10 as a Fixed-point Q16.16 value.
Definition fp16.cpp:9
const fp16 FP_PI_4
pi/4 A quarter of pi as a Fixed-point Q16.16 value.
Definition fp16.cpp:12
const fp16 FP_PI
pi The circumference of a circle with diameter 1 as a Fixed-point Q16.16 value.
Definition fp16.cpp:10
The qLibs++ library namespace.
Definition fp16.cpp:4