retro_miscellaneous.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2010-2018 The RetroArch team
  2. *
  3. * ---------------------------------------------------------------------------------------
  4. * The following license statement only applies to this file (retro_miscellaneous.h).
  5. * ---------------------------------------------------------------------------------------
  6. *
  7. * Permission is hereby granted, free of charge,
  8. * to any person obtaining a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef __RARCH_MISCELLANEOUS_H
  23. #define __RARCH_MISCELLANEOUS_H
  24. #define RARCH_MAX_SUBSYSTEMS 10
  25. #define RARCH_MAX_SUBSYSTEM_ROMS 10
  26. #include <stdint.h>
  27. #include <boolean.h>
  28. #include <retro_inline.h>
  29. #if defined(_WIN32) && !defined(_XBOX)
  30. #ifndef WIN32_LEAN_AND_MEAN
  31. #define WIN32_LEAN_AND_MEAN
  32. #endif
  33. #include <windows.h>
  34. #elif defined(_WIN32) && defined(_XBOX)
  35. #include <Xtl.h>
  36. #endif
  37. #if defined(__CELLOS_LV2__)
  38. #include <sys/fs_external.h>
  39. #endif
  40. #include <limits.h>
  41. #ifdef _MSC_VER
  42. #include <compat/msvc.h>
  43. #endif
  44. static INLINE void bits_or_bits(uint32_t *a, uint32_t *b, uint32_t count)
  45. {
  46. uint32_t i;
  47. for (i = 0; i < count;i++)
  48. a[i] |= b[i];
  49. }
  50. static INLINE void bits_clear_bits(uint32_t *a, uint32_t *b, uint32_t count)
  51. {
  52. uint32_t i;
  53. for (i = 0; i < count;i++)
  54. a[i] &= ~b[i];
  55. }
  56. static INLINE bool bits_any_set(uint32_t* ptr, uint32_t count)
  57. {
  58. uint32_t i;
  59. for (i = 0; i < count; i++)
  60. {
  61. if (ptr[i] != 0)
  62. return true;
  63. }
  64. return false;
  65. }
  66. #ifndef PATH_MAX_LENGTH
  67. #if defined(__CELLOS_LV2__)
  68. #define PATH_MAX_LENGTH CELL_FS_MAX_FS_PATH_LENGTH
  69. #elif defined(_XBOX1) || defined(_3DS) || defined(PSP) || defined(PS2) || defined(GEKKO)|| defined(WIIU) || defined(ORBIS)
  70. #define PATH_MAX_LENGTH 512
  71. #else
  72. #define PATH_MAX_LENGTH 4096
  73. #endif
  74. #endif
  75. #ifndef MAX
  76. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  77. #endif
  78. #ifndef MIN
  79. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  80. #endif
  81. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  82. #define BITS_GET_ELEM(a, i) ((a).data[i])
  83. #define BITS_GET_ELEM_PTR(a, i) ((a)->data[i])
  84. #define BIT_SET(a, bit) ((a)[(bit) >> 3] |= (1 << ((bit) & 7)))
  85. #define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
  86. #define BIT_GET(a, bit) (((a)[(bit) >> 3] >> ((bit) & 7)) & 1)
  87. #define BIT16_SET(a, bit) ((a) |= (1 << ((bit) & 15)))
  88. #define BIT16_CLEAR(a, bit) ((a) &= ~(1 << ((bit) & 15)))
  89. #define BIT16_GET(a, bit) (((a) >> ((bit) & 15)) & 1)
  90. #define BIT16_CLEAR_ALL(a) ((a) = 0)
  91. #define BIT32_SET(a, bit) ((a) |= (1 << ((bit) & 31)))
  92. #define BIT32_CLEAR(a, bit) ((a) &= ~(1 << ((bit) & 31)))
  93. #define BIT32_GET(a, bit) (((a) >> ((bit) & 31)) & 1)
  94. #define BIT32_CLEAR_ALL(a) ((a) = 0)
  95. #define BIT64_SET(a, bit) ((a) |= (UINT64_C(1) << ((bit) & 63)))
  96. #define BIT64_CLEAR(a, bit) ((a) &= ~(UINT64_C(1) << ((bit) & 63)))
  97. #define BIT64_GET(a, bit) (((a) >> ((bit) & 63)) & 1)
  98. #define BIT64_CLEAR_ALL(a) ((a) = 0)
  99. #define BIT128_SET(a, bit) ((a).data[(bit) >> 5] |= (1 << ((bit) & 31)))
  100. #define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(1 << ((bit) & 31)))
  101. #define BIT128_GET(a, bit) (((a).data[(bit) >> 5] >> ((bit) & 31)) & 1)
  102. #define BIT128_CLEAR_ALL(a) memset(&(a), 0, sizeof(a))
  103. #define BIT128_SET_PTR(a, bit) BIT128_SET(*a, bit)
  104. #define BIT128_CLEAR_PTR(a, bit) BIT128_CLEAR(*a, bit)
  105. #define BIT128_GET_PTR(a, bit) BIT128_GET(*a, bit)
  106. #define BIT128_CLEAR_ALL_PTR(a) BIT128_CLEAR_ALL(*a)
  107. #define BIT256_SET(a, bit) BIT128_SET(a, bit)
  108. #define BIT256_CLEAR(a, bit) BIT128_CLEAR(a, bit)
  109. #define BIT256_GET(a, bit) BIT128_GET(a, bit)
  110. #define BIT256_CLEAR_ALL(a) BIT128_CLEAR_ALL(a)
  111. #define BIT256_SET_PTR(a, bit) BIT256_SET(*a, bit)
  112. #define BIT256_CLEAR_PTR(a, bit) BIT256_CLEAR(*a, bit)
  113. #define BIT256_GET_PTR(a, bit) BIT256_GET(*a, bit)
  114. #define BIT256_CLEAR_ALL_PTR(a) BIT256_CLEAR_ALL(*a)
  115. #define BITS_COPY16_PTR(a,bits) \
  116. { \
  117. BIT128_CLEAR_ALL_PTR(a); \
  118. BITS_GET_ELEM_PTR(a, 0) = (bits) & 0xffff; \
  119. }
  120. #define BITS_COPY32_PTR(a,bits) \
  121. { \
  122. BIT128_CLEAR_ALL_PTR(a); \
  123. BITS_GET_ELEM_PTR(a, 0) = (bits); \
  124. }
  125. /* Helper macros and struct to keep track of many booleans. */
  126. /* This struct has 256 bits. */
  127. typedef struct
  128. {
  129. uint32_t data[8];
  130. } retro_bits_t;
  131. #ifdef _WIN32
  132. # ifdef _WIN64
  133. # define PRI_SIZET PRIu64
  134. # else
  135. # if _MSC_VER == 1800
  136. # define PRI_SIZET PRIu32
  137. # else
  138. # define PRI_SIZET "u"
  139. # endif
  140. # endif
  141. #elif PS2
  142. # define PRI_SIZET "u"
  143. #else
  144. # if (SIZE_MAX == 0xFFFF)
  145. # define PRI_SIZET "hu"
  146. # elif (SIZE_MAX == 0xFFFFFFFF)
  147. # define PRI_SIZET "u"
  148. # elif (SIZE_MAX == 0xFFFFFFFFFFFFFFFF)
  149. # define PRI_SIZET "lu"
  150. # else
  151. # error PRI_SIZET: unknown SIZE_MAX
  152. # endif
  153. #endif
  154. #endif