1 /* Copyright (C) 2010-2018 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (retro_math.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 _LIBRETRO_COMMON_MATH_H
24 #define _LIBRETRO_COMMON_MATH_H
25
26 #include <stdint.h>
27
28 #if defined(_WIN32) && !defined(_XBOX)
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
31 #elif defined(_WIN32) && defined(_XBOX)
32 #include <Xtl.h>
33 #endif
34
35 #include <limits.h>
36
37 #ifdef _MSC_VER
38 #include <compat/msvc.h>
39 #endif
40 #include <retro_inline.h>
41
42 #ifndef M_PI
43 #if !defined(USE_MATH_DEFINES)
44 #define M_PI 3.14159265358979323846264338327
45 #endif
46 #endif
47
48 #ifndef MAX
49 #define MAX(a, b) ((a) > (b) ? (a) : (b))
50 #endif
51
52 #ifndef MIN
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #endif
55
56 /**
57 * next_pow2:
58 * @v : initial value
59 *
60 * Get next power of 2 value based on initial value.
61 *
62 * Returns: next power of 2 value (derived from @v).
63 **/
next_pow2(uint32_t v)64 INLINE uint32_t next_pow2(uint32_t v)
65 {
66 v--;
67 v |= v >> 1;
68 v |= v >> 2;
69 v |= v >> 4;
70 v |= v >> 8;
71 v |= v >> 16;
72 v++;
73 return v;
74 }
75
76 /**
77 * prev_pow2:
78 * @v : initial value
79 *
80 * Get previous power of 2 value based on initial value.
81 *
82 * Returns: previous power of 2 value (derived from @v).
83 **/
prev_pow2(uint32_t v)84 INLINE uint32_t prev_pow2(uint32_t v)
85 {
86 v |= v >> 1;
87 v |= v >> 2;
88 v |= v >> 4;
89 v |= v >> 8;
90 v |= v >> 16;
91 return v - (v >> 1);
92 }
93
94 #endif
95