5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_ALIGNMENT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_ALIGNMENT_HPP_
29#include "arene/base/stdlib_choice/climits.hpp"
30#include "arene/base/stdlib_choice/cstddef.hpp"
31#include "arene/base/stdlib_choice/cstdint.hpp"
32#include "arene/base/stdlib_choice/enable_if.hpp"
33#include "arene/base/stdlib_choice/is_integral.hpp"
34#include "arene/base/stdlib_choice/is_unsigned.hpp"
35#include "arene/base/stdlib_choice/make_unsigned.hpp"
36#include "arene/base/stdlib_choice/numeric_limits.hpp"
44namespace alignment_detail {
47using pointer_to_anything =
void const volatile*;
65 Alignment <= std::numeric_limits<Integer>::max(),
66 "Attempting to align to a value that is larger than the "
67 "maximum representable value."
71 return (
static_cast<unsigned_integer
>(value) & mask) == 0U;
79template <std::
size_t Alignment>
82constexpr auto is_aligned(alignment_detail::pointer_to_anything
const pointer) ->
bool {
99 constraints<std::enable_if_t<std::is_integral<Integer>::value>> =
nullptr>
103 Alignment <= std::numeric_limits<Integer>::max(),
104 "Attempting to align to a value that is larger than the "
105 "maximum representable value."
109 return static_cast<Integer
>(
static_cast<unsigned_integer
>(value) & ~mask);
122 constraints<std::enable_if_t<std::is_integral<Integer>::value>> =
nullptr>
126 Alignment <= std::numeric_limits<Integer>::max(),
127 "Attempting to align to a value that is larger than the "
128 "maximum representable value."
132 return static_cast<Integer
>((
static_cast<unsigned_integer
>(value) + mask) & ~mask);
141template <std::
size_t Alignment>
161template <typename Integer, constraints<std::enable_if_t<std::is_unsigned<Integer>::value>> =
nullptr>
168 if (one_bits != max_bits) {
169 constexpr Integer one{1U};
174 return static_cast<Integer
>((one << one_bits) - one);
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr auto align_floor(Integer value) -> Integer
Align a value to the nearest multiple of alignment, rounding down.
Definition alignment.hpp:100
auto alignment_offset(alignment_detail::pointer_to_anything const ptr) noexcept -> std::size_t
Calculate the amount by which ptr needs to be adjusted to have the specified Alignment.
Definition alignment.hpp:143
constexpr auto align_ceil(Integer value) -> Integer
Align a value to the nearest multiple of alignment, rounding up.
Definition alignment.hpp:123
constexpr auto is_power_of_2(Integer value) noexcept -> bool
Check if an integer is a positive power-of-two.
Definition power_of_2.hpp:35
constexpr auto bit_mask(std::uint32_t const one_bits) -> Integer
Construct a bit-mask with the lowest one_bits bits set to 1 and the rest of the bits set to 0.
Definition alignment.hpp:162
constexpr auto is_aligned(Integer value) -> bool
Check if an integer is a multiple of the specified power-of-two alignment.
Definition alignment.hpp:62
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition cstdint.hpp:41
::uintptr_t uintptr_t
An unsigned integer type the same size as a pointer.
Definition cstdint.hpp:81
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252