ClickHouse/src/Functions/bitHammingDistance.cpp

37 lines
1000 B
C++
Raw Normal View History

#include <Functions/FunctionBinaryArithmetic.h>
2019-11-06 10:35:23 +00:00
#include <Functions/FunctionFactory.h>
#include <bit>
2019-11-06 10:35:23 +00:00
namespace DB
{
template <typename A, typename B>
struct BitHammingDistanceImpl
{
using ResultType = UInt8;
static const constexpr bool allow_fixed_string = false;
static const constexpr bool allow_string_integer = false;
2019-11-06 10:35:23 +00:00
template <typename Result = ResultType>
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
2019-11-06 10:35:23 +00:00
{
UInt64 res = static_cast<UInt64>(a) ^ static_cast<UInt64>(b);
return std::popcount(res);
2019-11-06 10:35:23 +00:00
}
#if USE_EMBEDDED_COMPILER
static constexpr bool compilable = false; /// special type handling, some other time
#endif
};
2019-11-06 10:35:23 +00:00
struct NameBitHammingDistance
2019-11-06 10:35:23 +00:00
{
static constexpr auto name = "bitHammingDistance";
};
using FunctionBitHammingDistance = BinaryArithmeticOverloadResolver<BitHammingDistanceImpl, NameBitHammingDistance>;
2019-11-06 10:35:23 +00:00
REGISTER_FUNCTION(BitHammingDistance)
2019-11-06 10:35:23 +00:00
{
factory.registerFunction<FunctionBitHammingDistance>();
}
}