On 17/07/12 13:21, Tom de Vries wrote: > attached patch implements an if-to-switch conversion tree pass > pass_if_to_switch. I will follow up this email with an infrastructure patch > that > provides double_int_popcount and popcount_hwi.
Bootstrapped and reg-tested (Ada inclusive) on x86_64, with pass_if_to_switch as only client. OK for trunk? Thanks, - Tom 2012-07-16 Tom de Vries <t...@codesourcery.com> * double-int.h (double_int_popcount): New inline function. * hwint.c (popcount_hwi): New function. * hwint.h (popcount_hwi): Declare function. New inline function.
Index: gcc/double-int.h =================================================================== --- gcc/double-int.h (revision 189508) +++ gcc/double-int.h (working copy) @@ -284,6 +284,14 @@ double_int_equal_p (double_int cst1, dou return cst1.low == cst2.low && cst1.high == cst2.high; } +/* Return number of set bits of CST. */ + +static inline int +double_int_popcount (double_int cst) +{ + return popcount_hwi (cst.high) + popcount_hwi (cst.low); +} + /* Legacy interface with decomposed high/low parts. */ Index: gcc/hwint.c =================================================================== --- gcc/hwint.c (revision 189508) +++ gcc/hwint.c (working copy) @@ -107,6 +107,22 @@ ffs_hwi (unsigned HOST_WIDE_INT x) return 1 + floor_log2 (x & -x); } +/* Return the number of set bits in X. */ + +int +popcount_hwi (unsigned HOST_WIDE_INT x) +{ + int i, ret = 0; + + for (i = 0; i < sizeof (x); i += 1) + { + ret += x & 1; + x >>= 1; + } + + return ret; +} + #endif /* GCC_VERSION < 3004 */ Index: gcc/hwint.h =================================================================== --- gcc/hwint.h (revision 189508) +++ gcc/hwint.h (working copy) @@ -179,6 +179,9 @@ extern int clz_hwi (unsigned HOST_WIDE_I extern int ctz_hwi (unsigned HOST_WIDE_INT x); extern int ffs_hwi (unsigned HOST_WIDE_INT x); +/* Return the number of set bits in X. */ +extern int popcount_hwi (unsigned HOST_WIDE_INT x); + /* Return log2, or -1 if not exact. */ extern int exact_log2 (unsigned HOST_WIDE_INT); @@ -231,6 +234,18 @@ ffs_hwi (unsigned HOST_WIDE_INT x) # endif } +static inline int +popcount_hwi (unsigned HOST_WIDE_INT x) +{ +# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG + return __builtin_popcountl (x); +# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG + return __builtin_popcountll (x); +# else + return __builtin_popcount (x); +# endif +} + static inline int floor_log2 (unsigned HOST_WIDE_INT x) {