common_funcs.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #if !defined(ARCHITECTURE_x86_64) && !defined(_M_ARM)
  6. #include <cstdlib> // for exit
  7. #endif
  8. #include "common/common_types.h"
  9. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  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 macro to properly align structure members.
  14. // Calling INSERT_PADDING_BYTES will add a new member variable with a name like "pad121",
  15. // depending on the current source line to make sure variable names are unique.
  16. #define INSERT_PADDING_BYTES(num_bytes) u8 CONCAT2(pad, __LINE__)[(num_bytes)]
  17. #define INSERT_PADDING_WORDS(num_words) u32 CONCAT2(pad, __LINE__)[(num_words)]
  18. // Inlining
  19. #ifdef _WIN32
  20. #define FORCE_INLINE __forceinline
  21. #else
  22. #define FORCE_INLINE inline __attribute__((always_inline))
  23. #endif
  24. #ifndef _MSC_VER
  25. #ifdef ARCHITECTURE_x86_64
  26. #define Crash() __asm__ __volatile__("int $3")
  27. #elif defined(_M_ARM)
  28. #define Crash() __asm__ __volatile__("trap")
  29. #else
  30. #define Crash() exit(1)
  31. #endif
  32. // GCC 4.8 defines all the rotate functions now
  33. // Small issue with GCC's lrotl/lrotr intrinsics is they are still 32bit while we require 64bit
  34. #ifdef _rotl
  35. #define rotl _rotl
  36. #else
  37. inline u32 rotl(u32 x, int shift) {
  38. shift &= 31;
  39. if (!shift)
  40. return x;
  41. return (x << shift) | (x >> (32 - shift));
  42. }
  43. #endif
  44. #ifdef _rotr
  45. #define rotr _rotr
  46. #else
  47. inline u32 rotr(u32 x, int shift) {
  48. shift &= 31;
  49. if (!shift)
  50. return x;
  51. return (x >> shift) | (x << (32 - shift));
  52. }
  53. #endif
  54. inline u64 _rotl64(u64 x, unsigned int shift) {
  55. unsigned int n = shift % 64;
  56. return (x << n) | (x >> (64 - n));
  57. }
  58. inline u64 _rotr64(u64 x, unsigned int shift) {
  59. unsigned int n = shift % 64;
  60. return (x >> n) | (x << (64 - n));
  61. }
  62. #else // _MSC_VER
  63. #if (_MSC_VER < 1900)
  64. // Function Cross-Compatibility
  65. #define snprintf _snprintf
  66. #endif
  67. // Locale Cross-Compatibility
  68. #define locale_t _locale_t
  69. extern "C" {
  70. __declspec(dllimport) void __stdcall DebugBreak(void);
  71. }
  72. #define Crash() DebugBreak()
  73. // cstdlib provides these on MSVC
  74. #define rotr _rotr
  75. #define rotl _rotl
  76. #endif // _MSC_VER ndef
  77. // Generic function to get last error message.
  78. // Call directly after the command or use the error num.
  79. // This function might change the error code.
  80. // Defined in Misc.cpp.
  81. const char* GetLastErrorMsg();