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