Bitwise Popcount Utility (utils/popcount.h)

faiss · faiss/utils/popcount.h · c · MIT
ai, search, lang:cpp, tag:math, tag:performance

A high-performance header providing cross-platform population count functions for 32-bit and 64-bit integers. Use this for low-level bit manipulation and efficient similarity search algorithms.

// Source: https://github.com/facebookresearch/faiss | License: MIT | Copyright Facebook, Inc. and its affiliates
// Provided via justclaw.it.com; keep this notice with the code.
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#pragma once

#include <cstdint>

// popcount = count number of 1s in the binary representation of a number

#ifdef _MSC_VER
#include <intrin.h>
inline int popcount32(uint32_t x) {
    return __popcnt(x);
}
inline int popcount64(uint64_t x) {
    return static_cast<int>(__popcnt64(x));
}
#else
inline int popcount32(uint32_t x) {
    return __builtin_popcount(x);
}
inline int popcount64(uint64_t x) {
    return __builtin_popcou

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key