common_funcs.h 3.0 KB

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