IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
memsys
public
ice
mem_align.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/mem_size_types.hxx
>
6
#include <type_traits>
7
8
namespace
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>
12
struct
AlignResult
13
{
14
T
value
;
15
T
padding
;
16
ice::ualign
alignment
;
17
};
18
19
template
<
typename
T>
requires
std::is_pointer_v<T*>
20
struct
AlignResult
<T*>
21
{
22
T*
value
;
23
ice::usize
padding
;
24
ice::ualign
alignment
;
25
};
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
{
43
ICE_ASSERT_CORE
(alignment !=
ice::ualign::invalid
);
44
ice::AlignResult<T>
result{
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
ICE_ASSERT_CORE
#define ICE_ASSERT_CORE(expression)
Definition
assert_core.hxx:43
mem_size_types.hxx
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::is_aligned
bool is_aligned(void const *ptr, ice::ualign alignment) noexcept
Definition
mem_align.hxx:27
ice::align_to
constexpr auto align_to(T value, ice::ualign alignment) noexcept -> ice::AlignResult< T >
Definition
mem_align.hxx:41
ice::uptr
std::uintptr_t uptr
Definition
types.hxx:29
ice::u32
std::uint32_t u32
Definition
types.hxx:26
ice::ualign
ualign
Definition
mem_size_types.hxx:39
ice::ualign::invalid
@ invalid
Definition
mem_size_types.hxx:40
ice::AlignResult< T * >::value
T * value
Definition
mem_align.hxx:22
ice::AlignResult< T * >::padding
ice::usize padding
Definition
mem_align.hxx:23
ice::AlignResult< T * >::alignment
ice::ualign alignment
Definition
mem_align.hxx:24
ice::AlignResult
Definition
mem_align.hxx:13
ice::AlignResult::padding
T padding
Definition
mem_align.hxx:15
ice::AlignResult::alignment
ice::ualign alignment
Definition
mem_align.hxx:16
ice::AlignResult::value
T value
Definition
mem_align.hxx:14
ice::usize
Represents a unsigned size value on the given platform.
Definition
mem_size_types.hxx:26
Generated by
1.18.0