common_funcs.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "common_types.h"
  6. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  7. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  8. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  9. #define DO_CONCAT2(x, y) x ## y
  10. // helper macro to properly align structure members.
  11. // Calling INSERT_PADDING_BYTES will add a new member variable with a name like "pad121",
  12. // depending on the current source line to make sure variable names are unique.
  13. #define INSERT_PADDING_BYTES(num_bytes) u8 CONCAT2(pad, __LINE__)[(num_bytes)]
  14. #define INSERT_PADDING_WORDS(num_words) u32 CONCAT2(pad, __LINE__)[(num_words)]
  15. #ifdef _WIN32
  16. // Alignment
  17. #define FORCE_INLINE __forceinline
  18. #define MEMORY_ALIGNED16(x) __declspec(align(16)) x
  19. #define MEMORY_ALIGNED32(x) __declspec(align(32)) x
  20. #define MEMORY_ALIGNED64(x) __declspec(align(64)) x
  21. #define MEMORY_ALIGNED128(x) __declspec(align(128)) x
  22. #else
  23. #define FORCE_INLINE inline __attribute__((always_inline))
  24. #define MEMORY_ALIGNED16(x) __attribute__((aligned(16))) x
  25. #define MEMORY_ALIGNED32(x) __attribute__((aligned(32))) x
  26. #define MEMORY_ALIGNED64(x) __attribute__((aligned(64))) x
  27. #define MEMORY_ALIGNED128(x) __attribute__((aligned(128))) x
  28. #endif
  29. #ifndef _MSC_VER
  30. #ifdef ARCHITECTURE_x86_64
  31. #define Crash() __asm__ __volatile__("int $3")
  32. #elif defined(_M_ARM)
  33. #define Crash() __asm__ __volatile__("trap")
  34. #else
  35. #define Crash() exit(1)
  36. #endif
  37. // GCC 4.8 defines all the rotate functions now
  38. // Small issue with GCC's lrotl/lrotr intrinsics is they are still 32bit while we require 64bit
  39. #ifndef _rotl
  40. inline u32 _rotl(u32 x, int shift) {
  41. shift &= 31;
  42. if (!shift) return x;
  43. return (x << shift) | (x >> (32 - shift));
  44. }
  45. inline u32 _rotr(u32 x, int shift) {
  46. shift &= 31;
  47. if (!shift) return x;
  48. return (x >> shift) | (x << (32 - shift));
  49. }
  50. #endif
  51. inline u64 _rotl64(u64 x, unsigned int shift){
  52. unsigned int n = shift % 64;
  53. return (x << n) | (x >> (64 - n));
  54. }
  55. inline u64 _rotr64(u64 x, unsigned int shift){
  56. unsigned int n = shift % 64;
  57. return (x >> n) | (x << (64 - n));
  58. }
  59. #else // _MSC_VER
  60. #if (_MSC_VER < 1900)
  61. // Function Cross-Compatibility
  62. #define snprintf _snprintf
  63. #endif
  64. // Locale Cross-Compatibility
  65. #define locale_t _locale_t
  66. extern "C" {
  67. __declspec(dllimport) void __stdcall DebugBreak(void);
  68. }
  69. #define Crash() {DebugBreak();}
  70. #endif // _MSC_VER ndef
  71. // Generic function to get last error message.
  72. // Call directly after the command or use the error num.
  73. // This function might change the error code.
  74. // Defined in Misc.cpp.
  75. const char* GetLastErrorMsg();