diff options
author | Andrew Waterman <andrew@sifive.com> | 2021-02-04 03:04:33 -0800 |
---|---|---|
committer | Andrew Waterman <andrew@sifive.com> | 2021-02-04 03:04:33 -0800 |
commit | c234f51c04b7b8188adc6d7f1f57f1dfedee8d08 (patch) | |
tree | f2bde625e0b2d5844c166c6c2796826860b643c7 | |
parent | d87a21ac8aa6fefdc435722c4620d2a9410fef5d (diff) | |
download | riscv-isa-sim-sparse-mem.zip riscv-isa-sim-sparse-mem.tar.gz riscv-isa-sim-sparse-mem.tar.bz2 |
Add int_map and use it to speed up sparse memsparse-mem
-rw-r--r-- | riscv/devices.cc | 2 | ||||
-rw-r--r-- | riscv/int_map.h | 14 |
2 files changed, 7 insertions, 9 deletions
diff --git a/riscv/devices.cc b/riscv/devices.cc index cde5b75..2d902a9 100644 --- a/riscv/devices.cc +++ b/riscv/devices.cc @@ -92,7 +92,7 @@ bool mmio_plugin_device_t::store(reg_t addr, size_t len, const uint8_t* bytes) } mem_t::mem_t(reg_t size) - : sparse_memory_map(log2(size) + (size & (size-1) ? 1 : 0)), sz(size) + : sz(size) { if (size == 0 || size % PGSIZE != 0) throw std::runtime_error("memory size must be a positive multiple of 4 KiB"); diff --git a/riscv/int_map.h b/riscv/int_map.h index ed1e7a4..10da616 100644 --- a/riscv/int_map.h +++ b/riscv/int_map.h @@ -1,16 +1,15 @@ #ifndef _RISCV_INT_MAP_H #define _RISCV_INT_MAP_H +#include "common.h" #include <cstdint> #include <climits> -#include <cassert> // Like std::map, but keys are integers -template<typename K, typename V, int lg_radix = 11> +template<typename K, typename V, int lg_n = sizeof(K) * CHAR_BIT, int lg_radix = 11> class int_map { public: - int_map(int lg_n = sizeof(K) * CHAR_BIT) - : lg_n(lg_n) + int_map() { memset(array, 0, sizeof(array)); } @@ -24,15 +23,14 @@ class int_map { return reinterpret_cast<V*>(&array[idx]); } - if (array[idx] == nullptr) - array[idx] = new int_map<K, V, lg_radix>(lg_n - lg_radix); + if (unlikely(array[idx] == nullptr)) + array[idx] = new int_map<K, V, lg_n < 0 ? 0 : lg_n - lg_radix, lg_radix>; return array[idx]->lookup(k >> lg_radix); } private: - int lg_n; - int_map<K, V, lg_radix>* array[size_t(1) << lg_radix]; + int_map<K, V, lg_n < 0 ? 0 : lg_n - lg_radix, lg_radix>* array[size_t(1) << lg_radix]; }; #endif |