From 372752c7393025342997f8ba6c076576280b3019 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 24 Apr 2015 01:37:12 +1000 Subject: Math Lib: add count_bits_i utility function --- source/blender/blenlib/BLI_math_bits.h | 6 ++++++ source/blender/blenlib/intern/math_bits_inline.c | 10 ++++++++++ 2 files changed, 16 insertions(+) (limited to 'source/blender') 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__ */ -- cgit v1.2.3