common_funcs.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <iterator>
  7. #if !defined(ARCHITECTURE_x86_64)
  8. #include <cstdlib> // for exit
  9. #endif
  10. #include "common/common_types.h"
  11. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  12. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  13. #define DO_CONCAT2(x, y) x##y
  14. /// Helper macros to insert unused bytes or words to properly align structs. These values will be
  15. /// zero-initialized.
  16. #define INSERT_PADDING_BYTES(num_bytes) \
  17. std::array<u8, num_bytes> CONCAT2(pad, __LINE__) {}
  18. #define INSERT_PADDING_WORDS(num_words) \
  19. std::array<u32, num_words> CONCAT2(pad, __LINE__) {}
  20. /// These are similar to the INSERT_PADDING_* macros but do not zero-initialize the contents.
  21. /// This keeps the structure trivial to construct.
  22. #define INSERT_PADDING_BYTES_NOINIT(num_bytes) std::array<u8, num_bytes> CONCAT2(pad, __LINE__)
  23. #define INSERT_PADDING_WORDS_NOINIT(num_words) std::array<u32, num_words> CONCAT2(pad, __LINE__)
  24. #ifndef _MSC_VER
  25. #ifdef ARCHITECTURE_x86_64
  26. #define Crash() __asm__ __volatile__("int $3")
  27. #else
  28. #define Crash() exit(1)
  29. #endif
  30. #else // _MSC_VER
  31. // Locale Cross-Compatibility
  32. #define locale_t _locale_t
  33. extern "C" {
  34. __declspec(dllimport) void __stdcall DebugBreak(void);
  35. }
  36. #define Crash() DebugBreak()
  37. #endif // _MSC_VER ndef
  38. #define DECLARE_ENUM_FLAG_OPERATORS(type) \
  39. [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
  40. using T = std::underlying_type_t<type>; \
  41. return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
  42. } \
  43. [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
  44. using T = std::underlying_type_t<type>; \
  45. return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
  46. } \
  47. [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
  48. using T = std::underlying_type_t<type>; \
  49. return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \
  50. } \
  51. [[nodiscard]] constexpr type operator<<(type a, type b) noexcept { \
  52. using T = std::underlying_type_t<type>; \
  53. return static_cast<type>(static_cast<T>(a) << static_cast<T>(b)); \
  54. } \
  55. [[nodiscard]] constexpr type operator>>(type a, type b) noexcept { \
  56. using T = std::underlying_type_t<type>; \
  57. return static_cast<type>(static_cast<T>(a) >> static_cast<T>(b)); \
  58. } \
  59. constexpr type& operator|=(type& a, type b) noexcept { \
  60. a = a | b; \
  61. return a; \
  62. } \
  63. constexpr type& operator&=(type& a, type b) noexcept { \
  64. a = a & b; \
  65. return a; \
  66. } \
  67. constexpr type& operator^=(type& a, type b) noexcept { \
  68. a = a ^ b; \
  69. return a; \
  70. } \
  71. constexpr type& operator<<=(type& a, type b) noexcept { \
  72. a = a << b; \
  73. return a; \
  74. } \
  75. constexpr type& operator>>=(type& a, type b) noexcept { \
  76. a = a >> b; \
  77. return a; \
  78. } \
  79. [[nodiscard]] constexpr type operator~(type key) noexcept { \
  80. using T = std::underlying_type_t<type>; \
  81. return static_cast<type>(~static_cast<T>(key)); \
  82. } \
  83. [[nodiscard]] constexpr bool True(type key) noexcept { \
  84. using T = std::underlying_type_t<type>; \
  85. return static_cast<T>(key) != 0; \
  86. } \
  87. [[nodiscard]] constexpr bool False(type key) noexcept { \
  88. using T = std::underlying_type_t<type>; \
  89. return static_cast<T>(key) == 0; \
  90. }
  91. #define YUZU_NON_COPYABLE(cls) \
  92. cls(const cls&) = delete; \
  93. cls& operator=(const cls&) = delete
  94. #define YUZU_NON_MOVEABLE(cls) \
  95. cls(cls&&) = delete; \
  96. cls& operator=(cls&&) = delete
  97. namespace Common {
  98. [[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) {
  99. return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24;
  100. }
  101. // std::size() does not support zero-size C arrays. We're fixing that.
  102. template <class C>
  103. constexpr auto Size(const C& c) -> decltype(c.size()) {
  104. return std::size(c);
  105. }
  106. template <class C>
  107. constexpr std::size_t Size(const C& c) {
  108. if constexpr (sizeof(C) == 0) {
  109. return 0;
  110. } else {
  111. return std::size(c);
  112. }
  113. }
  114. } // namespace Common