38#define TGX_CAST32(a) ((int32_t)(a))
40#define DEPRECATED(X) [[deprecated(" " X " ")]]
49#ifndef TGX_FORCED_INLINE
51 #define TGX_FORCED_INLINE __forceinline
52 #elif defined(__GNUC__) || defined(__clang__)
53 #define TGX_FORCED_INLINE inline __attribute__((always_inline))
55 #define TGX_FORCED_INLINE inline
61static_assert(
sizeof(int) >= 4,
"The TGX library only works on 32 bits or 64 bits architecture. Sorry!");
65#if defined(ARDUINO_TEENSY41)
68 extern "C" uint8_t external_psram_size;
71 #define TGX_IS_PROGMEM(X) ((((uint32_t)(X)) >= 0x60000000)&&(((uint32_t)(X)) < 0x70000000))
74 #define TGX_IS_EXTMEM(X) ((((uint32_t)(X)) >= 0x70000000)&&(((uint32_t)(X)) < 0x80000000))
79#define M_PI 3.14159265358979323846
92 template<
int N>
struct DummyType
103 template<
bool BB1,
bool BB2>
struct DummyTypeBB
110 template<
typename T =
int>
struct DefaultFPType
112#if TGX_SINGLE_PRECISION_COMPUTATIONS
113 typedef float fptype;
115 typedef double fptype;
119#if TGX_SINGLE_PRECISION_COMPUTATIONS
121 template<>
struct DefaultFPType<double>
123 typedef double fptype;
129 template <
typename,
typename>
struct is_same {
static const bool value =
false; };
130 template <
typename T>
struct is_same<T, T> {
static const bool value =
true; };
137 return __builtin_bswap16(v);
139 return ((v >> 8) | (v << 8));
145 template<
typename T> TGX_INLINE
inline void swap(T& a, T& b) { T c(a); a = b; b = c; }
149 template<
typename T> TGX_INLINE
inline T
min(T a, T b) {
return((a < b) ? a : b); }
153 template<
typename T> TGX_INLINE
inline T
max(T a, T b) {
return((a > b) ? a : b); }
157 template<
typename T> TGX_INLINE
inline T
clamp(T v, T vmin, T vmax)
159 return max(vmin,
min(vmax, v));
164 TGX_INLINE
inline float roundfp(
const float f) {
return roundf(f); }
168 TGX_INLINE
inline double roundfp(
const double f) {
return round(f); }
172 TGX_INLINE
inline uint32_t
float_as_uint32(
float f) { uint32_t u; memcpy(&u, &f,
sizeof(uint32_t));
return u; }
176 TGX_INLINE
inline float uint32_as_float(uint32_t u) {
float f; memcpy(&f, &u,
sizeof(
float));
return f; }
182 TGX_INLINE
inline int32_t
safeMultB(int32_t A, int32_t B)
184 if ((A == 0) || (B == 0))
return B;
185 const int32_t max32 = 2147483647;
186 const int32_t nB = max32 / ((A > 0) ? A : (-A));
187 return ((B <= nB) ? B : nB);
199#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
203 "recip0.s %0, %2\n\t"
205 "msub.s %1, %2, %0\n\t"
206 "madd.s %0, %0, %1\n\t"
208 "msub.s %1, %2, %0\n\t"
215#elif TGX_USE_FAST_INV_TRICK
225#if TGX_USE_FMA_MATH_MISC
226 y *= fmaf(-x, y, 2.00128722f);
227 y *= fmaf(-x, y, 2.00000072f);
229 y *= 2.00128722f - x * y;
230 y *= 2.00000072f - x * y;
234 return ((x == 0) ? 1.0f : (1.0f / x));
245#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
250 "recip0.s %0, %2\n\t"
252 "msub.s %1, %2, %0\n\t"
259#elif TGX_USE_FAST_INV_TRICK
262#if TGX_USE_FMA_MATH_MISC
263 y *= fmaf(-x, y, 2.00128722f);
265 y *= 2.00128722f - x * y;
283 return ((x == 0) ? 1.0 : (1.0 / x));
313#if TGX_USE_FAST_SQRT_TRICK
317#if TGX_USE_FMA_MATH_MISC
318 return x * y * fmaf(-x, y * y, 1.89099002f);
320 return x * y * (1.89099002f - x * y * y);
345#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
347 float t0, t1, t2, t3, result;
349 "rsqrt0.s %0, %5\n\t"
350 "mul.s %1, %5, %0\n\t"
352 "mul.s %3, %2, %0\n\t"
354 "msub.s %4, %1, %0\n\t"
355 "madd.s %0, %3, %4\n\t"
356 "mul.s %1, %5, %0\n\t"
357 "mul.s %3, %2, %0\n\t"
359 "msub.s %4, %1, %0\n\t"
370 const float s = sqrtf(x);
371 return (s == 0) ? 1.0f : (1.0f / s);
381 const double s = sqrt(x);
382 return (s == 0) ? 1.0 : (1.0 / s);
392#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
394 float t0, t1, t2, t3, result;
396 "rsqrt0.s %0, %5\n\t"
397 "mul.s %1, %5, %0\n\t"
399 "mul.s %3, %2, %0\n\t"
401 "msub.s %4, %1, %0\n\t"
411#elif TGX_USE_FAST_INV_SQRT_TRICK
415#if TGX_USE_FMA_MATH_MISC
416 return y * fmaf(-x, y * y, 1.89099002f);
418 return y * (1.89099002f - x * y * y);
443#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
453 const int32_t i = (int32_t)x;
454 return i - ((float)i > x);
456 return (int32_t)floorf(x);
469 if (deg < 0.0f) deg = -deg;
470 if (deg >= 180.0f)
return -1.0f;
471 constexpr float INV_90 = 1.0f / 90.0f;
472 const float u = (deg - 90.0f) * INV_90;
473 const float au = (u < 0.0f) ? -u : u;
474 const float c = -u * (2.0f - au);
475 const float ac = (c < 0.0f) ? -c : c;
476 return c + 0.225f * (c * ac - c);
487 if (deg > 180.0f)
return 0.0f;
488 if (deg < -180.0f)
return 0.0f;
489 constexpr float INV_180 = 1.0f / 180.0f;
490 const float x = deg * INV_180;
491 const float ax = (x < 0.0f) ? -x : x;
492 const float s = 4.0f * x * (1.0f - ax);
493 const float as = (s < 0.0f) ? -s : s;
494 return s + 0.225f * (s * as - s);
TGX_INLINE int32_t lfloorf(float x)
Compute (int32_t)floorf(x).
Definition: Misc.h:441
TGX_INLINE T max(T a, T b)
Don't know why but much faster than fmaxf() for floats.
Definition: Misc.h:153
TGX_INLINE void swap(T &a, T &b)
Baby let me swap you one more time...
Definition: Misc.h:145
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:182
TGX_INLINE uint32_t float_as_uint32(float f)
Reinterpret the bits of a float as a uint32_t.
Definition: Misc.h:172
TGX_INLINE float tgx_fast_sin_deg_clamped(float deg)
Fast sine approximation for an angle expressed in degrees.
Definition: Misc.h:485
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition: Misc.h:390
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:291
TGX_INLINE T clamp(T v, T vmin, T vmax)
Template clamp version.
Definition: Misc.h:157
TGX_INLINE float roundfp(const float f)
Rounding for floats.
Definition: Misc.h:164
TGX_INLINE T min(T a, T b)
Don't know why but faster than fminf() for floats.
Definition: Misc.h:149
TGX_INLINE float tgx_fast_cos_deg_clamped(float deg)
Fast cosine approximation for an angle expressed in degrees.
Definition: Misc.h:467
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition: Misc.h:343
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:311
TGX_INLINE float uint32_as_float(uint32_t u)
Reinterpret the bits of a uint32_t as a float.
Definition: Misc.h:176
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition: Misc.h:197
TGX_INLINE float fast_inv_approx(float x)
Fast (very approximate) computation of 1/x.
Definition: Misc.h:243
TGX_INLINE uint16_t BigEndian16(uint16_t v)
little endian / big endian conversion
Definition: Misc.h:134
Configuration file depending on the architecture.