Core.h 8.2 KB

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