15 typedef unsigned __int32 uint32_t;
16 typedef unsigned __int64 uint64_t;
34 #undef PLATFORM_64_BIT
35 #undef PLATFORM_32_BIT
36 #if __amd64__ || __x86_64__ || _WIN64 || _M_X64
37 #define PLATFORM_64_BIT
39 #define PLATFORM_32_BIT
44 template<
typename TBitset>
47 typedef TBitset ElementType;
48 typedef size_t ResultType;
51 template<
typename Iterator1,
typename Iterator2>
52 inline ResultType operator()(Iterator1 a, Iterator2 b,
size_t size)
const
54 return (*a ^ *b).count();
60 static const unsigned char pop_count_LUT[256] = {
61 #define ALICEVISION_B2(n) n, n + 1, n + 1, n + 2
62 #define ALICEVISION_B4(n) ALICEVISION_B2(n), ALICEVISION_B2(n + 1), ALICEVISION_B2(n + 1), ALICEVISION_B2(n + 2)
63 #define ALICEVISION_B6(n) ALICEVISION_B4(n), ALICEVISION_B4(n + 1), ALICEVISION_B4(n + 1), ALICEVISION_B4(n + 2)
79 typedef T ElementType;
80 typedef unsigned int ResultType;
84 static inline unsigned int popcnt32(uint32_t n)
89 #if (defined __GNUC__ || defined __clang__)
90 return __builtin_popcountl(n);
92 n -= ((n >> 1) & 0x55555555);
93 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
94 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
98 static inline unsigned int popcnt64(uint64_t n)
100 #if defined _MSC_VER && defined PLATFORM_64_BIT
101 return __popcnt64(n);
103 #if (defined __GNUC__ || defined __clang__)
104 return __builtin_popcountll(n);
106 n -= ((n >> 1) & 0x5555555555555555LL);
107 n = (n & 0x3333333333333333LL) + ((n >> 2) & 0x3333333333333333LL);
108 return (((n + (n >> 4)) & 0x0f0f0f0f0f0f0f0fLL) * 0x0101010101010101LL) >> 56;
113 template<
typename Iterator1,
typename Iterator2>
114 inline ResultType operator()(Iterator1 a, Iterator2 b,
size_t size)
const
116 ResultType result = 0;
119 #ifdef PLATFORM_64_BIT
120 if (size %
sizeof(uint64_t) == 0)
122 const uint64_t* pa =
reinterpret_cast<const uint64_t*
>(a);
123 const uint64_t* pb =
reinterpret_cast<const uint64_t*
>(b);
124 size /= (
sizeof(uint64_t) /
sizeof(
unsigned char));
125 for (
size_t i = 0; i < size; ++i, ++pa, ++pb)
127 result += popcnt64(*pa ^ *pb);
130 else if (size %
sizeof(uint32_t) == 0)
132 const uint32_t* pa =
reinterpret_cast<const uint32_t*
>(a);
133 const uint32_t* pb =
reinterpret_cast<const uint32_t*
>(b);
134 size /= (
sizeof(uint32_t) /
sizeof(
unsigned char));
135 for (
size_t i = 0; i < size; ++i, ++pa, ++pb)
142 const ElementType* a2 =
reinterpret_cast<const ElementType*
>(a);
143 const ElementType* b2 =
reinterpret_cast<const ElementType*
>(b);
144 for (
size_t i = 0; i < size / (
sizeof(
unsigned char)); ++i)
146 result += pop_count_LUT[a2[i] ^ b2[i]];
149 #else // PLATFORM_64_BIT
150 if (size %
sizeof(uint32_t) == 0)
152 const uint32_t* pa =
reinterpret_cast<const uint32_t*
>(a);
153 const uint32_t* pb =
reinterpret_cast<const uint32_t*
>(b);
154 size /= (
sizeof(uint32_t) /
sizeof(
unsigned char));
155 for (
size_t i = 0; i < size; ++i, ++pa, ++pb)
162 const ElementType* a2 =
reinterpret_cast<const ElementType*
>(a);
163 const ElementType* b2 =
reinterpret_cast<const ElementType*
>(b);
164 for (
size_t i = 0; i < size / (
sizeof(
unsigned char)); ++i)
166 result += pop_count_LUT[a2[i] ^ b2[i]];
169 #endif // PLATFORM_64_BIT
178 template<
typename Iterator1,
typename Iterator2>
179 inline double operator()(Iterator1 a, Iterator2 b,
size_t size)
const
182 typename Hamming<T>::ResultType h = metric(a, b, size);