Skip to content

Commit

Permalink
Replace __builtin_popcount with std::bitset<>::count (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwal authored Jul 19, 2023
1 parent 460762c commit fb28b34
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pdq/cpp/common/pdqhamming.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
#ifndef PDQHAMMING_H
#define PDQHAMMING_H

#include <bitset>

#include <pdq/cpp/common/pdqbasetypes.h>

// OLD NOTICE:
// If your compiler doesn't support __builtin_popcount then feel free to
// undefine this. (Experiments have shown that using builtin popcount helps
// performance by a few percent -- worth using but OK to live without.)
#if !defined(_MSC_VER) && !defined(WIN32) && !defined(_WIN32) && \
!defined(__WIN32__) && !defined(WIN64) && !defined(_WIN64) && \
!defined(__WIN64__)
//
// NOTICE:
// __builin_popcount was replaced by std::bitset<>::count()
// The define is left here for backwards compatibility
// if you want to use the lookup table.
#define USE_BUILTIN_POPCOUNT
#endif

namespace facebook {
namespace pdq {
Expand All @@ -23,16 +27,16 @@ namespace hashing {
#ifdef USE_BUILTIN_POPCOUNT
// Inlined right here
inline int hammingNorm8(Hash8 h) {
return __builtin_popcount((unsigned)h);
return std::bitset<8>(h).count();
}
inline int hammingNorm16(Hash16 h) {
return __builtin_popcount((unsigned)h);
return std::bitset<16>(h).count();
}
inline int hammingDistance8(Hash8 a, Hash8 b) {
return __builtin_popcount((unsigned)(a ^ b));
return std::bitset<8>(a ^ b).count();
}
inline int hammingDistance16(Hash16 a, Hash16 b) {
return __builtin_popcount((unsigned)(a ^ b));
return std::bitset<16>(a ^ b).count();
}
#else
// Implemented in pdqhamming.cpp with lookup tables.
Expand Down

0 comments on commit fb28b34

Please sign in to comment.