fix: resolve compiler warnings in distance and hamming utilities (#4937)

Summary:
- Fix compiler warnings in distance computation, hamming distance, heap, partitioning, and quantize_lut utilities
- Fixes include `-Wshadow`, `-Wunused-parameter`, `-Wformat`, and signed/unsigned comparisons

All changes are mechanical. No functional changes.

Part 6/13 of the compiler warnings cleanup (split from https://github.com/facebookresearch/faiss/issues/4810 per maintainer request).

Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4937

Test Plan:
- [x] No functional changes — all fixes are mechanical renames and annotations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

Reviewed By: mnorris11

Differential Revision: D96996091

Pulled By: alibeklfc

fbshipit-source-id: 50073b6dd9cf7ed555df8b367a729d2dfbe3b003
This commit is contained in:
Max Buckley
2026-03-19 11:44:46 -07:00
committed by meta-codesync[bot]
co-authored by Claude Opus 4.6
parent 645a742b51
commit 62a1969098
11 changed files with 177 additions and 151 deletions
+10 -10
View File
@@ -17,7 +17,7 @@ namespace faiss {
template <typename C>
void HeapArray<C>::heapify() {
#pragma omp parallel for
for (int64_t j = 0; j < nh; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(nh); j++) {
heap_heapify<C>(k, val + j * k, ids + j * k);
}
}
@@ -25,7 +25,7 @@ void HeapArray<C>::heapify() {
template <typename C>
void HeapArray<C>::reorder() {
#pragma omp parallel for
for (int64_t j = 0; j < nh; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(nh); j++) {
heap_reorder<C>(k, val + j * k, ids + j * k);
}
}
@@ -37,7 +37,7 @@ void HeapArray<C>::addn(size_t nj, const T* vin, TI j0, size_t i0, int64_t ni) {
}
assert(i0 >= 0 && i0 + ni <= nh);
#pragma omp parallel for if (ni * nj > 100000)
for (int64_t i = i0; i < i0 + ni; i++) {
for (int64_t i = i0; i < static_cast<int64_t>(i0) + ni; i++) {
T* __restrict simi = get_val(i);
TI* __restrict idxi = get_ids(i);
const T* ip_line = vin + (i - i0) * nj;
@@ -68,7 +68,7 @@ void HeapArray<C>::addn_with_ids(
}
assert(i0 >= 0 && i0 + ni <= nh);
#pragma omp parallel for if (ni * nj > 100000)
for (int64_t i = i0; i < i0 + ni; i++) {
for (int64_t i = i0; i < static_cast<int64_t>(i0) + ni; i++) {
T* __restrict simi = get_val(i);
TI* __restrict idxi = get_ids(i);
const T* ip_line = vin + (i - i0) * nj;
@@ -96,7 +96,7 @@ void HeapArray<C>::addn_query_subset_with_ids(
id_stride = nj;
}
#pragma omp parallel for if (nsubset * nj > 100000)
for (int64_t si = 0; si < nsubset; si++) {
for (int64_t si = 0; si < static_cast<int64_t>(nsubset); si++) {
TI i = subset[si];
T* __restrict simi = get_val(i);
TI* __restrict idxi = get_ids(i);
@@ -115,7 +115,7 @@ void HeapArray<C>::addn_query_subset_with_ids(
template <typename C>
void HeapArray<C>::per_line_extrema(T* out_val, TI* out_ids) const {
#pragma omp parallel for if (nh * k > 100000)
for (int64_t j = 0; j < nh; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(nh); j++) {
int64_t imin = -1;
typename C::T xval = C::Crev::neutral();
const typename C::T* x_ = val + j * k;
@@ -187,7 +187,7 @@ void merge_knn_results(
std::vector<distance_t> buf2(nshard);
distance_t* heap_vals = buf2.data();
#pragma omp for
for (long i = 0; i < n; i++) {
for (long i = 0; i < static_cast<long>(n); i++) {
// the heap maps values to the shard where they are
// produced.
const distance_t* D_in = all_distances + i * k;
@@ -195,7 +195,7 @@ void merge_knn_results(
int heap_size = 0;
// push the first element of each shard (if not -1)
for (long s = 0; s < nshard; s++) {
for (long s = 0; s < static_cast<long>(nshard); s++) {
pointer[s] = 0;
if (I_in[stride * s] >= 0) {
heap_push<C>(
@@ -210,7 +210,7 @@ void merge_knn_results(
distance_t* D = distances + i * k;
idx_t* I = labels + i * k;
int j;
size_t j;
for (j = 0; j < k && heap_size > 0; j++) {
// pop element from best shard
int s = shard_ids[0]; // top of heap
@@ -221,7 +221,7 @@ void merge_knn_results(
// pop from shard, advance pointer for this shard
heap_pop<C>(heap_size--, heap_vals, shard_ids);
p++;
if (p < k && I_in[stride * s + p] >= 0) {
if (static_cast<size_t>(p) < k && I_in[stride * s + p] >= 0) {
heap_push<C>(
++heap_size,
heap_vals,
+23 -21
View File
@@ -207,7 +207,7 @@ void fvec_norms_L2(
size_t d,
size_t nx) {
#pragma omp parallel for if (nx > 10000)
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
nr[i] = sqrtf(fvec_norm_L2sqr_dispatch(x + i * d, d));
}
}
@@ -218,7 +218,7 @@ void fvec_norms_L2sqr(
size_t d,
size_t nx) {
#pragma omp parallel for if (nx > 10000)
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
nr[i] = fvec_norm_L2sqr_dispatch(x + i * d, d);
}
}
@@ -247,14 +247,14 @@ void fvec_norms_L2sqr(
}
void fvec_renorm_L2_noomp(size_t d, size_t nx, float* __restrict x) {
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
FVEC_RENORM_L2_IMPL
}
}
void fvec_renorm_L2_omp(size_t d, size_t nx, float* __restrict x) {
#pragma omp parallel for if (nx > 10000)
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
FVEC_RENORM_L2_IMPL
}
}
@@ -290,7 +290,7 @@ void exhaustive_inner_product_seq(
{
SingleResultHandler resi(res);
#pragma omp for
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
const float* x_i = x + i * d;
const float* y_j = y;
@@ -324,7 +324,7 @@ void exhaustive_L2sqr_seq(
{
SingleResultHandler resi(res);
#pragma omp for
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
const float* x_i = x + i * d;
const float* y_j = y;
resi.begin(i);
@@ -462,7 +462,7 @@ void exhaustive_L2sqr_blas_default_impl(
ip_block.get(),
&nyi);
}
for (int64_t i = i0; i < i1; i++) {
for (size_t i = i0; i < i1; i++) {
float* ip_line = ip_block.get() + (i - i0) * (j1 - j0);
for (size_t j = j0; j < j1; j++) {
@@ -497,7 +497,7 @@ void exhaustive_L2sqr_blas(
size_t nx,
size_t ny,
BlockResultHandler& res,
const float* y_norms = nullptr) {
const float* /* y_norms */ = nullptr) {
exhaustive_L2sqr_blas_default_impl(x, y, d, nx, ny, res);
}
@@ -563,7 +563,7 @@ void exhaustive_L2sqr_blas_cmax_avx2(
ip_block.get(),
&nyi);
}
for (int64_t i = i0; i < i1; i++) {
for (size_t i = i0; i < i1; i++) {
float* ip_line = ip_block.get() + (i - i0) * (j1 - j0);
_mm_prefetch((const char*)ip_line, _MM_HINT_NTA);
@@ -772,7 +772,7 @@ void exhaustive_L2sqr_blas_cmax_sve(
ip_block.get(),
&nyi);
}
for (int64_t i = i0; i < i1; i++) {
for (size_t i = i0; i < i1; i++) {
const size_t count = j1 - j0;
float* ip_line = ip_block.get() + (i - i0) * count;
@@ -947,7 +947,8 @@ struct Run_search_inner_product {
size_t d,
size_t nx,
size_t ny) {
if (res.sel || nx < distance_compute_blas_threshold) {
if (res.sel ||
nx < static_cast<size_t>(distance_compute_blas_threshold)) {
exhaustive_inner_product_seq(x, y, d, nx, ny, res);
} else {
exhaustive_inner_product_blas(x, y, d, nx, ny, res);
@@ -965,7 +966,8 @@ struct Run_search_L2sqr {
size_t nx,
size_t ny,
const float* y_norm2) {
if (res.sel || nx < distance_compute_blas_threshold) {
if (res.sel ||
nx < static_cast<size_t>(distance_compute_blas_threshold)) {
exhaustive_L2sqr_seq(x, y, d, nx, ny, res);
} else {
exhaustive_L2sqr_blas(x, y, d, nx, ny, res, y_norm2);
@@ -1131,7 +1133,7 @@ void fvec_inner_products_by_idx(
size_t nx,
size_t ny) {
#pragma omp parallel for
for (int64_t j = 0; j < nx; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(nx); j++) {
const int64_t* __restrict idsj = ids + j * ny;
const float* xj = x + j * d;
float* __restrict ipj = ip + j * ny;
@@ -1156,7 +1158,7 @@ void fvec_L2sqr_by_idx(
size_t nx,
size_t ny) {
#pragma omp parallel for
for (int64_t j = 0; j < nx; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(nx); j++) {
const int64_t* __restrict idsj = ids + j * ny;
const float* xj = x + j * d;
float* __restrict disj = dis + j * ny;
@@ -1179,7 +1181,7 @@ void pairwise_indexed_L2sqr(
const int64_t* iy,
float* dis) {
#pragma omp parallel for if (n > 1)
for (int64_t j = 0; j < n; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(n); j++) {
if (ix[j] >= 0 && iy[j] >= 0) {
dis[j] = fvec_L2sqr_dispatch(x + d * ix[j], y + d * iy[j], d);
} else {
@@ -1197,7 +1199,7 @@ void pairwise_indexed_inner_product(
const int64_t* iy,
float* dis) {
#pragma omp parallel for if (n > 1)
for (int64_t j = 0; j < n; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(n); j++) {
if (ix[j] >= 0 && iy[j] >= 0) {
dis[j] = fvec_inner_product_dispatch(
x + d * ix[j], y + d * iy[j], d);
@@ -1226,7 +1228,7 @@ void knn_inner_products_by_idx(
}
#pragma omp parallel for if (nx > 100)
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
const float* x_ = x + i * d;
const int64_t* idsi = ids + i * ld_ids;
size_t j;
@@ -1235,7 +1237,7 @@ void knn_inner_products_by_idx(
minheap_heapify(k, simi, idxi);
for (j = 0; j < nsubset; j++) {
if (idsi[j] < 0 || idsi[j] >= ny) {
if (idsi[j] < 0 || static_cast<size_t>(idsi[j]) >= ny) {
break;
}
float ip = fvec_inner_product_dispatch(x_, y + d * idsi[j], d);
@@ -1264,14 +1266,14 @@ void knn_L2sqr_by_idx(
ld_ids = ny;
}
#pragma omp parallel for if (nx > 100)
for (int64_t i = 0; i < nx; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
const float* x_ = x + i * d;
const int64_t* __restrict idsi = ids + i * ld_ids;
float* __restrict simi = res_vals + i * k;
int64_t* __restrict idxi = res_ids + i * k;
maxheap_heapify(k, simi, idxi);
for (size_t j = 0; j < nsubset; j++) {
if (idsi[j] < 0 || idsi[j] >= ny) {
if (idsi[j] < 0 || static_cast<size_t>(idsi[j]) >= ny) {
break;
}
float disij = fvec_L2sqr_dispatch(x_, y + d * idsi[j], d);
@@ -1357,7 +1359,7 @@ void inner_product_to_L2sqr(
size_t n1,
size_t n2) {
#pragma omp parallel for
for (int64_t j = 0; j < n1; j++) {
for (int64_t j = 0; j < static_cast<int64_t>(n1); j++) {
float* disj = dis + j * n2;
for (size_t i = 0; i < n2; i++) {
disj[i] = nr1[j] + nr2[i] - 2 * disj[i];
@@ -35,6 +35,11 @@ bool exhaustive_L2sqr_fused_cmax(
return exhaustive_L2sqr_fused_cmax_simdlib(x, y, d, nx, ny, res, y_norms);
#else
// not supported, please use a general-purpose kernel
(void)x;
(void)y;
(void)d;
(void)res;
(void)y_norms;
return false;
#endif
}
@@ -260,7 +260,8 @@ void exhaustive_L2sqr_fused_cmax(
const size_t nx_p = (nx / NX_POINTS_PER_LOOP) * NX_POINTS_PER_LOOP;
// the main loop.
#pragma omp parallel for schedule(dynamic)
for (int64_t i = 0; i < nx_p; i += NX_POINTS_PER_LOOP) {
for (int64_t i = 0; i < static_cast<int64_t>(nx_p);
i += NX_POINTS_PER_LOOP) {
kernel<DIM, NX_POINTS_PER_LOOP, NY_POINTS_PER_LOOP>(
x, y, y_transposed.data(), ny, res, y_norms, i);
}
+8 -8
View File
@@ -41,14 +41,14 @@ struct ExtraDistanceComputer : FlatCodesDistanceComputer {
}
ExtraDistanceComputer(
const VD& vd,
const VD& vd_in,
const float* xb,
size_t nb,
const float* q = nullptr)
: FlatCodesDistanceComputer((uint8_t*)xb, vd.d * sizeof(float)),
vd(vd),
nb(nb),
q(q),
size_t nb_in,
const float* q_in = nullptr)
: FlatCodesDistanceComputer((uint8_t*)xb, vd_in.d * sizeof(float)),
vd(vd_in),
nb(nb_in),
q(q_in),
b(xb) {}
void set_query(const float* x) override {
@@ -119,7 +119,7 @@ void knn_extra_metrics(
size_t i1 = std::min(i0 + check_period, nx);
#pragma omp parallel for
for (int64_t i = i0; i < i1; i++) {
for (int64_t i = i0; i < static_cast<int64_t>(i1); i++) {
const float* x_i = x + i * d;
const float* y_j = y;
size_t j;
+13 -11
View File
@@ -10,9 +10,9 @@
namespace faiss {
// BitstringWriter and BitstringReader functions
inline BitstringWriter::BitstringWriter(uint8_t* code, size_t code_size)
: code(code), code_size(code_size), i(0) {
memset(code, 0, code_size);
inline BitstringWriter::BitstringWriter(uint8_t* code_in, size_t code_size_in)
: code(code_in), code_size(code_size_in), i(0) {
memset(code_in, 0, code_size_in);
}
inline void BitstringWriter::write(uint64_t x, int nbit) {
@@ -36,8 +36,10 @@ inline void BitstringWriter::write(uint64_t x, int nbit) {
}
}
inline BitstringReader::BitstringReader(const uint8_t* code, size_t code_size)
: code(code), code_size(code_size), i(0) {}
inline BitstringReader::BitstringReader(
const uint8_t* code_in,
size_t code_size_in)
: code(code_in), code_size(code_size_in), i(0) {}
inline uint64_t BitstringReader::read(int nbit) {
assert(code_size * 8 >= nbit + i);
@@ -85,18 +87,18 @@ struct HCounterState {
int k;
HCounterState(
int* counters,
int64_t* ids_per_dis,
int* counters_in,
int64_t* ids_per_dis_in,
const uint8_t* x,
int d,
int k)
: counters(counters),
ids_per_dis(ids_per_dis),
int k_in)
: counters(counters_in),
ids_per_dis(ids_per_dis_in),
hc(x, d / 8),
thres(d + 1),
count_lt(0),
count_eq(0),
k(k) {}
k(k_in) {}
void update_counter(const uint8_t* y, size_t j) {
int32_t dis = hc.hamming(y);
+23 -23
View File
@@ -187,7 +187,7 @@ void hammings_knn_hc(
for (size_t j0 = 0; j0 < n2; j0 += block_size) {
const size_t j1 = std::min(j0 + block_size, n2);
#pragma omp parallel for
for (int64_t i = 0; i < ha->nh; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(ha->nh); i++) {
HammingComputer hc(bs1 + i * bytes_per_code, bytes_per_code);
const uint8_t* __restrict bs2_ = bs2 + j0 * bytes_per_code;
@@ -270,7 +270,7 @@ void hammings_knn_mc(
for (size_t j0 = 0; j0 < nb; j0 += block_size) {
const size_t j1 = std::min(j0 + block_size, nb);
#pragma omp parallel for
for (int64_t i = 0; i < na; ++i) {
for (int64_t i = 0; i < static_cast<int64_t>(na); ++i) {
for (size_t j = j0; j < j1; ++j) {
if (!sel || sel->is_member(j)) {
cs[i].update_counter(b + j * bytes_per_code, j);
@@ -282,7 +282,7 @@ void hammings_knn_mc(
for (size_t i = 0; i < na; ++i) {
HCounterState<HammingComputer>& csi = cs[i];
int nres = 0;
size_t nres = 0;
for (int b_2 = 0; b_2 < nBuckets && nres < k; b_2++) {
for (int l = 0; l < csi.counters[b_2] && nres < k; l++) {
labels[i * k + nres] = csi.ids_per_dis[b_2 * k + l];
@@ -313,7 +313,7 @@ void hamming_range_search(
RangeSearchPartialResult pres(res);
#pragma omp for
for (int64_t i = 0; i < na; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(na); i++) {
HammingComputer hc(a + i * code_size, code_size);
const uint8_t* yi = b;
RangeQueryResult& qres = pres.new_result(i);
@@ -366,11 +366,11 @@ struct Run_hamming_range_search {
* equivalently to the lsb of the first byte that is stored.
*/
void fvec2bitvec(const float* __restrict x, uint8_t* __restrict b, size_t d) {
for (int i = 0; i < d; i += 8) {
for (size_t i = 0; i < d; i += 8) {
uint8_t w = 0;
uint8_t mask = 1;
int nj = i + 8 <= d ? 8 : d - i;
for (int j = 0; j < nj; j++) {
size_t nj = i + 8 <= d ? 8 : d - i;
for (size_t j = 0; j < nj; j++) {
if (x[i + j] >= 0) {
w |= mask;
}
@@ -390,7 +390,7 @@ void fvecs2bitvecs(
size_t n) {
const int64_t ncodes = ((d + 7) / 8);
#pragma omp parallel for if (n > 100000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
fvec2bitvec(x + i * d, b + i * ncodes, d);
}
}
@@ -402,7 +402,7 @@ void bitvecs2fvecs(
size_t n) {
const int64_t ncodes = ((d + 7) / 8);
#pragma omp parallel for if (n > 100000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
binary_to_real(d, b + i * ncodes, x + i * d);
}
}
@@ -441,13 +441,13 @@ void bitvec_shuffle(
const uint8_t* __restrict a,
uint8_t* __restrict b) {
for (size_t i = 0; i < db; i++) {
FAISS_THROW_IF_NOT(order[i] >= 0 && order[i] < da);
FAISS_THROW_IF_NOT(order[i] >= 0 && static_cast<size_t>(order[i]) < da);
}
size_t lda = (da + 7) / 8;
size_t ldb = (db + 7) / 8;
#pragma omp parallel for if (n > 10000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
const uint8_t* ai = a + i * lda;
uint8_t* bi = b + i * ldb;
memset(bi, 0, ldb);
@@ -722,11 +722,11 @@ void pack_bitstrings(
size_t code_size) {
FAISS_THROW_IF_NOT(code_size >= (M * nbit + 7) / 8);
#pragma omp parallel for if (n > 1000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
const int32_t* in = unpacked + i * M;
uint8_t* out = packed + i * code_size;
BitstringWriter wr(out, code_size);
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
wr.write(in[j], nbit);
}
}
@@ -740,16 +740,16 @@ void pack_bitstrings(
uint8_t* packed,
size_t code_size) {
int totbit = 0;
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
totbit += nbit[j];
}
FAISS_THROW_IF_NOT(code_size >= (totbit + 7) / 8);
FAISS_THROW_IF_NOT(code_size >= static_cast<size_t>((totbit + 7) / 8));
#pragma omp parallel for if (n > 1000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
const int32_t* in = unpacked + i * M;
uint8_t* out = packed + i * code_size;
BitstringWriter wr(out, code_size);
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
wr.write(in[j], nbit[j]);
}
}
@@ -764,11 +764,11 @@ void unpack_bitstrings(
int32_t* unpacked) {
FAISS_THROW_IF_NOT(code_size >= (M * nbit + 7) / 8);
#pragma omp parallel for if (n > 1000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
const uint8_t* in = packed + i * code_size;
int32_t* out = unpacked + i * M;
BitstringReader rd(in, code_size);
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
out[j] = rd.read(nbit);
}
}
@@ -782,16 +782,16 @@ void unpack_bitstrings(
size_t code_size,
int32_t* unpacked) {
int totbit = 0;
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
totbit += nbit[j];
}
FAISS_THROW_IF_NOT(code_size >= (totbit + 7) / 8);
FAISS_THROW_IF_NOT(code_size >= static_cast<size_t>((totbit + 7) / 8));
#pragma omp parallel for if (n > 1000)
for (int64_t i = 0; i < n; i++) {
for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
const uint8_t* in = packed + i * code_size;
int32_t* out = unpacked + i * M;
BitstringReader rd(in, code_size);
for (int j = 0; j < M; j++) {
for (size_t j = 0; j < M; j++) {
out[j] = rd.read(nbit[j]);
}
}
+11 -2
View File
@@ -92,6 +92,7 @@ struct HammingComputer4 {
void set(const uint8_t* a, int code_size) {
assert(code_size == 4);
(void)code_size;
a0 = *(uint32_t*)a;
}
@@ -115,6 +116,7 @@ struct HammingComputer8 {
void set(const uint8_t* a, int code_size) {
assert(code_size == 8);
(void)code_size;
a0 = *(uint64_t*)a;
}
@@ -138,6 +140,7 @@ struct HammingComputer16 {
void set(const uint8_t* a8, int code_size) {
assert(code_size == 16);
(void)code_size;
const uint64_t* a = (uint64_t*)a8;
a0 = a[0];
a1 = a[1];
@@ -167,6 +170,7 @@ struct HammingComputer20 {
void set(const uint8_t* a8, int code_size) {
assert(code_size == 20);
(void)code_size;
const uint64_t* a = (uint64_t*)a8;
a0 = a[0];
a1 = a[1];
@@ -195,6 +199,7 @@ struct HammingComputer32 {
void set(const uint8_t* a8, int code_size) {
assert(code_size == 32);
(void)code_size;
const uint64_t* a = (uint64_t*)a8;
a0 = a[0];
a1 = a[1];
@@ -224,6 +229,7 @@ struct HammingComputer64 {
void set(const uint8_t* a8, int code_size) {
assert(code_size == 64);
(void)code_size;
const uint64_t* a = (uint64_t*)a8;
a0 = a[0];
a1 = a[1];
@@ -255,8 +261,8 @@ struct HammingComputerDefault {
HammingComputerDefault() {}
HammingComputerDefault(const uint8_t* a8, int code_size) {
set(a8, code_size);
HammingComputerDefault(const uint8_t* a8_in, int code_size) {
set(a8_in, code_size);
}
void set(const uint8_t* a8_2, int code_size) {
@@ -363,6 +369,7 @@ struct GenHammingComputer8 {
GenHammingComputer8(const uint8_t* a, int code_size) {
assert(code_size == 8);
(void)code_size;
a0 = *(uint64_t*)a;
}
@@ -382,6 +389,7 @@ struct GenHammingComputer16 {
GenHammingComputer16(const uint8_t* a8, int code_size) {
assert(code_size == 16);
(void)code_size;
a = _mm_loadu_si128((const __m128i_u*)a8);
}
@@ -402,6 +410,7 @@ struct GenHammingComputer32 {
GenHammingComputer32(const uint8_t* a8, int code_size) {
assert(code_size == 32);
(void)code_size;
a = _mm256_loadu_si256((const __m256i_u*)a8);
}
+44 -37
View File
@@ -88,13 +88,15 @@ struct HammingComputer4 {
set(a, code_size);
}
void set(const uint8_t* a, int code_size) {
void set(const uint8_t* a, [[maybe_unused]] int code_size) {
assert(code_size == 4);
a0 = *(uint32_t*)a;
const uint32_t* a32 = reinterpret_cast<const uint32_t*>(a);
a0 = *a32;
}
inline int hamming(const uint8_t* b) const {
return popcount64(*(uint32_t*)b ^ a0);
const uint32_t* b32 = reinterpret_cast<const uint32_t*>(b);
return popcount64(*b32 ^ a0);
}
inline static constexpr int get_code_size() {
@@ -111,13 +113,15 @@ struct HammingComputer8 {
set(a, code_size);
}
void set(const uint8_t* a, int code_size) {
void set(const uint8_t* a, [[maybe_unused]] int code_size) {
assert(code_size == 8);
a0 = *(uint64_t*)a;
const uint64_t* a64 = reinterpret_cast<const uint64_t*>(a);
a0 = *a64;
}
inline int hamming(const uint8_t* b) const {
return popcount64(*(uint64_t*)b ^ a0);
const uint64_t* b64 = reinterpret_cast<const uint64_t*>(b);
return popcount64(*b64 ^ a0);
}
inline static constexpr int get_code_size() {
@@ -134,15 +138,15 @@ struct HammingComputer16 {
set(a8, code_size);
}
void set(const uint8_t* a8, int code_size) {
void set(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 16);
const uint64_t* a = (uint64_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
a0 = a[0];
a1 = a[1];
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
return popcount64(b[0] ^ a0) + popcount64(b[1] ^ a1);
}
@@ -163,21 +167,22 @@ struct HammingComputer20 {
set(a8, code_size);
}
void set(const uint8_t* a8, int code_size) {
void set(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 20);
const uint64_t* a = (uint64_t*)a8;
const uint32_t* b = (uint32_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
const uint32_t* a32 = reinterpret_cast<const uint32_t*>(a8);
a0 = a[0];
a1 = a[1];
// can't read a[2] since it is uint64_t, not uint32_t
// results in AddressSanitizer failure reading past end of array
a2 = b[4];
a2 = a32[4];
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
const uint32_t* b32_tail = reinterpret_cast<const uint32_t*>(b + 2);
return popcount64(b[0] ^ a0) + popcount64(b[1] ^ a1) +
popcount64(*(uint32_t*)(b + 2) ^ a2);
popcount64(*b32_tail ^ a2);
}
inline static constexpr int get_code_size() {
@@ -194,9 +199,9 @@ struct HammingComputer32 {
set(a8, code_size);
}
void set(const uint8_t* a8, int code_size) {
void set(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 32);
const uint64_t* a = (uint64_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
a0 = a[0];
a1 = a[1];
a2 = a[2];
@@ -204,7 +209,7 @@ struct HammingComputer32 {
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
return popcount64(b[0] ^ a0) + popcount64(b[1] ^ a1) +
popcount64(b[2] ^ a2) + popcount64(b[3] ^ a3);
}
@@ -223,9 +228,9 @@ struct HammingComputer64 {
set(a8, code_size);
}
void set(const uint8_t* a8, int code_size) {
void set(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 64);
const uint64_t* a = (uint64_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
a0 = a[0];
a1 = a[1];
a2 = a[2];
@@ -237,7 +242,7 @@ struct HammingComputer64 {
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
return popcount64(b[0] ^ a0) + popcount64(b[1] ^ a1) +
popcount64(b[2] ^ a2) + popcount64(b[3] ^ a3) +
popcount64(b[4] ^ a4) + popcount64(b[5] ^ a5) +
@@ -256,12 +261,12 @@ struct HammingComputerDefault {
HammingComputerDefault() {}
HammingComputerDefault(const uint8_t* a8, int code_size) {
set(a8, code_size);
HammingComputerDefault(const uint8_t* a8_in, int code_size) {
set(a8_in, code_size);
}
void set(const uint8_t* a8, int code_size) {
this->a8 = a8;
void set(const uint8_t* a8_in, int code_size) {
this->a8 = a8_in;
quotient8 = code_size / 8;
remainder8 = code_size % 8;
}
@@ -332,7 +337,7 @@ struct HammingComputerDefault {
[[fallthrough]];
case 1:
accu += hamdis_tab_ham_bytes[a[0] ^ b[0]];
[[fallthrough]];
break;
default:
break;
}
@@ -362,13 +367,15 @@ inline int generalized_hamming_64(uint64_t a) {
struct GenHammingComputer8 {
uint64_t a0;
GenHammingComputer8(const uint8_t* a, int code_size) {
GenHammingComputer8(const uint8_t* a, [[maybe_unused]] int code_size) {
assert(code_size == 8);
a0 = *(uint64_t*)a;
const uint64_t* a64 = reinterpret_cast<const uint64_t*>(a);
a0 = *a64;
}
inline int hamming(const uint8_t* b) const {
return generalized_hamming_64(*(uint64_t*)b ^ a0);
const uint64_t* b64 = reinterpret_cast<const uint64_t*>(b);
return generalized_hamming_64(*b64 ^ a0);
}
inline static constexpr int get_code_size() {
@@ -378,15 +385,15 @@ struct GenHammingComputer8 {
struct GenHammingComputer16 {
uint64_t a0, a1;
GenHammingComputer16(const uint8_t* a8, int code_size) {
GenHammingComputer16(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 16);
const uint64_t* a = (uint64_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
a0 = a[0];
a1 = a[1];
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
return generalized_hamming_64(b[0] ^ a0) +
generalized_hamming_64(b[1] ^ a1);
}
@@ -399,9 +406,9 @@ struct GenHammingComputer16 {
struct GenHammingComputer32 {
uint64_t a0, a1, a2, a3;
GenHammingComputer32(const uint8_t* a8, int code_size) {
GenHammingComputer32(const uint8_t* a8, [[maybe_unused]] int code_size) {
assert(code_size == 32);
const uint64_t* a = (uint64_t*)a8;
const uint64_t* a = reinterpret_cast<const uint64_t*>(a8);
a0 = a[0];
a1 = a[1];
a2 = a[2];
@@ -409,7 +416,7 @@ struct GenHammingComputer32 {
}
inline int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
return generalized_hamming_64(b[0] ^ a0) +
generalized_hamming_64(b[1] ^ a1) +
generalized_hamming_64(b[2] ^ a2) +
@@ -427,12 +434,12 @@ struct GenHammingComputerM8 {
GenHammingComputerM8(const uint8_t* a8, int code_size) {
assert(code_size % 8 == 0);
a = (uint64_t*)a8;
a = reinterpret_cast<const uint64_t*>(a8);
n = code_size / 8;
}
int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
const uint64_t* b = reinterpret_cast<const uint64_t*>(b8);
int accu = 0;
for (int i = 0; i < n; i++)
accu += generalized_hamming_64(a[i] ^ b[i]);
+8 -8
View File
@@ -50,8 +50,8 @@ typename C::T sample_threshold_median3(
T val3[3];
int vi = 0;
for (size_t i = 0; i < n; i++) {
T v = vals[(i * big_prime) % n];
for (size_t i = 0; i < static_cast<size_t>(n); i++) {
T v = vals[(i * big_prime) % static_cast<size_t>(n)];
// thresh_inf < v < thresh_sup (for CMax)
if (C::cmp(v, thresh_inf) && C::cmp(thresh_sup, v)) {
val3[vi++] = v;
@@ -291,7 +291,7 @@ void count_lt_and_eq(
n_lt += 16 - i_ge;
}
for (size_t i = n1 * 16; i < n; i++) {
for (int i = n1 * 16; i < n; i++) {
uint16_t v = *vals++;
if (C::cmp(thresh, v)) {
n_lt++;
@@ -369,7 +369,7 @@ int simd_compress_array(
}
// end with scalar
for (int i = (n & ~15); i < n; i++) {
for (size_t i = (n & ~size_t(15)); i < n; i++) {
if (C::cmp(thresh, vals[i])) {
vals[wp] = vals[i];
ids[wp] = ids[i];
@@ -1271,14 +1271,14 @@ void simd_histogram_16(
int* hist) {
memset(hist, 0, sizeof(*hist) * 16);
if (shift < 0) {
for (size_t i = 0; i < n; i++) {
for (size_t i = 0; i < static_cast<size_t>(n); i++) {
hist[data[i]]++;
}
} else {
int vmax0 = std::min((16 << shift) + min, 65536);
uint16_t vmax = uint16_t(vmax0 - 1 - min);
for (size_t i = 0; i < n; i++) {
for (size_t i = 0; i < static_cast<size_t>(n); i++) {
uint16_t v = data[i];
v -= min;
if (!(v <= vmax))
@@ -1304,11 +1304,11 @@ void simd_histogram_8(
int* hist) {
memset(hist, 0, sizeof(*hist) * 8);
if (shift < 0) {
for (size_t i = 0; i < n; i++) {
for (size_t i = 0; i < static_cast<size_t>(n); i++) {
hist[data[i]]++;
}
} else {
for (size_t i = 0; i < n; i++) {
for (size_t i = 0; i < static_cast<size_t>(n); i++) {
if (data[i] < min)
continue;
uint16_t v = data[i] - min;
+30 -30
View File
@@ -27,7 +27,7 @@ namespace {
// there can be NaNs in tables, they should be ignored
float tab_min(const float* tab, size_t n) {
float min = HUGE_VAL;
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
if (tab[i] < min) {
min = tab[i];
}
@@ -37,7 +37,7 @@ float tab_min(const float* tab, size_t n) {
float tab_max(const float* tab, size_t n) {
float max = -HUGE_VAL;
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
if (tab[i] > max) {
max = tab[i];
}
@@ -46,14 +46,14 @@ float tab_max(const float* tab, size_t n) {
}
void round_tab(float* tab, size_t n, float a, float bi) {
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
tab[i] = floorf((tab[i] - bi) * a + 0.5);
}
}
template <typename T>
void round_tab(const float* tab, size_t n, float a, float bi, T* tab_out) {
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
tab_out[i] = (T)floorf((tab[i] - bi) * a + 0.5);
}
}
@@ -68,7 +68,7 @@ void round_uint8_per_column(
float* b_out) {
float max_span = 0;
std::vector<float> mins(n);
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
mins[i] = tab_min(tab + i * d, d);
float span = tab_max(tab + i * d, d) - mins[i];
if (span > max_span) {
@@ -77,7 +77,7 @@ void round_uint8_per_column(
}
float a = 255 / max_span;
float b = 0;
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
b += mins[i];
round_tab(tab + i * d, d, a, mins[i]);
}
@@ -98,10 +98,10 @@ void round_uint8_per_column_multi(
float* b_out) {
float max_span = 0;
std::vector<float> mins(n);
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
float min_i = HUGE_VAL;
float max_i = -HUGE_VAL;
for (int j = 0; j < m; j++) {
for (size_t j = 0; j < m; j++) {
min_i = std::min(min_i, tab_min(tab + (j * n + i) * d, d));
max_i = std::max(max_i, tab_max(tab + (j * n + i) * d, d));
}
@@ -113,9 +113,9 @@ void round_uint8_per_column_multi(
}
float a = 255 / max_span;
float b = 0;
for (int i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
b += mins[i];
for (int j = 0; j < m; j++) {
for (size_t j = 0; j < m; j++) {
round_tab(tab + (j * n + i) * d, d, a, mins[i]);
}
}
@@ -147,7 +147,7 @@ void quantize_LUT_and_bias(
std::vector<float> mins(M);
float max_span_LUT = -HUGE_VAL, max_span_dis = 0;
b = 0;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
mins[i] = tab_min(LUT + i * ksub, ksub);
float span = tab_max(LUT + i * ksub, ksub) - mins[i];
max_span_LUT = std::max(max_span_LUT, span);
@@ -156,7 +156,7 @@ void quantize_LUT_and_bias(
}
a = std::min(255 / max_span_LUT, 65535 / max_span_dis);
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
round_tab(LUT + i * ksub, ksub, a, mins[i], LUTq + i * ksub);
}
memset(LUTq + M * ksub, 0, ksub * (M2 - M));
@@ -167,7 +167,7 @@ void quantize_LUT_and_bias(
float bias_max = tab_max(bias, nprobe);
max_span_dis = bias_max - bias_min;
b = 0;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
mins[i] = tab_min(LUT + i * ksub, ksub);
float span = tab_max(LUT + i * ksub, ksub) - mins[i];
max_span_LUT = std::max(max_span_LUT, span);
@@ -177,7 +177,7 @@ void quantize_LUT_and_bias(
a = std::min(255 / max_span_LUT, 65535 / max_span_dis);
b += bias_min;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
round_tab(LUT + i * ksub, ksub, a, mins[i], LUTq + i * ksub);
}
memset(LUTq + M * ksub, 0, ksub * (M2 - M));
@@ -192,10 +192,10 @@ void quantize_LUT_and_bias(
b = HUGE_VAL;
size_t ij = 0;
for (int j = 0; j < nprobe; j++) {
for (size_t j = 0; j < nprobe; j++) {
float max_span_dis_j = bias[j] - bias_min;
float b2j = bias[j];
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
mins[ij] = tab_min(LUT + ij * ksub, ksub);
float span = tab_max(LUT + ij * ksub, ksub) - mins[ij];
max_span_LUT = std::max(max_span_LUT, span);
@@ -212,8 +212,8 @@ void quantize_LUT_and_bias(
ij = 0;
size_t ij_2 = 0;
for (int j = 0; j < nprobe; j++) {
for (int i = 0; i < M; i++) {
for (size_t j = 0; j < nprobe; j++) {
for (size_t i = 0; i < M; i++) {
round_tab(
LUT + ij * ksub, ksub, a, mins[ij], LUTq + ij_2 * ksub);
ij++;
@@ -230,10 +230,10 @@ void quantize_LUT_and_bias(
std::vector<float> LUT2_storage(nprobe * M * ksub);
float* LUT2 = LUT2_storage.data();
size_t ijc = 0;
for (int j = 0; j < nprobe; j++) {
for (size_t j = 0; j < nprobe; j++) {
float bias_j = bias[j] / M;
for (int i = 0; i < M; i++) {
for (int c = 0; c < ksub; c++) {
for (size_t i = 0; i < M; i++) {
for (size_t c = 0; c < ksub; c++) {
LUT2[ijc] = LUT[ijc] + bias_j;
ijc++;
}
@@ -241,8 +241,8 @@ void quantize_LUT_and_bias(
}
std::vector<float> mins(M, HUGE_VAL), maxs(M, -HUGE_VAL);
size_t ij = 0;
for (int j = 0; j < nprobe; j++) {
for (int i = 0; i < M; i++) {
for (size_t j = 0; j < nprobe; j++) {
for (size_t i = 0; i < M; i++) {
mins[i] = std::min(mins[i], tab_min(LUT2 + ij * ksub, ksub));
maxs[i] = std::max(maxs[i], tab_max(LUT2 + ij * ksub, ksub));
ij++;
@@ -251,7 +251,7 @@ void quantize_LUT_and_bias(
float max_span = -HUGE_VAL;
b = 0;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
float span = maxs[i] - mins[i];
max_span = std::max(max_span, span);
b += mins[i];
@@ -259,8 +259,8 @@ void quantize_LUT_and_bias(
a = 255 / max_span;
ij = 0;
size_t ij_2 = 0;
for (int j = 0; j < nprobe; j++) {
for (int i = 0; i < M; i++) {
for (size_t j = 0; j < nprobe; j++) {
for (size_t i = 0; i < M; i++) {
round_tab(
LUT2 + ij * ksub, ksub, a, mins[i], LUTq + ij_2 * ksub);
ij++;
@@ -298,7 +298,7 @@ void aq_quantize_LUT_and_bias(
float bias_max = tab_max(bias, nprobe);
max_span_dis = bias_max - bias_min;
b = 0;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
mins[i] = tab_min(LUT + i * ksub, ksub);
float span = tab_max(LUT + i * ksub, ksub) - mins[i];
max_span_LUT = std::max(max_span_LUT, span);
@@ -308,7 +308,7 @@ void aq_quantize_LUT_and_bias(
a = std::min(255 / max_span_LUT, 65535 / max_span_dis);
b += bias_min;
for (int i = 0; i < M; i++) {
for (size_t i = 0; i < M; i++) {
round_tab(LUT + i * ksub, ksub, a, mins[i], LUTq + i * ksub);
}
memset(LUTq + M * ksub, 0, ksub * (M2 - M));
@@ -324,14 +324,14 @@ float aq_estimate_norm_scale(
size_t M_norm,
const float* LUT) {
float max_span_LUT = -HUGE_VAL;
for (int i = 0; i < M - M_norm; i++) {
for (size_t i = 0; i < M - M_norm; i++) {
float min = tab_min(LUT + i * ksub, ksub);
float span = tab_max(LUT + i * ksub, ksub) - min;
max_span_LUT = std::max(max_span_LUT, span);
}
float max_span_LUT_norm = -HUGE_VAL;
for (int i = M - M_norm; i < M; i++) {
for (size_t i = M - M_norm; i < M; i++) {
float min = tab_min(LUT + i * ksub, ksub);
float span = tab_max(LUT + i * ksub, ksub) - min;
max_span_LUT_norm = std::max(max_span_LUT_norm, span);