31#ifndef MTOOLS_TGX_EXTENSIONS
32#define MTOOLS_TGX_EXTENSIONS 0
37#if defined(TEENSYDUINO) || defined(ESP32) || defined(ARDUINO_ARCH_STM32)
39 #define TGX_ON_ARDUINO
41 #define TGX_USE_FAST_INV_SQRT_TRICK
42 #define TGX_USE_FAST_SQRT_TRICK
45 #define TGX_INLINE __attribute__((always_inline))
46 #define TGX_NOINLINE __attribute__((noinline, noclone)) FLASHMEM
62#ifndef TGX_SINGLE_PRECISION_COMPUTATIONS
63 #define TGX_SINGLE_PRECISION_COMPUTATIONS 1
67#define TGX_DEFAULT_NO_BLENDING -1.0f
70#if defined(TEENSYDUINO) || defined(ESP32)
73#define TGX_PROGMEM_DEFAULT_CACHE_SIZE 8192
77#define TGX_PROGMEM_DEFAULT_CACHE_SIZE 262144
82#define TGX_CAST32(a) ((int32_t)(a))
88#define DEPRECATED(X) [[deprecated(" " X " ")]]
92static_assert(
sizeof(int) >= 4,
"The TGX library only works on 32 bits or 64 bits architecture. Sorry!");
96#if defined(ARDUINO_TEENSY41)
99 extern "C" uint8_t external_psram_size;
102 #define TGX_IS_PROGMEM(X) ((((uint32_t)(X)) >= 0x60000000)&&(((uint32_t)(X)) < 0x70000000))
105 #define TGX_IS_EXTMEM(X) ((((uint32_t)(X)) >= 0x70000000)&&(((uint32_t)(X)) < 0x80000000))
110#define M_PI 3.14159265358979323846
123 template<
int N>
struct DummyType
134 template<
bool BB1,
bool BB2>
struct DummyTypeBB
141 template<
typename T =
int>
struct DefaultFPType
143#if TGX_SINGLE_PRECISION_COMPUTATIONS
144 typedef float fptype;
146 typedef double fptype;
150#if TGX_SINGLE_PRECISION_COMPUTATIONS
152 template<>
struct DefaultFPType<double>
154 typedef double fptype;
160 template <
typename,
typename>
struct is_same {
static const bool value =
false; };
161 template <
typename T>
struct is_same<T, T> {
static const bool value =
true; };
168 return __builtin_bswap16(v);
170 return ((v >> 8) | (v << 8));
176 template<
typename T> TGX_INLINE
inline void swap(T& a, T& b) { T c(a); a = b; b = c; }
180 template<
typename T> TGX_INLINE
inline T
min(
const T & a,
const T & b) {
return((a < b) ? a : b); }
184 template<
typename T> TGX_INLINE
inline T
max(
const T & a,
const T & b) {
return((a > b) ? a : b); }
188 template<
typename T> TGX_INLINE
inline T
clamp(
const T & v,
const T & vmin,
const T & vmax)
190 return max(vmin,
min(vmax, v));
195 TGX_INLINE
inline float roundfp(
const float f) {
return roundf(f); }
199 TGX_INLINE
inline double roundfp(
const double f) {
return round(f); }
206 TGX_INLINE
inline int32_t
safeMultB(int32_t A, int32_t B)
208 if ((A == 0) || (B == 0))
return B;
209 const int32_t max32 = 2147483647;
210 const int32_t nB = max32 / ((A > 0) ? A : (-A));
211 return ((B <= nB) ? B : nB);
221#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
225 "recip0.s %0, %2\n\t"
227 "msub.s %1, %2, %0\n\t"
228 "madd.s %0, %0, %1\n\t"
230 "msub.s %1, %2, %0\n\t"
237#elif defined (TGX_USE_FAST_INV_TRICK)
244 v.u = 0x5f375a86 - (v.u >> 1);
245 const float x2 = x * 0.5f;
246 const float threehalfs = 1.5f;
247 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
251 return ((x == 0) ? 1.0f : (1.0f / x));
262 return ((x == 0) ? 1.0 : (1.0 / x));
290#if defined (TGX_USE_FAST_SQRT_TRICK)
297 v.u = 0x5f375a86 - (v.u >> 1);
298 const float x2 = x * 0.5f;
299 const float threehalfs = 1.5f;
300 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
324#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
326 float t0, t1, t2, t3, result;
328 "rsqrt0.s %0, %5\n\t"
329 "mul.s %1, %5, %0\n\t"
331 "mul.s %3, %2, %0\n\t"
333 "msub.s %4, %1, %0\n\t"
334 "madd.s %0, %3, %4\n\t"
335 "mul.s %1, %5, %0\n\t"
336 "mul.s %3, %2, %0\n\t"
338 "msub.s %4, %1, %0\n\t"
349 const float s = sqrtf(x);
350 return (s == 0) ? 1.0f : (1.0f / s);
360 const double s = sqrt(x);
361 return (s == 0) ? 1.0 : (1.0 / sqrt(s));
370#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
372 float t0, t1, t2, t3, result;
374 "rsqrt0.s %0, %5\n\t"
375 "mul.s %1, %5, %0\n\t"
377 "mul.s %3, %2, %0\n\t"
379 "msub.s %4, %1, %0\n\t"
389#elif defined (TGX_USE_FAST_INV_SQRT_TRICK)
398 v.u = 0x5f375a86 - (v.u >> 1);
399 const float x2 = x * 0.5f;
400 const float threehalfs = 1.5f;
401 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
425#if defined(__XTENSA__) && !defined(__XTENSA_SOFT_FLOAT__)
434 return (int32_t)floorf(x);
TGX_INLINE int32_t lfloorf(float x)
Compute (int32_t)floorf(x).
Definition Misc.h:423
TGX_INLINE T min(const T &a, const T &b)
Don't know why but faster than fminf() for floats.
Definition Misc.h:180
TGX_INLINE T max(const T &a, const T &b)
Don't know why but much faster than fmaxf() for floats.
Definition Misc.h:184
TGX_INLINE T clamp(const T &v, const T &vmin, const T &vmax)
Template clamp version.
Definition Misc.h:188
TGX_INLINE void swap(T &a, T &b)
Baby let me swap you one more time...
Definition Misc.h:176
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:206
TGX_INLINE float fast_invsqrt(float x)
Compute a fast approximation of the inverse square root of a float.
Definition Misc.h:368
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition Misc.h:270
TGX_INLINE float roundfp(const float f)
Rounding for floats.
Definition Misc.h:195
TGX_INLINE float precise_invsqrt(float x)
Compute the inverse square root of a float (exact computation).
Definition Misc.h:322
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition Misc.h:288
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition Misc.h:219
TGX_INLINE uint16_t BigEndian16(uint16_t v)
little endian / big endian conversion
Definition Misc.h:165