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