38#define TGX_CAST32(a) ((int32_t)(a))
40#define DEPRECATED(X) [[deprecated(" " X " ")]]
44static_assert(
sizeof(int) >= 4,
"The TGX library only works on 32 bits or 64 bits architecture. Sorry!");
48#if defined(ARDUINO_TEENSY41)
51 extern "C" uint8_t external_psram_size;
54 #define TGX_IS_PROGMEM(X) ((((uint32_t)(X)) >= 0x60000000)&&(((uint32_t)(X)) < 0x70000000))
57 #define TGX_IS_EXTMEM(X) ((((uint32_t)(X)) >= 0x70000000)&&(((uint32_t)(X)) < 0x80000000))
62#define M_PI 3.14159265358979323846
75 template<
int N>
struct DummyType
86 template<
bool BB1,
bool BB2>
struct DummyTypeBB
93 template<
typename T =
int>
struct DefaultFPType
95#if TGX_SINGLE_PRECISION_COMPUTATIONS
98 typedef double fptype;
102#if TGX_SINGLE_PRECISION_COMPUTATIONS
104 template<>
struct DefaultFPType<double>
106 typedef double fptype;
112 template <
typename,
typename>
struct is_same {
static const bool value =
false; };
113 template <
typename T>
struct is_same<T, T> {
static const bool value =
true; };
120 return __builtin_bswap16(v);
122 return ((v >> 8) | (v << 8));
128 template<
typename T> TGX_INLINE
inline void swap(T& a, T& b) { T c(a); a = b; b = c; }
132 template<
typename T> TGX_INLINE
inline T
min(
const T & a,
const T & b) {
return((a < b) ? a : b); }
136 template<
typename T> TGX_INLINE
inline T
max(
const T & a,
const T & b) {
return((a > b) ? a : b); }
140 template<
typename T> TGX_INLINE
inline T
clamp(
const T & v,
const T & vmin,
const T & vmax)
142 return max(vmin,
min(vmax, v));
147 TGX_INLINE
inline float roundfp(
const float f) {
return roundf(f); }
151 TGX_INLINE
inline double roundfp(
const double f) {
return round(f); }
155 TGX_INLINE
inline uint32_t
float_as_uint32(
float f) { uint32_t u; memcpy(&u, &f,
sizeof(uint32_t));
return u; }
159 TGX_INLINE
inline float uint32_as_float(uint32_t u) {
float f; memcpy(&f, &u,
sizeof(
float));
return f; }
165 TGX_INLINE
inline int32_t
safeMultB(int32_t A, int32_t B)
167 if ((A == 0) || (B == 0))
return B;
168 const int32_t max32 = 2147483647;
169 const int32_t nB = max32 / ((A > 0) ? A : (-A));
170 return ((B <= nB) ? B : nB);
182#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
186 "recip0.s %0, %2\n\t"
188 "msub.s %1, %2, %0\n\t"
189 "madd.s %0, %0, %1\n\t"
191 "msub.s %1, %2, %0\n\t"
198#elif TGX_USE_FAST_INV_TRICK
208 y *= fmaf(-x, y, 2.00128722f);
209 y *= fmaf(-x, y, 2.00000072f);
212 return ((x == 0) ? 1.0f : (1.0f / x));
223 return ((x == 0) ? 1.0 : (1.0 / x));
253#if TGX_USE_FAST_SQRT_TRICK
257 return x * y * fmaf(-x, y * y, 1.89099002f);
281#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
283 float t0, t1, t2, t3, result;
285 "rsqrt0.s %0, %5\n\t"
286 "mul.s %1, %5, %0\n\t"
288 "mul.s %3, %2, %0\n\t"
290 "msub.s %4, %1, %0\n\t"
291 "madd.s %0, %3, %4\n\t"
292 "mul.s %1, %5, %0\n\t"
293 "mul.s %3, %2, %0\n\t"
295 "msub.s %4, %1, %0\n\t"
306 const float s = sqrtf(x);
307 return (s == 0) ? 1.0f : (1.0f / s);
317 const double s = sqrt(x);
318 return (s == 0) ? 1.0 : (1.0 / sqrt(s));
329#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
331 float t0, t1, t2, t3, result;
333 "rsqrt0.s %0, %5\n\t"
334 "mul.s %1, %5, %0\n\t"
336 "mul.s %3, %2, %0\n\t"
338 "msub.s %4, %1, %0\n\t"
348#elif TGX_USE_FAST_INV_SQRT_TRICK
352 return y * fmaf(-x, y * y, 1.89099002f);
376#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
385 return (int32_t)floorf(x);
TGX_INLINE int32_t lfloorf(float x)
Compute (int32_t)floorf(x).
Definition: Misc.h:374
TGX_INLINE T min(const T &a, const T &b)
Don't know why but faster than fminf() for floats.
Definition: Misc.h:132
TGX_INLINE T max(const T &a, const T &b)
Don't know why but much faster than fmaxf() for floats.
Definition: Misc.h:136
TGX_INLINE T clamp(const T &v, const T &vmin, const T &vmax)
Template clamp version.
Definition: Misc.h:140
TGX_INLINE void swap(T &a, T &b)
Baby let me swap you one more time...
Definition: Misc.h:128
TGX_INLINE int32_t safeMultB(int32_t A, int32_t B)
Return a value smaller or equal to B such that the multiplication by A is safe (no overflow with int3...
Definition: Misc.h:165
TGX_INLINE uint32_t float_as_uint32(float f)
Reinterpret the bits of a float as a uint32_t.
Definition: Misc.h:155
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition: Misc.h:327
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:231
TGX_INLINE float roundfp(const float f)
Rounding for floats.
Definition: Misc.h:147
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:279
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:251
TGX_INLINE float uint32_as_float(uint32_t u)
Reinterpret the bits of a uint32_t as a float.
Definition: Misc.h:159
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition: Misc.h:180
TGX_INLINE uint16_t BigEndian16(uint16_t v)
little endian / big endian conversion
Definition: Misc.h:117
Configuration file depending on the architecture.