git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / include / retro_miscellaneous.h
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 #include <limits.h>
47
48 #ifdef _MSC_VER
49 #include <compat/msvc.h>
50 #endif
51
52 #ifdef IOS
53 #include <sys/param.h>
54 #endif
55
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
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
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 static INLINE bool bits_any_different(uint32_t *a, uint32_t *b, uint32_t count)
82 {
83    uint32_t i;
84    for (i = 0; i < count; i++)
85    {
86       if (a[i] != b[i])
87          return true;
88    }
89    return false;
90 }
91
92 #ifndef PATH_MAX_LENGTH
93 #if defined(_XBOX1) || defined(_3DS) || defined(PSP) || defined(PS2) || defined(GEKKO)|| defined(WIIU) || defined(__PSL1GHT__) || defined(__PS3__)
94 #define PATH_MAX_LENGTH 512
95 #else
96 #define PATH_MAX_LENGTH 4096
97 #endif
98 #endif
99
100 #ifndef NAME_MAX_LENGTH
101 #define NAME_MAX_LENGTH 256
102 #endif
103
104 #ifndef MAX
105 #define MAX(a, b) ((a) > (b) ? (a) : (b))
106 #endif
107
108 #ifndef MIN
109 #define MIN(a, b) ((a) < (b) ? (a) : (b))
110 #endif
111
112 #define ARRAY_SIZE(a)              (sizeof(a) / sizeof((a)[0]))
113
114 #define BITS_GET_ELEM(a, i)        ((a).data[i])
115 #define BITS_GET_ELEM_PTR(a, i)    ((a)->data[i])
116
117 #define BIT_SET(a, bit)   ((a)[(bit) >> 3] |=  (1 << ((bit) & 7)))
118 #define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
119 #define BIT_GET(a, bit)   (((a)[(bit) >> 3] >> ((bit) & 7)) & 1)
120
121 #define BIT16_SET(a, bit)    ((a) |=  (1 << ((bit) & 15)))
122 #define BIT16_CLEAR(a, bit)  ((a) &= ~(1 << ((bit) & 15)))
123 #define BIT16_GET(a, bit)    (((a) >> ((bit) & 15)) & 1)
124 #define BIT16_CLEAR_ALL(a)   ((a) = 0)
125
126 #define BIT32_SET(a, bit)    ((a) |=  (UINT32_C(1) << ((bit) & 31)))
127 #define BIT32_CLEAR(a, bit)  ((a) &= ~(UINT32_C(1) << ((bit) & 31)))
128 #define BIT32_GET(a, bit)    (((a) >> ((bit) & 31)) & 1)
129 #define BIT32_CLEAR_ALL(a)   ((a) = 0)
130
131 #define BIT64_SET(a, bit)    ((a) |=  (UINT64_C(1) << ((bit) & 63)))
132 #define BIT64_CLEAR(a, bit)  ((a) &= ~(UINT64_C(1) << ((bit) & 63)))
133 #define BIT64_GET(a, bit)    (((a) >> ((bit) & 63)) & 1)
134 #define BIT64_CLEAR_ALL(a)   ((a) = 0)
135
136 #define BIT128_SET(a, bit)   ((a).data[(bit) >> 5] |=  (UINT32_C(1) << ((bit) & 31)))
137 #define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(UINT32_C(1) << ((bit) & 31)))
138 #define BIT128_GET(a, bit)   (((a).data[(bit) >> 5] >> ((bit) & 31)) & 1)
139 #define BIT128_CLEAR_ALL(a)  memset(&(a), 0, sizeof(a))
140
141 #define BIT128_SET_PTR(a, bit)   BIT128_SET(*a, bit)
142 #define BIT128_CLEAR_PTR(a, bit) BIT128_CLEAR(*a, bit)
143 #define BIT128_GET_PTR(a, bit)   BIT128_GET(*a, bit)
144 #define BIT128_CLEAR_ALL_PTR(a)  BIT128_CLEAR_ALL(*a)
145
146 #define BIT256_SET(a, bit)       BIT128_SET(a, bit)
147 #define BIT256_CLEAR(a, bit)     BIT128_CLEAR(a, bit)
148 #define BIT256_GET(a, bit)       BIT128_GET(a, bit)
149 #define BIT256_CLEAR_ALL(a)      BIT128_CLEAR_ALL(a)
150
151 #define BIT256_SET_PTR(a, bit)   BIT256_SET(*a, bit)
152 #define BIT256_CLEAR_PTR(a, bit) BIT256_CLEAR(*a, bit)
153 #define BIT256_GET_PTR(a, bit)   BIT256_GET(*a, bit)
154 #define BIT256_CLEAR_ALL_PTR(a)  BIT256_CLEAR_ALL(*a)
155
156 #define BIT512_SET(a, bit)       BIT256_SET(a, bit)
157 #define BIT512_CLEAR(a, bit)     BIT256_CLEAR(a, bit)
158 #define BIT512_GET(a, bit)       BIT256_GET(a, bit)
159 #define BIT512_CLEAR_ALL(a)      BIT256_CLEAR_ALL(a)
160
161 #define BIT512_SET_PTR(a, bit)   BIT512_SET(*a, bit)
162 #define BIT512_CLEAR_PTR(a, bit) BIT512_CLEAR(*a, bit)
163 #define BIT512_GET_PTR(a, bit)   BIT512_GET(*a, bit)
164 #define BIT512_CLEAR_ALL_PTR(a)  BIT512_CLEAR_ALL(*a)
165
166 #define BITS_COPY16_PTR(a,bits) \
167 { \
168    BIT128_CLEAR_ALL_PTR(a); \
169    BITS_GET_ELEM_PTR(a, 0) = (bits) & 0xffff; \
170 }
171
172 #define BITS_COPY32_PTR(a,bits) \
173 { \
174    BIT128_CLEAR_ALL_PTR(a); \
175    BITS_GET_ELEM_PTR(a, 0) = (bits); \
176 }
177
178 #define BITS_COPY64_PTR(a,bits) \
179 { \
180    BIT128_CLEAR_ALL_PTR(a); \
181    BITS_GET_ELEM_PTR(a, 0) = (bits); \
182    BITS_GET_ELEM_PTR(a, 1) = (bits >> 32); \
183 }
184
185 /* Helper macros and struct to keep track of many booleans. */
186 /* This struct has 256 bits. */
187 typedef struct
188 {
189    uint32_t data[8];
190 } retro_bits_t;
191
192 /* This struct has 512 bits. */
193 typedef struct
194 {
195    uint32_t data[16];
196 } retro_bits_512_t;
197
198 #ifdef _WIN32
199 #  ifdef _WIN64
200 #    define PRI_SIZET PRIu64
201 #  else
202 #    if _MSC_VER == 1800
203 #      define PRI_SIZET PRIu32
204 #    else
205 #      define PRI_SIZET "u"
206 #    endif
207 #  endif
208 #elif defined(PS2)
209 #  define PRI_SIZET "u"
210 #else
211 #  if (SIZE_MAX == 0xFFFF)
212 #    define PRI_SIZET "hu"
213 #  elif (SIZE_MAX == 0xFFFFFFFF)
214 #    define PRI_SIZET "u"
215 #  elif (SIZE_MAX == 0xFFFFFFFFFFFFFFFF)
216 #    define PRI_SIZET "lu"
217 #  else
218 #    error PRI_SIZET: unknown SIZE_MAX
219 #  endif
220 #endif
221
222 #endif