mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 15:04:10 +01:00
ircd::util: Add popcount wrapping; consolidate callsites.
This commit is contained in:
parent
855648de15
commit
fe0f398e14
9 changed files with 67 additions and 12 deletions
|
@ -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"
|
||||
|
|
|
@ -157,7 +157,7 @@ ircd::m::query(std::nothrow_t,
|
|||
|
||||
const auto got
|
||||
{
|
||||
__builtin_popcountl(mask)
|
||||
popcount(mask)
|
||||
};
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
59
include/ircd/util/popcount.h
Normal file
59
include/ircd/util/popcount.h
Normal 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
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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()))
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue