2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* math_funcs.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef MATH_FUNCS_H
|
|
|
|
#define MATH_FUNCS_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_defs.h"
|
2018-09-21 13:32:17 +02:00
|
|
|
#include "core/math/random_pcg.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/typedefs.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-28 18:29:15 +02:00
|
|
|
#include "thirdparty/misc/pcg.h"
|
|
|
|
|
2017-01-18 11:55:47 +01:00
|
|
|
#include <float.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
class Math {
|
2018-09-21 13:32:17 +02:00
|
|
|
static RandomPCG default_rand;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
public:
|
2017-01-12 12:55:19 +01:00
|
|
|
Math() {} // useless to instance
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-09-18 08:27:02 +02:00
|
|
|
// Not using 'RANDOM_MAX' to avoid conflict with system headers on some OSes (at least NetBSD).
|
|
|
|
static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double cos(double p_x) { return ::cos(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float cos(float p_x) { return ::cosf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double tan(double p_x) { return ::tan(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float tan(float p_x) { return ::tanf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double sinh(double p_x) { return ::sinh(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float sinh(float p_x) { return ::sinhf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2019-05-05 14:03:52 +02:00
|
|
|
static _ALWAYS_INLINE_ float sinc(float p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
|
|
|
|
static _ALWAYS_INLINE_ double sinc(double p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ float sincn(float p_x) { return sinc(Math_PI * p_x); }
|
|
|
|
static _ALWAYS_INLINE_ double sincn(double p_x) { return sinc(Math_PI * p_x); }
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double cosh(double p_x) { return ::cosh(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float cosh(float p_x) { return ::coshf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double tanh(double p_x) { return ::tanh(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float tanh(float p_x) { return ::tanhf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double asin(double p_x) { return ::asin(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float asin(float p_x) { return ::asinf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double acos(double p_x) { return ::acos(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float acos(float p_x) { return ::acosf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y, p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y, p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double sqrt(double p_x) { return ::sqrt(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float sqrt(float p_x) { return ::sqrtf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { return ::fmod(p_x, p_y); }
|
|
|
|
static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { return ::fmodf(p_x, p_y); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double floor(double p_x) { return ::floor(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float floor(float p_x) { return ::floorf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double ceil(double p_x) { return ::ceil(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float ceil(float p_x) { return ::ceilf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x, p_y); }
|
|
|
|
static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x, p_y); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double log(double p_x) { return ::log(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float log(float p_x) { return ::logf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2021-04-20 18:40:24 +02:00
|
|
|
static _ALWAYS_INLINE_ double log2(double p_x) { return ::log2(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float log2(float p_x) { return ::log2f(p_x); }
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); }
|
|
|
|
static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-09-19 06:09:35 +02:00
|
|
|
static _ALWAYS_INLINE_ bool is_nan(double p_val) {
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _isnan(p_val);
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ < 6
|
|
|
|
union {
|
|
|
|
uint64_t u;
|
|
|
|
double f;
|
|
|
|
} ieee754;
|
|
|
|
ieee754.f = p_val;
|
|
|
|
// (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000
|
|
|
|
return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000);
|
|
|
|
#else
|
|
|
|
return isnan(p_val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ bool is_nan(float p_val) {
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _isnan(p_val);
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ < 6
|
|
|
|
union {
|
|
|
|
uint32_t u;
|
|
|
|
float f;
|
|
|
|
} ieee754;
|
|
|
|
ieee754.f = p_val;
|
|
|
|
// -----------------------------------
|
|
|
|
// (single-precision floating-point)
|
|
|
|
// NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
|
|
|
|
// : (> 0x7f800000)
|
|
|
|
// where,
|
|
|
|
// s : sign
|
|
|
|
// x : non-zero number
|
|
|
|
// -----------------------------------
|
|
|
|
return ((ieee754.u & 0x7fffffff) > 0x7f800000);
|
|
|
|
#else
|
|
|
|
return isnan(p_val);
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ bool is_inf(double p_val) {
|
2017-03-05 16:44:50 +01:00
|
|
|
#ifdef _MSC_VER
|
2017-01-14 21:35:39 +01:00
|
|
|
return !_finite(p_val);
|
2017-06-14 17:11:53 +02:00
|
|
|
// use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ < 6
|
2017-04-26 17:49:08 +02:00
|
|
|
union {
|
|
|
|
uint64_t u;
|
|
|
|
double f;
|
|
|
|
} ieee754;
|
|
|
|
ieee754.f = p_val;
|
|
|
|
return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
|
2021-10-28 15:19:35 +02:00
|
|
|
((unsigned)ieee754.u == 0);
|
2017-03-05 16:44:50 +01:00
|
|
|
#else
|
2017-01-14 21:35:39 +01:00
|
|
|
return isinf(p_val);
|
2017-03-05 16:44:50 +01:00
|
|
|
#endif
|
2017-01-12 12:55:19 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ bool is_inf(float p_val) {
|
2017-03-05 16:44:50 +01:00
|
|
|
#ifdef _MSC_VER
|
2017-01-14 21:35:39 +01:00
|
|
|
return !_finite(p_val);
|
2017-06-14 17:11:53 +02:00
|
|
|
// use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ < 6
|
2017-04-26 17:49:08 +02:00
|
|
|
union {
|
|
|
|
uint32_t u;
|
|
|
|
float f;
|
|
|
|
} ieee754;
|
|
|
|
ieee754.f = p_val;
|
|
|
|
return (ieee754.u & 0x7fffffff) == 0x7f800000;
|
2017-03-05 16:44:50 +01:00
|
|
|
#else
|
2017-01-14 21:35:39 +01:00
|
|
|
return isinf(p_val);
|
2017-03-05 16:44:50 +01:00
|
|
|
#endif
|
2017-01-12 12:55:19 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double abs(double g) { return absd(g); }
|
|
|
|
static _ALWAYS_INLINE_ float abs(float g) { return absf(g); }
|
|
|
|
static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2018-06-10 22:06:44 +02:00
|
|
|
static _ALWAYS_INLINE_ double fposmod(double p_x, double p_y) {
|
|
|
|
double value = Math::fmod(p_x, p_y);
|
|
|
|
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
|
|
|
|
value += p_y;
|
|
|
|
}
|
|
|
|
value += 0.0;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ float fposmod(float p_x, float p_y) {
|
|
|
|
float value = Math::fmod(p_x, p_y);
|
|
|
|
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
|
|
|
|
value += p_y;
|
|
|
|
}
|
2022-02-06 12:14:58 +01:00
|
|
|
value += 0.0f;
|
2018-06-10 22:06:44 +02:00
|
|
|
return value;
|
|
|
|
}
|
2020-12-24 16:18:28 +01:00
|
|
|
static _ALWAYS_INLINE_ float fposmodp(float p_x, float p_y) {
|
|
|
|
float value = Math::fmod(p_x, p_y);
|
|
|
|
if (value < 0) {
|
|
|
|
value += p_y;
|
|
|
|
}
|
2022-02-06 12:14:58 +01:00
|
|
|
value += 0.0f;
|
2020-12-24 16:18:28 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ double fposmodp(double p_x, double p_y) {
|
|
|
|
double value = Math::fmod(p_x, p_y);
|
|
|
|
if (value < 0) {
|
|
|
|
value += p_y;
|
|
|
|
}
|
|
|
|
value += 0.0;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:51:03 +02:00
|
|
|
static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) {
|
|
|
|
int64_t value = p_x % p_y;
|
2018-10-27 22:12:27 +02:00
|
|
|
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
|
|
|
|
value += p_y;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2020-04-03 11:50:40 +02:00
|
|
|
static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * (Math_PI / 180.0); }
|
|
|
|
static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * (Math_PI / 180.0); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2020-04-03 11:50:40 +02:00
|
|
|
static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * (180.0 / Math_PI); }
|
|
|
|
static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * (180.0 / Math_PI); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-08-10 16:06:10 +02:00
|
|
|
static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
|
|
|
|
static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
|
|
|
|
|
2022-02-11 04:43:21 +01:00
|
|
|
static _ALWAYS_INLINE_ double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
|
|
|
|
return 0.5 *
|
|
|
|
((p_from * 2.0) +
|
|
|
|
(-p_pre + p_to) * p_weight +
|
|
|
|
(2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
|
|
|
|
(-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
|
|
|
|
return 0.5f *
|
|
|
|
((p_from * 2.0f) +
|
|
|
|
(-p_pre + p_to) * p_weight +
|
|
|
|
(2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
|
|
|
|
(-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
|
|
|
|
}
|
|
|
|
|
2019-07-14 06:30:45 +02:00
|
|
|
static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
|
|
|
|
double difference = fmod(p_to - p_from, Math_TAU);
|
|
|
|
double distance = fmod(2.0 * difference, Math_TAU) - difference;
|
|
|
|
return p_from + distance * p_weight;
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
|
|
|
|
float difference = fmod(p_to - p_from, (float)Math_TAU);
|
|
|
|
float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
|
|
|
|
return p_from + distance * p_weight;
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:06:10 +02:00
|
|
|
static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { return (p_value - p_from) / (p_to - p_from); }
|
|
|
|
static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { return (p_value - p_from) / (p_to - p_from); }
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
|
|
|
|
static _ALWAYS_INLINE_ float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2020-07-25 16:11:23 +02:00
|
|
|
static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_s) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_equal_approx(p_from, p_to)) {
|
2020-05-10 12:56:01 +02:00
|
|
|
return p_from;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-07-25 16:11:23 +02:00
|
|
|
double s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0, 1.0);
|
|
|
|
return s * s * (3.0 - 2.0 * s);
|
2019-03-19 12:39:43 +01:00
|
|
|
}
|
2020-07-25 16:11:23 +02:00
|
|
|
static _ALWAYS_INLINE_ float smoothstep(float p_from, float p_to, float p_s) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_equal_approx(p_from, p_to)) {
|
2020-05-10 12:56:01 +02:00
|
|
|
return p_from;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-07-25 16:11:23 +02:00
|
|
|
float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f);
|
|
|
|
return s * s * (3.0f - 2.0f * s);
|
2019-03-19 12:39:43 +01:00
|
|
|
}
|
2021-10-16 01:22:57 +02:00
|
|
|
static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; }
|
|
|
|
static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; }
|
2019-03-19 12:39:43 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
|
|
|
|
static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
|
|
|
|
static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2021-05-20 13:18:53 +02:00
|
|
|
static _ALWAYS_INLINE_ double round(double p_val) { return ::round(p_val); }
|
|
|
|
static _ALWAYS_INLINE_ float round(float p_val) { return ::roundf(p_val); }
|
2017-01-12 12:55:19 +01:00
|
|
|
|
2019-02-25 08:51:04 +01:00
|
|
|
static _ALWAYS_INLINE_ int64_t wrapi(int64_t value, int64_t min, int64_t max) {
|
2019-08-05 05:31:26 +02:00
|
|
|
int64_t range = max - min;
|
|
|
|
return range == 0 ? min : min + ((((value - min) % range) + range) % range);
|
2018-03-04 17:16:57 +01:00
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ double wrapf(double value, double min, double max) {
|
2019-08-05 05:31:26 +02:00
|
|
|
double range = max - min;
|
|
|
|
return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range));
|
2018-03-04 17:16:57 +01:00
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ float wrapf(float value, float min, float max) {
|
2019-08-05 05:31:26 +02:00
|
|
|
float range = max - min;
|
|
|
|
return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range));
|
2018-03-04 17:16:57 +01:00
|
|
|
}
|
2017-10-11 19:38:55 +02:00
|
|
|
|
2021-10-15 15:25:00 +02:00
|
|
|
static _ALWAYS_INLINE_ float fract(float value) {
|
|
|
|
return value - floor(value);
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ double fract(double value) {
|
|
|
|
return value - floor(value);
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ float pingpong(float value, float length) {
|
|
|
|
return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
|
|
|
|
}
|
|
|
|
static _ALWAYS_INLINE_ double pingpong(double value, double length) {
|
|
|
|
return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
|
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
// double only, as these functions are mainly used by the editor and not performance-critical,
|
2014-02-10 02:10:30 +01:00
|
|
|
static double ease(double p_x, double p_c);
|
2016-07-26 22:24:34 +02:00
|
|
|
static int step_decimals(double p_step);
|
2019-07-23 17:27:55 +02:00
|
|
|
static int range_step_decimals(double p_step);
|
2020-12-21 19:02:57 +01:00
|
|
|
static double snapped(double p_value, double p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static uint32_t larger_prime(uint32_t p_val);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-17 19:50:31 +02:00
|
|
|
static void seed(uint64_t x);
|
2017-01-14 21:35:39 +01:00
|
|
|
static void randomize();
|
|
|
|
static uint32_t rand_from_seed(uint64_t *seed);
|
2014-02-10 02:10:30 +01:00
|
|
|
static uint32_t rand();
|
2020-09-18 08:27:02 +02:00
|
|
|
static _ALWAYS_INLINE_ double randd() { return (double)rand() / (double)Math::RANDOM_32BIT_MAX; }
|
|
|
|
static _ALWAYS_INLINE_ float randf() { return (float)rand() / (float)Math::RANDOM_32BIT_MAX; }
|
2021-06-21 11:58:31 +02:00
|
|
|
static double randfn(double mean, double deviation);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
static double random(double from, double to);
|
2017-01-14 21:35:39 +01:00
|
|
|
static float random(float from, float to);
|
2020-07-26 12:52:24 +02:00
|
|
|
static int random(int from, int to);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2021-05-20 12:04:41 +02:00
|
|
|
static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b) {
|
2019-09-01 19:57:04 +02:00
|
|
|
// Check for exact equality first, required to handle "infinity" values.
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Then check for approximate equality.
|
2021-05-20 12:04:41 +02:00
|
|
|
float tolerance = CMP_EPSILON * abs(a);
|
2019-04-25 19:19:14 +02:00
|
|
|
if (tolerance < CMP_EPSILON) {
|
|
|
|
tolerance = CMP_EPSILON;
|
|
|
|
}
|
|
|
|
return abs(a - b) < tolerance;
|
|
|
|
}
|
|
|
|
|
2021-05-20 12:04:41 +02:00
|
|
|
static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b, float tolerance) {
|
2019-09-01 19:57:04 +02:00
|
|
|
// Check for exact equality first, required to handle "infinity" values.
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Then check for approximate equality.
|
2019-04-25 19:19:14 +02:00
|
|
|
return abs(a - b) < tolerance;
|
|
|
|
}
|
2016-10-18 22:50:21 +02:00
|
|
|
|
2021-05-20 12:04:41 +02:00
|
|
|
static _ALWAYS_INLINE_ bool is_zero_approx(float s) {
|
|
|
|
return abs(s) < CMP_EPSILON;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b) {
|
|
|
|
// Check for exact equality first, required to handle "infinity" values.
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Then check for approximate equality.
|
|
|
|
double tolerance = CMP_EPSILON * abs(a);
|
|
|
|
if (tolerance < CMP_EPSILON) {
|
|
|
|
tolerance = CMP_EPSILON;
|
|
|
|
}
|
|
|
|
return abs(a - b) < tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b, double tolerance) {
|
|
|
|
// Check for exact equality first, required to handle "infinity" values.
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Then check for approximate equality.
|
|
|
|
return abs(a - b) < tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ bool is_zero_approx(double s) {
|
2019-04-25 19:19:14 +02:00
|
|
|
return abs(s) < CMP_EPSILON;
|
2016-10-18 22:50:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ float absf(float g) {
|
2014-02-10 02:10:30 +01:00
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32_t i;
|
|
|
|
} u;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
u.f = g;
|
|
|
|
u.i &= 2147483647u;
|
2014-02-10 02:10:30 +01:00
|
|
|
return u.f;
|
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ double absd(double g) {
|
2014-02-10 02:10:30 +01:00
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
uint64_t i;
|
|
|
|
} u;
|
2017-03-05 16:44:50 +01:00
|
|
|
u.d = g;
|
|
|
|
u.i &= (uint64_t)9223372036854775807ll;
|
2014-02-10 02:10:30 +01:00
|
|
|
return u.d;
|
|
|
|
}
|
|
|
|
|
2021-05-20 22:25:58 +02:00
|
|
|
// This function should be as fast as possible and rounding mode should not matter.
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ int fast_ftoi(float a) {
|
2021-05-20 22:25:58 +02:00
|
|
|
// Assuming every supported compiler has `lrint()`.
|
|
|
|
return lrintf(a);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h) {
|
|
|
|
uint16_t h_exp, h_sig;
|
|
|
|
uint32_t f_sgn, f_exp, f_sig;
|
|
|
|
|
|
|
|
h_exp = (h & 0x7c00u);
|
|
|
|
f_sgn = ((uint32_t)h & 0x8000u) << 16;
|
|
|
|
switch (h_exp) {
|
|
|
|
case 0x0000u: /* 0 or subnormal */
|
|
|
|
h_sig = (h & 0x03ffu);
|
|
|
|
/* Signed zero */
|
|
|
|
if (h_sig == 0) {
|
|
|
|
return f_sgn;
|
|
|
|
}
|
|
|
|
/* Subnormal */
|
|
|
|
h_sig <<= 1;
|
|
|
|
while ((h_sig & 0x0400u) == 0) {
|
|
|
|
h_sig <<= 1;
|
|
|
|
h_exp++;
|
|
|
|
}
|
|
|
|
f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
|
|
|
|
f_sig = ((uint32_t)(h_sig & 0x03ffu)) << 13;
|
|
|
|
return f_sgn + f_exp + f_sig;
|
|
|
|
case 0x7c00u: /* inf or NaN */
|
|
|
|
/* All-ones exponent and a copy of the significand */
|
|
|
|
return f_sgn + 0x7f800000u + (((uint32_t)(h & 0x03ffu)) << 13);
|
|
|
|
default: /* normalized */
|
|
|
|
/* Just need to adjust the exponent and shift */
|
|
|
|
return f_sgn + (((uint32_t)(h & 0x7fffu) + 0x1c000u) << 13);
|
|
|
|
}
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) {
|
2016-10-19 16:14:41 +02:00
|
|
|
union {
|
|
|
|
uint32_t u32;
|
|
|
|
float f32;
|
|
|
|
} u;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
u.u32 = halfbits_to_floatbits(*h);
|
2016-10-19 16:14:41 +02:00
|
|
|
return u.f32;
|
|
|
|
}
|
|
|
|
|
2017-05-27 02:49:49 +02:00
|
|
|
static _ALWAYS_INLINE_ float half_to_float(const uint16_t h) {
|
|
|
|
return halfptr_to_float(&h);
|
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {
|
2017-03-05 16:44:50 +01:00
|
|
|
union {
|
|
|
|
float fv;
|
|
|
|
uint32_t ui;
|
|
|
|
} ci;
|
|
|
|
ci.fv = f;
|
|
|
|
|
|
|
|
uint32_t x = ci.ui;
|
|
|
|
uint32_t sign = (unsigned short)(x >> 31);
|
|
|
|
uint32_t mantissa;
|
|
|
|
uint32_t exp;
|
|
|
|
uint16_t hf;
|
|
|
|
|
|
|
|
// get mantissa
|
|
|
|
mantissa = x & ((1 << 23) - 1);
|
|
|
|
// get exponent bits
|
|
|
|
exp = x & (0xFF << 23);
|
|
|
|
if (exp >= 0x47800000) {
|
|
|
|
// check if the original single precision float number is a NaN
|
|
|
|
if (mantissa && (exp == (0xFF << 23))) {
|
|
|
|
// we have a single precision NaN
|
|
|
|
mantissa = (1 << 23) - 1;
|
|
|
|
} else {
|
|
|
|
// 16-bit half-float representation stores number as Inf
|
|
|
|
mantissa = 0;
|
|
|
|
}
|
|
|
|
hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
|
2021-10-28 15:19:35 +02:00
|
|
|
(uint16_t)(mantissa >> 13);
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
// check if exponent is <= -15
|
|
|
|
else if (exp <= 0x38000000) {
|
|
|
|
/*// store a denorm half-float value or zero
|
2016-10-19 16:14:41 +02:00
|
|
|
exp = (0x38000000 - exp) >> 23;
|
|
|
|
mantissa >>= (14 + exp);
|
|
|
|
|
|
|
|
hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
hf = 0; //denormals do not work for 3D, convert to zero
|
|
|
|
} else {
|
|
|
|
hf = (((uint16_t)sign) << 15) |
|
2021-10-28 15:19:35 +02:00
|
|
|
(uint16_t)((exp - 0x38000000) >> 13) |
|
|
|
|
(uint16_t)(mantissa >> 13);
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2016-10-19 16:14:41 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return hf;
|
|
|
|
}
|
2017-09-18 23:44:04 +02:00
|
|
|
|
|
|
|
static _ALWAYS_INLINE_ float snap_scalar(float p_offset, float p_step, float p_target) {
|
2020-12-21 19:02:57 +01:00
|
|
|
return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
|
2017-09-18 23:44:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 13:40:58 +01:00
|
|
|
static _ALWAYS_INLINE_ float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
|
2017-09-18 23:44:04 +02:00
|
|
|
if (p_step != 0) {
|
2020-12-21 19:02:57 +01:00
|
|
|
float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
|
2017-09-18 23:44:04 +02:00
|
|
|
float b = a;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_target >= 0) {
|
2017-09-18 23:44:04 +02:00
|
|
|
b -= p_separation;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-09-18 23:44:04 +02:00
|
|
|
b += p_step;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-18 23:44:04 +02:00
|
|
|
return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
|
|
|
|
}
|
|
|
|
return p_target;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MATH_FUNCS_H
|