-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligned_array.hpp
411 lines (371 loc) · 9.49 KB
/
aligned_array.hpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* @file aligned_array.hpp
* @author <[email protected]>
* @date Tue Jul 14 14:43:20 2015
*
* @brief The following code declares class aligned_array, an STL-like
* container (as wrapper) for arrays of constant size. The memory is
* aligned on a 16-byte boundary
*
*/
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <sps/cenv.h>
#include <sps/sps_export.h>
#include <sps/memory>
#include <cstddef>
#include <stdexcept>
#include <iterator>
#include <algorithm>
#include <cassert>
#include <utility> // std::swap
namespace sps {
template <typename T, std::size_t N>
class SPS_EXPORT aligned_array {
public:
#ifdef __GNUG__
T elems[N] __attribute__((aligned(4*sizeof(T)))); ///< fixed-size array of elements
#elif defined(_MSC_VER) && (_MSC_VER >= 1900)
__declspec(align(4*sizeof(T))) T elems[N]; ///< fixed-size array of elements
#elif defined(_MSC_VER)
// Workaround - aligned enough for double precision
ALIGN32_BEGIN T elems[N] ALIGN32_END; ///< fixed-size array of elements
#else
# error Unsupported Compiler
#endif
public:
/** @name Type definitions
*
*/
///@{
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
///@}
/** @name Iterator support
*
*/
///@{
iterator begin() {
return elems;
}
const_iterator begin() const {
return elems;
}
iterator end() {
return elems+N;
}
const_iterator end() const {
return elems+N;
}
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
reverse_iterator rbegin() {
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
///@}
reference operator[](size_type i) {
return elems[i];
}
const_reference operator[](size_type i) const {
return elems[i];
}
/** @name at() with range check
*
*/
///@{
reference at(size_type i) {
rangecheck(i);
return elems[i];
}
const_reference at(size_type i) const {
rangecheck(i);
return elems[i];
}
///@}
/** @name front() and back()
*
*/
///@{
reference front() {
return elems[0];
}
const_reference front() const {
return elems[0];
}
reference back() {
return elems[N-1];
}
const_reference back() const {
return elems[N-1];
}
///@}
// size is constant
static size_type size() {
return N;
}
static bool empty() {
return false;
}
static size_type max_size() {
return N;
}
enum { static_size = N };
/**
* swap (note: linear complexity in N, constant for given instantiation)
*
* @param T
* @param y
*/
void swap(aligned_array<T, N>& y) {
#if (defined(_MSC_VER) && (_MSC_VER > 1900)) || (defined(_MSC_VER) && !defined(_DEBUG))
// Gives no warnings, but linker errors in debug mode (older visual studio)
std::swap_ranges(
begin(),
end(),
stdext::make_checked_array_iterator(
y.begin(),
std::distance(begin(), end())));
#else
std::swap_ranges(begin(), end(), y.begin());
#endif
}
/**
* Direct access to data (read-only)
*
*
* @return
*/
const T* data() const {
return elems;
}
/**
* Use array as C array (direct read/write access to data)
*
*
* @return
*/
T* data() {
return elems;
}
/**
* assignment with type conversion
*
* @tparam T2
* @param rhs
*
* @return
*/
template <typename T2>
aligned_array<T, N>& operator= (const aligned_array<T2, N>& rhs) {
std::copy(rhs.begin(), rhs.end(), begin());
return *this;
}
/**
* assign one value to all elements
*
* @param value
*/
void assign(const T& value) {
#if defined(_MSC_VER) && defined(NDEBUG)
std::fill_n(
stdext::make_checked_array_iterator(
begin(),
std::distance(begin(), end())), size(), value);
#else
std::fill_n(begin(), size(), value);
#endif
}
private:
/**
* check range (may be private because it is static)
*
* @param i
*/
static void rangecheck(size_type i) {
if (i >= size()) {
throw std::out_of_range("aligned_array<>: index out of range");
}
}
};
template <typename T>
class aligned_array<T, 0> {
public:
char c; // to ensure different array intances return unique values for begin/end
public:
// type definitions
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// iterator support
iterator begin() {
return reinterpret_cast< iterator >( &c );
}
const_iterator begin() const {
return reinterpret_cast< const_iterator >( &c );
}
iterator end() {
return reinterpret_cast< iterator >( &c );
}
const_iterator end() const {
return reinterpret_cast< const_iterator >( &c );
}
// reverse iterator support
#if 1
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#else
// workaround for broken reverse_iterator implementations
typedef std::reverse_iterator<iterator, T> reverse_iterator;
typedef std::reverse_iterator<const_iterator, T> const_reverse_iterator;
#endif
reverse_iterator rbegin() {
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
// at() with range check
reference at(size_type i) {
throw std::out_of_range("aligned_array<0>: index out of range");
}
const_reference at(size_type i) const {
throw std::out_of_range("<0>: index out of range");
}
// size is constant
static size_type size() {
return 0;
}
static bool empty() {
return true;
}
static size_type max_size() {
return 0;
}
enum { static_size = 0 };
// swap
void swap(aligned_array<T, 0>& y) {
// could swap value of c, but value is not part of documented array state
}
// direct access to data
const T* data() const {
return NULL;
}
T* data() {
return NULL;
}
// assignment with type conversion
template < typename T2 >
aligned_array<T, 0>& operator= (const aligned_array<T2, 0>& rhs) {
return *this;
}
// Calling these operations are undefined behaviour for 0-size arrays,
// but Library TR1 requires their presence.
// operator[]
reference operator[](size_type i) {
makes_no_sense();
}
const_reference operator[](size_type i) const {
makes_no_sense();
}
// front() and back()
reference front() {
makes_no_sense();
}
const_reference front() const {
makes_no_sense();
}
reference back() {
makes_no_sense();
}
const_reference back() const {
makes_no_sense();
}
private:
// helper for operations that have undefined behaviour for 0-size arrays,
// assert( false ); added to make lack of support clear
static void makes_no_sense() {
assert(true);
throw std::out_of_range("aligned_array<0>: index out of range");
}
};
// comparisons
template<class T, std::size_t N>
bool operator== (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return std::equal(x.begin(), x.end(), y.begin());
}
template<class T, std::size_t N>
bool operator< (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return std::lexicographical_compare(
x.begin(),
x.end(),
y.begin(),
y.end());
}
template<class T, std::size_t N>
bool operator!= (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return !(x == y);
}
template<class T, std::size_t N>
bool operator> (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return y < x;
}
template<class T, std::size_t N>
bool operator<= (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return !(y < x);
}
template<class T, std::size_t N>
bool operator>= (const aligned_array<T, N>& x, const aligned_array<T, N>& y) {
return !(x < y);
}
// global swap()
template<class T, std::size_t N>
inline void swap(aligned_array<T, N>& x, aligned_array<T, N>& y) {
x.swap(y);
}
template class SPS_EXPORT aligned_array<float, 4>;
template class SPS_EXPORT aligned_array<double, 4>;
} // namespace sps
/* Local variables: */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* indent-tabs-mode: nil */
/* End: */