mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
9.6 KiB
C
390 lines
9.6 KiB
C
4 years ago
|
/**
|
||
6 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
6 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
6 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
6 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_DYN_BITSET_H
|
||
|
#define SRSRAN_DYN_BITSET_H
|
||
6 years ago
|
|
||
4 years ago
|
#include "adt_utils.h"
|
||
4 years ago
|
#include "srsran/srslog/bundled/fmt/format.h"
|
||
6 years ago
|
#include <cstdint>
|
||
5 years ago
|
#include <inttypes.h>
|
||
6 years ago
|
#include <string>
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
6 years ago
|
|
||
6 years ago
|
constexpr uint32_t ceil_div(uint32_t x, uint32_t y)
|
||
|
{
|
||
4 years ago
|
return (x + y - 1) / y;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
template <size_t N, bool reversed = false>
|
||
|
class bounded_bitset
|
||
|
{
|
||
|
typedef uint64_t word_t;
|
||
|
static const size_t bits_per_word = 8 * sizeof(word_t);
|
||
|
|
||
|
public:
|
||
4 years ago
|
constexpr bounded_bitset() = default;
|
||
6 years ago
|
|
||
4 years ago
|
constexpr explicit bounded_bitset(size_t cur_size_) : cur_size(cur_size_) {}
|
||
6 years ago
|
|
||
|
constexpr size_t max_size() const noexcept { return N; }
|
||
|
|
||
|
size_t size() const noexcept { return cur_size; }
|
||
|
|
||
4 years ago
|
void resize(size_t new_size)
|
||
6 years ago
|
{
|
||
|
if (new_size > max_size()) {
|
||
4 years ago
|
std::string msg =
|
||
|
"ERROR: new size=" + std::to_string(new_size) + " exceeds bitset capacity=" + std::to_string(max_size());
|
||
|
THROW_BAD_ACCESS(msg.c_str());
|
||
4 years ago
|
}
|
||
|
if (new_size == cur_size) {
|
||
6 years ago
|
return;
|
||
6 years ago
|
}
|
||
|
cur_size = new_size;
|
||
|
sanitize_();
|
||
6 years ago
|
for (size_t i = nof_words_(); i < max_nof_words_(); ++i) {
|
||
4 years ago
|
buffer[i] = static_cast<word_t>(0);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
void set(size_t pos, bool val)
|
||
5 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(pos, true);
|
||
5 years ago
|
if (val) {
|
||
|
set_(pos);
|
||
|
} else {
|
||
|
reset_(pos);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
void set(size_t pos)
|
||
6 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(pos, true);
|
||
6 years ago
|
set_(pos);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
void reset(size_t pos)
|
||
6 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(pos, true);
|
||
6 years ago
|
reset_(pos);
|
||
6 years ago
|
}
|
||
|
|
||
|
void reset() noexcept
|
||
|
{
|
||
|
for (size_t i = 0; i < nof_words_(); ++i) {
|
||
|
buffer[i] = static_cast<word_t>(0);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
bool test(size_t pos) const
|
||
6 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(pos, true);
|
||
6 years ago
|
return test_(pos);
|
||
|
}
|
||
|
|
||
|
bounded_bitset<N, reversed>& flip() noexcept
|
||
|
{
|
||
|
for (size_t i = 0; i < nof_words_(); ++i) {
|
||
|
buffer[i] = ~buffer[i];
|
||
|
}
|
||
|
sanitize_();
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
4 years ago
|
bounded_bitset<N, reversed>& fill(size_t startpos, size_t endpos, bool value = true)
|
||
6 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(startpos, false);
|
||
|
assert_within_bounds_(endpos, false);
|
||
6 years ago
|
// NOTE: can be optimized
|
||
|
if (value) {
|
||
|
for (size_t i = startpos; i < endpos; ++i) {
|
||
6 years ago
|
set_(i);
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
for (size_t i = startpos; i < endpos; ++i) {
|
||
6 years ago
|
reset_(i);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bool all() const noexcept
|
||
|
{
|
||
|
const size_t nw = nof_words_();
|
||
|
if (nw == 0) {
|
||
|
return true;
|
||
|
}
|
||
|
word_t allset = ~static_cast<word_t>(0);
|
||
|
for (size_t i = 0; i < nw - 1; i++) {
|
||
|
if (buffer[i] != allset) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return buffer[nw - 1] == (allset >> (nw * bits_per_word - size()));
|
||
|
}
|
||
|
|
||
|
bool any() const noexcept
|
||
|
{
|
||
|
for (size_t i = 0; i < nof_words_(); ++i) {
|
||
|
if (buffer[i] != static_cast<word_t>(0)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
4 years ago
|
bool any(size_t start, size_t stop) const
|
||
6 years ago
|
{
|
||
4 years ago
|
assert_within_bounds_(start, false);
|
||
|
assert_within_bounds_(stop, false);
|
||
6 years ago
|
// NOTE: can be optimized
|
||
|
for (size_t i = start; i < stop; ++i) {
|
||
|
if (test_(i)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool none() const noexcept { return !any(); }
|
||
|
|
||
|
size_t count() const noexcept
|
||
|
{
|
||
|
size_t result = 0;
|
||
|
for (size_t i = 0; i < nof_words_(); i++) {
|
||
|
// result += __builtin_popcountl(buffer[i]);
|
||
4 years ago
|
// Note: use an "int" for count triggers popcount optimization if SSE instructions are enabled.
|
||
|
int c = 0;
|
||
|
for (word_t w = buffer[i]; w > 0; c++) {
|
||
|
w &= w - 1;
|
||
6 years ago
|
}
|
||
4 years ago
|
result += c;
|
||
6 years ago
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool operator==(const bounded_bitset<N, reversed>& other) const noexcept
|
||
6 years ago
|
{
|
||
|
if (size() != other.size()) {
|
||
|
return false;
|
||
|
}
|
||
|
for (uint32_t i = 0; i < nof_words_(); ++i) {
|
||
5 years ago
|
if (buffer[i] != other.buffer[i]) {
|
||
6 years ago
|
return false;
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool operator!=(const bounded_bitset<N, reversed>& other) const noexcept { return not(*this == other); }
|
||
6 years ago
|
|
||
4 years ago
|
bounded_bitset<N, reversed>& operator|=(const bounded_bitset<N, reversed>& other)
|
||
6 years ago
|
{
|
||
|
if (other.size() != size()) {
|
||
4 years ago
|
std::string msg = "operator|= called for bitsets of different sizes (" + std::to_string(size()) +
|
||
|
"!=" + std::to_string(other.size()) + ")";
|
||
|
THROW_BAD_ACCESS(msg.c_str());
|
||
6 years ago
|
}
|
||
|
for (size_t i = 0; i < nof_words_(); ++i) {
|
||
|
buffer[i] |= other.buffer[i];
|
||
|
}
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
4 years ago
|
bounded_bitset<N, reversed>& operator&=(const bounded_bitset<N, reversed>& other)
|
||
6 years ago
|
{
|
||
|
if (other.size() != size()) {
|
||
4 years ago
|
std::string msg = "operator&= called for bitsets of different sizes (" + std::to_string(size()) +
|
||
|
"!=" + std::to_string(other.size()) + ")";
|
||
|
THROW_BAD_ACCESS(msg.c_str());
|
||
6 years ago
|
}
|
||
|
for (size_t i = 0; i < nof_words_(); ++i) {
|
||
|
buffer[i] &= other.buffer[i];
|
||
|
}
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bounded_bitset<N, reversed> operator~() const noexcept
|
||
|
{
|
||
|
bounded_bitset<N, reversed> ret(*this);
|
||
|
ret.flip();
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
4 years ago
|
template <typename OutputIt>
|
||
|
OutputIt to_string(OutputIt&& mem_buffer) const
|
||
6 years ago
|
{
|
||
4 years ago
|
if (size() == 0) {
|
||
|
return mem_buffer;
|
||
|
}
|
||
|
|
||
6 years ago
|
std::string s;
|
||
|
s.assign(size(), '0');
|
||
|
if (not reversed) {
|
||
|
for (size_t i = size(); i > 0; --i) {
|
||
4 years ago
|
fmt::format_to(mem_buffer, "{}", test(i - 1) ? '1' : '0');
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
for (size_t i = 0; i < size(); ++i) {
|
||
4 years ago
|
fmt::format_to(mem_buffer, "{}", test(i) ? '1' : '0');
|
||
6 years ago
|
}
|
||
|
}
|
||
4 years ago
|
return mem_buffer;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
uint64_t to_uint64() const
|
||
6 years ago
|
{
|
||
|
if (nof_words_() > 1) {
|
||
4 years ago
|
std::string msg = "ERROR: cannot convert bitset of size=" + std::to_string(size()) + " to uint64_t";
|
||
|
THROW_BAD_ACCESS(msg.c_str());
|
||
6 years ago
|
}
|
||
|
return get_word_(0);
|
||
|
}
|
||
|
|
||
4 years ago
|
template <typename OutputIt>
|
||
|
OutputIt to_hex(OutputIt&& mem_buffer) const noexcept
|
||
6 years ago
|
{
|
||
4 years ago
|
if (size() == 0) {
|
||
|
return mem_buffer;
|
||
6 years ago
|
}
|
||
4 years ago
|
// first word may not print 16 hex digits
|
||
|
int i = nof_words_() - 1;
|
||
|
size_t rem_symbols = ceil_div((size() - (size() / bits_per_word) * bits_per_word), 4U);
|
||
|
fmt::format_to(mem_buffer, "{:0>{}x}", buffer[i], rem_symbols);
|
||
|
// remaining words will occupy 16 hex digits
|
||
|
for (--i; i >= 0; --i) {
|
||
|
fmt::format_to(mem_buffer, "{:0>16x}", buffer[i]);
|
||
|
}
|
||
|
return mem_buffer;
|
||
6 years ago
|
}
|
||
|
|
||
|
private:
|
||
4 years ago
|
word_t buffer[(N - 1) / bits_per_word + 1] = {0};
|
||
|
size_t cur_size = 0;
|
||
6 years ago
|
|
||
|
void sanitize_()
|
||
|
{
|
||
5 years ago
|
size_t n = size() % bits_per_word;
|
||
|
size_t nwords = nof_words_();
|
||
|
if (n != 0 and nwords > 0) {
|
||
|
buffer[nwords - 1] &= ~((~static_cast<word_t>(0)) << n);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
bool test_(size_t pos) const noexcept
|
||
|
{
|
||
|
pos = reversed ? size() - 1 - pos : pos;
|
||
|
return ((get_word_(pos) & maskbit(pos)) != static_cast<word_t>(0));
|
||
|
}
|
||
|
|
||
6 years ago
|
void set_(size_t pos) noexcept
|
||
|
{
|
||
|
pos = reversed ? size() - 1 - pos : pos;
|
||
|
get_word_(pos) |= maskbit(pos);
|
||
|
}
|
||
|
|
||
|
void reset_(size_t pos) noexcept
|
||
|
{
|
||
|
pos = reversed ? size() - 1 - pos : pos;
|
||
|
get_word_(pos) &= ~(maskbit(pos));
|
||
|
}
|
||
|
|
||
6 years ago
|
size_t nof_words_() const noexcept { return size() > 0 ? (size() - 1) / bits_per_word + 1 : 0; }
|
||
|
|
||
|
word_t& get_word_(size_t pos) noexcept { return buffer[pos / bits_per_word]; }
|
||
|
|
||
|
const word_t& get_word_(size_t pos) const { return buffer[pos / bits_per_word]; }
|
||
|
|
||
|
size_t word_idx_(size_t pos) const { return pos / bits_per_word; }
|
||
|
|
||
4 years ago
|
void assert_within_bounds_(size_t pos, bool strict) const
|
||
|
{
|
||
|
if (pos > size() or (strict and pos == size())) {
|
||
|
std::string msg =
|
||
|
"ERROR: index=" + std::to_string(pos) + "is out of bounds for bitset of size=" + std::to_string(size());
|
||
|
THROW_BAD_ACCESS(msg.c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static word_t maskbit(size_t pos) { return (static_cast<word_t>(1)) << (pos % bits_per_word); }
|
||
6 years ago
|
|
||
|
static size_t max_nof_words_() { return (N - 1) / bits_per_word + 1; }
|
||
6 years ago
|
};
|
||
|
|
||
|
template <size_t N, bool reversed>
|
||
|
inline bounded_bitset<N, reversed> operator&(const bounded_bitset<N, reversed>& lhs,
|
||
4 years ago
|
const bounded_bitset<N, reversed>& rhs) noexcept
|
||
6 years ago
|
{
|
||
|
bounded_bitset<N, reversed> res(lhs);
|
||
|
res &= rhs;
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
template <size_t N, bool reversed>
|
||
|
inline bounded_bitset<N, reversed> operator|(const bounded_bitset<N, reversed>& lhs,
|
||
|
const bounded_bitset<N, reversed>& rhs) noexcept
|
||
|
{
|
||
|
bounded_bitset<N, reversed> res(lhs);
|
||
|
res |= rhs;
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
template <size_t N, bool reversed>
|
||
|
inline bounded_bitset<N, reversed> fliplr(const bounded_bitset<N, reversed>& other) noexcept
|
||
|
{
|
||
|
bounded_bitset<N, reversed> ret(other.size());
|
||
|
for (uint32_t i = 0; i < ret.size(); ++i) {
|
||
|
if (other.test(i)) {
|
||
|
ret.set(ret.size() - 1 - i);
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
6 years ago
|
|
||
4 years ago
|
namespace fmt {
|
||
4 years ago
|
/// Custom formatter for bounded_bitset<N, reversed>
|
||
|
template <size_t N, bool reversed>
|
||
4 years ago
|
struct formatter<srsran::bounded_bitset<N, reversed> > {
|
||
4 years ago
|
enum { hexadecimal, binary } mode = binary;
|
||
|
|
||
|
template <typename ParseContext>
|
||
|
auto parse(ParseContext& ctx) -> decltype(ctx.begin())
|
||
|
{
|
||
|
auto it = ctx.begin();
|
||
4 years ago
|
while (it != ctx.end() and *it != '}') {
|
||
4 years ago
|
if (*it == 'x') {
|
||
|
mode = hexadecimal;
|
||
|
}
|
||
|
++it;
|
||
|
}
|
||
|
|
||
|
return it;
|
||
|
}
|
||
|
|
||
|
template <typename FormatContext>
|
||
4 years ago
|
auto format(const srsran::bounded_bitset<N, reversed>& s, FormatContext& ctx)
|
||
4 years ago
|
-> decltype(std::declval<FormatContext>().out())
|
||
|
{
|
||
|
if (mode == hexadecimal) {
|
||
|
return s.template to_hex(ctx.out());
|
||
|
}
|
||
|
return s.template to_string(ctx.out());
|
||
|
}
|
||
|
};
|
||
4 years ago
|
} // namespace fmt
|
||
4 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_DYN_BITSET_H
|