1 /* Copyright  (C) 2010-2016 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 #include <stdint.h>
27 #include <math.h>
28 
29 #if defined(XENON)
30 #include <time/time.h>
31 #elif defined(GEKKO) || defined(__PSL1GHT__) || defined(__QNX__)
32 #include <unistd.h>
33 #elif defined(PSP)
34 #include <pspthreadman.h>
35 #elif defined(VITA)
36 #include <psp2/kernel/threadmgr.h>
37 #elif defined(_3DS)
38 #include <3ds.h>
39 #else
40 #include <time.h>
41 #endif
42 
43 #if defined(_WIN32) && !defined(_XBOX)
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #elif defined(_WIN32) && defined(_XBOX)
47 #include <Xtl.h>
48 #endif
49 
50 #include <limits.h>
51 
52 #ifdef _MSC_VER
53 #include <compat/msvc.h>
54 #endif
55 #include <retro_inline.h>
56 
57 #ifndef PATH_MAX_LENGTH
58 #if defined(_XBOX1) || defined(_3DS) || defined(PSP) || defined(GEKKO)
59 #define PATH_MAX_LENGTH 512
60 #else
61 #define PATH_MAX_LENGTH 4096
62 #endif
63 #endif
64 
65 #ifndef M_PI
66 #define M_PI 3.14159265358979323846264338327
67 #endif
68 
69 #ifndef MAX
70 #define MAX(a, b) ((a) > (b) ? (a) : (b))
71 #endif
72 
73 #ifndef MIN
74 #define MIN(a, b) ((a) < (b) ? (a) : (b))
75 #endif
76 
77 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78 #define RARCH_SCALE_BASE 256
79 
80 /**
81  * retro_sleep:
82  * @msec         : amount in milliseconds to sleep
83  *
84  * Sleeps for a specified amount of milliseconds (@msec).
85  **/
retro_sleep(unsigned msec)86 static INLINE void retro_sleep(unsigned msec)
87 {
88 #if defined(PSP) || defined(VITA)
89    sceKernelDelayThread(1000 * msec);
90 #elif defined(_3DS)
91    svcSleepThread(1000000 * (s64)msec);
92 #elif defined(_WIN32)
93    Sleep(msec);
94 #elif defined(XENON)
95    udelay(1000 * msec);
96 #elif defined(GEKKO) || defined(__PSL1GHT__) || defined(__QNX__)
97    usleep(1000 * msec);
98 #else
99    struct timespec tv = {0};
100    tv.tv_sec = msec / 1000;
101    tv.tv_nsec = (msec % 1000) * 1000000;
102    nanosleep(&tv, NULL);
103 #endif
104 }
105 
106 /**
107  * next_pow2:
108  * @v         : initial value
109  *
110  * Get next power of 2 value based on  initial value.
111  *
112  * Returns: next power of 2 value (derived from @v).
113  **/
next_pow2(uint32_t v)114 static INLINE uint32_t next_pow2(uint32_t v)
115 {
116    v--;
117    v |= v >> 1;
118    v |= v >> 2;
119    v |= v >> 4;
120    v |= v >> 8;
121    v |= v >> 16;
122    v++;
123    return v;
124 }
125 
126 /**
127  * prev_pow2:
128  * @v         : initial value
129  *
130  * Get previous power of 2 value based on initial value.
131  *
132  * Returns: previous power of 2 value (derived from @v).
133  **/
prev_pow2(uint32_t v)134 static INLINE uint32_t prev_pow2(uint32_t v)
135 {
136    v |= v >> 1;
137    v |= v >> 2;
138    v |= v >> 4;
139    v |= v >> 8;
140    v |= v >> 16;
141    return v - (v >> 1);
142 }
143 
144 /**
145  * db_to_gain:
146  * @db          : Decibels.
147  *
148  * Converts decibels to voltage gain.
149  *
150  * Returns: voltage gain value.
151  **/
db_to_gain(float db)152 static INLINE float db_to_gain(float db)
153 {
154    return powf(10.0f, db / 20.0f);
155 }
156 
157 /* Helper macros and struct to keep track of many booleans.
158  * To check for multiple bits, use &&, not &.
159  * For OR, | can be used. */
160 typedef struct
161 {
162    uint32_t data[8];
163 } retro_bits_t;
164 
165 #define BIT_SET(a, bit)   ((a)[(bit) >> 3] |=  (1 << ((bit) & 7)))
166 #define BIT_CLEAR(a, bit) ((a)[(bit) >> 3] &= ~(1 << ((bit) & 7)))
167 #define BIT_GET(a, bit)   ((a)[(bit) >> 3] &   (1 << ((bit) & 7)))
168 
169 #define BIT16_SET(a, bit)    ((a) |=  (1 << ((bit) & 15)))
170 #define BIT16_CLEAR(a, bit)  ((a) &= ~(1 << ((bit) & 15)))
171 #define BIT16_GET(a, bit) (!!((a) &   (1 << ((bit) & 15))))
172 #define BIT16_CLEAR_ALL(a)   ((a) = 0)
173 
174 #define BIT32_SET(a, bit)    ((a) |=  (1 << ((bit) & 31)))
175 #define BIT32_CLEAR(a, bit)  ((a) &= ~(1 << ((bit) & 31)))
176 #define BIT32_GET(a, bit) (!!((a) &   (1 << ((bit) & 31))))
177 #define BIT32_CLEAR_ALL(a)   ((a) = 0)
178 
179 #define BIT64_SET(a, bit)    ((a) |=  (UINT64_C(1) << ((bit) & 63)))
180 #define BIT64_CLEAR(a, bit)  ((a) &= ~(UINT64_C(1) << ((bit) & 63)))
181 #define BIT64_GET(a, bit) (!!((a) &   (UINT64_C(1) << ((bit) & 63))))
182 #define BIT64_CLEAR_ALL(a)   ((a) = 0)
183 
184 #define BIT128_SET(a, bit)   ((a).data[(bit) >> 5] |=  (1 << ((bit) & 31))
185 #define BIT128_CLEAR(a, bit) ((a).data[(bit) >> 5] &= ~(1 << ((bit) & 31)))
186 #define BIT128_GET(a, bit)   ((a).data[(bit) >> 5] &   (1 << ((bit) & 31)))
187 #define BIT128_CLEAR_ALL(a)  memset(&(a), 0, sizeof(a));
188 
189 #endif
190