From 28cbc89840ceff1961a6448a454701724d4fdd4d Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 17 Jan 2025 14:30:55 -0500 Subject: [PATCH] hi --- CMakeLists.txt | 2 +- include/blt/std/memory_util.h | 263 ++++++++++++++++++++-------------- libraries/parallel-hashmap | 2 +- 3 files changed, 159 insertions(+), 108 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c96aed6..34bb18a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 3.0.3) +set(BLT_VERSION 3.0.4) set(BLT_TARGET BLT) diff --git a/include/blt/std/memory_util.h b/include/blt/std/memory_util.h index 85d5230..a399aa0 100644 --- a/include/blt/std/memory_util.h +++ b/include/blt/std/memory_util.h @@ -27,6 +27,7 @@ #include #include #include +#include #if defined(__clang__) || defined(__llvm__) || defined(__GNUC__) || defined(__GNUG__) @@ -142,112 +143,6 @@ namespace blt::mem return prev_size + default_allocation_block; } - // TODO: check if the platform actually has enough room in their pointers for this. plus endianness - - struct bit_storage - { - static constexpr std::size_t START_BIT = 48; - static constexpr std::size_t END_BIT = sizeof(std::uintptr_t) * CHAR_BIT; - static constexpr std::size_t AVAILABLE_BITS = END_BIT - START_BIT; - - static constexpr std::size_t make_storage_ones() - { - std::size_t result = 0; - for (std::size_t i = START_BIT; i < END_BIT; i++) - result |= 1ul << i; - return result; - } - - static constexpr std::size_t make_ptr_ones() - { - std::size_t result = 0; - for (std::size_t i = 0; i < START_BIT; i++) - result |= 1ul << i; - return result; - } - - std::uint16_t bits : AVAILABLE_BITS; - }; - - template - struct pointer_storage - { - static constexpr std::size_t STORAGE_ALL_ONES = bit_storage::make_storage_ones(); - static constexpr std::size_t PTR_ALL_ONES = bit_storage::make_ptr_ones(); - - explicit pointer_storage(Ptr* ptr): ptr_bits(reinterpret_cast(ptr)) - { - } - - explicit pointer_storage(Ptr* ptr, const bit_storage bits): ptr_bits(reinterpret_cast(ptr)) - { - storage(bits); - } - - [[nodiscard]] bit_storage storage() const noexcept - { - bit_storage storage{}; - storage.bits = ptr_bits & STORAGE_ALL_ONES; - return storage; - } - - pointer_storage& storage(const bit_storage bits) noexcept - { - ptr_bits |= (ptr_bits & PTR_ALL_ONES) | (bits.bits << bit_storage::START_BIT); - return *this; - } - - pointer_storage& operator=(const bit_storage bits) noexcept - { - storage(bits); - return *this; - } - - pointer_storage& clear_storage() noexcept - { - ptr_bits &= PTR_ALL_ONES; - return *this; - } - - // changes pointer without changing the tag bits - pointer_storage& pointer(Ptr* ptr) noexcept - { - const bit_storage old_storage = storage(); - ptr_bits = (reinterpret_cast(ptr) & PTR_ALL_ONES) | old_storage.bits; - return *this; - } - - pointer_storage& operator=(Ptr* ptr) noexcept - { - pointer(ptr); - return *this; - } - - pointer_storage& clear_pointer() noexcept - { - ptr_bits &= STORAGE_ALL_ONES; - return *this; - } - - Ptr* get() const noexcept - { - return reinterpret_cast(ptr_bits & PTR_ALL_ONES); - } - - Ptr& operator*() const noexcept - { - return *get(); - } - - Ptr* operator->() const noexcept - { - return get(); - } - - private: - std::uintptr_t ptr_bits; - }; - template void print_bytes(OStream& stream, const Value& value) { @@ -295,6 +190,162 @@ namespace blt::mem stream << line; stream << '\n'; } + + // TODO: check if the platform actually has enough room in their pointers for this. plus endianness + + struct bit_storage + { + static constexpr std::size_t START_BIT = 48; + static constexpr std::size_t END_BIT = sizeof(std::uintptr_t) * CHAR_BIT; + static constexpr std::size_t AVAILABLE_BITS = END_BIT - START_BIT; + + static constexpr std::size_t make_storage_ones() + { + std::size_t result = 0; + for (std::size_t i = START_BIT; i < END_BIT; i++) + result |= 1ul << i; + return result; + } + + static constexpr std::size_t make_ptr_ones() + { + std::size_t result = 0; + for (std::size_t i = 0; i < START_BIT; i++) + result |= 1ul << i; + return result; + } + + bit_storage(): bits(0) + { + } + + explicit bit_storage(const std::uint16_t bits): bits(bits) + { + } + + std::uint16_t bits : AVAILABLE_BITS; + }; + + template + struct pointer_storage + { + static constexpr std::size_t STORAGE_ALL_ONES = bit_storage::make_storage_ones(); + static constexpr std::size_t PTR_ALL_ONES = bit_storage::make_ptr_ones(); + + explicit pointer_storage(Ptr* ptr): ptr_bits(reinterpret_cast(ptr)) + { + } + + explicit pointer_storage(Ptr* ptr, const bit_storage bits): ptr_bits(reinterpret_cast(ptr)) + { + storage(bits); + } + + template , bool> = false> + explicit pointer_storage(Ptr* ptr, const T& type): ptr_bits(reinterpret_cast(ptr)) + { + storage(type); + } + + [[nodiscard]] bit_storage storage() const noexcept + { + bit_storage storage{}; + storage.bits = (ptr_bits & STORAGE_ALL_ONES) >> bit_storage::START_BIT; + return storage; + } + + [[nodiscard]] bool bit(const std::size_t index) const noexcept + { + if (index >= bit_storage::END_BIT) + return false; + return (ptr_bits >> (bit_storage::START_BIT + index)) & 1; + } + + pointer_storage& bit(const std::size_t index, const bool b) noexcept + { + if (index >= bit_storage::END_BIT) + return *this; + ptr_bits &= ~(1ul << bit_storage::START_BIT + index); + ptr_bits |= (static_cast(b) << bit_storage::START_BIT + index); + return *this; + } + + template, bool> = false> + pointer_storage& storage(const T& type) + { + static_assert(sizeof(T) <= sizeof(std::uintptr_t), "Type takes too many bits to be stored!"); + static constexpr std::uintptr_t store_bits = (2 << (bit_storage::AVAILABLE_BITS - 1)) - 1; + std::uintptr_t bit_store = 0; + bit_storage store{}; + std::memcpy(&bit_store, &type, sizeof(T)); + store.bits |= bit_store & store_bits; + storage(store); + return *this; + } + + pointer_storage& storage(const bit_storage bits) noexcept + { + ptr_bits = ((ptr_bits & PTR_ALL_ONES) | (static_cast(bits.bits) << bit_storage::START_BIT)); + return *this; + } + + pointer_storage& operator=(const bit_storage bits) noexcept + { + storage(bits); + return *this; + } + + template, bool> = false> + pointer_storage& operator=(const T& type) + { + storage(type); + return *this; + } + + pointer_storage& clear_storage() noexcept + { + ptr_bits &= PTR_ALL_ONES; + return *this; + } + + // changes pointer without changing the tag bits + pointer_storage& pointer(Ptr* ptr) noexcept + { + const bit_storage old_storage = storage(); + ptr_bits = (reinterpret_cast(ptr) & PTR_ALL_ONES) | old_storage.bits; + return *this; + } + + pointer_storage& operator=(Ptr* ptr) noexcept + { + pointer(ptr); + return *this; + } + + pointer_storage& clear_pointer() noexcept + { + ptr_bits &= STORAGE_ALL_ONES; + return *this; + } + + Ptr* get() const noexcept + { + return reinterpret_cast(ptr_bits & PTR_ALL_ONES); + } + + Ptr& operator*() const noexcept + { + return *get(); + } + + Ptr* operator->() const noexcept + { + return get(); + } + + private: + std::uintptr_t ptr_bits; + }; } namespace blt diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 8a889d3..d88c5e1 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 8a889d3699b3c09ade435641fb034427f3fd12b6 +Subproject commit d88c5e15079047777b418132ece5879e7c9aaa2b