ircd::util: Add popcount wrapping; consolidate callsites.

This commit is contained in:
Jason Volk 2023-02-07 11:27:51 -08:00
parent 855648de15
commit fe0f398e14
9 changed files with 67 additions and 12 deletions

View File

@ -43,6 +43,7 @@
#include "byte_view.h"
#include "buffer/buffer.h"
#include "vg.h"
#include "util/popcount.h"
#include "simd/simd.h"
#include "simt/simt.h"
#include "allocator/allocator.h"

View File

@ -157,7 +157,7 @@ ircd::m::query(std::nothrow_t,
const auto got
{
__builtin_popcountl(mask)
popcount(mask)
};
return ret;

View File

@ -25,12 +25,7 @@ noexcept
{
uint ret(0), i(0);
for(; i < lanes<T>(); ++i)
if constexpr(sizeof_lane<T>() <= sizeof(int))
ret += __builtin_popcount(a[i]);
else if constexpr(sizeof_lane<T>() <= sizeof(long))
ret += __builtin_popcountl(a[i]);
else
ret += __builtin_popcountll(a[i]);
ret += popcount(a[i]);
return ret;
}

View File

@ -132,7 +132,7 @@ const
{
size_t ret(0);
for(size_t i(0); i < words; ++i)
ret += std::popcount(buf[i]);
ret += popcount(buf[i]);
return ret;
}

View File

@ -0,0 +1,59 @@
// The Construct
//
// Copyright (C) The Construct Developers, Authors & Contributors
// Copyright (C) 2016-2023 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_UTIL_POPCOUNT_H
namespace ircd {
inline namespace util
{
template<class T>
constexpr int popcount(const T) noexcept;
template<> constexpr int popcount(const unsigned long long) noexcept;
template<> constexpr int popcount(const unsigned long) noexcept;
template<> constexpr int popcount(const unsigned int) noexcept;
}}
template<>
constexpr int
ircd::util::popcount(const unsigned long long a)
noexcept
{
#if __has_feature(__cpp_lib_bitops)
return std::popcount(a);
#else
return __builtin_popcountll(a);
#endif
}
template<>
constexpr int
ircd::util::popcount(const unsigned long a)
noexcept
{
#if __has_feature(__cpp_lib_bitops)
return std::popcount(a);
#else
return __builtin_popcountl(a);
#endif
}
template<>
constexpr int
ircd::util::popcount(const unsigned int a)
noexcept
{
#if __has_feature(__cpp_lib_bitops)
return std::popcount(a);
#else
return __builtin_popcount(a);
#endif
}

View File

@ -1054,7 +1054,7 @@ ircd::m::exists_count(const vector_view<const id::event> &event_ids)
const auto ret
{
__builtin_popcountl(mask)
popcount(mask)
};
assert(size_t(ret) <= event_ids.size());

View File

@ -240,7 +240,7 @@ ircd::m::get(const vector_view<const event::idx> &event_idx,
const auto found
{
__builtin_popcountl(mask)
popcount(mask)
};
if(unlikely(size_t(found) < event_idx.size()))

View File

@ -200,7 +200,7 @@ ircd::m::index(const vector_view<event::idx> &out_,
db::read(column, keys, bufs)
};
ret += __builtin_popcountl(found_mask);
ret += popcount(found_mask);
}
return ret;

View File

@ -90,7 +90,7 @@ ircd::m::room_state_fetch_result(room::state::fetch &f,
};
f.responses += i;
f.exists += __builtin_popcountl(exists);
f.exists += popcount(exists);
for(size_t j(0); j < i; ++j)
{
if(exists & (1UL << j))