common_funcs.h 7.9 KB

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