ClickHouse/src/Functions/bitHammingDistance.cpp

36 lines
992 B
C++
Raw Normal View History

#include <Functions/FunctionBinaryArithmetic.h>
2019-11-06 10:35:23 +00:00
#include <Functions/FunctionFactory.h>
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);
2020-05-22 13:23:49 +00:00
return __builtin_popcountll(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>();
}
}