-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.h
160 lines (130 loc) · 3.98 KB
/
array.h
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
#ifndef SIMD_ARRAY_H
#define SIMD_ARRAY_H
#include "memalign.h"
#include <stdint.h>
#include <typeinfo>
#include <algorithm> // for std::copy
#include <list> // for std::initializer_list
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
STL container for an SIMD optimized, 32-bit aligned array of template elements
In order to optimize with SIMD instructions, the actual size of the allocated
space for the vector's elements is rounded up to the nearest multiple of 4.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
template <class T>
class SIMDArray
{
public:
/**
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Iterators
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* STL-like iteraters over the array
*
* :see
* http://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls/8054856#8054856
* https://gist.github.com/jeetsukumaran/307264#file-custom_iterator-cpp-L50
* http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622
*/
class iterator
{
public:
typedef iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::forward_iterator_tag iterator_category;
typedef int difference_type;
iterator(pointer ptr) : ptr_(ptr) {}
~iterator(){}
self_type operator++() { self_type i = *this; ptr_++; return i; }
self_type operator++( int i ) { ptr_++; return *this; }
reference operator*() { return *(*ptr_); }
pointer operator->() { return *ptr_; }
bool operator==(const self_type& rhs) { return ptr_ == rhs.ptr_; }
bool operator!=(const self_type& rhs) { return ptr_ != rhs.ptr_; }
private:
pointer ptr_;
};
class const_iterator
{
public:
typedef const_iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::forward_iterator_tag iterator_category;
typedef int difference_type;
const_iterator(pointer ptr) : ptr_(ptr) {}
~const_iterator(){}
self_type operator++() { self_type i = *this; ptr_++; return i; }
self_type operator++( int i ) { ptr_++; return *this; }
const reference operator*() { return *(*ptr_); }
const pointer operator->() { return *ptr_; }
bool operator==(const self_type& rhs) { return ptr_ == rhs.ptr_; }
bool operator!=(const self_type& rhs) { return ptr_ != rhs.ptr_; }
private:
pointer ptr_;
};
iterator begin() { return iterator(this->_elems); }
iterator end() { return iterator(this->_elems + this->_size); }
const_iterator begin() const { return const_iterator(this->elems_); }
const_iterator end() const { return const_iterator(this->elems_ + this->size_); }
// Create an empty SIMDArray of 'length' elements
// Also serves as a target constructor for the other constructors
SIMDArray( size_t length )
{
this->_length = length;
div_t d = div(length,4); // calculate quotient and remainder in one step
this->_size = (size_t)( d.quot ? length+d.rem : length);
ALIGNED_MALLOC(this->elems,32,this->_size * sizeof(T));
}
// default constructor
SIMDArray() : SIMDArray(0){}
// C++11 list initialization constructor
SIMDArray( std::initializer_list<T> list ) : SIMDArray( (size_t)list.length )
{
// initialize the members from the list
// for (auto it = list.begin(); it != list.end(); ++it)
// {
// }
}
~SIMDArray()
{
free(this->elems);
}
T operator[] ( size_t i ) const
{
return this->elems[i];
}
T& operator[] ( size_t i )
{
return this->elems[i];
}
size_t length() const
{
return this->_length;
}
size_t size() const
{
return this->_size;
}
/**
*
* return an new SIMDArray Object
*
*/
SIMDArray<T> slice( size_t begin, size_t end )
{
auto other = new SIMDArray( end - begin );
std::copy( this->elems + begin, this->elems + end, other.begin() );
return other;
}
T* elems ALIGN(32);
protected:
size_t _length; // length may be odd or even
size_t _size; // size will always be even and a multiple of 4
};
#endif