File: alignment.cpp

package info (click to toggle)
immer 0.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,812 kB
  • sloc: cpp: 39,727; python: 534; makefile: 227; lisp: 175; sh: 114; javascript: 64
file content (160 lines) | stat: -rw-r--r-- 3,643 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <immer/box.hpp>
#include <immer/flex_vector.hpp>
#include <immer/map.hpp>
#include <immer/set.hpp>
#include <immer/table.hpp>
#include <immer/vector.hpp>

#include <catch2/catch_test_macros.hpp>

#include <array>
#include <cstdint>

// 16-byte alignment seems to happen by default for box, map, set, is
// only broken by 32, but that incurs over-alignment which requires a custom
// allocator anyway...
constexpr unsigned alignment = 16;

// just in case alignment issues only show up for the non-first element
constexpr int N = 4;

// A structure with the same alignment requirements as Eigen's vectorized types
// (which is what originally exposed this issue)
struct AlignConstrainedType
{
    alignas(alignment) std::array<double, 4> buf = {{0, 0, 0, 0}};

    AlignConstrainedType() = default;

    // For the set case
    AlignConstrainedType(double k)
        : buf{{k, k, k, k}} {};

    // For the set case
    bool operator==(const AlignConstrainedType& o) const
    {
        return o.buf == buf;
    }

    // https://stackoverflow.com/a/42093940
    bool is_aligned() const
    {
        const auto rem = reinterpret_cast<intptr_t>(this) % alignment;
        return !rem;
    }
};
static_assert(alignof(AlignConstrainedType) == alignment, "bad alignment");
static_assert(alignof(AlignConstrainedType) <= alignof(max_align_t),
              "bad size alignment");

// For the set case
namespace std {
template <>
struct hash<AlignConstrainedType>
{
    size_t operator()(const AlignConstrainedType& v) const
    {
        return v.buf[0] + v.buf[1] + v.buf[2] +
               v.buf[3]; // terrible but valid hash
    };
};
} // namespace std

// For the table case
struct TableElem
{
    int id;
    AlignConstrainedType value;
};
static_assert(alignof(TableElem) == alignment, "bad alignment");

TEST_CASE("Sanity check: std::vector")
{
    std::vector<AlignConstrainedType> v;
    for (int i = 0; i < N; ++i) {
        v.emplace_back();
    }

    for (const auto& elem : v) {
        CHECK(elem.is_aligned());
    }
}

TEST_CASE("Simple alignment, vector")
{
    immer::vector<AlignConstrainedType> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).push_back({});
    }

    for (const auto& elem : v) {
        CHECK(elem.is_aligned());
    }
}

TEST_CASE("Simple alignment, vector of box")
{
    // Checking many boxes just in case one of them happens to align
    immer::vector<immer::box<AlignConstrainedType>> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).push_back({});
    }

    for (const auto& elem : v) {
        CHECK(elem->is_aligned());
    }
}

TEST_CASE("Simple alignment, flex_vector")
{
    immer::flex_vector<AlignConstrainedType> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).push_back({});
    }

    for (const auto& elem : v) {
        CHECK(elem.is_aligned());
    }
}

TEST_CASE("Simple alignment, set")
{
    immer::set<AlignConstrainedType> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).insert({static_cast<double>(i)});
    }

    for (const auto& elem : v) {
        CHECK(elem.is_aligned());
    }
}

TEST_CASE("Simple alignment, map")
{
    immer::map<int, AlignConstrainedType> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).insert({i, AlignConstrainedType{}});
    }

    for (const auto& elem : v) {
        CHECK(elem.second.is_aligned());
    }
}

TEST_CASE("Simple alignment, table")
{
    immer::table<TableElem> v;

    for (int i = 0; i < N; ++i) {
        v = std::move(v).insert({i, AlignConstrainedType{}});
    }

    for (const auto& elem : v) {
        CHECK(elem.value.is_aligned());
    }
}