intrinsics.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. namespace nall {
  3. using uint = unsigned;
  4. enum class Compiler : uint { Clang, GCC, Microsoft, Unknown };
  5. enum class Platform : uint { Windows, MacOS, Linux, BSD, Android, Unknown };
  6. enum class API : uint { Windows, Posix, Unknown };
  7. enum class DisplayServer : uint { Windows, Quartz, Xorg, Unknown };
  8. enum class Architecture : uint { x86, amd64, ARM32, ARM64, PPC32, PPC64, Unknown };
  9. enum class Endian : uint { LSB, MSB, Unknown };
  10. enum class Build : uint { Debug, Stable, Size, Release, Performance };
  11. static inline constexpr auto compiler() -> Compiler;
  12. static inline constexpr auto platform() -> Platform;
  13. static inline constexpr auto api() -> API;
  14. static inline constexpr auto display() -> DisplayServer;
  15. static inline constexpr auto architecture() -> Architecture;
  16. static inline constexpr auto endian() -> Endian;
  17. static inline constexpr auto build() -> Build;
  18. }
  19. /* Compiler detection */
  20. namespace nall {
  21. #if defined(__clang__)
  22. #define COMPILER_CLANG
  23. constexpr auto compiler() -> Compiler { return Compiler::Clang; }
  24. #pragma clang diagnostic warning "-Wreturn-type"
  25. #pragma clang diagnostic ignored "-Wunused-result"
  26. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  27. #pragma clang diagnostic ignored "-Wempty-body"
  28. #pragma clang diagnostic ignored "-Wparentheses"
  29. #pragma clang diagnostic ignored "-Wswitch"
  30. #pragma clang diagnostic ignored "-Wswitch-bool"
  31. #pragma clang diagnostic ignored "-Wtautological-compare"
  32. #pragma clang diagnostic ignored "-Wabsolute-value"
  33. #pragma clang diagnostic ignored "-Wshift-count-overflow"
  34. #pragma clang diagnostic ignored "-Wtrigraphs"
  35. //temporary
  36. #pragma clang diagnostic ignored "-Winconsistent-missing-override"
  37. //#pragma clang diagnostic error "-Wdeprecated-declarations"
  38. #elif defined(__GNUC__)
  39. #define COMPILER_GCC
  40. constexpr auto compiler() -> Compiler { return Compiler::GCC; }
  41. #pragma GCC diagnostic warning "-Wreturn-type"
  42. #pragma GCC diagnostic ignored "-Wunused-result"
  43. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  44. #pragma GCC diagnostic ignored "-Wpragmas"
  45. #pragma GCC diagnostic ignored "-Wswitch-bool"
  46. #pragma GCC diagnostic ignored "-Wtrigraphs"
  47. #elif defined(_MSC_VER)
  48. #define COMPILER_MICROSOFT
  49. constexpr auto compiler() -> Compiler { return Compiler::Microsoft; }
  50. #pragma warning(disable:4996) //libc "deprecation" warnings
  51. #else
  52. #warning "unable to detect compiler"
  53. #define COMPILER_UNKNOWN
  54. constexpr auto compiler() -> Compiler { return Compiler::Unknown; }
  55. #endif
  56. }
  57. /* Platform detection */
  58. namespace nall {
  59. #if defined(_WIN32)
  60. #define PLATFORM_WINDOWS
  61. #define API_WINDOWS
  62. #define DISPLAY_WINDOWS
  63. constexpr auto platform() -> Platform { return Platform::Windows; }
  64. constexpr auto api() -> API { return API::Windows; }
  65. constexpr auto display() -> DisplayServer { return DisplayServer::Windows; }
  66. #elif defined(__APPLE__)
  67. #define PLATFORM_MACOS
  68. #define API_POSIX
  69. #define DISPLAY_QUARTZ
  70. constexpr auto platform() -> Platform { return Platform::MacOS; }
  71. constexpr auto api() -> API { return API::Posix; }
  72. constexpr auto display() -> DisplayServer { return DisplayServer::Quartz; }
  73. #elif defined(__ANDROID__)
  74. #define PLATFORM_ANDROID
  75. #define API_POSIX
  76. #define DISPLAY_UNKNOWN
  77. constexpr auto platform() -> Platform { return Platform::Android; }
  78. constexpr auto api() -> API { return API::Posix; }
  79. constexpr auto display() -> DisplayServer { return DisplayServer::Unknown; }
  80. #elif defined(linux) || defined(__linux__)
  81. #define PLATFORM_LINUX
  82. #define API_POSIX
  83. #define DISPLAY_XORG
  84. constexpr auto platform() -> Platform { return Platform::Linux; }
  85. constexpr auto api() -> API { return API::Posix; }
  86. constexpr auto display() -> DisplayServer { return DisplayServer::Xorg; }
  87. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
  88. #define PLATFORM_BSD
  89. #define API_POSIX
  90. #define DISPLAY_XORG
  91. constexpr auto platform() -> Platform { return Platform::BSD; }
  92. constexpr auto api() -> API { return API::Posix; }
  93. constexpr auto display() -> DisplayServer { return DisplayServer::Xorg; }
  94. #else
  95. #warning "unable to detect platform"
  96. #define PLATFORM_UNKNOWN
  97. #define API_UNKNOWN
  98. #define DISPLAY_UNKNOWN
  99. constexpr auto platform() -> Platform { return Platform::Unknown; }
  100. constexpr auto api() -> API { return API::Unknown; }
  101. constexpr auto display() -> DisplayServer { return DisplayServer::Unknown; }
  102. #endif
  103. }
  104. #if defined(PLATFORM_MACOS)
  105. #include <machine/endian.h>
  106. #elif defined(PLATFORM_LINUX)
  107. #include <endian.h>
  108. #elif defined(PLATFORM_BSD)
  109. #include <sys/endian.h>
  110. #endif
  111. /* Architecture detection */
  112. namespace nall {
  113. #if defined(__i386__) || defined(_M_IX86)
  114. #define ARCHITECTURE_X86
  115. constexpr auto architecture() -> Architecture { return Architecture::x86; }
  116. #elif defined(__amd64__) || defined(_M_AMD64)
  117. #define ARCHITECTURE_AMD64
  118. constexpr auto architecture() -> Architecture { return Architecture::amd64; }
  119. #elif defined(__aarch64__)
  120. #define ARCHITECTURE_ARM64
  121. constexpr auto architecture() -> Architecture { return Architecture::ARM64; }
  122. #elif defined(__arm__)
  123. #define ARCHITECTURE_ARM32
  124. constexpr auto architecture() -> Architecture { return Architecture::ARM32; }
  125. #elif defined(__ppc64__) || defined(_ARCH_PPC64)
  126. #define ARCHITECTURE_PPC64
  127. constexpr auto architecture() -> Architecture { return Architecture::PPC64; }
  128. #elif defined(__ppc__) || defined(_ARCH_PPC) || defined(_M_PPC)
  129. #define ARCHITECTURE_PPC32
  130. constexpr auto architecture() -> Architecture { return Architecture::PPC32; }
  131. #else
  132. #warning "unable to detect architecture"
  133. #define ARCHITECTURE_UNKNOWN
  134. constexpr auto architecture() -> Architecture { return Architecture::Unknown; }
  135. #endif
  136. }
  137. /* Endian detection */
  138. namespace nall {
  139. #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(__amd64__) || defined(_M_IX86) || defined(_M_AMD64)
  140. #define ENDIAN_LSB
  141. constexpr auto endian() -> Endian { return Endian::LSB; }
  142. #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || defined(__BIG_ENDIAN__) || defined(__powerpc__) || defined(_M_PPC)
  143. #define ENDIAN_MSB
  144. constexpr auto endian() -> Endian { return Endian::MSB; }
  145. #else
  146. #warning "unable to detect endian"
  147. #define ENDIAN_UNKNOWN
  148. constexpr auto endian() -> Endian { return Endian::Unknown; }
  149. #endif
  150. }
  151. /* Build optimization level detection */
  152. #undef DEBUG
  153. #undef NDEBUG
  154. namespace nall {
  155. #if defined(BUILD_DEBUG)
  156. #define DEBUG
  157. constexpr auto build() -> Build { return Build::Debug; }
  158. #elif defined(BUILD_STABLE)
  159. #define DEBUG
  160. constexpr auto build() -> Build { return Build::Stable; }
  161. #elif defined(BUILD_SIZE)
  162. #define NDEBUG
  163. constexpr auto build() -> Build { return Build::Size; }
  164. #elif defined(BUILD_RELEASE)
  165. #define NDEBUG
  166. constexpr auto build() -> Build { return Build::Release; }
  167. #elif defined(BUILD_PERFORMANCE)
  168. #define NDEBUG
  169. constexpr auto build() -> Build { return Build::Performance; }
  170. #else
  171. //default to debug mode
  172. #define DEBUG
  173. constexpr auto build() -> Build { return Build::Debug; }
  174. #endif
  175. }