[Bf-blender-cvs] [372752c] master: Math Lib: add count_bits_i utility function

Campbell Barton noreply at git.blender.org
Thu Apr 23 17:38:30 CEST 2015


Commit: 372752c7393025342997f8ba6c076576280b3019
Author: Campbell Barton
Date:   Fri Apr 24 01:37:12 2015 +1000
Branches: master
https://developer.blender.org/rB372752c7393025342997f8ba6c076576280b3019

Math Lib: add count_bits_i utility function

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

M	source/blender/blenlib/BLI_math_bits.h
M	source/blender/blenlib/intern/math_bits_inline.c

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

diff --git a/source/blender/blenlib/BLI_math_bits.h b/source/blender/blenlib/BLI_math_bits.h
index 2569c4c..876c0d9 100644
--- a/source/blender/blenlib/BLI_math_bits.h
+++ b/source/blender/blenlib/BLI_math_bits.h
@@ -34,6 +34,12 @@ extern "C" {
 MINLINE unsigned int highest_order_bit_i(unsigned int n);
 MINLINE unsigned short highest_order_bit_s(unsigned short n);
 
+#ifdef __GNUC__
+#  define count_bits_i(i) __builtin_popcount(i)
+#else
+MINLINE int count_bits_i(unsigned int n);
+#endif
+
 #if BLI_MATH_DO_INLINE
 #include "intern/math_bits_inline.c"
 #endif
diff --git a/source/blender/blenlib/intern/math_bits_inline.c b/source/blender/blenlib/intern/math_bits_inline.c
index f5bec40..11ee6e7 100644
--- a/source/blender/blenlib/intern/math_bits_inline.c
+++ b/source/blender/blenlib/intern/math_bits_inline.c
@@ -46,4 +46,14 @@ MINLINE unsigned short highest_order_bit_s(unsigned short n)
 	return (unsigned short)(n - (n >> 1));
 }
 
+#ifndef __GNUC__
+MINLINE int count_bits_i(unsigned int i)
+{
+	/* variable-precision SWAR algorithm. */
+	i = i - ((i >> 1) & 0x55555555);
+	i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
+	return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
+}
+#endif
+
 #endif /* __MATH_BITS_INLINE_C__ */




More information about the Bf-blender-cvs mailing list