1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (retro_math.h).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
23 #ifndef _LIBRETRO_COMMON_MATH_H
24 #define _LIBRETRO_COMMON_MATH_H
28 #if defined(_WIN32) && !defined(_XBOX)
29 #define WIN32_LEAN_AND_MEAN
31 #elif defined(_WIN32) && defined(_XBOX)
38 #include <compat/msvc.h>
40 #include <retro_inline.h>
43 #if !defined(USE_MATH_DEFINES)
44 #define M_PI 3.14159265358979323846264338327
49 #define MAX(a, b) ((a) > (b) ? (a) : (b))
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 * Get next power of 2 value based on initial value.
62 * Returns: next power of 2 value (derived from @v).
64 static INLINE uint32_t next_pow2(uint32_t v)
80 * Get previous power of 2 value based on initial value.
82 * Returns: previous power of 2 value (derived from @v).
84 static INLINE uint32_t prev_pow2(uint32_t v)
98 * Get the clamped value based on initial value.
100 * Returns: clamped value (derived from @v).
102 static INLINE float clamp_value(float v, float min, float max)
104 return v <= min ? min : v >= max ? max : v;
111 * Get the clamped 0.0-1.0 value based on initial value.
113 * Returns: clamped 0.0-1.0 value (derived from @v).
115 static INLINE float saturate_value(float v)
117 return clamp_value(v, 0.0f, 1.0f);
122 * @a : left hand vector value
123 * @b : right hand vector value
125 * Get the dot product of the two passed in vectors.
127 * Returns: dot product value (derived from @a and @b).
129 static INLINE float dot_product(const float* a, const float* b)
131 return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
135 * convert_rgb_to_yxy:
136 * @rgb : in RGB colour space value
137 * @Yxy : out Yxy colour space value
139 * Convert from RGB colour space to Yxy colour space.
141 * Returns: Yxy colour space value (derived from @rgb).
143 static INLINE void convert_rgb_to_yxy(const float* rgb, float* Yxy)
147 float one[3] = {1.0, 1.0, 1.0};
148 float rgb_xyz[3][3] = {
149 {0.4124564, 0.3575761, 0.1804375},
150 {0.2126729, 0.7151522, 0.0721750},
151 {0.0193339, 0.1191920, 0.9503041}
154 xyz[0] = dot_product(rgb_xyz[0], rgb);
155 xyz[1] = dot_product(rgb_xyz[1], rgb);
156 xyz[2] = dot_product(rgb_xyz[2], rgb);
158 inv = 1.0f / dot_product(xyz, one);
160 Yxy[1] = xyz[0] * inv;
161 Yxy[2] = xyz[1] * inv;
165 * convert_yxy_to_rgb:
166 * @rgb : in Yxy colour space value
167 * @Yxy : out rgb colour space value
169 * Convert from Yxy colour space to rgb colour space.
171 * Returns: rgb colour space value (derived from @Yxy).
173 static INLINE void convert_yxy_to_rgb(const float* Yxy, float* rgb)
176 float xyz_rgb[3][3] = {
177 {3.2404542, -1.5371385, -0.4985314},
178 {-0.9692660, 1.8760108, 0.0415560},
179 {0.0556434, -0.2040259, 1.0572252}
181 xyz[0] = Yxy[0] * Yxy[1] / Yxy[2];
183 xyz[2] = Yxy[0] * (1.0 - Yxy[1] - Yxy[2]) / Yxy[2];
185 rgb[0] = dot_product(xyz_rgb[0], xyz);
186 rgb[1] = dot_product(xyz_rgb[1], xyz);
187 rgb[2] = dot_product(xyz_rgb[2], xyz);