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 (TGX_USE_FAST_INV_TRICK)
228 v.u = 0x5f375a86 - (v.u >> 1);
229 const float x2 = x * 0.5f;
230 const float threehalfs = 1.5f;
231 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
235 return ((x == 0) ? 1.0f : (1.0f / x));
246 return ((x == 0) ? 1.0 : (1.0 / x));
274#if defined (TGX_USE_FAST_SQRT_TRICK)
281 v.u = 0x5f375a86 - (v.u >> 1);
282 const float x2 = x * 0.5f;
283 const float threehalfs = 1.5f;
284 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
308 const float s = sqrtf(x);
309 return (s == 0) ? 1.0f : (1.0f / s);
318 const double s = sqrt(x);
319 return (s == 0) ? 1.0 : (1.0 / sqrt(s));
328#if defined (TGX_USE_FAST_INV_SQRT_TRICK)
337 v.u = 0x5f375a86 - (v.u >> 1);
338 const float x2 = x * 0.5f;
339 const float threehalfs = 1.5f;
340 v.f = v.f * (threehalfs - (x2 * v.f * v.f));
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:326
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:254
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:306
TGX_INLINE float fast_sqrt(float x)
Compute a fast approximation of the square root of a float.
Definition: Misc.h:272
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