-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathcomplex.cpp
More file actions
204 lines (169 loc) · 5.92 KB
/
complex.cpp
File metadata and controls
204 lines (169 loc) · 5.92 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
/*******************************************************
* 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 <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/err_common.hpp>
#include <common/half.hpp>
#include <handle.hpp>
#include <implicit.hpp>
#include <optypes.hpp>
#include <af/arith.h>
#include <af/array.h>
#include <af/data.h>
#include <af/defines.h>
#include <complex.hpp>
using af::dim4;
using arrayfire::common::half;
using detail::cdouble;
using detail::cfloat;
using detail::conj;
using detail::imag;
using detail::real;
template<typename To, typename Ti>
static inline af_array cplx(const af_array lhs, const af_array rhs,
const dim4 &odims) {
af_array res =
getHandle(cplx<To, Ti>(castArray<Ti>(lhs), castArray<Ti>(rhs), odims));
return res;
}
af_err af_cplx2(af_array *out, const af_array lhs, const af_array rhs,
bool batchMode) {
try {
af_dtype type = implicit(lhs, rhs);
if (type == c32 || type == c64) {
AF_ERROR("Inputs to cplx2 can not be of complex type", AF_ERR_ARG);
}
if (type != f64) { type = f32; }
dim4 odims =
getOutDims(getInfo(lhs).dims(), getInfo(rhs).dims(), batchMode);
if (odims.ndims() == 0) {
return af_create_handle(out, 0, nullptr, type);
}
af_array res;
switch (type) {
case f32: res = cplx<cfloat, float>(lhs, rhs, odims); break;
case f64: res = cplx<cdouble, double>(lhs, rhs, odims); break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_cplx(af_array *out, const af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (type == c32 || type == c64) {
AF_ERROR("Inputs to cplx2 can not be of complex type", AF_ERR_ARG);
}
if (info.ndims() == 0) { return af_retain_array(out, in); }
af_array tmp;
AF_CHECK(af_constant(&tmp, 0, info.ndims(), info.dims().get(), type));
af_array res;
switch (type) {
case f32: res = cplx<cfloat, float>(in, tmp, info.dims()); break;
case f64: res = cplx<cdouble, double>(in, tmp, info.dims()); break;
default: TYPE_ERROR(0, type);
}
AF_CHECK(af_release_array(tmp));
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_real(af_array *out, const af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (type != c32 && type != c64) { return af_retain_array(out, in); }
if (info.ndims() == 0) { return af_retain_array(out, in); }
af_array res;
switch (type) {
case c32:
res = getHandle(real<float, cfloat>(getArray<cfloat>(in)));
break;
case c64:
res = getHandle(real<double, cdouble>(getArray<cdouble>(in)));
break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_imag(af_array *out, const af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (type != c32 && type != c64) {
return af_constant(out, 0, info.ndims(), info.dims().get(), type);
}
if (info.ndims() == 0) { return af_retain_array(out, in); }
af_array res;
switch (type) {
case c32:
res = getHandle(imag<float, cfloat>(getArray<cfloat>(in)));
break;
case c64:
res = getHandle(imag<double, cdouble>(getArray<cdouble>(in)));
break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_conjg(af_array *out, const af_array in) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (type != c32 && type != c64) { return af_retain_array(out, in); }
if (info.ndims() == 0) { return af_retain_array(out, in); }
af_array res;
switch (type) {
case c32:
res = getHandle(conj<cfloat>(getArray<cfloat>(in)));
break;
case c64:
res = getHandle(conj<cdouble>(getArray<cdouble>(in)));
break;
default: TYPE_ERROR(0, type);
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
af_err af_abs(af_array *out, const af_array in) {
try {
const ArrayInfo &in_info = getInfo(in);
af_dtype in_type = in_info.getType();
af_array res;
// Convert all inputs to floats / doubles
af_dtype type = implicit(in_type, f32);
if (in_type == f16) { type = f16; }
if (in_info.ndims() == 0) { return af_retain_array(out, in); }
switch (type) {
// clang-format off
case f32: res = getHandle(detail::abs<float, float>(castArray<float>(in))); break;
case f64: res = getHandle(detail::abs<double, double>(castArray<double>(in))); break;
case c32: res = getHandle(detail::abs<float, cfloat>(castArray<cfloat>(in))); break;
case c64: res = getHandle(detail::abs<double, cdouble>(castArray<cdouble>(in))); break;
case f16: res = getHandle(detail::abs<half, half>(getArray<half>(in))); break;
// clang-format on
default: TYPE_ERROR(1, in_type); break;
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}