forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim4.cpp
More file actions
executable file
·249 lines (216 loc) · 5.39 KB
/
dim4.cpp
File metadata and controls
executable file
·249 lines (216 loc) · 5.39 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
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <limits>
#include <numeric>
#include <cmath>
#include <vector>
#include <iostream>
#include <af/dim4.hpp>
#include <ArrayInfo.hpp>
#include <err_common.hpp>
namespace af
{
using std::vector;
using std::numeric_limits;
using std::abs;
dim4::dim4()
{
dims[0] = 0;
dims[1] = 0;
dims[2] = 0;
dims[3] = 0;
}
dim4::dim4( dim_type first,
dim_type second,
dim_type third,
dim_type fourth)
{
dims[0] = first;
dims[1] = second;
dims[2] = third;
dims[3] = fourth;
}
dim4::dim4(const dim4& other)
{
dims[0] = other.dims[0];
dims[1] = other.dims[1];
dims[2] = other.dims[2];
dims[3] = other.dims[3];
}
dim4::dim4(const unsigned ndims_, const dim_type * const dims_)
{
for (unsigned i = 0; i < 4; i++) {
dims[i] = ndims_ > i ? dims_[i] : 1;
}
}
dim_type
dim4::elements() const
{
return dims[0] * dims[1] * dims[2] * dims[3];
}
dim_type
dim4::elements()
{
return static_cast<const dim4&>(*this).elements();
}
dim_type
dim4::ndims() const
{
dim_type ret = 4;
for(int i = 3; i >= 1; i--) {
if(dims[i] == 1) { ret--; }
else { break; }
}
return ret;
}
dim_type
dim4::ndims()
{
return static_cast<const dim4&>(*this).ndims();
}
const dim_type&
dim4::operator[](const unsigned dim) const
{
return dims[dim];
}
dim_type &
dim4::operator[](const unsigned dim)
{
return const_cast<dim_type&>(static_cast<const dim4&>((*this))[dim]);
}
bool
dim4::operator==(const dim4 &other) const
{
bool ret = true;
for(unsigned i = 0; i < 4 && ret; i++) {
ret = (*this)[i] == other[i];
}
return ret;
}
bool
dim4::operator!=(const dim4 &other) const
{
return !((*this) == other);
}
dim4&
dim4::operator*=(const dim4 &other)
{
for(unsigned i = 0; i < 4; i++) {
(*this)[i] *= other[i];
}
return *this;
}
dim4&
dim4::operator+=(const dim4 &other)
{
for(unsigned i = 0; i < 4; i++) {
(*this)[i] = (*this)[i] + other[i];
}
return *this;
}
dim4&
dim4::operator-=(const dim4 &other)
{
for(unsigned i = 0; i < 4; i++) {
(*this)[i] = (*this)[i] - other[i];
}
return *this;
}
dim4 operator+(const dim4& first, const dim4& second)
{
dim4 dims;
for(unsigned i = 0; i < 4; i++) {
dims[i] = first[i] + second[i];
}
return dims;
}
dim4 operator-(const dim4& first, const dim4& second)
{
dim4 dims;
for(unsigned i = 0; i < 4; i++) {
dims[i] = first[i] - second[i];
}
return dims;
}
dim4 operator*(const dim4& first, const dim4& second)
{
dim4 dims;
for(unsigned i = 0; i < 4; i++) {
dims[i] = first[i] * second[i];
}
return dims;
}
bool
isEnd(const af_seq &seq) { return (seq.end <= -1); }
bool
isSpan(const af_seq &seq) { return (seq.step == 0 && seq.begin == 1 && seq.end == 1); }
size_t
seqElements(const af_seq &seq) {
size_t out = 0;
if (seq.step > 0) { out = ((seq.end - seq.begin) / abs(seq.step)) + 1; }
else if (seq.step < 0) { out = ((seq.begin - seq.end) / abs(seq.step)) + 1; }
else { out = numeric_limits<size_t>::max(); }
return out;
}
dim4
toDims(const vector<af_seq>& seqs, dim4 parentDims)
{
dim4 outDims(1, 1, 1, 1);
for(unsigned i = 0; i < seqs.size(); i++ ) {
if (isSpan(seqs[i])) {
outDims[i] = parentDims[i];
} else if (isEnd(seqs[i])) {
if(seqs[i].begin == -1) { // only end is passed as seq
outDims[i] = 1;
} else if (seqs[i].begin < 0) {
af_seq temp = {parentDims[i] + seqs[i].begin,
parentDims[i] + seqs[i].end,
seqs[i].step};
outDims[i] = seqElements(temp);
} else { // end is passed as a part of seq
af_seq temp = {seqs[i].begin, parentDims[i] + seqs[i].end, seqs[i].step};
outDims[i] = seqElements(temp);
}
} else {
DIM_ASSERT(1, seqs[i].begin >= 0 && seqs[i].begin < parentDims[i]);
DIM_ASSERT(1, seqs[i].end < parentDims[i]);
outDims[i] = seqElements(seqs[i]);
}
if (outDims[i] > parentDims[i])
AF_ERROR("Size mismatch between input and output", AF_ERR_SIZE);
}
return outDims;
}
dim4
toOffset(const vector<af_seq>& seqs, dim4 parentDims)
{
dim4 outOffsets(0, 0, 0, 0);
for(unsigned i = 0; i < seqs.size(); i++ ) {
if (seqs[i].step !=0 && seqs[i].begin >= 0) {
outOffsets[i] = seqs[i].begin;
} else if (seqs[i].begin <= -1) {
outOffsets[i] = parentDims[i] + seqs[i].begin;
} else {
outOffsets[i] = 0;
}
if (outOffsets[i] >= parentDims[i])
AF_ERROR("Index out of range", AF_ERR_SIZE);
}
return outOffsets;
}
dim4
toStride(const vector<af_seq>& seqs, af::dim4 parentDims)
{
dim4 out(calcStrides(parentDims));
for(unsigned i = 0; i < seqs.size(); i++ ) {
if (seqs[i].step != 0) { out[i] *= seqs[i].step; }
}
return out;
}
}