diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-04-23 18:37:12 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-04-23 18:37:12 +0300 |
commit | 372752c7393025342997f8ba6c076576280b3019 (patch) | |
tree | 72fdd54167e8e5315b738dff3e96e33d3eaf9aff /source/blender | |
parent | 27e03dcd21872d9daf73adf42c69c04c5806ac07 (diff) |
Math Lib: add count_bits_i utility function
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_math_bits.h | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_bits_inline.c | 10 |
2 files changed, 16 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_math_bits.h b/source/blender/blenlib/BLI_math_bits.h index 2569c4cb41e..876c0d92e31 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 f5bec405b2c..11ee6e7fa5b 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__ */ |