common_funcs.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #else // _MSC_VER
  34. // Locale Cross-Compatibility
  35. #define locale_t _locale_t
  36. extern "C" {
  37. __declspec(dllimport) void __stdcall DebugBreak(void);
  38. }
  39. #define Crash() DebugBreak()
  40. #endif // _MSC_VER ndef
  41. #define DECLARE_ENUM_FLAG_OPERATORS(type) \
  42. [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
  43. using T = std::underlying_type_t<type>; \
  44. return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
  45. } \
  46. [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
  47. using T = std::underlying_type_t<type>; \
  48. return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
  49. } \
  50. [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
  51. using T = std::underlying_type_t<type>; \
  52. return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \
  53. } \
  54. [[nodiscard]] constexpr type operator<<(type a, type b) noexcept { \
  55. using T = std::underlying_type_t<type>; \
  56. return static_cast<type>(static_cast<T>(a) << static_cast<T>(b)); \
  57. } \
  58. [[nodiscard]] constexpr type operator>>(type a, type b) noexcept { \
  59. using T = std::underlying_type_t<type>; \
  60. return static_cast<type>(static_cast<T>(a) >> static_cast<T>(b)); \
  61. } \
  62. constexpr type& operator|=(type& a, type b) noexcept { \
  63. a = a | b; \
  64. return a; \
  65. } \
  66. constexpr type& operator&=(type& a, type b) noexcept { \
  67. a = a & b; \
  68. return a; \
  69. } \
  70. constexpr type& operator^=(type& a, type b) noexcept { \
  71. a = a ^ b; \
  72. return a; \
  73. } \
  74. constexpr type& operator<<=(type& a, type b) noexcept { \
  75. a = a << b; \
  76. return a; \
  77. } \
  78. constexpr type& operator>>=(type& a, type b) noexcept { \
  79. a = a >> b; \
  80. return a; \
  81. } \
  82. [[nodiscard]] constexpr type operator~(type key) noexcept { \
  83. using T = std::underlying_type_t<type>; \
  84. return static_cast<type>(~static_cast<T>(key)); \
  85. } \
  86. [[nodiscard]] constexpr bool True(type key) noexcept { \
  87. using T = std::underlying_type_t<type>; \
  88. return static_cast<T>(key) != 0; \
  89. } \
  90. [[nodiscard]] constexpr bool False(type key) noexcept { \
  91. using T = std::underlying_type_t<type>; \
  92. return static_cast<T>(key) == 0; \
  93. }
  94. #define YUZU_NON_COPYABLE(cls) \
  95. cls(const cls&) = delete; \
  96. cls& operator=(const cls&) = delete
  97. #define YUZU_NON_MOVEABLE(cls) \
  98. cls(cls&&) = delete; \
  99. cls& operator=(cls&&) = delete
  100. namespace Common {
  101. [[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) {
  102. return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24;
  103. }
  104. // std::size() does not support zero-size C arrays. We're fixing that.
  105. template <class C>
  106. constexpr auto Size(const C& c) -> decltype(c.size()) {
  107. return std::size(c);
  108. }
  109. template <class C>
  110. constexpr std::size_t Size(const C& c) {
  111. if constexpr (sizeof(C) == 0) {
  112. return 0;
  113. } else {
  114. return std::size(c);
  115. }
  116. }
  117. } // namespace Common