aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorgatecat <gatecat@ds0.me>2021-02-24 18:22:16 +0000
committerGitHub <noreply@github.com>2021-02-24 18:22:16 +0000
commite2cdaa653c805f9bfb6f0ab36295858e5dd3179d (patch)
tree0b60c118fb212a301674c409df2e3dcb7fad1bf1 /common
parent4026082470d021e0118a8dc03da3a0c4be8bd171 (diff)
parente0a4af09ed607582cb0f6daf8192d2421eb720e9 (diff)
downloadnextpnr-e2cdaa653c805f9bfb6f0ab36295858e5dd3179d.tar.gz
nextpnr-e2cdaa653c805f9bfb6f0ab36295858e5dd3179d.tar.bz2
nextpnr-e2cdaa653c805f9bfb6f0ab36295858e5dd3179d.zip
Merge pull request #597 from litghost/add_dynamic_bitarray
Add dynamic bitarray to common library.
Diffstat (limited to 'common')
-rw-r--r--common/dynamic_bitarray.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/common/dynamic_bitarray.h b/common/dynamic_bitarray.h
new file mode 100644
index 00000000..10a85fbc
--- /dev/null
+++ b/common/dynamic_bitarray.h
@@ -0,0 +1,79 @@
+/*
+ * 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 <cstdint>
+#include <limits>
+#include <vector>
+
+// This class implements a simple dynamic bitarray, backed by some resizable
+// random access storage. The default is to use a std::vector<uint8_t>.
+
+#ifndef DYNAMIC_BITARRAY_H
+#define DYNAMIC_BITARRAY_H
+
+namespace nextpnr {
+
+template <typename Storage = std::vector<uint8_t>> class DynamicBitarray
+{
+ public:
+ static_assert(!std::numeric_limits<typename Storage::value_type>::is_signed, "Storage must be unsigned!");
+
+ void fill(bool value)
+ {
+ std::fill(storage.begin(), storage.end(), value ? std::numeric_limits<typename Storage::value_type>::max() : 0);
+ }
+
+ constexpr size_t bits_per_value() const { return std::numeric_limits<typename Storage::value_type>::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() - 1) / 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 */