aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/bitHammingDistance.cpp
blob: 2eaa397dd041a6b4f0cbd3eb83d8c60cdfd654fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Functions/FunctionBinaryArithmetic.h>
#include <Functions/FunctionFactory.h>
#include <bit>

namespace DB
{
template <typename A, typename B>
struct BitHammingDistanceImpl
{
    using ResultType = UInt8;
    static constexpr bool allow_fixed_string = true;
    static constexpr bool allow_string_integer = false;

    template <typename Result = ResultType>
    static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
    {
        UInt64 res = static_cast<UInt64>(a) ^ static_cast<UInt64>(b);
        return std::popcount(res);
    }

#if USE_EMBEDDED_COMPILER
    static constexpr bool compilable = false; /// special type handling, some other time
#endif
};

struct NameBitHammingDistance
{
    static constexpr auto name = "bitHammingDistance";
};
using FunctionBitHammingDistance = BinaryArithmeticOverloadResolver<BitHammingDistanceImpl, NameBitHammingDistance>;

REGISTER_FUNCTION(BitHammingDistance)
{
    factory.registerFunction<FunctionBitHammingDistance>();
}
}