#include #include #include namespace DB { namespace { template struct BitCountImpl { using ResultType = UInt8; static constexpr bool allow_fixed_string = false; static const constexpr bool allow_string_integer = false; static inline ResultType apply(A a) { /// We count bits in the value representation in memory. For example, we support floats. /// We need to avoid sign-extension when converting signed numbers to larger type. So, uint8_t(-1) has 8 bits. if constexpr (std::is_same_v || std::is_same_v) return __builtin_popcountll(a); if constexpr (std::is_same_v || std::is_same_v || std::is_unsigned_v) return __builtin_popcount(a); if constexpr (std::is_same_v) return __builtin_popcount(static_cast(a)); if constexpr (std::is_same_v) return __builtin_popcount(static_cast(a)); else return __builtin_popcountll(bit_cast(a)); } #if USE_EMBEDDED_COMPILER static constexpr bool compilable = false; #endif }; struct NameBitCount { static constexpr auto name = "bitCount"; }; using FunctionBitCount = FunctionUnaryArithmetic; } /// The function has no ranges of monotonicity. template <> struct FunctionUnaryArithmeticMonotonicity { static bool has() { return false; } static IFunction::Monotonicity get(const Field &, const Field &) { return {}; } }; REGISTER_FUNCTION(BitCount) { factory.registerFunction(); } }