Skip to content

Commit

Permalink
Prefer inline dca_popcount() implementation.
Browse files Browse the repository at this point in the history
Don't use GCC built-in (which results in a function call) unless
hardware support for popcnt instruction is available.
  • Loading branch information
foo86 committed May 6, 2015
1 parent 0b343fd commit c1069c6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion libdcadec/common.h
Expand Up @@ -67,11 +67,23 @@ static inline uint16_t dca_bswap16(uint16_t x) { return (x << 8) | (x >> 8); }
#define dca_bswap64(x) __builtin_bswap64(x)
#define dca_clz32(x) __builtin_clz(x)
#define dca_clz64(x) __builtin_clzll(x)
#define dca_popcount(x) __builtin_popcount(x)
#else
#error Unsupported compiler
#endif

#if (defined __GNUC__) && (defined __POPCNT__)
#define dca_popcount(x) __builtin_popcount(x)
#else
static inline int dca_popcount(uint32_t x)
{
x -= (x >> 1) & 0x55555555;
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0f0f0f0f;
x += x >> 8;
return (x + (x >> 16)) & 0x3f;
}
#endif

#define dca_countof(x) (sizeof(x) / sizeof((x)[0]))

#define DCA_BSWAP16_C(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
Expand Down

0 comments on commit c1069c6

Please sign in to comment.