forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunary.cpp
More file actions
163 lines (127 loc) · 3.41 KB
/
unary.cpp
File metadata and controls
163 lines (127 loc) · 3.41 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
/*******************************************************
* 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 <unary.hpp>
#include <implicit.hpp>
using namespace detail;
template<typename T, af_op_t op>
static inline af_array unaryOp(const af_array in)
{
af_array res = getHandle(unaryOp<T, op>(castArray<T>(in)));
return res;
}
template<af_op_t op>
static af_err af_unary(af_array *out, const af_array in)
{
try {
ArrayInfo in_info = getInfo(in);
ARG_ASSERT(1, in_info.isReal());
af_dtype in_type = in_info.getType();
af_array res;
// Convert all inputs to floats / doubles
af_dtype type = implicit(in_type, f32);
switch (type) {
case f32 : res = unaryOp<float , op>(in); break;
case f64 : res = unaryOp<double , op>(in); break;
default:
TYPE_ERROR(1, in_type); break;
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
#define UNARY(fn) \
af_err af_##fn(af_array *out, const af_array in) \
{ \
return af_unary<af_##fn##_t>(out, in); \
}
UNARY(sin)
UNARY(cos)
UNARY(tan)
UNARY(asin)
UNARY(acos)
UNARY(atan)
UNARY(sinh)
UNARY(cosh)
UNARY(tanh)
UNARY(asinh)
UNARY(acosh)
UNARY(atanh)
UNARY(round)
UNARY(floor)
UNARY(ceil)
UNARY(exp)
UNARY(expm1)
UNARY(erf)
UNARY(erfc)
UNARY(log)
UNARY(log10)
UNARY(log1p)
UNARY(sqrt)
UNARY(cbrt)
UNARY(tgamma)
UNARY(lgamma)
af_err af_not(af_array *out, const af_array in)
{
try {
af_array tmp;
ArrayInfo in_info = getInfo(in);
AF_CHECK(af_constant(&tmp, 0,
in_info.ndims(),
in_info.dims().get(), in_info.getType()));
AF_CHECK(af_neq(out, in, tmp, false));
AF_CHECK(af_destroy_array(tmp));
} CATCHALL;
return AF_SUCCESS;
}
template<typename T, af_op_t op>
static inline af_array checkOp(const af_array in)
{
af_array res = getHandle(checkOp<T, op>(castArray<T>(in)));
return res;
}
template<af_op_t op>
static af_err af_check(af_array *out, const af_array in)
{
try {
ArrayInfo in_info = getInfo(in);
ARG_ASSERT(1, in_info.isReal());
af_dtype in_type = in_info.getType();
af_array res;
// Convert all inputs to floats / doubles
af_dtype type = implicit(in_type, f32);
switch (type) {
case f32 : res = checkOp<float , op>(in); break;
case f64 : res = checkOp<double , op>(in); break;
default:
TYPE_ERROR(1, in_type); break;
}
std::swap(*out, res);
}
CATCHALL;
return AF_SUCCESS;
}
#define CHECK(fn) \
af_err af_##fn(af_array *out, const af_array in) \
{ \
return af_check<af_##fn##_t>(out, in); \
}
CHECK(isinf)
CHECK(isnan)
CHECK(iszero)