acl/core/memory_utils.h¶
#include "acl/core/memory_utils.h"
The functions for alignment, safe casts, byte order, unaligned memory access, and memory prefetch.
static_condition¶
template<bool expression_result>
struct static_condition
A compile time condition that the code can test without a warning about a constant expression.
static_condition::test¶
static constexpr bool test()
Returns the value of the expression.
static_condition<false>¶
template<>
struct static_condition<false>
The specialization of static_condition for a false expression.
static_condition<false>::test¶
static constexpr bool test()
Returns the value of the expression.
is_power_of_two¶
constexpr bool is_power_of_two(size_t input)
Returns true if the input is a power of two. Zero is not a power of two.
is_alignment_valid¶
template<typename Type>
constexpr bool is_alignment_valid(size_t alignment)
Returns true if the alignment is a power of two and is at least the alignment of the given type.
is_aligned_to¶
template<typename PtrType>
bool is_aligned_to(PtrType* value, size_t alignment)
Returns true if the pointer is a multiple of the alignment. The alignment must be a power of two.
template<typename IntegralType>
bool is_aligned_to(IntegralType value, size_t alignment)
Returns true if the value is a multiple of the alignment. The alignment must be a power of two.
is_aligned¶
template<typename PtrType>
constexpr bool is_aligned(PtrType* value)
Returns true if the pointer has the alignment of the type that it points to.
align_to¶
template<typename PtrType>
PtrType* align_to(PtrType* value, size_t alignment)
Returns the pointer, rounded up to the next multiple of the alignment. The alignment must be a power of two.
template<typename IntegralType>
IntegralType align_to(IntegralType value, size_t alignment)
Returns the value, rounded up to the next multiple of the alignment. The alignment must be a power of two.
get_required_padding¶
template<typename PreviousMemberType, typename NextMemberType>
constexpr size_t get_required_padding()
Returns the number of padding bytes between a member of the first type and a member of the second type that follows it.
get_array_size¶
template<typename ElementType, size_t num_elements>
constexpr size_t get_array_size(ElementType const (&)[num_elements])
Returns the number of elements in a C array.
safe_ptr_cast¶
template<typename dest_ptr_t, typename src_ptr_t>
dest_ptr_t* safe_ptr_cast(src_ptr_t* input)
Casts a pointer type into a pointer of a different type with an alignment runtime check.
template<typename dest_ptr_t, typename src_int_t>
dest_ptr_t* safe_ptr_cast(src_int_t input)
Casts an integral type into a pointer type with an alignment runtime check.
safe_static_cast¶
template<typename DstType, typename SrcType>
DstType safe_static_cast(SrcType input)
Converts a value with static_cast. When asserts are enabled, the function asserts if the conversion loses data.
add_offset_to_ptr¶
template<typename OutputPtrType, typename InputPtrType, typename offset_type>
OutputPtrType* add_offset_to_ptr(InputPtrType* ptr, offset_type offset)
Adds an offset in bytes to a pointer and returns it as a pointer to the output type. When asserts are enabled, the function asserts if the result does not have the alignment of the output type.
byte_swap¶
uint16_t byte_swap(uint16_t value)
uint32_t byte_swap(uint32_t value)
uint64_t byte_swap(uint64_t value)
Returns the value with the order of its bytes reversed.
memcpy_bits¶
void memcpy_bits(
void* dest,
uint64_t dest_bit_offset,
const void* src,
uint64_t src_bit_offset,
uint64_t num_bits_to_copy)
We copy bits assuming big-endian ordering for dest and src.
unaligned_load¶
template<typename data_type>
data_type unaligned_load(const void* input)
Reads a value from memory that can be unaligned.
aligned_load¶
template<typename data_type>
data_type aligned_load(const void* input)
Reads a value from memory that has the alignment of the type.
unaligned_write¶
template<typename data_type>
void unaligned_write(data_type input, void* output)
Writes a value to memory that can be unaligned.
memory_prefetch¶
void memory_prefetch(const void* ptr)
Tells the processor to load the cache line at the given address into the L1 cache. The function does not wait for the data and does nothing on platforms without a prefetch instruction.
memory_prefetch_into_L1¶
void memory_prefetch_into_L1(const void* ptr)
Tells the processor to load the cache line at the given address into the L1 cache. The function does not wait for the data and does nothing on platforms without a prefetch instruction.
memory_prefetch_into_L2¶
void memory_prefetch_into_L2(const void* ptr)
Tells the processor to load the cache line at the given address into the L2 cache. The function does not wait for the data and does nothing on platforms without a prefetch instruction.
memory_fetch_into_L1¶
void memory_fetch_into_L1(const void* ptr)
Reads one byte at the given address to load its cache line into the L1 cache. Unlike a prefetch, the read must complete.