IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
mem_align.hxx
Go to the documentation of this file.
1
3
4#pragma once
6#include <type_traits>
7
8namespace ice
9{
10
11 template<typename T> requires std::is_pointer_v<T> || std::is_same_v<T, ice::usize> || std::is_integral_v<T>
18
19 template<typename T> requires std::is_pointer_v<T*>
26
27 inline bool is_aligned(void const* ptr, ice::ualign alignment) noexcept
28 {
29 ice::u32 const align_mask = (static_cast<std::underlying_type_t<ice::ualign>>(alignment) - 1);
30 ice::uptr const ptr_value = reinterpret_cast<ice::uptr>(ptr);
31 return (ptr_value & align_mask) == 0;
32 }
33
34 inline bool is_aligned(ice::u32 val, ice::ualign alignment) noexcept
35 {
36 ice::u32 const align_mask = (static_cast<std::underlying_type_t<ice::ualign>>(alignment) - 1);
37 return (val & align_mask) == 0;
38 }
39
40 template<typename T> requires std::is_pointer_v<T> || std::is_same_v<T, ice::usize> || std::is_integral_v<T>
41 constexpr auto align_to(T value, ice::ualign alignment) noexcept -> ice::AlignResult<T>
42 {
45 .alignment = alignment
46 };
47
48 ice::u32 const align_mask = (static_cast<std::underlying_type_t<ice::ualign>>(alignment) - 1);
49 if constexpr (std::is_pointer_v<T>)
50 {
51 ice::uptr const ptr_value = reinterpret_cast<ice::uptr>(value);
52 result.padding = { (ice::uptr{0} - ptr_value) & align_mask };
53 result.value = reinterpret_cast<T>(ptr_value + result.padding.value);
54 }
55 else if constexpr (std::is_same_v<T, ice::usize>)
56 {
57 result.padding = { ((typename T::base_type{0}) - (value.value)) & align_mask };
58 result.value = value + result.padding;
59 }
60 else
61 {
62 result.padding = (T{ 0 } - value) & align_mask;
63 result.value = value + result.padding;
64 }
65 return result;
66 }
67
68} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
SPDX-License-Identifier: MIT.
Definition array.hxx:12
bool is_aligned(void const *ptr, ice::ualign alignment) noexcept
Definition mem_align.hxx:27
constexpr auto align_to(T value, ice::ualign alignment) noexcept -> ice::AlignResult< T >
Definition mem_align.hxx:41
std::uintptr_t uptr
Definition types.hxx:29
std::uint32_t u32
Definition types.hxx:26
ualign
Definition mem_size_types.hxx:39
@ invalid
Definition mem_size_types.hxx:40
T * value
Definition mem_align.hxx:22
ice::usize padding
Definition mem_align.hxx:23
ice::ualign alignment
Definition mem_align.hxx:24
Definition mem_align.hxx:13
T padding
Definition mem_align.hxx:15
ice::ualign alignment
Definition mem_align.hxx:16
T value
Definition mem_align.hxx:14
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26