// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. // This source code is licensed under both the GPLv2 (found in the // COPYING file in the root directory) and Apache 2.0 License // (found in the LICENSE.Apache file in the root directory). #pragma once #include #include #ifdef _MSC_VER #include #endif namespace ROCKSDB_NAMESPACE { template inline int BitsSetToOne(T v) { static_assert(std::is_integral::value, "non-integral type"); #ifdef _MSC_VER static_assert(sizeof(T) <= sizeof(uint64_t), "type too big"); if (sizeof(T) > sizeof(uint32_t)) { return static_cast(__popcnt64(static_cast(v))); } else { return static_cast(__popcnt(static_cast(v))); } #else static_assert(sizeof(T) <= sizeof(unsigned long long), "type too big"); if (sizeof(T) > sizeof(unsigned long)) { return __builtin_popcountll(static_cast(v)); } else if (sizeof(T) > sizeof(unsigned int)) { return __builtin_popcountl(static_cast(v)); } else { return __builtin_popcount(static_cast(v)); } #endif } } // namespace ROCKSDB_NAMESPACE