nvcore.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // This code is in the public domain -- Ignacio Castaño <[email protected]>
  2. #ifndef NV_CORE_H
  3. #define NV_CORE_H
  4. #define NVCORE_SHARED 0
  5. #define NV_NO_ASSERT 1
  6. // Function linkage
  7. #if NVCORE_SHARED
  8. #ifdef NVCORE_EXPORTS
  9. #define NVCORE_API DLL_EXPORT
  10. #define NVCORE_CLASS DLL_EXPORT_CLASS
  11. #else
  12. #define NVCORE_API DLL_IMPORT
  13. #define NVCORE_CLASS DLL_IMPORT
  14. #endif
  15. #else // NVCORE_SHARED
  16. #define NVCORE_API
  17. #define NVCORE_CLASS
  18. #endif // NVCORE_SHARED
  19. // Platform definitions
  20. #include "posh.h"
  21. #define NV_OS_STRING POSH_OS_STRING
  22. #if defined POSH_OS_LINUX
  23. # define NV_OS_LINUX 1
  24. # define NV_OS_UNIX 1
  25. #elif defined POSH_OS_ORBIS
  26. # define NV_OS_ORBIS 1
  27. #elif defined POSH_OS_FREEBSD
  28. # define NV_OS_FREEBSD 1
  29. # define NV_OS_UNIX 1
  30. #elif defined POSH_OS_OPENBSD
  31. # define NV_OS_OPENBSD 1
  32. # define NV_OS_UNIX 1
  33. #elif defined POSH_OS_CYGWIN32
  34. # define NV_OS_CYGWIN 1
  35. #elif defined POSH_OS_MINGW
  36. # define NV_OS_MINGW 1
  37. # define NV_OS_WIN32 1
  38. #elif defined POSH_OS_OSX
  39. # define NV_OS_DARWIN 1
  40. # define NV_OS_UNIX 1
  41. #elif defined POSH_OS_IOS
  42. # define NV_OS_DARWIN 1 //ACS should we keep this on IOS?
  43. # define NV_OS_UNIX 1
  44. # define NV_OS_IOS 1
  45. #elif defined POSH_OS_UNIX
  46. # define NV_OS_UNIX 1
  47. #elif defined POSH_OS_WIN64
  48. # define NV_OS_WIN32 1
  49. # define NV_OS_WIN64 1
  50. #elif defined POSH_OS_WIN32
  51. # define NV_OS_WIN32 1
  52. #elif defined POSH_OS_XBOX
  53. # define NV_OS_XBOX 1
  54. #else
  55. # error "Unsupported OS"
  56. #endif
  57. #ifndef NV_OS_WIN32
  58. # define NV_OS_WIN32 0
  59. #endif // NV_OS_WIN32
  60. #ifndef NV_OS_WIN64
  61. # define NV_OS_WIN64 0
  62. #endif // NV_OS_WIN64
  63. #ifndef NV_OS_MINGW
  64. # define NV_OS_MINGW 0
  65. #endif // NV_OS_MINGW
  66. #ifndef NV_OS_CYGWIN
  67. # define NV_OS_CYGWIN 0
  68. #endif // NV_OS_CYGWIN
  69. #ifndef NV_OS_LINUX
  70. # define NV_OS_LINUX 0
  71. #endif // NV_OS_LINUX
  72. #ifndef NV_OS_FREEBSD
  73. # define NV_OS_FREEBSD 0
  74. #endif // NV_OS_FREEBSD
  75. #ifndef NV_OS_OPENBSD
  76. # define NV_OS_OPENBSD 0
  77. #endif // NV_OS_OPENBSD
  78. #ifndef NV_OS_UNIX
  79. # define NV_OS_UNIX 0
  80. #endif // NV_OS_UNIX
  81. #ifndef NV_OS_DARWIN
  82. # define NV_OS_DARWIN 0
  83. #endif // NV_OS_DARWIN
  84. #ifndef NV_OS_XBOX
  85. # define NV_OS_XBOX 0
  86. #endif // NV_OS_XBOX
  87. #ifndef NV_OS_ORBIS
  88. # define NV_OS_ORBIS 0
  89. #endif // NV_OS_ORBIS
  90. #ifndef NV_OS_IOS
  91. # define NV_OS_IOS 0
  92. #endif // NV_OS_IOS
  93. // Threading:
  94. // some platforms don't implement __thread or similar for thread-local-storage
  95. #if NV_OS_UNIX || NV_OS_ORBIS || NV_OS_IOS //ACStodoIOS darwin instead of ios?
  96. # define NV_OS_USE_PTHREAD 1
  97. # if NV_OS_DARWIN || NV_OS_IOS
  98. # define NV_OS_HAS_TLS_QUALIFIER 0
  99. # else
  100. # define NV_OS_HAS_TLS_QUALIFIER 1
  101. # endif
  102. #else
  103. # define NV_OS_USE_PTHREAD 0
  104. # define NV_OS_HAS_TLS_QUALIFIER 1
  105. #endif
  106. // CPUs:
  107. #define NV_CPU_STRING POSH_CPU_STRING
  108. #if defined POSH_CPU_X86_64
  109. //# define NV_CPU_X86 1
  110. # define NV_CPU_X86_64 1
  111. #elif defined POSH_CPU_X86
  112. # define NV_CPU_X86 1
  113. #elif defined POSH_CPU_PPC
  114. # define NV_CPU_PPC 1
  115. #elif defined POSH_CPU_STRONGARM
  116. # define NV_CPU_ARM 1
  117. #elif defined POSH_CPU_AARCH64
  118. # define NV_CPU_AARCH64 1
  119. #elif defined POSH_CPU_RISCV64
  120. # define NV_CPU_RISCV64 1
  121. #elif defined POSH_CPU_LOONGARCH64
  122. # define NV_CPU_LOONGARCH64 1
  123. #elif defined POSH_CPU_EMSCRIPTEN
  124. # define NV_CPU_EMSCRIPTEN 1
  125. #else
  126. # error "Unsupported CPU"
  127. #endif
  128. #ifndef NV_CPU_X86
  129. # define NV_CPU_X86 0
  130. #endif // NV_CPU_X86
  131. #ifndef NV_CPU_X86_64
  132. # define NV_CPU_X86_64 0
  133. #endif // NV_CPU_X86_64
  134. #ifndef NV_CPU_PPC
  135. # define NV_CPU_PPC 0
  136. #endif // NV_CPU_PPC
  137. #ifndef NV_CPU_ARM
  138. # define NV_CPU_ARM 0
  139. #endif // NV_CPU_ARM
  140. #ifndef NV_CPU_AARCH64
  141. # define NV_CPU_AARCH64 0
  142. #endif // NV_CPU_AARCH64
  143. #ifndef NV_CPU_RISCV64
  144. # define NV_CPU_RISCV64 0
  145. #endif // NV_CPU_RISCV64
  146. #ifndef NV_CPU_LOONGARCH64
  147. # define NV_CPU_LOONGARCH64 0
  148. #endif // NV_CPU_LOONGARCH64
  149. #ifndef NV_CPU_EMSCRIPTEN
  150. # define NV_CPU_EMSCRIPTEN 0
  151. #endif // NV_CPU_EMSCRIPTEN
  152. // Compiler:
  153. #if defined POSH_COMPILER_CLANG
  154. # define NV_CC_CLANG 1
  155. # define NV_CC_GNUC 1 // Clang is compatible with GCC.
  156. # define NV_CC_STRING "clang"
  157. # pragma clang diagnostic ignored "-Wmissing-braces"
  158. # pragma clang diagnostic ignored "-Wshadow"
  159. # pragma clang diagnostic ignored "-Wunused-local-typedef"
  160. # pragma clang diagnostic ignored "-Wunused-function"
  161. # pragma clang diagnostic ignored "-Wunused-variable"
  162. # pragma clang diagnostic ignored "-Wunused-parameter"
  163. # pragma clang diagnostic ignored "-Wsometimes-uninitialized"
  164. #elif defined POSH_COMPILER_GCC
  165. # define NV_CC_GNUC 1
  166. # define NV_CC_STRING "gcc"
  167. # pragma GCC diagnostic ignored "-Wshadow"
  168. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  169. # pragma GCC diagnostic ignored "-Wunused-function"
  170. # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  171. # pragma GCC diagnostic ignored "-Wunused-variable"
  172. # pragma GCC diagnostic ignored "-Wunused-parameter"
  173. # pragma GCC diagnostic ignored "-Warray-bounds"
  174. #elif defined POSH_COMPILER_MSVC
  175. # define NV_CC_MSVC 1
  176. # define NV_CC_STRING "msvc"
  177. #else
  178. # error "Unsupported compiler"
  179. #endif
  180. #ifndef NV_CC_GNUC
  181. # define NV_CC_GNUC 0
  182. #endif // NV_CC_GNUC
  183. #ifndef NV_CC_MSVC
  184. # define NV_CC_MSVC 0
  185. #endif // NV_CC_MSVC
  186. #ifndef NV_CC_CLANG
  187. # define NV_CC_CLANG 0
  188. #endif // NV_CC_CLANG
  189. #if NV_CC_MSVC
  190. #define NV_CC_CPP11 (__cplusplus > 199711L || _MSC_VER >= 1800) // Visual Studio 2013 has all the features we use, but doesn't advertise full C++11 support yet.
  191. #else
  192. // @@ IC: This works in CLANG, about GCC?
  193. // @@ ES: Doesn't work in gcc. These 3 features are available in GCC >= 4.4.
  194. #ifdef __clang__
  195. #define NV_CC_CPP11 (__has_feature(cxx_deleted_functions) && __has_feature(cxx_rvalue_references) && __has_feature(cxx_static_assert))
  196. #elif defined __GNUC__
  197. #define NV_CC_CPP11 ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  198. #endif
  199. #endif
  200. // Endiannes:
  201. #define NV_LITTLE_ENDIAN POSH_LITTLE_ENDIAN
  202. #define NV_BIG_ENDIAN POSH_BIG_ENDIAN
  203. #define NV_ENDIAN_STRING POSH_ENDIAN_STRING
  204. // Type definitions:
  205. typedef posh_u8_t uint8;
  206. typedef posh_i8_t int8;
  207. typedef posh_u16_t uint16;
  208. typedef posh_i16_t int16;
  209. typedef posh_u32_t uint32;
  210. typedef posh_i32_t int32;
  211. typedef posh_u64_t uint64;
  212. typedef posh_i64_t int64;
  213. // Aliases
  214. typedef uint32 uint;
  215. // Version string:
  216. #define NV_VERSION_STRING \
  217. NV_OS_STRING "/" NV_CC_STRING "/" NV_CPU_STRING"/" \
  218. NV_ENDIAN_STRING"-endian - " __DATE__ "-" __TIME__
  219. // Disable copy constructor and assignment operator.
  220. #if NV_CC_CPP11
  221. #define NV_FORBID_COPY(C) \
  222. C( const C & ) = delete; \
  223. C &operator=( const C & ) = delete
  224. #else
  225. #define NV_FORBID_COPY(C) \
  226. private: \
  227. C( const C & ); \
  228. C &operator=( const C & )
  229. #endif
  230. // Disable dynamic allocation on the heap.
  231. // See Prohibiting Heap-Based Objects in More Effective C++.
  232. #define NV_FORBID_HEAPALLOC() \
  233. private: \
  234. void *operator new(size_t size); \
  235. void *operator new[](size_t size)
  236. // String concatenation macros.
  237. #define NV_STRING_JOIN2(arg1, arg2) NV_DO_STRING_JOIN2(arg1, arg2)
  238. #define NV_DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2
  239. #define NV_STRING_JOIN3(arg1, arg2, arg3) NV_DO_STRING_JOIN3(arg1, arg2, arg3)
  240. #define NV_DO_STRING_JOIN3(arg1, arg2, arg3) arg1 ## arg2 ## arg3
  241. #define NV_STRING2(x) #x
  242. #define NV_STRING(x) NV_STRING2(x)
  243. #if NV_CC_MSVC
  244. #define NV_MULTI_LINE_MACRO_BEGIN do {
  245. #define NV_MULTI_LINE_MACRO_END \
  246. __pragma(warning(push)) \
  247. __pragma(warning(disable:4127)) \
  248. } while(false) \
  249. __pragma(warning(pop))
  250. #else
  251. #define NV_MULTI_LINE_MACRO_BEGIN do {
  252. #define NV_MULTI_LINE_MACRO_END } while(false)
  253. #endif
  254. #if NV_CC_CPP11
  255. #define nvStaticCheck(x) static_assert((x), "Static assert "#x" failed")
  256. #else
  257. #define nvStaticCheck(x) typedef char NV_STRING_JOIN2(__static_assert_,__LINE__)[(x)]
  258. #endif
  259. #define NV_COMPILER_CHECK(x) nvStaticCheck(x) // I like this name best.
  260. // Make sure type definitions are fine.
  261. NV_COMPILER_CHECK(sizeof(int8) == 1);
  262. NV_COMPILER_CHECK(sizeof(uint8) == 1);
  263. NV_COMPILER_CHECK(sizeof(int16) == 2);
  264. NV_COMPILER_CHECK(sizeof(uint16) == 2);
  265. NV_COMPILER_CHECK(sizeof(int32) == 4);
  266. NV_COMPILER_CHECK(sizeof(uint32) == 4);
  267. NV_COMPILER_CHECK(sizeof(int32) == 4);
  268. NV_COMPILER_CHECK(sizeof(uint32) == 4);
  269. #define NV_ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  270. #if 0 // Disabled in The Witness.
  271. #if NV_CC_MSVC
  272. #define NV_MESSAGE(x) message(__FILE__ "(" NV_STRING(__LINE__) ") : " x)
  273. #else
  274. #define NV_MESSAGE(x) message(x)
  275. #endif
  276. #else
  277. #define NV_MESSAGE(x)
  278. #endif
  279. // Startup initialization macro.
  280. #define NV_AT_STARTUP(some_code) \
  281. namespace { \
  282. static struct NV_STRING_JOIN2(AtStartup_, __LINE__) { \
  283. NV_STRING_JOIN2(AtStartup_, __LINE__)() { some_code; } \
  284. } \
  285. NV_STRING_JOIN3(AtStartup_, __LINE__, Instance); \
  286. }
  287. // Indicate the compiler that the parameter is not used to suppress compier warnings.
  288. #define NV_UNUSED(a) ((a)=(a))
  289. // Null index. @@ Move this somewhere else... it's only used by nvmesh.
  290. //const unsigned int NIL = unsigned int(~0);
  291. //#define NIL uint(~0)
  292. // Null pointer.
  293. #ifndef NULL
  294. #define NULL 0
  295. #endif
  296. // Platform includes
  297. #if NV_CC_MSVC
  298. # if NV_OS_WIN32
  299. # include "defsvcwin32.h"
  300. # elif NV_OS_XBOX
  301. # include "defsvcxbox.h"
  302. # else
  303. # error "MSVC: Platform not supported"
  304. # endif
  305. #elif NV_CC_GNUC
  306. # if NV_OS_LINUX
  307. # include "defsgnuclinux.h"
  308. # elif NV_OS_DARWIN || NV_OS_FREEBSD || NV_OS_OPENBSD
  309. # include "defsgnucdarwin.h"
  310. # elif NV_OS_MINGW || (NV_CC_CLANG && NV_OS_WIN32)
  311. # include "defsgnucwin32.h"
  312. # elif NV_OS_CYGWIN
  313. # error "GCC: Cygwin not supported"
  314. # else
  315. # error "GCC: Platform not supported"
  316. # endif
  317. #endif
  318. #endif // NV_CORE_H