contrib/faiss/faiss/utils/hamming_distance/common.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef FAISS_hamming_common_h |
9 | | #define FAISS_hamming_common_h |
10 | | |
11 | | #include <cstdint> |
12 | | |
13 | | #include <faiss/impl/platform_macros.h> |
14 | | |
15 | | /* The Hamming distance type */ |
16 | | using hamdis_t = int32_t; |
17 | | |
18 | | namespace faiss { |
19 | | |
20 | | // trust the compiler to provide efficient popcount implementations |
21 | 0 | inline int popcount32(uint32_t x) { |
22 | 0 | return __builtin_popcount(x); |
23 | 0 | } |
24 | | |
25 | | // popcount |
26 | 0 | inline int popcount64(uint64_t x) { |
27 | 0 | return __builtin_popcountl(x); |
28 | 0 | } |
29 | | |
30 | | // This table was moved from .cpp to .h file, because |
31 | | // otherwise it was causing compilation errors while trying to |
32 | | // compile swig modules on Windows. |
33 | | // todo for C++17: switch to 'inline constexpr' |
34 | | static constexpr uint8_t hamdis_tab_ham_bytes[256] = { |
35 | | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, |
36 | | 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
37 | | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, |
38 | | 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
39 | | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, |
40 | | 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
41 | | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, |
42 | | 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
43 | | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, |
44 | | 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
45 | | 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}; |
46 | | |
47 | | } // namespace faiss |
48 | | |
49 | | #endif |