Core.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Determine platform
  5. #if defined(JPH_PLATFORM_BLUE)
  6. // Correct define already defined, this overrides everything else
  7. #elif defined(_WIN32) || defined(_WIN64)
  8. #define JPH_PLATFORM_WINDOWS
  9. #elif defined(__ANDROID__) // Android is linux too, so that's why we check it first
  10. #define JPH_PLATFORM_ANDROID
  11. #elif defined(__linux__)
  12. #define JPH_PLATFORM_LINUX
  13. #elif defined(__APPLE__)
  14. #include <TargetConditionals.h>
  15. #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
  16. #define JPH_PLATFORM_MACOS
  17. #else
  18. #define JPH_PLATFORM_IOS
  19. #endif
  20. #endif
  21. // Determine compiler and turn off warnings
  22. #if defined(__clang__)
  23. #define JPH_COMPILER_CLANG
  24. #pragma clang diagnostic ignored "-Wc++98-compat"
  25. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  26. #pragma clang diagnostic ignored "-Wfloat-equal"
  27. #pragma clang diagnostic ignored "-Wnewline-eof"
  28. #pragma clang diagnostic ignored "-Wsign-conversion"
  29. #pragma clang diagnostic ignored "-Wold-style-cast"
  30. #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
  31. #pragma clang diagnostic ignored "-Wnested-anon-types"
  32. #pragma clang diagnostic ignored "-Wglobal-constructors"
  33. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  34. #pragma clang diagnostic ignored "-Wnonportable-system-include-path"
  35. #pragma clang diagnostic ignored "-Wlanguage-extension-token"
  36. #pragma clang diagnostic ignored "-Wunused-parameter"
  37. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  38. #pragma clang diagnostic ignored "-Wcovered-switch-default"
  39. #pragma clang diagnostic ignored "-Wcast-align"
  40. #pragma clang diagnostic ignored "-Winvalid-offsetof"
  41. #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
  42. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  43. #pragma clang diagnostic ignored "-Wctad-maybe-unsupported"
  44. #ifndef JPH_PLATFORM_ANDROID
  45. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
  46. #endif
  47. #elif defined(__GNUC__)
  48. #define JPH_COMPILER_GCC
  49. #pragma GCC diagnostic ignored "-Wcomment"
  50. #pragma GCC diagnostic ignored "-Winvalid-offsetof"
  51. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  52. #elif defined(_MSC_VER)
  53. #define JPH_COMPILER_MSVC
  54. #pragma warning (disable : 4514) // 'X' : unreferenced inline function has been removed
  55. #pragma warning (disable : 4710) // 'X' : function not inlined
  56. #pragma warning (disable : 4711) // function 'X' selected for automatic inline expansion
  57. #pragma warning (disable : 4820) // 'X': 'Y' bytes padding added after data member 'Z'
  58. #pragma warning (disable : 4100) // 'X' : unreferenced formal parameter
  59. #pragma warning (disable : 4626) // 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted
  60. #pragma warning (disable : 5027) // 'X' : move assignment operator was implicitly defined as deleted because a base class move assignment operator is inaccessible or deleted
  61. #pragma warning (disable : 4365) // 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch
  62. #pragma warning (disable : 4324) // 'X' : structure was padded due to alignment specifier
  63. #pragma warning (disable : 4625) // 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted
  64. #pragma warning (disable : 5026) // 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted
  65. #pragma warning (disable : 4623) // 'X' : default constructor was implicitly defined as deleted
  66. #pragma warning (disable : 4201) // nonstandard extension used: nameless struct/union
  67. #pragma warning (disable : 4371) // 'X': layout of class may have changed from a previous version of the compiler due to better packing of member 'Y'
  68. #pragma warning (disable : 5045) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
  69. #pragma warning (disable : 4583) // 'X': destructor is not implicitly called
  70. #pragma warning (disable : 4582) // 'X': constructor is not implicitly called
  71. #pragma warning (disable : 5219) // implicit conversion from 'X' to 'Y', possible loss of data
  72. #endif
  73. // Detect CPU architecture
  74. #if defined(__x86_64__) || defined(_M_X64)
  75. // X86 CPU architecture
  76. #define JPH_CPU_X64
  77. #define JPH_USE_SSE
  78. // Detect enabled instruction sets
  79. #if (defined(__F16C__) || defined(__AVX2__)) && !defined(JPH_USE_F16C)
  80. #define JPH_USE_F16C
  81. #endif
  82. #if (defined(__LZCNT__) || defined(__AVX2__)) && !defined(JPH_USE_LZCNT)
  83. #define JPH_USE_LZCNT
  84. #endif
  85. #if (defined(__BMI__) || defined(__AVX2__)) && !defined(JPH_USE_TZCNT)
  86. #define JPH_USE_TZCNT
  87. #endif
  88. #if defined(__AVX__) && !defined(JPH_USE_AVX)
  89. #define JPH_USE_AVX
  90. #endif
  91. #if defined(__AVX2__) && !defined(JPH_USE_AVX2)
  92. #define JPH_USE_AVX2
  93. #endif
  94. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  95. #if defined(__FMA__) && !defined(JPH_USE_FMADD)
  96. #define JPH_USE_FMADD
  97. #endif
  98. #elif defined(JPH_COMPILER_MSVC)
  99. #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
  100. #define JPH_USE_FMADD
  101. #endif
  102. #else
  103. #error Undefined compiler
  104. #endif
  105. #elif defined(__aarch64__) || defined(_M_ARM64)
  106. // ARM64 CPU architecture
  107. #define JPH_CPU_ARM64
  108. #define JPH_USE_NEON
  109. #else
  110. #error Unsupported CPU architecture
  111. #endif
  112. // OS-specific includes
  113. #if defined(JPH_PLATFORM_WINDOWS)
  114. #define JPH_BREAKPOINT __debugbreak()
  115. #elif defined(JPH_PLATFORM_BLUE)
  116. // Configuration for a popular game console.
  117. // This file is not distributed because it would violate an NDA.
  118. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  119. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS and JPH_PLATFORM_BLUE_GET_TICK_FREQUENCY and include the right header).
  120. #include <Core/PlatformBlue.h>
  121. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  122. #include <float.h>
  123. #include <limits.h>
  124. #include <string.h>
  125. #if defined(JPH_CPU_X64)
  126. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  127. #elif defined(JPH_CPU_ARM64)
  128. #define JPH_BREAKPOINT __builtin_trap()
  129. #endif
  130. #else
  131. #error Unknown platform
  132. #endif
  133. // Crashes the application
  134. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  135. // Standard C++ includes
  136. #include <vector>
  137. #include <algorithm>
  138. #include <utility>
  139. #include <cmath>
  140. #include <sstream>
  141. #include <functional>
  142. #if defined(JPH_USE_SSE)
  143. #include <immintrin.h>
  144. #elif defined(JPH_USE_NEON)
  145. #include <arm_neon.h>
  146. #endif
  147. namespace JPH {
  148. using namespace std;
  149. // Standard types
  150. using uint = unsigned int;
  151. using uint8 = uint8_t;
  152. using uint16 = uint16_t;
  153. using uint32 = uint32_t;
  154. using uint64 = uint64_t;
  155. // Assert sizes of types
  156. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  157. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  158. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  159. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  160. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  161. static_assert(sizeof(void *) == 8, "Invalid size of pointer");
  162. // Define inline macro
  163. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  164. #define JPH_INLINE __inline__ __attribute__((always_inline))
  165. #elif defined(JPH_COMPILER_MSVC)
  166. #define JPH_INLINE __forceinline
  167. #else
  168. #error Undefined
  169. #endif
  170. // Cache line size (used for aligning to cache line)
  171. #ifndef JPH_CACHE_LINE_SIZE
  172. #define JPH_CACHE_LINE_SIZE 64
  173. #endif
  174. // Define macro to get current function name
  175. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  176. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  177. #elif defined(JPH_COMPILER_MSVC)
  178. #define JPH_FUNCTION_NAME __FUNCTION__
  179. #else
  180. #error Undefined
  181. #endif
  182. // Stack allocation
  183. #define JPH_STACK_ALLOC(n) alloca(n)
  184. // Shorthand for #ifdef _DEBUG / #endif
  185. #ifdef _DEBUG
  186. #define JPH_IF_DEBUG(...) __VA_ARGS__
  187. #else
  188. #define JPH_IF_DEBUG(...)
  189. #endif
  190. // Macro to indicate that a parameter / variable is unused
  191. #define JPH_UNUSED(x) (void)x
  192. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  193. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  194. // In clang it appears you cannot turn off -ffast-math and -ffp-contract=fast for a code block
  195. // There is #pragma clang fp contract (off) but that doesn't seem to work under clang 9 & 10 when -ffast-math is specified on the commandline (you override it to turn it on, but not off)
  196. // There is #pragma float_control(precise, on) but that doesn't work under clang 9.
  197. // So for now we compile clang without -ffast-math so the macros are empty
  198. #define JPH_PRECISE_MATH_ON
  199. #define JPH_PRECISE_MATH_OFF
  200. #elif defined(JPH_COMPILER_MSVC)
  201. // Unfortunately there is no way to push the state of fp_contract, so we have to assume it was turned on before JPH_PRECISE_MATH_ON
  202. #define JPH_PRECISE_MATH_ON \
  203. __pragma(float_control(precise, on, push)) \
  204. __pragma(fp_contract(off))
  205. #define JPH_PRECISE_MATH_OFF \
  206. __pragma(fp_contract(on)) \
  207. __pragma(float_control(pop))
  208. #else
  209. #error Undefined
  210. #endif
  211. } // JPH