forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
executable file
·345 lines (282 loc) · 10.7 KB
/
binary.cpp
File metadata and controls
executable file
·345 lines (282 loc) · 10.7 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
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
/*******************************************************
* 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 <af/array.h>
#include <af/defines.h>
#include <af/arith.h>
#include <af/data.h>
#include <ArrayInfo.hpp>
#include <optypes.hpp>
#include <implicit.hpp>
#include <err_common.hpp>
#include <handle.hpp>
#include <backend.hpp>
#include <arith.hpp>
#include <logic.hpp>
using namespace detail;
using af::dim4;
template<typename T, af_op_t op>
static inline af_array arithOp(const af_array lhs, const af_array rhs,
const dim4 &odims)
{
af_array res = getHandle(arithOp<T, op>(castArray<T>(lhs), castArray<T>(rhs), odims));
return res;
}
template<af_op_t op>
static af_err af_arith(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype otype = implicit(lhs, rhs);
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (otype) {
case f32: res = arithOp<float , op>(lhs, rhs, odims); break;
case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
case c32: res = arithOp<cfloat , op>(lhs, rhs, odims); break;
case c64: res = arithOp<cdouble, op>(lhs, rhs, odims); break;
case s32: res = arithOp<int , op>(lhs, rhs, odims); break;
case u32: res = arithOp<uint , op>(lhs, rhs, odims); break;
case u8 : res = arithOp<uchar , op>(lhs, rhs, odims); break;
case b8 : res = arithOp<char , op>(lhs, rhs, odims); break;
case s64: res = arithOp<intl , op>(lhs, rhs, odims); break;
case u64: res = arithOp<uintl , op>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, otype);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
template<af_op_t op>
static af_err af_arith_real(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype otype = implicit(lhs, rhs);
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (otype) {
case f32: res = arithOp<float , op>(lhs, rhs, odims); break;
case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
case s32: res = arithOp<int , op>(lhs, rhs, odims); break;
case u32: res = arithOp<uint , op>(lhs, rhs, odims); break;
case u8 : res = arithOp<uchar , op>(lhs, rhs, odims); break;
case b8 : res = arithOp<char , op>(lhs, rhs, odims); break;
case s64: res = arithOp<intl , op>(lhs, rhs, odims); break;
case u64: res = arithOp<uintl , op>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, otype);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_add(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_add_t>(out, lhs, rhs, batchMode);
}
af_err af_mul(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_mul_t>(out, lhs, rhs, batchMode);
}
af_err af_sub(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_sub_t>(out, lhs, rhs, batchMode);
}
af_err af_div(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_div_t>(out, lhs, rhs, batchMode);
}
af_err af_maxof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_max_t>(out, lhs, rhs, batchMode);
}
af_err af_minof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith<af_min_t>(out, lhs, rhs, batchMode);
}
af_err af_rem(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith_real<af_rem_t>(out, lhs, rhs, batchMode);
}
af_err af_mod(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_arith_real<af_mod_t>(out, lhs, rhs, batchMode);
}
af_err af_pow(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
if (linfo.isComplex() || rinfo.isComplex()) {
AF_ERROR("Powers of Complex numbers not supported", AF_ERR_NOT_SUPPORTED);
}
} CATCHALL;
return af_arith_real<af_pow_t>(out, lhs, rhs, batchMode);
}
af_err af_atan2(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype type = implicit(lhs, rhs);
if (type != f32 && type != f64) {
AF_ERROR("Only floating point arrays are supported for atan2 ",
AF_ERR_NOT_SUPPORTED);
}
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (type) {
case f32: res = arithOp<float , af_atan2_t>(lhs, rhs, odims); break;
case f64: res = arithOp<double, af_atan2_t>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_hypot(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype type = implicit(lhs, rhs);
if (type != f32 && type != f64) {
AF_ERROR("Only floating point arrays are supported for hypot ",
AF_ERR_NOT_SUPPORTED);
}
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (type) {
case f32: res = arithOp<float , af_hypot_t>(lhs, rhs, odims); break;
case f64: res = arithOp<double, af_hypot_t>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T, af_op_t op>
static inline af_array logicOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
af_array res = getHandle(logicOp<T, op>(getArray<T>(lhs), getArray<T>(rhs), odims));
return res;
}
template<af_op_t op>
static af_err af_logic(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype type = implicit(lhs, rhs);
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (type) {
case f32: res = logicOp<float , op>(lhs, rhs, odims); break;
case f64: res = logicOp<double , op>(lhs, rhs, odims); break;
case c32: res = logicOp<cfloat , op>(lhs, rhs, odims); break;
case c64: res = logicOp<cdouble, op>(lhs, rhs, odims); break;
case s32: res = logicOp<int , op>(lhs, rhs, odims); break;
case u32: res = logicOp<uint , op>(lhs, rhs, odims); break;
case u8 : res = logicOp<uchar , op>(lhs, rhs, odims); break;
case b8 : res = logicOp<char , op>(lhs, rhs, odims); break;
case s64: res = logicOp<intl , op>(lhs, rhs, odims); break;
case u64: res = logicOp<uintl , op>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_eq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_eq_t>(out, lhs, rhs, batchMode);
}
af_err af_neq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_neq_t>(out, lhs, rhs, batchMode);
}
af_err af_gt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_gt_t>(out, lhs, rhs, batchMode);
}
af_err af_ge(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_ge_t>(out, lhs, rhs, batchMode);
}
af_err af_lt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_lt_t>(out, lhs, rhs, batchMode);
}
af_err af_le(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_le_t>(out, lhs, rhs, batchMode);
}
af_err af_and(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_and_t>(out, lhs, rhs, batchMode);
}
af_err af_or(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_logic<af_or_t>(out, lhs, rhs, batchMode);
}
template<typename T, af_op_t op>
static inline af_array bitOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
af_array res = getHandle(bitOp<T, op>(getArray<T>(lhs), getArray<T>(rhs), odims));
return res;
}
template<af_op_t op>
static af_err af_bitwise(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
try {
const af_dtype type = implicit(lhs, rhs);
ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);
dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);
af_array res;
switch (type) {
case s32: res = bitOp<int , op>(lhs, rhs, odims); break;
case u32: res = bitOp<uint , op>(lhs, rhs, odims); break;
case u8 : res = bitOp<uchar , op>(lhs, rhs, odims); break;
case b8 : res = bitOp<char , op>(lhs, rhs, odims); break;
case s64: res = bitOp<intl , op>(lhs, rhs, odims); break;
case u64: res = bitOp<uintl , op>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_bitand(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_bitwise<af_bitand_t>(out, lhs, rhs, batchMode);
}
af_err af_bitor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_bitwise<af_bitor_t>(out, lhs, rhs, batchMode);
}
af_err af_bitxor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_bitwise<af_bitxor_t>(out, lhs, rhs, batchMode);
}
af_err af_bitshiftl(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_bitwise<af_bitshiftl_t>(out, lhs, rhs, batchMode);
}
af_err af_bitshiftr(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
return af_bitwise<af_bitshiftr_t>(out, lhs, rhs, batchMode);
}