|
@@ -37,60 +37,41 @@
|
|
|
* Basic definitions and simple functions to be used everywhere.
|
|
|
*/
|
|
|
|
|
|
+// Include first in case the platform needs to pre-define/include some things.
|
|
|
#include "platform_config.h"
|
|
|
|
|
|
+// Should be available everywhere.
|
|
|
+#include "core/error_list.h"
|
|
|
+#include "core/int_types.h"
|
|
|
+
|
|
|
+// Turn argument to string constant:
|
|
|
+// https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
|
|
|
#ifndef _STR
|
|
|
#define _STR(m_x) #m_x
|
|
|
#define _MKSTR(m_x) _STR(m_x)
|
|
|
#endif
|
|
|
|
|
|
-//should always inline no matter what
|
|
|
+// Should always inline no matter what.
|
|
|
#ifndef _ALWAYS_INLINE_
|
|
|
-
|
|
|
-#if defined(__GNUC__) && (__GNUC__ >= 4)
|
|
|
-#define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
|
|
|
-#elif defined(__llvm__)
|
|
|
+#if defined(__GNUC__)
|
|
|
#define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
|
|
|
#elif defined(_MSC_VER)
|
|
|
#define _ALWAYS_INLINE_ __forceinline
|
|
|
#else
|
|
|
#define _ALWAYS_INLINE_ inline
|
|
|
#endif
|
|
|
-
|
|
|
#endif
|
|
|
|
|
|
-//should always inline, except in some cases because it makes debugging harder
|
|
|
+// Should always inline, except in debug builds because it makes debugging harder.
|
|
|
#ifndef _FORCE_INLINE_
|
|
|
-
|
|
|
#ifdef DISABLE_FORCED_INLINE
|
|
|
#define _FORCE_INLINE_ inline
|
|
|
#else
|
|
|
#define _FORCE_INLINE_ _ALWAYS_INLINE_
|
|
|
#endif
|
|
|
-
|
|
|
#endif
|
|
|
|
|
|
-//custom, gcc-safe offsetof, because gcc complains a lot.
|
|
|
-template <class T>
|
|
|
-T *_nullptr() {
|
|
|
- T *t = NULL;
|
|
|
- return t;
|
|
|
-}
|
|
|
-
|
|
|
-#define OFFSET_OF(st, m) \
|
|
|
- ((size_t)((char *)&(_nullptr<st>()->m) - (char *)0))
|
|
|
-/**
|
|
|
- * Some platforms (devices) don't define NULL
|
|
|
- */
|
|
|
-
|
|
|
-#ifndef NULL
|
|
|
-#define NULL 0
|
|
|
-#endif
|
|
|
-
|
|
|
-/**
|
|
|
- * Windows badly defines a lot of stuff we'll never use. Undefine it.
|
|
|
- */
|
|
|
-
|
|
|
+// Windows badly defines a lot of stuff we'll never use. Undefine it.
|
|
|
#ifdef _WIN32
|
|
|
#undef min // override standard definition
|
|
|
#undef max // override standard definition
|
|
@@ -105,18 +86,11 @@ T *_nullptr() {
|
|
|
#undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum
|
|
|
#endif
|
|
|
|
|
|
-#include "core/int_types.h"
|
|
|
-
|
|
|
-#include "core/error_list.h"
|
|
|
-
|
|
|
-/** Generic ABS function, for math uses please use Math::abs */
|
|
|
-
|
|
|
+// Generic ABS function, for math uses please use Math::abs.
|
|
|
#ifndef ABS
|
|
|
#define ABS(m_v) (((m_v) < 0) ? (-(m_v)) : (m_v))
|
|
|
#endif
|
|
|
|
|
|
-#define ABSDIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
|
|
|
-
|
|
|
#ifndef SGN
|
|
|
#define SGN(m_v) (((m_v) < 0) ? (-1.0) : (+1.0))
|
|
|
#endif
|
|
@@ -133,49 +107,24 @@ T *_nullptr() {
|
|
|
#define CLAMP(m_a, m_min, m_max) (((m_a) < (m_min)) ? (m_min) : (((m_a) > (m_max)) ? m_max : m_a))
|
|
|
#endif
|
|
|
|
|
|
-/** Generic swap template */
|
|
|
+// Generic swap template.
|
|
|
#ifndef SWAP
|
|
|
-
|
|
|
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
|
|
|
template <class T>
|
|
|
inline void __swap_tmpl(T &x, T &y) {
|
|
|
-
|
|
|
T aux = x;
|
|
|
x = y;
|
|
|
y = aux;
|
|
|
}
|
|
|
+#endif // SWAP
|
|
|
|
|
|
-#endif //swap
|
|
|
-
|
|
|
-/* clang-format off */
|
|
|
-#define HEX2CHR(m_hex) \
|
|
|
- ((m_hex >= '0' && m_hex <= '9') ? (m_hex - '0') : \
|
|
|
- ((m_hex >= 'A' && m_hex <= 'F') ? (10 + m_hex - 'A') : \
|
|
|
- ((m_hex >= 'a' && m_hex <= 'f') ? (10 + m_hex - 'a') : 0)))
|
|
|
-/* clang-format on */
|
|
|
-
|
|
|
-// Macro to check whether we are compiled by clang
|
|
|
-// and we have a specific builtin
|
|
|
-#if defined(__llvm__) && defined(__has_builtin)
|
|
|
-#define _llvm_has_builtin(x) __has_builtin(x)
|
|
|
-#else
|
|
|
-#define _llvm_has_builtin(x) 0
|
|
|
-#endif
|
|
|
-
|
|
|
-#if (defined(__GNUC__) && (__GNUC__ >= 5)) || _llvm_has_builtin(__builtin_mul_overflow)
|
|
|
-#define _mul_overflow __builtin_mul_overflow
|
|
|
-#endif
|
|
|
-
|
|
|
-#if (defined(__GNUC__) && (__GNUC__ >= 5)) || _llvm_has_builtin(__builtin_add_overflow)
|
|
|
-#define _add_overflow __builtin_add_overflow
|
|
|
-#endif
|
|
|
-
|
|
|
-/** Function to find the next power of 2 to an integer */
|
|
|
+/* Functions to handle powers of 2 and shifting. */
|
|
|
|
|
|
+// Function to find the next power of 2 to an integer.
|
|
|
static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
|
|
|
-
|
|
|
- if (x == 0)
|
|
|
+ if (x == 0) {
|
|
|
return 0;
|
|
|
+ }
|
|
|
|
|
|
--x;
|
|
|
x |= x >> 1;
|
|
@@ -187,8 +136,8 @@ static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
|
|
|
return ++x;
|
|
|
}
|
|
|
|
|
|
+// Function to find the previous power of 2 to an integer.
|
|
|
static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
|
|
|
-
|
|
|
x |= x >> 1;
|
|
|
x |= x >> 2;
|
|
|
x |= x >> 4;
|
|
@@ -197,40 +146,45 @@ static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
|
|
|
return x - (x >> 1);
|
|
|
}
|
|
|
|
|
|
+// Function to find the closest power of 2 to an integer.
|
|
|
static _FORCE_INLINE_ unsigned int closest_power_of_2(unsigned int x) {
|
|
|
-
|
|
|
unsigned int nx = next_power_of_2(x);
|
|
|
unsigned int px = previous_power_of_2(x);
|
|
|
return (nx - x) > (x - px) ? px : nx;
|
|
|
}
|
|
|
|
|
|
-// We need this definition inside the function below.
|
|
|
-static inline int get_shift_from_power_of_2(unsigned int p_pixel);
|
|
|
+// Get a shift value from a power of 2.
|
|
|
+static inline int get_shift_from_power_of_2(unsigned int p_bits) {
|
|
|
+ for (unsigned int i = 0; i < 32; i++) {
|
|
|
+ if (p_bits == (unsigned int)(1 << i)) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return -1;
|
|
|
+}
|
|
|
|
|
|
template <class T>
|
|
|
static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
|
|
|
-
|
|
|
--x;
|
|
|
|
|
|
// The number of operations on x is the base two logarithm
|
|
|
- // of the p_number of bits in the type. Add three to account
|
|
|
+ // of the number of bits in the type. Add three to account
|
|
|
// for sizeof(T) being in bytes.
|
|
|
size_t num = get_shift_from_power_of_2(sizeof(T)) + 3;
|
|
|
|
|
|
- // If the compiler is smart, it unrolls this loop
|
|
|
- // If its dumb, this is a bit slow.
|
|
|
- for (size_t i = 0; i < num; i++)
|
|
|
+ // If the compiler is smart, it unrolls this loop.
|
|
|
+ // If it's dumb, this is a bit slow.
|
|
|
+ for (size_t i = 0; i < num; i++) {
|
|
|
x |= x >> (1 << i);
|
|
|
+ }
|
|
|
|
|
|
return ++x;
|
|
|
}
|
|
|
|
|
|
-/** Function to find the nearest (bigger) power of 2 to an integer */
|
|
|
-
|
|
|
+// Function to find the nearest (bigger) power of 2 to an integer.
|
|
|
static inline unsigned int nearest_shift(unsigned int p_number) {
|
|
|
-
|
|
|
for (int i = 30; i >= 0; i--) {
|
|
|
-
|
|
|
if (p_number & (1 << i))
|
|
|
return i + 1;
|
|
|
}
|
|
@@ -238,41 +192,20 @@ static inline unsigned int nearest_shift(unsigned int p_number) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/** get a shift value from a power of 2 */
|
|
|
-static inline int get_shift_from_power_of_2(unsigned int p_pixel) {
|
|
|
- // return a GL_TEXTURE_SIZE_ENUM
|
|
|
-
|
|
|
- for (unsigned int i = 0; i < 32; i++) {
|
|
|
-
|
|
|
- if (p_pixel == (unsigned int)(1 << i))
|
|
|
- return i;
|
|
|
- }
|
|
|
-
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
-/** Swap 16 bits value for endianness */
|
|
|
-#if defined(__GNUC__) || _llvm_has_builtin(__builtin_bswap16)
|
|
|
+// Swap 16, 32 and 64 bits value for endianness.
|
|
|
+#if defined(__GNUC__)
|
|
|
#define BSWAP16(x) __builtin_bswap16(x)
|
|
|
+#define BSWAP32(x) __builtin_bswap32(x)
|
|
|
+#define BSWAP64(x) __builtin_bswap64(x)
|
|
|
#else
|
|
|
static inline uint16_t BSWAP16(uint16_t x) {
|
|
|
return (x >> 8) | (x << 8);
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
-/** Swap 32 bits value for endianness */
|
|
|
-#if defined(__GNUC__) || _llvm_has_builtin(__builtin_bswap32)
|
|
|
-#define BSWAP32(x) __builtin_bswap32(x)
|
|
|
-#else
|
|
|
static inline uint32_t BSWAP32(uint32_t x) {
|
|
|
return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
-/** Swap 64 bits value for endianness */
|
|
|
-#if defined(__GNUC__) || _llvm_has_builtin(__builtin_bswap64)
|
|
|
-#define BSWAP64(x) __builtin_bswap64(x)
|
|
|
-#else
|
|
|
static inline uint64_t BSWAP64(uint64_t x) {
|
|
|
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
|
|
|
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
|
|
@@ -281,40 +214,24 @@ static inline uint64_t BSWAP64(uint64_t x) {
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-/** When compiling with RTTI, we can add an "extra"
|
|
|
- * layer of safeness in many operations, so dynamic_cast
|
|
|
- * is used besides casting by enum.
|
|
|
- */
|
|
|
-
|
|
|
+// Generic comparator used in Map, List, etc.
|
|
|
template <class T>
|
|
|
struct Comparator {
|
|
|
-
|
|
|
_ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
|
|
|
};
|
|
|
|
|
|
+// Global lock macro, relies on the static Mutex::_global_mutex.
|
|
|
void _global_lock();
|
|
|
void _global_unlock();
|
|
|
|
|
|
struct _GlobalLock {
|
|
|
-
|
|
|
_GlobalLock() { _global_lock(); }
|
|
|
~_GlobalLock() { _global_unlock(); }
|
|
|
};
|
|
|
|
|
|
#define GLOBAL_LOCK_FUNCTION _GlobalLock _global_lock_;
|
|
|
|
|
|
-#ifdef NO_SAFE_CAST
|
|
|
-#define SAFE_CAST static_cast
|
|
|
-#else
|
|
|
-#define SAFE_CAST dynamic_cast
|
|
|
-#endif
|
|
|
-
|
|
|
-#define MT_SAFE
|
|
|
-
|
|
|
-#define __STRX(m_index) #m_index
|
|
|
-#define __STR(m_index) __STRX(m_index)
|
|
|
-
|
|
|
-#ifdef __GNUC__
|
|
|
+#if defined(__GNUC__)
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
#else
|
|
@@ -330,14 +247,12 @@ struct _GlobalLock {
|
|
|
#define _PRINTF_FORMAT_ATTRIBUTE_2_3
|
|
|
#endif
|
|
|
|
|
|
-/** This is needed due to a strange OpenGL API that expects a pointer
|
|
|
- * type for an argument that is actually an offset.
|
|
|
- */
|
|
|
+// This is needed due to a strange OpenGL API that expects a pointer
|
|
|
+// type for an argument that is actually an offset.
|
|
|
#define CAST_INT_TO_UCHAR_PTR(ptr) ((uint8_t *)(uintptr_t)(ptr))
|
|
|
|
|
|
// Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
|
|
|
// https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
|
|
|
-
|
|
|
template <size_t... Is>
|
|
|
struct IndexSequence {};
|
|
|
|