From 598ab525da3df3fef2033c159c570688c7282a8f Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 6 Mar 2020 17:18:10 +0100 Subject: Cleanup: Replace ABS/SQUARE/CUBE with function calls While it might be handy to have type-less functionality which is similar to how C++ math is implemented it can not be easily achieved with just preprocessor in a way which does not have side-effects on wrong usage. There macros where often used on a non-trivial expression, and there was at least one usage where it was causing an actual side effect/bug on Windows (see change around square_f(sh[index++]) in studiolight.c). For such cases it is handy to have a function which is guaranteed to have zero side-effects. The motivation behind actually removing the macros is that there is already a way to do similar calculation. Also, not having such macros is a way to guarantee that its usage is not changed in a way which have side-effects and that it's not used as an inspiration for cases where it should not be used. Differential Revision: https://developer.blender.org/D7051 --- source/blender/blenlib/BLI_math_base.h | 17 ++++++ source/blender/blenlib/BLI_utildefines.h | 27 ---------- source/blender/blenlib/intern/kdtree_impl.h | 4 +- source/blender/blenlib/intern/listbase.c | 3 +- source/blender/blenlib/intern/math_base_inline.c | 66 +++++++++++++++++++++++ source/blender/blenlib/intern/math_color_inline.c | 6 +-- source/blender/blenlib/intern/math_geom.c | 4 +- source/blender/blenlib/intern/math_rotation.c | 2 +- source/blender/blenlib/intern/string.c | 3 +- 9 files changed, 95 insertions(+), 37 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 4f841f75d3a..7555d3d7daf 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -114,6 +114,23 @@ MINLINE float sasqrt(float fac); MINLINE float interpf(float a, float b, float t); MINLINE double interpd(double a, double b, double t); +/* NOTE: Compilers will upcast all types smaller than int to int when performing arithmetic + * operation. */ +MINLINE int square_s(short a); +MINLINE int square_uchar(unsigned char a); +MINLINE int cube_s(short a); +MINLINE int cube_uchar(unsigned char a); + +MINLINE int square_i(int a); +MINLINE unsigned int square_uint(unsigned int a); +MINLINE float square_f(float a); +MINLINE double square_d(double a); + +MINLINE int cube_i(int a); +MINLINE unsigned int cube_uint(unsigned int a); +MINLINE float cube_f(float a); +MINLINE double cube_d(double a); + MINLINE float min_ff(float a, float b); MINLINE float max_ff(float a, float b); MINLINE float min_fff(float a, float b, float c); diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 5d80da6de77..3565b6fb26b 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -293,33 +293,6 @@ extern "C" { /** \name Simple Math Macros * \{ */ -/* avoid multiple access for supported compilers */ -#if defined(__GNUC__) || defined(__clang__) - -# define ABS(a) \ - ({ \ - typeof(a) a_ = (a); \ - ((a_) < 0 ? (-(a_)) : (a_)); \ - }) -# define SQUARE(a) \ - ({ \ - typeof(a) a_ = (a); \ - ((a_) * (a_)); \ - }) -# define CUBE(a) \ - ({ \ - typeof(a) a_ = (a); \ - ((a_) * (a_) * (a_)); \ - }) - -#else - -# define ABS(a) ((a) < 0 ? (-(a)) : (a)) -# define SQUARE(a) ((a) * (a)) -# define CUBE(a) ((a) * (a) * (a)) - -#endif - /* Float equality checks. */ #define IS_EQ(a, b) \ diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index 404e136a0a6..9e11ea903c2 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -79,7 +79,7 @@ static float len_squared_vnvn(const float v0[KD_DIMS], const float v1[KD_DIMS]) { float d = 0.0f; for (uint j = 0; j < KD_DIMS; j++) { - d += SQUARE(v0[j] - v1[j]); + d += square_f(v0[j] - v1[j]); } return d; } @@ -893,7 +893,7 @@ int BLI_kdtree_nd_(calc_duplicates_fast)(const KDTree *tree, struct DeDuplicateParams p = { .nodes = tree->nodes, .range = range, - .range_sq = SQUARE(range), + .range_sq = square_f(range), .duplicates = duplicates, .duplicates_found = &found, }; diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index fe5f9f7673f..e43c4b07a87 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -485,7 +485,8 @@ bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) BLI_assert(BLI_findindex(listbase, link) != -1); /* find link to insert before/after */ - for (int i = 0; i < ABS(step); i++) { + const int abs_step = abs(step); + for (int i = 0; i < abs_step; i++) { hook = is_up ? hook->prev : hook->next; if (!hook) { return false; diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index c0ee658d434..a293695154a 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -375,6 +375,72 @@ MINLINE float wrapf(float value, float max, float min) return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min; } +// Square. + +MINLINE int square_s(short a) +{ + return a * a; +} + +MINLINE int square_i(int a) +{ + return a * a; +} + +MINLINE unsigned int square_uint(unsigned int a) +{ + return a * a; +} + +MINLINE int square_uchar(unsigned char a) +{ + return a * a; +} + +MINLINE float square_f(float a) +{ + return a * a; +} + +MINLINE double square_d(double a) +{ + return a * a; +} + +// Cube. + +MINLINE int cube_s(short a) +{ + return a * a * a; +} + +MINLINE int cube_i(int a) +{ + return a * a * a; +} + +MINLINE unsigned int cube_uint(unsigned int a) +{ + return a * a * a; +} + +MINLINE int cube_uchar(unsigned char a) +{ + return a * a * a; +} + +MINLINE float cube_f(float a) +{ + return a * a * a; +} + +MINLINE double cube_d(double a) +{ + return a * a * a; +} + +// Min/max + MINLINE float min_ff(float a, float b) { return (a < b) ? a : b; diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index 66fd2780cf8..85aabbb672a 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -304,11 +304,11 @@ MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const int limit) { const int r = (int)col_a[0] - (int)col_b[0]; - if (ABS(r) < limit) { + if (abs(r) < limit) { const int g = (int)col_a[1] - (int)col_b[1]; - if (ABS(g) < limit) { + if (abs(g) < limit) { const int b = (int)col_a[2] - (int)col_b[2]; - if (ABS(b) < limit) { + if (abs(b) < limit) { return 1; } } diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 0033265a5a2..7da080dbe9a 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -655,7 +655,7 @@ float dist_squared_ray_to_seg_v3(const float ray_origin[3], *r_depth = depth; } - return len_squared_v3(dvec) - SQUARE(depth); + return len_squared_v3(dvec) - square_f(depth); } /* Returns the coordinates of the nearest vertex and @@ -1311,7 +1311,7 @@ int isect_seg_seg_v2_point_ex(const float v0[2], float u_a, u_b; if (equals_v2v2(v0, v1)) { - if (len_squared_v2v2(v2, v3) > SQUARE(eps)) { + if (len_squared_v2v2(v2, v3) > square_f(eps)) { /* use non-point segment as basis */ SWAP(const float *, v0, v2); SWAP(const float *, v1, v3); diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c index 2a6652fa424..cc8042316f8 100644 --- a/source/blender/blenlib/intern/math_rotation.c +++ b/source/blender/blenlib/intern/math_rotation.c @@ -1953,7 +1953,7 @@ void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4 copy_m3_m4(mat3, mat); if (!is_orthonormal_m3(mat3) || (determinant_m4(mat) < 0.0f) || - len_squared_v3(dscale) > SQUARE(1e-4f)) { + len_squared_v3(dscale) > square_f(1e-4f)) { /* extract R and S */ float tmp[4][4]; diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 99db2f0a290..db97cd3b065 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "MEM_guardedalloc.h" @@ -1119,7 +1120,7 @@ void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base BLI_STATIC_ASSERT(ARRAY_SIZE(units_base_2) == ARRAY_SIZE(units_base_10), "array size mismatch"); - while ((ABS(bytes_converted) >= base) && ((order + 1) < tot_units)) { + while ((fabs(bytes_converted) >= base) && ((order + 1) < tot_units)) { bytes_converted /= base; order++; } -- cgit v1.2.3