From 3650294e512d48f2f2bb1bfdd867f672b7dc62dd Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Tue, 23 Feb 2021 15:36:51 -0800 Subject: Add dynamic bitarray to common library. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- common/dynamic_bitarray.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 common/dynamic_bitarray.h (limited to 'common') diff --git a/common/dynamic_bitarray.h b/common/dynamic_bitarray.h new file mode 100644 index 00000000..7a7b2bf9 --- /dev/null +++ b/common/dynamic_bitarray.h @@ -0,0 +1,82 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2021 Symbiflow Authors + * + * 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 appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include +#include +#include + +// This class implements a simple dynamic bitarray, backed by some resizable +// random access storage. The default is to use a std::vector. + +#ifndef DYNAMIC_BITARRAY_H +#define DYNAMIC_BITARRAY_H + +namespace nextpnr { + +template > class DynamicBitarray +{ + public: + static_assert(!std::numeric_limits::is_signed, "Storage must be unsigned!"); + + void fill(bool value) + { + std::fill(storage.begin(), storage.end(), value ? std::numeric_limits::max() : 0); + } + + constexpr size_t bits_per_value() const + { + return sizeof(typename Storage::value_type) * std::numeric_limits::digits; + } + + bool get(size_t bit) const + { + size_t element_index = bit / bits_per_value(); + size_t bit_offset = bit % bits_per_value(); + + auto element = storage.at(element_index); + return (element & (1 << bit_offset)) != 0; + } + + void set(size_t bit, bool value) + { + size_t element_index = bit / bits_per_value(); + size_t bit_offset = bit % bits_per_value(); + + if (value) { + storage.at(element_index) |= (1 << bit_offset); + } else { + storage.at(element_index) &= ~(1 << bit_offset); + } + } + + void resize(size_t number_bits) + { + size_t required_storage = (number_bits + bits_per_value()) / bits_per_value(); + storage.resize(required_storage); + } + + size_t size() const { return storage.size() * bits_per_value(); } + + private: + Storage storage; +}; + +}; // namespace nextpnr + +#endif /* DYNAMIC_BITARRAY_H */ -- cgit v1.2.3 From 6d193ffd8b6482cddceb5c9050e36315d99d3363 Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Wed, 24 Feb 2021 08:52:26 -0800 Subject: Fix some bugs found in review. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- common/dynamic_bitarray.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'common') diff --git a/common/dynamic_bitarray.h b/common/dynamic_bitarray.h index 7a7b2bf9..10a85fbc 100644 --- a/common/dynamic_bitarray.h +++ b/common/dynamic_bitarray.h @@ -39,10 +39,7 @@ template > class DynamicBitarray std::fill(storage.begin(), storage.end(), value ? std::numeric_limits::max() : 0); } - constexpr size_t bits_per_value() const - { - return sizeof(typename Storage::value_type) * std::numeric_limits::digits; - } + constexpr size_t bits_per_value() const { return std::numeric_limits::digits; } bool get(size_t bit) const { @@ -67,7 +64,7 @@ template > class DynamicBitarray void resize(size_t number_bits) { - size_t required_storage = (number_bits + bits_per_value()) / bits_per_value(); + size_t required_storage = (number_bits + bits_per_value() - 1) / bits_per_value(); storage.resize(required_storage); } -- cgit v1.2.3