New PackedPointer class
This commit is contained in:
parent
cc129eb4e2
commit
a2d6944249
2 changed files with 195 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
/* Copyright 2016-2024 Michele Santullo
|
||||
/* Copyright 2016-2025 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
|
|
194
include/duckhandy/packed_pointer.hpp
Normal file
194
include/duckhandy/packed_pointer.hpp
Normal file
|
@ -0,0 +1,194 @@
|
|||
/* Copyright 2016-2025 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "duckhandy" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef id83CE9FAB90684250B98EA584C548E13F
|
||||
#define id83CE9FAB90684250B98EA584C548E13F
|
||||
|
||||
#include "bitfield_pack.hpp"
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <climits>
|
||||
|
||||
namespace dhandy {
|
||||
namespace implem {
|
||||
|
||||
//This monster expression is just the one-liner version of this
|
||||
//implementation of log2:
|
||||
//unsigned int v; // 32-bit value to find the log2 of
|
||||
//register unsigned int r; // result of log2(v) will go here
|
||||
//register unsigned int shift;
|
||||
//
|
||||
//r = (v > 0xFFFF) << 4; v >>= r;
|
||||
//shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;
|
||||
//shift = (v > 0xF ) << 2; v >>= shift; r |= shift;
|
||||
//shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;
|
||||
// r |= (v >> 1);
|
||||
//see https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog for
|
||||
//an explanation of that.
|
||||
//This is a shorter version of it but it only works if the input is a power
|
||||
//of 2:
|
||||
//(((((NUM&0xAAAAAAAA)!=0)|((NUM&0xFFFF0000)!=0)<<4)|((NUM&0xFF00FF00)!=0)<<
|
||||
//3)|((NUM&0xF0F0F0F0)!=0)<<2)|((NUM&0xCCCCCCCC)!=0)<<1
|
||||
//Source for that is also available at the page linked above.
|
||||
template <std::uint32_t NUM> const constexpr std::size_t
|
||||
pointer_unused_bit_count =
|
||||
((((((NUM > 0xFFFF) << 4) | (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) <<
|
||||
3)) | ((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) <<
|
||||
4)) > 0xFF) << 3)) > 0xF) << 2)) | (((((NUM >> ((NUM > 0xFFFF) << 4)) >>
|
||||
(((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) >> ((((NUM >> ((NUM >
|
||||
0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) > 0xF)
|
||||
<< 2)) > 0x3) << 1)) | (((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> (
|
||||
(NUM > 0xFFFF) << 4)) > 0xFF) << 3)) >> ((((NUM >> ((NUM > 0xFFFF) << 4)
|
||||
) >> (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) > 0xF) << 2)) >> ((
|
||||
(((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) << 4)) >
|
||||
0xFF) << 3)) >> ((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM >
|
||||
0xFFFF) << 4)) > 0xFF) << 3)) > 0xF) << 2)) > 0x3) << 1)) >> 1))
|
||||
;
|
||||
|
||||
template <std::size_t... S>
|
||||
BitfieldPack<
|
||||
std::uintptr_t,
|
||||
sizeof(std::uintptr_t) * CHAR_BIT - sizeof...(S), (S+1)/(S+1)...
|
||||
>
|
||||
guess_bitfield_type (std::index_sequence<S...>);
|
||||
|
||||
template <typename T>
|
||||
using BaseBitfieldPackForPackedPointer = decltype(
|
||||
implem::guess_bitfield_type(
|
||||
std::make_index_sequence<implem::pointer_unused_bit_count<alignof(T)>>()
|
||||
)
|
||||
);
|
||||
} //namespace implem
|
||||
|
||||
template <typename T>
|
||||
class PackedPointer : private implem::BaseBitfieldPackForPackedPointer<T> {
|
||||
static_assert(sizeof(T*) == sizeof(std::uintptr_t), "Mismatching int/pointer size");
|
||||
typedef implem::BaseBitfieldPackForPackedPointer<T> parent_type;
|
||||
public:
|
||||
static const constexpr std::size_t FlagsCount = parent_type::TotalEntries - 1;
|
||||
|
||||
PackedPointer();
|
||||
PackedPointer (T* ptr);
|
||||
~PackedPointer() = default;
|
||||
|
||||
operator T*() const;
|
||||
operator bool() const;
|
||||
template <std::size_t Idx> bool flag () const;
|
||||
template <std::size_t Idx> void set_flag (bool v);
|
||||
|
||||
PackedPointer& operator= (T* ptr);
|
||||
T* operator->();
|
||||
const T* operator->() const;
|
||||
T& operator*();
|
||||
const T& operator*() const;
|
||||
bool operator== (const PackedPointer& other) const;
|
||||
bool operator== (T* other) const;
|
||||
bool operator!= (const PackedPointer& other) const;
|
||||
bool operator!= (T* other) const;
|
||||
|
||||
private:
|
||||
//static const constexpr uintptr_t PtrMask = static_cast<uintptr_t>(-1) << FlagsCount;
|
||||
};
|
||||
|
||||
template <typename T> const constexpr std::size_t PackedPointer<T>::FlagsCount;
|
||||
//template <typename T> const constexpr std::uintptr_t PackedPointer<T>::PtrMask;
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::PackedPointer() :
|
||||
PackedPointer(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::PackedPointer (T* ptr) :
|
||||
parent_type(0)
|
||||
{
|
||||
(*this) = ptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::operator T*() const {
|
||||
return reinterpret_cast<T*>(this->get(0) << FlagsCount);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::operator bool() const {
|
||||
return static_cast<T*>(*this) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::size_t Idx>
|
||||
bool PackedPointer<T>::flag() const {
|
||||
static_assert(Idx < FlagsCount, "Index out of range");
|
||||
return static_cast<bool>(this->get(Idx + 1));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::size_t Idx>
|
||||
void PackedPointer<T>::set_flag (bool v) {
|
||||
static_assert(Idx < FlagsCount, "Index out of range");
|
||||
this->set(Idx + 1, (v ? 1 : 0));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>& PackedPointer<T>::operator= (T* ptr) {
|
||||
this->set(0, reinterpret_cast<std::uintptr_t>(ptr) >> FlagsCount);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* PackedPointer<T>::operator->() {
|
||||
return static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T* PackedPointer<T>::operator->() const {
|
||||
return static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T& PackedPointer<T>::operator*() {
|
||||
return *static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T& PackedPointer<T>::operator*() const {
|
||||
return *static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator== (const PackedPointer& other) const {
|
||||
return this == &other or static_cast<T*>(*this) == static_cast<T*>(other);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator== (T* other) const {
|
||||
return static_cast<T*>(*this) == other;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator!= (const PackedPointer& other) const {
|
||||
return not this->operator==(other);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator!= (T* other) const {
|
||||
return not this->operator==(other);
|
||||
}
|
||||
} //namepace dhandy
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue