common_funcs.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <algorithm>
  6. #include <array>
  7. #include <string>
  8. #if !defined(ARCHITECTURE_x86_64)
  9. #include <cstdlib> // for exit
  10. #endif
  11. #include "common/common_types.h"
  12. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  13. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  14. #define DO_CONCAT2(x, y) x##y
  15. /// Helper macros to insert unused bytes or words to properly align structs. These values will be
  16. /// zero-initialized.
  17. #define INSERT_PADDING_BYTES(num_bytes) \
  18. std::array<u8, num_bytes> CONCAT2(pad, __LINE__) {}
  19. #define INSERT_PADDING_WORDS(num_words) \
  20. std::array<u32, num_words> CONCAT2(pad, __LINE__) {}
  21. /// These are similar to the INSERT_PADDING_* macros but do not zero-initialize the contents.
  22. /// This keeps the structure trivial to construct.
  23. #define INSERT_PADDING_BYTES_NOINIT(num_bytes) std::array<u8, num_bytes> CONCAT2(pad, __LINE__)
  24. #define INSERT_PADDING_WORDS_NOINIT(num_words) std::array<u32, num_words> CONCAT2(pad, __LINE__)
  25. #ifndef _MSC_VER
  26. #ifdef ARCHITECTURE_x86_64
  27. #define Crash() __asm__ __volatile__("int $3")
  28. #else
  29. #define Crash() exit(1)
  30. #endif
  31. #else // _MSC_VER
  32. // Locale Cross-Compatibility
  33. #define locale_t _locale_t
  34. extern "C" {
  35. __declspec(dllimport) void __stdcall DebugBreak(void);
  36. }
  37. #define Crash() DebugBreak()
  38. #endif // _MSC_VER ndef
  39. // Generic function to get last error message.
  40. // Call directly after the command or use the error num.
  41. // This function might change the error code.
  42. // Defined in Misc.cpp.
  43. [[nodiscard]] std::string GetLastErrorMsg();
  44. #define DECLARE_ENUM_FLAG_OPERATORS(type) \
  45. [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
  46. using T = std::underlying_type_t<type>; \
  47. return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
  48. } \
  49. [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
  50. using T = std::underlying_type_t<type>; \
  51. return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
  52. } \
  53. [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
  54. using T = std::underlying_type_t<type>; \
  55. return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \
  56. } \
  57. constexpr type& operator|=(type& a, type b) noexcept { \
  58. a = a | b; \
  59. return a; \
  60. } \
  61. constexpr type& operator&=(type& a, type b) noexcept { \
  62. a = a & b; \
  63. return a; \
  64. } \
  65. constexpr type& operator^=(type& a, type b) noexcept { \
  66. a = a ^ b; \
  67. return a; \
  68. } \
  69. [[nodiscard]] constexpr type operator~(type key) noexcept { \
  70. using T = std::underlying_type_t<type>; \
  71. return static_cast<type>(~static_cast<T>(key)); \
  72. } \
  73. [[nodiscard]] constexpr bool True(type key) noexcept { \
  74. using T = std::underlying_type_t<type>; \
  75. return static_cast<T>(key) != 0; \
  76. } \
  77. [[nodiscard]] constexpr bool False(type key) noexcept { \
  78. using T = std::underlying_type_t<type>; \
  79. return static_cast<T>(key) == 0; \
  80. }
  81. /// Evaluates a boolean expression, and returns a result unless that expression is true.
  82. #define R_UNLESS(expr, res) \
  83. { \
  84. if (!(expr)) { \
  85. return res; \
  86. } \
  87. }
  88. namespace Common {
  89. [[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) {
  90. return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24;
  91. }
  92. } // namespace Common