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