Commit 3ac48de1 authored by Vincent Rabaud's avatar Vincent Rabaud

- speed up the Hamming distance

parent cbdc9188
...@@ -138,27 +138,24 @@ Hamming::ResultType Hamming::operator()(const unsigned char* a, const unsigned c ...@@ -138,27 +138,24 @@ Hamming::ResultType Hamming::operator()(const unsigned char* a, const unsigned c
} }
else else
#endif #endif
//for portability just use unsigned long -- and use the __builtin_popcountl (see docs for __builtin_popcountl) //for portability just use unsigned long -- and use the __builtin_popcountll (see docs for __builtin_popcountll)
//as opposed to size_t -- TODO smart switching, if the size_t is a 64bits, use __builtin_popcountll typedef unsigned long long pop_t;
typedef unsigned long pop_t;
size_t i;
const size_t modulo = size % sizeof(pop_t); const size_t modulo = size % sizeof(pop_t);
const size_t end = size - modulo; const pop_t * a2 = reinterpret_cast<const pop_t*> (a);
for (i = 0; i < end; i += sizeof(pop_t)) const pop_t * b2 = reinterpret_cast<const pop_t*> (b);
{ const pop_t * a2_end = a2 + (size/sizeof(pop_t));
pop_t a2 = *reinterpret_cast<const pop_t*> (a + i);
pop_t b2 = *reinterpret_cast<const pop_t*> (b + i); for (; a2 != a2_end; ++a2, ++b2)
result += __builtin_popcountl(a2 ^ b2); result += __builtin_popcountll((*a2) ^ (*b2));
}
if (modulo) if (modulo)
{ {
//in the case where size is not divisible by sizeof(size_t) //in the case where size is not divisible by sizeof(size_t)
//need to mask of the bits at the end //need to mask off the bits at the end
pop_t a2=0,b2=0; pop_t a_final=0,b_final=0;
memcpy(&a2,a+end,modulo); memcpy(&a_final,a2,modulo);
memcpy(&b2,b+end,modulo); memcpy(&b_final,b2,modulo);
//std::cout << std::hex << (a2^b2) << std::endl; result += __builtin_popcountll(a_final ^ b_final);
result += __builtin_popcountl(a2 ^ b2);
} }
return result; return result;
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment