functions: vectorize bitHammingDistance

This commit is contained in:
ricky 2024-10-18 18:01:42 +08:00
parent 7fd2207626
commit 03534fa386
2 changed files with 34 additions and 9 deletions

View File

@ -211,10 +211,10 @@ if (ARCH_S390X)
add_contrib(crc32-s390x-cmake crc32-s390x) add_contrib(crc32-s390x-cmake crc32-s390x)
endif() endif()
add_contrib (SimSIMD-cmake SimSIMD)
option(ENABLE_USEARCH "Enable USearch" ${ENABLE_LIBRARIES}) option(ENABLE_USEARCH "Enable USearch" ${ENABLE_LIBRARIES})
if (ENABLE_USEARCH) if (ENABLE_USEARCH)
add_contrib (FP16-cmake FP16) add_contrib (FP16-cmake FP16)
add_contrib (SimSIMD-cmake SimSIMD)
add_contrib (usearch-cmake usearch) # requires: FP16, SimdSIMD add_contrib (usearch-cmake usearch) # requires: FP16, SimdSIMD
else () else ()
message(STATUS "Not using USearch") message(STATUS "Not using USearch")

View File

@ -1,6 +1,8 @@
#include <bit>
#include <Functions/FunctionBinaryArithmetic.h> #include <Functions/FunctionBinaryArithmetic.h>
#include <Functions/FunctionFactory.h> #include <Functions/FunctionFactory.h>
#include <bit> #pragma clang diagnostic ignored "-Wused-but-marked-unused"
#include <simsimd/simsimd.h>
namespace DB namespace DB
@ -25,11 +27,33 @@ struct BitHammingDistanceImpl
/// This behavior can change in the future. /// This behavior can change in the future.
if constexpr (sizeof(A) <= sizeof(UInt64) && sizeof(B) <= sizeof(UInt64)) if constexpr (sizeof(A) <= sizeof(UInt64) && sizeof(B) <= sizeof(UInt64))
{
if constexpr (std::is_same_v<A, B>)
{
simsimd_distance_t distance;
simsimd_hamming_b8(
reinterpret_cast<const simsimd_b8_t *>(&a), reinterpret_cast<const simsimd_b8_t *>(&b), sizeof(A), &distance);
return static_cast<UInt64>(distance);
}
else
{ {
UInt64 res = static_cast<UInt64>(a) ^ static_cast<UInt64>(b); UInt64 res = static_cast<UInt64>(a) ^ static_cast<UInt64>(b);
return std::popcount(res); return std::popcount(res);
} }
}
else if constexpr (is_big_int_v<A> && is_big_int_v<B>) else if constexpr (is_big_int_v<A> && is_big_int_v<B>)
{
if constexpr (std::is_same_v<A, B>)
{
simsimd_distance_t distance;
simsimd_hamming_b8(
reinterpret_cast<const simsimd_b8_t *>(&a.items),
reinterpret_cast<const simsimd_b8_t *>(&b.items),
sizeof(a.items),
&distance);
return static_cast<ResultType>(distance);
}
else
{ {
auto xored = a ^ b; auto xored = a ^ b;
@ -38,6 +62,7 @@ struct BitHammingDistanceImpl
res += std::popcount(item); res += std::popcount(item);
return res; return res;
} }
}
else else
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Unsupported data type combination in function 'bitHammingDistance'"); throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Unsupported data type combination in function 'bitHammingDistance'");
} }