tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
pointer_int_pair_info.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_POINTER_INT_PAIR_INFO_HPP
4#define TETL_MEMORY_POINTER_INT_PAIR_INFO_HPP
5
11
12namespace etl {
13
14template <typename PointerT, unsigned IntBits, typename PtrTraits>
16 // clang-format off
17 static_assert(PtrTraits::free_bits < numeric_limits<etl::uintptr_t>::digits, "cannot use a pointer type that has all bits free");
18 static_assert(IntBits <= PtrTraits::free_bits, "pointer_int_pair with integer size too large for pointer");
19 // clang-format on
20
21 using pointer_type = PointerT;
22 using pointer_traits = PtrTraits;
23 static constexpr auto int_bits = IntBits;
24 static constexpr auto free_bits = pointer_traits::free_bits;
25
27 static constexpr auto ptr_mask = ~static_cast<etl::uintptr_t>((static_cast<etl::intptr_t>(1) << free_bits) - 1);
28
31 static constexpr auto int_shift = static_cast<etl::uintptr_t>(pointer_traits::free_bits - int_bits);
32
35 static constexpr auto int_mask = static_cast<etl::uintptr_t>((static_cast<etl::intptr_t>(1) << int_bits) - 1);
36
38 static constexpr auto shifted_int_mask = static_cast<etl::uintptr_t>(int_mask << int_shift);
39
40 [[nodiscard]] static auto get_pointer(etl::intptr_t value) -> pointer_type
41 {
42 return pointer_traits::get_from_void_pointer(etl::bit_cast<void*>(static_cast<etl::uintptr_t>(value) & ptr_mask)
43 );
44 }
45
46 [[nodiscard]] static auto get_int(etl::intptr_t value) -> etl::intptr_t
47 {
48 return (static_cast<etl::uintptr_t>(value) >> int_shift) & int_mask;
49 }
50
51 [[nodiscard]] static auto update_ptr(etl::intptr_t value, pointer_type ptr) -> etl::intptr_t
52 {
53 // Preserve all low bits, just update the pointer.
54 auto* voidPtr = pointer_traits::get_as_void_pointer(ptr);
55 auto ptrWord = etl::bit_cast<etl::uintptr_t>(voidPtr);
56 return static_cast<etl::intptr_t>(ptrWord | (static_cast<etl::uintptr_t>(value) & ~ptr_mask));
57 }
58
59 [[nodiscard]] static auto update_int(etl::intptr_t value, etl::intptr_t integer) -> etl::intptr_t
60 {
61 // Preserve all bits other than the ones we are updating.
62 auto const uinteger = static_cast<etl::uintptr_t>(integer);
63 return static_cast<etl::intptr_t>(
64 (static_cast<etl::uintptr_t>(value) & ~shifted_int_mask) | uinteger << int_shift
65 );
66 }
67};
68
69} // namespace etl
70
71#endif // TETL_MEMORY_POINTER_INT_PAIR_INFO_HPP
constexpr auto bit_cast(From const &src) noexcept -> To
Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value...
Definition bit_cast.hpp:38
Definition adjacent_find.hpp:8
TETL_BUILTIN_UINTPTR uintptr_t
Unsigned integer type capable of holding a pointer.
Definition uintptr_t.hpp:11
TETL_BUILTIN_INTPTR intptr_t
Signed integer type capable of holding a pointer.
Definition intptr_t.hpp:11
Definition pointer_int_pair_info.hpp:15
static constexpr auto int_mask
This is the unshifted mask for valid bits of the int type.
Definition pointer_int_pair_info.hpp:35
static constexpr auto int_bits
Definition pointer_int_pair_info.hpp:23
PointerT pointer_type
Definition pointer_int_pair_info.hpp:21
static constexpr auto free_bits
Definition pointer_int_pair_info.hpp:24
PtrTraits pointer_traits
Definition pointer_int_pair_info.hpp:22
static auto update_ptr(etl::intptr_t value, pointer_type ptr) -> etl::intptr_t
Definition pointer_int_pair_info.hpp:51
static constexpr auto ptr_mask
The bits that come from the pointer.
Definition pointer_int_pair_info.hpp:27
static auto update_int(etl::intptr_t value, etl::intptr_t integer) -> etl::intptr_t
Definition pointer_int_pair_info.hpp:59
static constexpr auto int_shift
The number of low bits that we reserve for other uses; and keep zero.
Definition pointer_int_pair_info.hpp:31
static constexpr auto shifted_int_mask
This is the bits for the integer shifted in place.
Definition pointer_int_pair_info.hpp:38
static auto get_int(etl::intptr_t value) -> etl::intptr_t
Definition pointer_int_pair_info.hpp:46
static auto get_pointer(etl::intptr_t value) -> pointer_type
Definition pointer_int_pair_info.hpp:40