2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: some of this file.
23 * ***** END GPL LICENSE BLOCK *****
26 #ifndef __BLI_MATH_BASE_H__
27 #define __BLI_MATH_BASE_H__
29 /** \file BLI_math_base.h
34 # define _USE_MATH_DEFINES
38 #include "BLI_math_inline.h"
41 #include <ieeefp.h> /* for finite() */
45 #define M_PI 3.14159265358979323846 /* pi */
48 #define M_PI_2 1.57079632679489661923 /* pi/2 */
51 #define M_PI_4 0.78539816339744830962 /* pi/4 */
54 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
57 #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
60 #define M_SQRT3 1.73205080756887729352 /* sqrt(3) */
63 #define M_SQRT1_3 0.57735026918962576450 /* 1/sqrt(3) */
66 #define M_1_PI 0.318309886183790671538 /* 1/pi */
69 #define M_E 2.7182818284590452354 /* e */
72 #define M_LOG2E 1.4426950408889634074 /* log_2 e */
75 #define M_LOG10E 0.43429448190325182765 /* log_10 e */
78 #define M_LN2 0.69314718055994530942 /* log_e 2 */
81 #define M_LN10 2.30258509299404568402 /* log_e 10 */
85 # define NAN_FLT __builtin_nanf("")
87 /* evil quiet NaN definition */
88 static const int NAN_INT = 0x7FC00000;
89 # define NAN_FLT (*((float *)(&NAN_INT)))
92 /* do not redefine functions from C99, POSIX.1-2001 or MSVC12 (partial C99) */
93 #if !(defined(_ISOC99_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || defined(_MSC_VER))
96 #define sqrtf(a) ((float)sqrt(a))
99 #define powf(a, b) ((float)pow(a, b))
102 #define cosf(a) ((float)cos(a))
105 #define sinf(a) ((float)sin(a))
108 #define acosf(a) ((float)acos(a))
111 #define asinf(a) ((float)asin(a))
114 #define atan2f(a, b) ((float)atan2(a, b))
117 #define tanf(a) ((float)tan(a))
120 #define atanf(a) ((float)atan(a))
123 #define floorf(a) ((float)floor(a))
126 #define ceilf(a) ((float)ceil(a))
129 #define fabsf(a) ((float)fabs(a))
132 #define logf(a) ((float)log(a))
135 #define expf(a) ((float)exp(a))
138 #define fmodf(a, b) ((float)fmod(a, b))
141 #define hypotf(a, b) ((float)hypot(a, b))
144 #define copysignf(a, b) ((float)copysign(a, b))
147 #endif /* C99, POSIX.1-2001 or MSVC12 (partial C99) */
150 # if defined(_MSC_VER)
151 # define finite(n) _finite(n)
155 #if BLI_MATH_DO_INLINE
156 #include "intern/math_base_inline.c"
159 #ifdef BLI_MATH_GCC_WARN_PRAGMA
160 # pragma GCC diagnostic push
161 # pragma GCC diagnostic ignored "-Wredundant-decls"
164 /******************************* Float ******************************/
166 MINLINE float pow2f(float x);
167 MINLINE float pow3f(float x);
168 MINLINE float pow4f(float x);
169 MINLINE float pow7f(float x);
171 MINLINE float sqrt3f(float f);
172 MINLINE double sqrt3d(double d);
174 MINLINE float sqrtf_signed(float f);
176 MINLINE float saacosf(float f);
177 MINLINE float saasinf(float f);
178 MINLINE float sasqrtf(float f);
179 MINLINE float saacos(float fac);
180 MINLINE float saasin(float fac);
181 MINLINE float sasqrt(float fac);
183 MINLINE float interpf(float a, float b, float t);
185 MINLINE float min_ff(float a, float b);
186 MINLINE float max_ff(float a, float b);
187 MINLINE float min_fff(float a, float b, float c);
188 MINLINE float max_fff(float a, float b, float c);
189 MINLINE float min_ffff(float a, float b, float c, float d);
190 MINLINE float max_ffff(float a, float b, float c, float d);
192 MINLINE int min_ii(int a, int b);
193 MINLINE int max_ii(int a, int b);
194 MINLINE int min_iii(int a, int b, int c);
195 MINLINE int max_iii(int a, int b, int c);
196 MINLINE int min_iiii(int a, int b, int c, int d);
197 MINLINE int max_iiii(int a, int b, int c, int d);
199 MINLINE int compare_ff(float a, float b, const float max_diff);
200 MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps);
202 MINLINE float signf(float f);
203 MINLINE int signum_i_ex(float a, float eps);
204 MINLINE int signum_i(float a);
206 MINLINE float power_of_2(float f);
208 /* these don't really fit anywhere but were being copied about a lot */
209 MINLINE int is_power_of_2_i(int n);
210 MINLINE int power_of_2_max_i(int n);
211 MINLINE int power_of_2_min_i(int n);
213 MINLINE unsigned int power_of_2_max_u(unsigned int x);
214 MINLINE unsigned int power_of_2_min_u(unsigned int x);
216 MINLINE int iroundf(float a);
217 MINLINE int divide_round_i(int a, int b);
218 MINLINE int mod_i(int i, int n);
220 int pow_i(int base, int exp);
221 double double_round(double x, int ndigits);
223 #ifdef BLI_MATH_GCC_WARN_PRAGMA
224 # pragma GCC diagnostic pop
227 /* asserts, some math functions expect normalized inputs
228 * check the vector is unit length, or zero length (which can't be helped in some cases).
231 /* note: 0.0001 is too small becaues normals may be converted from short's: see [#34322] */
232 # define BLI_ASSERT_UNIT_EPSILON 0.0002f
233 # define BLI_ASSERT_UNIT_V3(v) { \
234 const float _test_unit = len_squared_v3(v); \
235 BLI_assert((fabsf(_test_unit - 1.0f) < BLI_ASSERT_UNIT_EPSILON) || \
236 (fabsf(_test_unit) < BLI_ASSERT_UNIT_EPSILON)); \
239 # define BLI_ASSERT_UNIT_V2(v) { \
240 const float _test_unit = len_squared_v2(v); \
241 BLI_assert((fabsf(_test_unit - 1.0f) < BLI_ASSERT_UNIT_EPSILON) || \
242 (fabsf(_test_unit) < BLI_ASSERT_UNIT_EPSILON)); \
245 # define BLI_ASSERT_UNIT_QUAT(q) { \
246 const float _test_unit = dot_qtqt(q, q); \
247 BLI_assert((fabsf(_test_unit - 1.0f) < BLI_ASSERT_UNIT_EPSILON * 10) || \
248 (fabsf(_test_unit) < BLI_ASSERT_UNIT_EPSILON * 10)); \
251 # define BLI_ASSERT_ZERO_M3(m) { \
252 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 9) != 0.0); \
255 # define BLI_ASSERT_ZERO_M4(m) { \
256 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 16) != 0.0); \
258 # define BLI_ASSERT_UNIT_M3(m) { \
259 BLI_ASSERT_UNIT_V3((m)[0]); \
260 BLI_ASSERT_UNIT_V3((m)[1]); \
261 BLI_ASSERT_UNIT_V3((m)[2]); \
264 # define BLI_ASSERT_UNIT_V2(v) (void)(v)
265 # define BLI_ASSERT_UNIT_V3(v) (void)(v)
266 # define BLI_ASSERT_UNIT_QUAT(v) (void)(v)
267 # define BLI_ASSERT_ZERO_M3(m) (void)(m)
268 # define BLI_ASSERT_ZERO_M4(m) (void)(m)
269 # define BLI_ASSERT_UNIT_M3(m) (void)(m)
272 #endif /* __BLI_MATH_BASE_H__ */