common_funcs.h 3.0 KB

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