1 /* Copyright  (C) 2010-2020 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 
23 #ifndef __RARCH_MISCELLANEOUS_H
24 #define __RARCH_MISCELLANEOUS_H
25 
26 #define RARCH_MAX_SUBSYSTEMS 10
27 #define RARCH_MAX_SUBSYSTEM_ROMS 10
28 
29 #include <stdint.h>
30 #include <boolean.h>
31 #include <retro_inline.h>
32 
33 #if defined(_WIN32)
34 
35 #if defined(_XBOX)
36 #include <Xtl.h>
37 #else
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN
40 #endif
41 #include <windows.h>
42 #endif
43 
44 #endif
45 
46 #if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
47 #include <sys/fs_external.h>
48 #endif
49 
50 #include <limits.h>
51 
52 #ifdef _MSC_VER
53 #include <compat/msvc.h>
54 #endif
55 
bits_or_bits(uint32_t * a,uint32_t * b,uint32_t count)56 static INLINE void bits_or_bits(uint32_t *a, uint32_t *b, uint32_t count)
57 {
58    uint32_t i;
59    for (i = 0; i < count;i++)
60       a[i] |= b[i];
61 }
62 
bits_clear_bits(uint32_t * a,uint32_t * b,uint32_t count)63 static INLINE void bits_clear_bits(uint32_t *a, uint32_t *b, uint32_t count)
64 {
65    uint32_t i;
66    for (i = 0; i < count;i++)
67       a[i] &= ~b[i];
68 }
69 
bits_any_set(uint32_t * ptr,uint32_t count)70 static INLINE bool bits_any_set(uint32_t* ptr, uint32_t count)
71 {
72    uint32_t i;
73    for (i = 0; i < count; i++)
74    {
75       if (ptr[i] != 0)
76          return true;
77    }
78    return false;
79 }
80 
81 #ifndef PATH_MAX_LENGTH
82 #if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
83 #define PATH_MAX_LENGTH CELL_FS_MAX_FS_PATH_LENGTH
84 #elif defined(_XBOX1) || defined(_3DS) || defined(PSP) || defined(PS2) || defined(GEKKO)|| defined(WIIU) || defined(ORBIS)
85 #define PATH_MAX_LENGTH 512
86 #else
87 #define PATH_MAX_LENGTH 4096
88 #endif
89 #endif
90 
91 #ifndef MAX
92 #define MAX(a, b) ((a) > (b) ? (a) : (b))
93 #endif
94 
95 #ifndef MIN
96 #define MIN(a, b) ((a) < (b) ? (a) : (b))
97 #endif
98 
99 #define ARRAY_SIZE(a)              (sizeof(a) / sizeof((a)[0]))
100 
101 #define BITS_GET_ELEM(a, i)        ((a).data[i])
102 #define BITS_GET_ELEM_PTR(a, i)    ((a)->data[i])
103 
104 #define BIT_SET(a, bit)   ((a)[(bit) >> 3] |=  (1 << ((bit) & 7)))
105 #define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
106 #define BIT_GET(a, bit)   (((a)[(bit) >> 3] >> ((bit) & 7)) & 1)
107 
108 #define BIT16_SET(a, bit)    ((a) |=  (1 << ((bit) & 15)))
109 #define BIT16_CLEAR(a, bit)  ((a) &= ~(1 << ((bit) & 15)))
110 #define BIT16_GET(a, bit)    (((a) >> ((bit) & 15)) & 1)
111 #define BIT16_CLEAR_ALL(a)   ((a) = 0)
112 
113 #define BIT32_SET(a, bit)    ((a) |=  (UINT32_C(1) << ((bit) & 31)))
114 #define BIT32_CLEAR(a, bit)  ((a) &= ~(UINT32_C(1) << ((bit) & 31)))
115 #define BIT32_GET(a, bit)    (((a) >> ((bit) & 31)) & 1)
116 #define BIT32_CLEAR_ALL(a)   ((a) = 0)
117 
118 #define BIT64_SET(a, bit)    ((a) |=  (UINT64_C(1) << ((bit) & 63)))
119 #define BIT64_CLEAR(a, bit)  ((a) &= ~(UINT64_C(1) << ((bit) & 63)))
120 #define BIT64_GET(a, bit)    (((a) >> ((bit) & 63)) & 1)
121 #define BIT64_CLEAR_ALL(a)   ((a) = 0)
122 
123 #define BIT128_SET(a, bit)   ((a).data[(bit) >> 5] |=  (UINT32_C(1) << ((bit) & 31)))
124 #define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(UINT32_C(1) << ((bit) & 31)))
125 #define BIT128_GET(a, bit)   (((a).data[(bit) >> 5] >> ((bit) & 31)) & 1)
126 #define BIT128_CLEAR_ALL(a)  memset(&(a), 0, sizeof(a))
127 
128 #define BIT128_SET_PTR(a, bit)   BIT128_SET(*a, bit)
129 #define BIT128_CLEAR_PTR(a, bit) BIT128_CLEAR(*a, bit)
130 #define BIT128_GET_PTR(a, bit)   BIT128_GET(*a, bit)
131 #define BIT128_CLEAR_ALL_PTR(a)  BIT128_CLEAR_ALL(*a)
132 
133 #define BIT256_SET(a, bit)       BIT128_SET(a, bit)
134 #define BIT256_CLEAR(a, bit)     BIT128_CLEAR(a, bit)
135 #define BIT256_GET(a, bit)       BIT128_GET(a, bit)
136 #define BIT256_CLEAR_ALL(a)      BIT128_CLEAR_ALL(a)
137 
138 #define BIT256_SET_PTR(a, bit)   BIT256_SET(*a, bit)
139 #define BIT256_CLEAR_PTR(a, bit) BIT256_CLEAR(*a, bit)
140 #define BIT256_GET_PTR(a, bit)   BIT256_GET(*a, bit)
141 #define BIT256_CLEAR_ALL_PTR(a)  BIT256_CLEAR_ALL(*a)
142 
143 #define BITS_COPY16_PTR(a,bits) \
144 { \
145    BIT128_CLEAR_ALL_PTR(a); \
146    BITS_GET_ELEM_PTR(a, 0) = (bits) & 0xffff; \
147 }
148 
149 #define BITS_COPY32_PTR(a,bits) \
150 { \
151    BIT128_CLEAR_ALL_PTR(a); \
152    BITS_GET_ELEM_PTR(a, 0) = (bits); \
153 }
154 
155 /* Helper macros and struct to keep track of many booleans. */
156 /* This struct has 256 bits. */
157 typedef struct
158 {
159    uint32_t data[8];
160 } retro_bits_t;
161 
162 #ifdef _WIN32
163 #  ifdef _WIN64
164 #    define PRI_SIZET PRIu64
165 #  else
166 #    if _MSC_VER == 1800
167 #      define PRI_SIZET PRIu32
168 #    else
169 #      define PRI_SIZET "u"
170 #    endif
171 #  endif
172 #elif defined(PS2)
173 #  define PRI_SIZET "u"
174 #else
175 #  if (SIZE_MAX == 0xFFFF)
176 #    define PRI_SIZET "hu"
177 #  elif (SIZE_MAX == 0xFFFFFFFF)
178 #    define PRI_SIZET "u"
179 #  elif (SIZE_MAX == 0xFFFFFFFFFFFFFFFF)
180 #    define PRI_SIZET "lu"
181 #  else
182 #    error PRI_SIZET: unknown SIZE_MAX
183 #  endif
184 #endif
185 
186 #endif
187