-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathalignment.h
More file actions
59 lines (50 loc) · 1.4 KB
/
alignment.h
File metadata and controls
59 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef RDESTL_ALIGNMENT_H
#define RDESTL_ALIGNMENT_H
#include <cstddef>
#include "rdestl_common.h"
namespace rde
{
namespace internal
{
#pragma warning(push)
// structure was padded due to __declspec(align())
#pragma warning(disable: 4324)
template<typename T>
struct alignof_helper
{
char x;
T y;
};
#ifdef _MSC_VER
__declspec(align(16)) struct aligned16 { std::uint64_t member[2]; };
#else
struct __attribute__((aligned(16))) aligned16 { std::uint64_t member[2]; };
#endif
#pragma warning(pop)
template<size_t N> struct type_with_alignment
{
typedef char err_invalid_alignment[N > 0 ? -1 : 1];
};
template<> struct type_with_alignment<0> {};
template<> struct type_with_alignment<1> { std::uint8_t member; };
template<> struct type_with_alignment<2> { std::uint16_t member; };
template<> struct type_with_alignment<4> { std::uint32_t member; };
template<> struct type_with_alignment<8> { std::uint64_t member; };
template<> struct type_with_alignment<16> { aligned16 member; };
} // namespace internal
template<typename T>
struct rde_alignof
{
enum
{
res = offsetof(internal::alignof_helper<T>, y)
};
};
template<typename T>
struct aligned_as
{
typedef typename internal::type_with_alignment<rde_alignof<T>::res> res;
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_ALIGNMENT_H