Core.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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(__SSE4_1__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_1)
  89. #define JPH_USE_SSE4_1
  90. #endif
  91. #if (defined(__SSE4_2__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_2)
  92. #define JPH_USE_SSE4_2
  93. #endif
  94. #if defined(__AVX__) && !defined(JPH_USE_AVX)
  95. #define JPH_USE_AVX
  96. #endif
  97. #if defined(__AVX2__) && !defined(JPH_USE_AVX2)
  98. #define JPH_USE_AVX2
  99. #endif
  100. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  101. #if defined(__FMA__) && !defined(JPH_USE_FMADD)
  102. #define JPH_USE_FMADD
  103. #endif
  104. #elif defined(JPH_COMPILER_MSVC)
  105. #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
  106. #define JPH_USE_FMADD
  107. #endif
  108. #else
  109. #error Undefined compiler
  110. #endif
  111. #elif defined(__aarch64__) || defined(_M_ARM64)
  112. // ARM64 CPU architecture
  113. #define JPH_CPU_ARM64
  114. #define JPH_USE_NEON
  115. #else
  116. #error Unsupported CPU architecture
  117. #endif
  118. // OS-specific includes
  119. #if defined(JPH_PLATFORM_WINDOWS)
  120. #define JPH_BREAKPOINT __debugbreak()
  121. #elif defined(JPH_PLATFORM_BLUE)
  122. // Configuration for a popular game console.
  123. // This file is not distributed because it would violate an NDA.
  124. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  125. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS and JPH_PLATFORM_BLUE_GET_TICK_FREQUENCY and include the right header).
  126. #include <Core/PlatformBlue.h>
  127. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  128. #include <float.h>
  129. #include <limits.h>
  130. #include <string.h>
  131. #if defined(JPH_CPU_X64)
  132. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  133. #elif defined(JPH_CPU_ARM64)
  134. #define JPH_BREAKPOINT __builtin_trap()
  135. #endif
  136. #else
  137. #error Unknown platform
  138. #endif
  139. // Crashes the application
  140. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  141. // Standard C++ includes
  142. #include <vector>
  143. #include <algorithm>
  144. #include <utility>
  145. #include <cmath>
  146. #include <sstream>
  147. #include <functional>
  148. #if defined(JPH_USE_SSE)
  149. #include <immintrin.h>
  150. #elif defined(JPH_USE_NEON)
  151. #include <arm_neon.h>
  152. #endif
  153. namespace JPH {
  154. using namespace std;
  155. // Standard types
  156. using uint = unsigned int;
  157. using uint8 = uint8_t;
  158. using uint16 = uint16_t;
  159. using uint32 = uint32_t;
  160. using uint64 = uint64_t;
  161. // Assert sizes of types
  162. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  163. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  164. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  165. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  166. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  167. static_assert(sizeof(void *) == 8, "Invalid size of pointer");
  168. // Define inline macro
  169. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  170. #define JPH_INLINE __inline__ __attribute__((always_inline))
  171. #elif defined(JPH_COMPILER_MSVC)
  172. #define JPH_INLINE __forceinline
  173. #else
  174. #error Undefined
  175. #endif
  176. // Cache line size (used for aligning to cache line)
  177. #ifndef JPH_CACHE_LINE_SIZE
  178. #define JPH_CACHE_LINE_SIZE 64
  179. #endif
  180. // Define macro to get current function name
  181. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  182. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  183. #elif defined(JPH_COMPILER_MSVC)
  184. #define JPH_FUNCTION_NAME __FUNCTION__
  185. #else
  186. #error Undefined
  187. #endif
  188. // Stack allocation
  189. #define JPH_STACK_ALLOC(n) alloca(n)
  190. // Shorthand for #ifdef _DEBUG / #endif
  191. #ifdef _DEBUG
  192. #define JPH_IF_DEBUG(...) __VA_ARGS__
  193. #else
  194. #define JPH_IF_DEBUG(...)
  195. #endif
  196. // Macro to indicate that a parameter / variable is unused
  197. #define JPH_UNUSED(x) (void)x
  198. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  199. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  200. // In clang it appears you cannot turn off -ffast-math and -ffp-contract=fast for a code block
  201. // 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)
  202. // There is #pragma float_control(precise, on) but that doesn't work under clang 9.
  203. // So for now we compile clang without -ffast-math so the macros are empty
  204. #define JPH_PRECISE_MATH_ON
  205. #define JPH_PRECISE_MATH_OFF
  206. #elif defined(JPH_COMPILER_MSVC)
  207. // 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
  208. #define JPH_PRECISE_MATH_ON \
  209. __pragma(float_control(precise, on, push)) \
  210. __pragma(fp_contract(off))
  211. #define JPH_PRECISE_MATH_OFF \
  212. __pragma(fp_contract(on)) \
  213. __pragma(float_control(pop))
  214. #else
  215. #error Undefined
  216. #endif
  217. } // JPH