12#include <include/qlibs_types.hpp>
13#if !defined( ARDUINO_PLATFORM )
27 using fp16Raw_t = int32_t;
36 fp16Raw_t value{ overflow };
39 static bool flag_rounding;
40 static bool saturation;
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;
52 static uint32_t overflowCheck( uint32_t res,
54 const uint32_t y )
noexcept;
55 static fp16Raw_t saturate(
const fp16Raw_t nsInput,
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,
97 uint8_t skip )
noexcept;
98 static char* toASCII(
const fp16Raw_t num,
100 int decimals )
noexcept;
101 static fp16Raw_t rs( fp16Raw_t x )
noexcept;
102 static fp16Raw_t log2i( fp16Raw_t x )
noexcept;
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 );
108 constexpr fp16() : value( 0 ) {}
117 return overflow == value;
128 return ( exp_max == value ) || ( -exp_max == value );
135 inline fp16Raw_t
raw(
void )
const noexcept
141 inline fp16 operator+(
const fp16 &other )
noexcept
143 return fp16( { add( value, other.value ) } );
145 inline fp16& operator+=(
const fp16 &other )
noexcept
147 value = add( value, other.value );
150 inline fp16 operator-() const noexcept
152 return fp16( { -value } );
154 inline fp16& operator-=(
const fp16 &other )
noexcept
156 value = sub( value, other.value );
159 inline fp16 operator-(
const fp16 &other )
noexcept
161 return fp16( { sub( value, other.value ) } );
163 inline fp16 operator*(
const fp16 &other )
noexcept
165 return fp16( { mul( value, other.value ) } );
167 inline fp16& operator*=(
const fp16 &other )
noexcept
169 value = mul( value, other.value );
172 inline fp16 operator/(
const fp16 &other )
noexcept
174 return fp16( { div( value, other.value ) } );
176 inline fp16& operator/=(
const fp16 &other )
noexcept
178 value = div( value, other.value );
181 inline fp16& operator++() noexcept
183 value = add( value, one );
186 inline fp16 operator++(
int)
noexcept
189 value = add( value, one );;
192 inline fp16& operator--() noexcept
194 value = sub( value, one );
197 inline fp16 operator--(
int)
noexcept
200 value = sub( value, one );;
203 inline bool operator>(
const fp16 &other)
const noexcept
205 return value > other.value;
207 inline bool operator>=(
const fp16 &other)
const noexcept
209 return value >= other.value;
211 inline bool operator<(
const fp16 &other)
const noexcept
213 return value < other.value;
215 inline bool operator<=(
const fp16 &other)
const noexcept
217 return value <= other.value;
219 inline bool operator==(
const fp16 &other)
const noexcept
221 return value == other.value;
223 inline bool operator!=(
const fp16 &other)
const noexcept
225 return value != other.value;
227 inline fp16& operator=(
int x )
noexcept
229 value = fromInt( x );
232 inline fp16& operator=(
float x )
noexcept
234 value = fromFloat( x );
237 inline fp16& operator=(
double x )
noexcept
239 value = fromDouble( x );
242 inline fp16& operator=(
const fp16 &other )
noexcept
254 static int toInt(
const fp16 &x )
noexcept;
277 return fp16( { fromInt( x ) } );
287 return fp16( { fromFloat( x ) } );
297 return fp16( { fromDouble( x ) } );
309 return fp16( {
static_cast<fp16Raw_t
>(
static_cast<uint32_t
>( x.raw() ) & integer_mask ) } );
315 return fp16( { ceil( x.raw() ) } );
325 return fp16( { x.raw() + one } );
335 return fp16( { absolute( x.raw() ) } );
346 return fp16( { sqrt( x.raw() ) } );
357 return fp16( { exp( x.raw() ) } );
368 return fp16( { log( x.raw() ) } );
379 return fp16( { log2( x.raw() ) } );
390 return fp16( { radToDeg( x.raw() ) } );
401 return fp16( { degToRad( x.raw() ) } );
413 return fp16( { wrapToPi( x.raw() ) } );
425 return fp16( { wrapTo180( x.raw() ) } );
436 return fp16( { sin( x.raw() ) } );
447 return fp16( { cos( x.raw() ) } );
458 return fp16( { tan( x.raw() ) } );
470 const fp16 &x )
noexcept
473 return fp16( { atan2( y.raw(), x.raw() ) } );
485 return fp16( { atan( x.raw() ) } );
496 return fp16( { asin( x.raw() ) } );
507 return fp16( { acos( x.raw() ) } );
519 return fp16( { cosh( x.raw() ) } );
531 return fp16( { sinh( x.raw() ) } );
543 return fp16( { tanh( x.raw() ) } );
554 const fp16 &y )
noexcept
557 return fp16( { pow( x.raw(), y.raw() ) } );
573 int decimals )
noexcept
575 return toASCII( x.raw(), str, decimals );
581 #if !defined( ARDUINO_PLATFORM )
582 inline std::ostream& operator<<( std::ostream& os,
585 char buff[ 64 ] = { 0 };
586 os << fp16::toASCII( obj, buff, static_cast<int>( os.precision() ) );
591 constexpr fp16
operator"" _fp(
long double val )
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 ) } };
595 constexpr fp16
operator"" _fp(
unsigned long long val)
597 return { {
static_cast<fp16Raw_t
>(
static_cast<uint32_t
>( val ) << 16 ) } };
603 extern const fp16
FP_E;
613 extern const fp16
FP_PI;
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