Add an IsOddParity bitmath function

This commit is contained in:
Jonathan G Rennison
2021-12-05 17:40:42 +00:00
parent ff714b7648
commit c6bd01b2f2

View File

@@ -317,6 +317,30 @@ static inline uint CountBits(T value)
#endif
}
/**
* Return whether the input has odd parity (odd number of bits set).
*
* @param value the value to return the parity of.
* @return true if the parity is odd.
*/
template <typename T>
static inline bool IsOddParity(T value)
{
static_assert(sizeof(T) <= sizeof(unsigned long long));
#ifdef WITH_BITMATH_BUILTINS
typename std::make_unsigned<T>::type unsigned_value = value;
if (sizeof(T) <= sizeof(unsigned int)) {
return __builtin_parity(unsigned_value);
} else if (sizeof(T) == sizeof(unsigned long)) {
return __builtin_parityl(unsigned_value);
} else {
return __builtin_parityll(unsigned_value);
}
#else
return CountBits<T>(value) & 1;
#endif
}
/**
* Test whether \a value has exactly 1 bit set
*