migrate from bit-manipulation builtins in fbcode/faiss

Summary: Replaces `__builtin_ctz` with `std::countr_zero` in `impl/fast_scan/simd_result_handlers.h`'s per-lane result-scanning loops. `utils/popcount.h` and `impl/platform_macros.h` are portability wrappers (their sole purpose is bridging MSVC/GCC-Clang builtins), so they're left out of scope here.

Differential Revision: D121529297
This commit is contained in:
Yedidya Feldblum
2026-09-24 07:45:24 -07:00
committed by Facebook GitHub Bot
parent 2496f63f76
commit 5d218e2b16
+11 -10
View File
@@ -8,6 +8,7 @@
#pragma once
#include <algorithm>
#include <bit>
#include <type_traits>
#include <utility>
#include <vector>
@@ -374,7 +375,7 @@ struct SingleResultHandler : ResultHandlerCompare<C, with_id_map, SL> {
if (this->sel != nullptr) {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
auto real_idx = this->adjust_id(b, j);
lt_mask -= 1 << j;
if (this->sel->is_member(real_idx)) {
@@ -391,7 +392,7 @@ struct SingleResultHandler : ResultHandlerCompare<C, with_id_map, SL> {
} else {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
this->scan_cnt++;
T d = d32tab[j];
@@ -498,7 +499,7 @@ struct HeapHandler : ResultHandlerCompare<C, with_id_map, SL> {
if (this->sel != nullptr) {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
auto real_idx = this->adjust_id(b, j);
lt_mask -= 1 << j;
if (this->sel->is_member(real_idx)) {
@@ -520,7 +521,7 @@ struct HeapHandler : ResultHandlerCompare<C, with_id_map, SL> {
} else {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
this->scan_cnt++;
T dis_for_j = d32tab[j];
@@ -637,7 +638,7 @@ struct ReservoirHandler : ResultHandlerCompare<C, with_id_map, SL> {
if (this->sel != nullptr) {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
auto real_idx = this->adjust_id(b, j);
lt_mask -= 1 << j;
if (this->sel->is_member(real_idx)) {
@@ -651,7 +652,7 @@ struct ReservoirHandler : ResultHandlerCompare<C, with_id_map, SL> {
} else {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
T dis_for_j = d32tab[j];
this->scan_cnt++;
@@ -771,7 +772,7 @@ struct RangeHandler : ResultHandlerCompare<C, with_id_map, SL> {
if (this->sel != nullptr) {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
auto real_idx = this->adjust_id(b, j);
@@ -786,7 +787,7 @@ struct RangeHandler : ResultHandlerCompare<C, with_id_map, SL> {
} else {
while (lt_mask) {
// find first non-zero
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
T dis = d32tab[j];
n_per_query[q]++;
@@ -944,7 +945,7 @@ struct SingleQueryResultCollectHandler
if (this->sel != nullptr) {
while (lt_mask) {
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
auto real_idx = this->adjust_id(b, j);
lt_mask -= 1 << j;
if (this->sel->is_member(real_idx)) {
@@ -955,7 +956,7 @@ struct SingleQueryResultCollectHandler
}
} else {
while (lt_mask) {
int j = __builtin_ctz(lt_mask);
int j = std::countr_zero(lt_mask);
lt_mask -= 1 << j;
T dis = d32tab[j];
int64_t idx = this->adjust_id(b, j);