Core.h 8.2 KB

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