diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 93cd902c27f..c456ab0ecef 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -103,7 +103,6 @@ MINLINE float pow2f(float x); MINLINE float pow3f(float x); MINLINE float pow4f(float x); MINLINE float pow7f(float x); -MINLINE float safe_powf(float base, float exponent); MINLINE float sqrt3f(float f); MINLINE double sqrt3d(double d); @@ -218,10 +217,6 @@ MINLINE unsigned short round_db_to_ushort_clamp(double a); MINLINE int round_db_to_int_clamp(double a); MINLINE unsigned int round_db_to_uint_clamp(double a); -MINLINE float safe_divide(float a, float b); -MINLINE float safe_modf(float a, float b); -MINLINE float safe_logf(float a, float base); - int pow_i(int base, int exp); double double_round(double x, int ndigits); diff --git a/source/blender/blenlib/BLI_math_base_safe.h b/source/blender/blenlib/BLI_math_base_safe.h new file mode 100644 index 00000000000..42980c3f532 --- /dev/null +++ b/source/blender/blenlib/BLI_math_base_safe.h @@ -0,0 +1,50 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __BLI_MATH_BASE_SAFE_H__ +#define __BLI_MATH_BASE_SAFE_H__ + +/** \file + * \ingroup bli + * + * This file provides safe alternatives to common math functions like sqrt, powf. + * In this context "safe" means that the output is not NaN if the input is not NaN. + */ + +#include "BLI_math_base.h" + +#if BLI_MATH_DO_INLINE +# include "intern/math_base_safe_inline.c" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +MINLINE float safe_divide(float a, float b); +MINLINE float safe_modf(float a, float b); +MINLINE float safe_logf(float a, float base); +MINLINE float safe_sqrtf(float a); +MINLINE float safe_inverse_sqrtf(float a); +MINLINE float safe_asinf(float a); +MINLINE float safe_acosf(float a); +MINLINE float safe_powf(float base, float exponent); + +#ifdef __cplusplus +} +#endif + +#endif /* __BLI_MATH_BASE_SAFE_H__ */ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 1ed5c8e9b10..c6e04d4147a 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -86,6 +86,7 @@ set(SRC intern/listbase.c intern/math_base.c intern/math_base_inline.c + intern/math_base_safe_inline.c intern/math_bits_inline.c intern/math_color.c intern/math_color_blend_inline.c @@ -209,6 +210,7 @@ set(SRC BLI_map_slots.hh BLI_math.h BLI_math_base.h + BLI_math_base_safe.h BLI_math_bits.h BLI_math_color.h BLI_math_color_blend.h @@ -328,6 +330,7 @@ endif() # no need to compile object files for inline headers. set_source_files_properties( intern/math_base_inline.c + intern/math_base_safe_inline.c intern/math_bits_inline.c intern/math_color_blend_inline.c intern/math_color_inline.c diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index bd4eb12a72c..1b388dcf11f 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -70,13 +70,6 @@ MINLINE float pow7f(float x) { return pow2f(pow3f(x)) * x; } -MINLINE float safe_powf(float base, float exponent) -{ - if (UNLIKELY(base < 0.0f && exponent != (int)exponent)) { - return 0.0f; - } - return powf(base, exponent); -} MINLINE float sqrt3f(float f) { @@ -348,24 +341,6 @@ MINLINE signed char round_db_to_char_clamp(double a){ #undef _round_clamp_fl_impl #undef _round_clamp_db_impl -MINLINE float safe_divide(float a, float b) -{ - return (b != 0.0f) ? a / b : 0.0f; -} - -MINLINE float safe_modf(float a, float b) -{ - return (b != 0.0f) ? fmodf(a, b) : 0.0f; -} - -MINLINE float safe_logf(float a, float base) -{ - if (UNLIKELY(a <= 0.0f || base <= 0.0f)) { - return 0.0f; - } - return safe_divide(logf(a), logf(base)); -} - /* integer division that rounds 0.5 up, particularly useful for color blending * with integers, to avoid gradual darkening when rounding down */ MINLINE int divide_round_i(int a, int b) diff --git a/source/blender/blenlib/intern/math_base_safe_inline.c b/source/blender/blenlib/intern/math_base_safe_inline.c new file mode 100644 index 00000000000..5600382e395 --- /dev/null +++ b/source/blender/blenlib/intern/math_base_safe_inline.c @@ -0,0 +1,79 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __MATH_BASE_SAFE_INLINE_C__ +#define __MATH_BASE_SAFE_INLINE_C__ + +#include "BLI_math_base_safe.h" +#include "BLI_utildefines.h" + +#ifdef __cplusplus +extern "C" { +#endif + +MINLINE float safe_divide(float a, float b) +{ + return (b != 0.0f) ? a / b : 0.0f; +} + +MINLINE float safe_modf(float a, float b) +{ + return (b != 0.0f) ? fmodf(a, b) : 0.0f; +} + +MINLINE float safe_logf(float a, float base) +{ + if (UNLIKELY(a <= 0.0f || base <= 0.0f)) { + return 0.0f; + } + return safe_divide(logf(a), logf(base)); +} + +MINLINE float safe_sqrtf(float a) +{ + return sqrtf(MAX2(a, 0.0f)); +} + +MINLINE float safe_inverse_sqrtf(float a) +{ + return (a > 0.0f) ? 1.0f / sqrtf(a) : 0.0f; +} + +MINLINE float safe_asinf(float a) +{ + CLAMP(a, -1.0f, 1.0f); + return asinf(a); +} + +MINLINE float safe_acosf(float a) +{ + CLAMP(a, -1.0f, 1.0f); + return acosf(a); +} + +MINLINE float safe_powf(float base, float exponent) +{ + if (UNLIKELY(base < 0.0f && exponent != (int)exponent)) { + return 0.0f; + } + return powf(base, exponent); +} + +#ifdef __cplusplus +} +#endif + +#endif /* __MATH_BASE_SAFE_INLINE_C__ */ diff --git a/tests/gtests/blenlib/BLI_math_base_safe_test.cc b/tests/gtests/blenlib/BLI_math_base_safe_test.cc new file mode 100644 index 00000000000..2e3e083cf92 --- /dev/null +++ b/tests/gtests/blenlib/BLI_math_base_safe_test.cc @@ -0,0 +1,37 @@ +/* Apache License, Version 2.0 */ + +#include "testing/testing.h" + +#include "BLI_math_base_safe.h" + +TEST(math_base, SafePowf) +{ + EXPECT_FLOAT_EQ(safe_powf(4.0f, 3.0f), 64.0f); + EXPECT_FLOAT_EQ(safe_powf(3.2f, 5.6f), 674.2793796f); + EXPECT_FLOAT_EQ(safe_powf(4.0f, -2.0f), 0.0625f); + EXPECT_FLOAT_EQ(safe_powf(6.0f, -3.2f), 0.003235311f); + EXPECT_FLOAT_EQ(safe_powf(-4.0f, 6), 4096.0f); + EXPECT_FLOAT_EQ(safe_powf(-3.0f, 5.5), 0.0f); + EXPECT_FLOAT_EQ(safe_powf(-2.5f, -4.0f), 0.0256f); + EXPECT_FLOAT_EQ(safe_powf(-3.7f, -4.5f), 0.0f); +} + +TEST(math_base, SafeModf) +{ + EXPECT_FLOAT_EQ(safe_modf(3.4, 2.2f), 1.2f); + EXPECT_FLOAT_EQ(safe_modf(3.4, -2.2f), 1.2f); + EXPECT_FLOAT_EQ(safe_modf(-3.4, -2.2f), -1.2f); + EXPECT_FLOAT_EQ(safe_modf(-3.4, 0.0f), 0.0f); + EXPECT_FLOAT_EQ(safe_modf(0.0f, 3.0f), 0.0f); + EXPECT_FLOAT_EQ(safe_modf(55.0f, 10.0f), 5.0f); +} + +TEST(math_base, SafeLogf) +{ + EXPECT_FLOAT_EQ(safe_logf(3.3f, 2.5f), 1.302995247f); + EXPECT_FLOAT_EQ(safe_logf(0.0f, 3.0f), 0.0f); + EXPECT_FLOAT_EQ(safe_logf(3.0f, 0.0f), 0.0f); + EXPECT_FLOAT_EQ(safe_logf(-2.0f, 4.3f), 0.0f); + EXPECT_FLOAT_EQ(safe_logf(2.0f, -4.3f), 0.0f); + EXPECT_FLOAT_EQ(safe_logf(-2.0f, -4.3f), 0.0f); +} diff --git a/tests/gtests/blenlib/BLI_math_base_test.cc b/tests/gtests/blenlib/BLI_math_base_test.cc index 66506d56f46..dc20c75576d 100644 --- a/tests/gtests/blenlib/BLI_math_base_test.cc +++ b/tests/gtests/blenlib/BLI_math_base_test.cc @@ -113,35 +113,3 @@ TEST(math_base, Log2CeilU) EXPECT_EQ(log2_ceil_u(9), 4); EXPECT_EQ(log2_ceil_u(123456), 17); } - -TEST(math_base, SafePowf) -{ - EXPECT_FLOAT_EQ(safe_powf(4.0f, 3.0f), 64.0f); - EXPECT_FLOAT_EQ(safe_powf(3.2f, 5.6f), 674.2793796f); - EXPECT_FLOAT_EQ(safe_powf(4.0f, -2.0f), 0.0625f); - EXPECT_FLOAT_EQ(safe_powf(6.0f, -3.2f), 0.003235311f); - EXPECT_FLOAT_EQ(safe_powf(-4.0f, 6), 4096.0f); - EXPECT_FLOAT_EQ(safe_powf(-3.0f, 5.5), 0.0f); - EXPECT_FLOAT_EQ(safe_powf(-2.5f, -4.0f), 0.0256f); - EXPECT_FLOAT_EQ(safe_powf(-3.7f, -4.5f), 0.0f); -} - -TEST(math_base, SafeModf) -{ - EXPECT_FLOAT_EQ(safe_modf(3.4, 2.2f), 1.2f); - EXPECT_FLOAT_EQ(safe_modf(3.4, -2.2f), 1.2f); - EXPECT_FLOAT_EQ(safe_modf(-3.4, -2.2f), -1.2f); - EXPECT_FLOAT_EQ(safe_modf(-3.4, 0.0f), 0.0f); - EXPECT_FLOAT_EQ(safe_modf(0.0f, 3.0f), 0.0f); - EXPECT_FLOAT_EQ(safe_modf(55.0f, 10.0f), 5.0f); -} - -TEST(math_base, SafeLogf) -{ - EXPECT_FLOAT_EQ(safe_logf(3.3f, 2.5f), 1.302995247f); - EXPECT_FLOAT_EQ(safe_logf(0.0f, 3.0f), 0.0f); - EXPECT_FLOAT_EQ(safe_logf(3.0f, 0.0f), 0.0f); - EXPECT_FLOAT_EQ(safe_logf(-2.0f, 4.3f), 0.0f); - EXPECT_FLOAT_EQ(safe_logf(2.0f, -4.3f), 0.0f); - EXPECT_FLOAT_EQ(safe_logf(-2.0f, -4.3f), 0.0f); -} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index 496fe44234e..d151dacd7a4 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -58,6 +58,7 @@ BLENDER_TEST(BLI_linklist_lockfree "bf_blenlib;bf_intern_numaapi") BLENDER_TEST(BLI_listbase "bf_blenlib") BLENDER_TEST(BLI_map "bf_blenlib") BLENDER_TEST(BLI_math_base "bf_blenlib") +BLENDER_TEST(BLI_math_base_safe "bf_blenlib") BLENDER_TEST(BLI_math_bits "bf_blenlib") BLENDER_TEST(BLI_math_color "bf_blenlib") BLENDER_TEST(BLI_math_geom "bf_blenlib")