[Bf-blender-cvs] [04125d8a9cb] blender-v2.79a-release: Math Lib: clamped rounding utility functions

Campbell Barton noreply at git.blender.org
Wed Jan 3 12:47:53 CET 2018


Commit: 04125d8a9cbd1272486c327e65445b77f3335460
Author: Campbell Barton
Date:   Mon Sep 18 21:03:10 2017 +1000
Branches: blender-v2.79a-release
https://developer.blender.org/rB04125d8a9cbd1272486c327e65445b77f3335460

Math Lib: clamped rounding utility functions

===================================================================

M	source/blender/blenlib/BLI_math_base.h
M	source/blender/blenlib/intern/math_base_inline.c

===================================================================

diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h
index c44b666faea..e7e89a6424a 100644
--- a/source/blender/blenlib/BLI_math_base.h
+++ b/source/blender/blenlib/BLI_math_base.h
@@ -153,6 +153,20 @@ MINLINE int iroundf(float a);
 MINLINE int divide_round_i(int a, int b);
 MINLINE int mod_i(int i, int n);
 
+MINLINE signed char    round_fl_to_char_clamp(float a);
+MINLINE unsigned char  round_fl_to_uchar_clamp(float a);
+MINLINE short          round_fl_to_short_clamp(float a);
+MINLINE unsigned short round_fl_to_ushort_clamp(float a);
+MINLINE int            round_fl_to_int_clamp(float a);
+MINLINE unsigned int   round_fl_to_uint_clamp(float a);
+
+MINLINE signed char    round_db_to_char_clamp(double a);
+MINLINE unsigned char  round_db_to_uchar_clamp(double a);
+MINLINE short          round_db_to_short_clamp(double a);
+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);
+
 int pow_i(int base, int exp);
 double double_round(double x, int ndigits);
 
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c
index 6574c001a23..bde0940ebbd 100644
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@ -33,6 +33,7 @@
 #include <float.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 
 #ifdef __SSE2__
 #  include <emmintrin.h>
@@ -181,11 +182,44 @@ MINLINE unsigned power_of_2_min_u(unsigned x)
 	return x - (x >> 1);
 }
 
+/* rounding and clamping */
+
 MINLINE int iroundf(float a)
 {
 	return (int)floorf(a + 0.5f);
 }
 
+#define _round_clamp_fl_impl(arg, ty, min, max) { \
+	float r = floorf(arg + 0.5f); \
+	if      (UNLIKELY(r <= (float)min)) return (ty)min; \
+	else if (UNLIKELY(r >= (float)max)) return (ty)max; \
+	else return (ty)r; \
+}
+
+#define _round_clamp_db_impl(arg, ty, min, max) { \
+	double r = floor(arg + 0.5); \
+	if      (UNLIKELY(r <= (double)min)) return (ty)min; \
+	else if (UNLIKELY(r >= (double)max)) return (ty)max; \
+	else return (ty)r; \
+}
+
+MINLINE signed char    round_fl_to_char_clamp(float a) { _round_clamp_fl_impl(a, signed char, SCHAR_MIN, SCHAR_MAX) }
+MINLINE unsigned char  round_fl_to_uchar_clamp(float a) { _round_clamp_fl_impl(a, unsigned char, 0, UCHAR_MAX) }
+MINLINE short          round_fl_to_short_clamp(float a) { _round_clamp_fl_impl(a, short, SHRT_MIN, SHRT_MAX) }
+MINLINE unsigned short round_fl_to_ushort_clamp(float a) { _round_clamp_fl_impl(a, unsigned short, 0, USHRT_MAX) }
+MINLINE int            round_fl_to_int_clamp(float a) { _round_clamp_fl_impl(a, int, INT_MIN, INT_MAX) }
+MINLINE unsigned int   round_fl_to_uint_clamp(float a) { _round_clamp_fl_impl(a, unsigned int, 0, UINT_MAX) }
+
+MINLINE signed char    round_db_to_char_clamp(double a) { _round_clamp_db_impl(a, signed char, SCHAR_MIN, SCHAR_MAX) }
+MINLINE unsigned char  round_db_to_uchar_clamp(double a) { _round_clamp_db_impl(a, unsigned char, 0, UCHAR_MAX) }
+MINLINE short          round_db_to_short_clamp(double a) { _round_clamp_db_impl(a, short, SHRT_MIN, SHRT_MAX) }
+MINLINE unsigned short round_db_to_ushort_clamp(double a) { _round_clamp_db_impl(a, unsigned short, 0, USHRT_MAX) }
+MINLINE int            round_db_to_int_clamp(double a) { _round_clamp_db_impl(a, int, INT_MIN, INT_MAX) }
+MINLINE unsigned int   round_db_to_uint_clamp(double a) { _round_clamp_db_impl(a, unsigned int, 0, UINT_MAX) }
+
+#undef _round_clamp_fl_impl
+#undef _round_clamp_db_impl
+
 /* 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)



More information about the Bf-blender-cvs mailing list