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
|
/**
* This module provides the inner loops for the clip ufunc
*/
#define _UMATHMODULE
#define _MULTIARRAYMODULE
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "numpy/halffloat.h"
#include "numpy/ndarraytypes.h"
#include "numpy/npy_common.h"
#include "numpy/npy_math.h"
#include "numpy/utils.h"
#include "fast_loop_macros.h"
#include "../common/numpy_tag.h"
template <class T>
T
_NPY_MIN(T a, T b, npy::integral_tag const &)
{
return PyArray_MIN(a, b);
}
template <class T>
T
_NPY_MAX(T a, T b, npy::integral_tag const &)
{
return PyArray_MAX(a, b);
}
npy_half
_NPY_MIN(npy_half a, npy_half b, npy::half_tag const &)
{
return npy_half_isnan(a) || npy_half_le(a, b) ? (a) : (b);
}
npy_half
_NPY_MAX(npy_half a, npy_half b, npy::half_tag const &)
{
return npy_half_isnan(a) || npy_half_ge(a, b) ? (a) : (b);
}
template <class T>
T
_NPY_MIN(T a, T b, npy::floating_point_tag const &)
{
return npy_isnan(a) ? (a) : PyArray_MIN(a, b);
}
template <class T>
T
_NPY_MAX(T a, T b, npy::floating_point_tag const &)
{
return npy_isnan(a) ? (a) : PyArray_MAX(a, b);
}
template <class T>
T
_NPY_MIN(T a, T b, npy::complex_tag const &)
{
return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CLT(a, b)
? (a)
: (b);
}
template <class T>
T
_NPY_MAX(T a, T b, npy::complex_tag const &)
{
return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CGT(a, b)
? (a)
: (b);
}
template <class T>
T
_NPY_MIN(T a, T b, npy::date_tag const &)
{
return (a) == NPY_DATETIME_NAT ? (a)
: (b) == NPY_DATETIME_NAT ? (b)
: (a) < (b) ? (a)
: (b);
}
template <class T>
T
_NPY_MAX(T a, T b, npy::date_tag const &)
{
return (a) == NPY_DATETIME_NAT ? (a)
: (b) == NPY_DATETIME_NAT ? (b)
: (a) > (b) ? (a)
: (b);
}
/* generic dispatcher */
template <class Tag, class T = typename Tag::type>
T
_NPY_MIN(T const &a, T const &b)
{
return _NPY_MIN(a, b, Tag{});
}
template <class Tag, class T = typename Tag::type>
T
_NPY_MAX(T const &a, T const &b)
{
return _NPY_MAX(a, b, Tag{});
}
template <class Tag, class T>
T
_NPY_CLIP(T x, T min, T max)
{
return _NPY_MIN<Tag>(_NPY_MAX<Tag>((x), (min)), (max));
}
template <class Tag, class T = typename Tag::type>
static void
_npy_clip_(T **args, npy_intp const *dimensions, npy_intp const *steps)
{
npy_intp n = dimensions[0];
if (steps[1] == 0 && steps[2] == 0) {
/* min and max are constant throughout the loop, the most common case
*/
/* NOTE: it may be possible to optimize these checks for nan */
T min_val = *args[1];
T max_val = *args[2];
T *ip1 = args[0], *op1 = args[3];
npy_intp is1 = steps[0] / sizeof(T), os1 = steps[3] / sizeof(T);
/* contiguous, branch to let the compiler optimize */
if (is1 == 1 && os1 == 1) {
for (npy_intp i = 0; i < n; i++, ip1++, op1++) {
*op1 = _NPY_CLIP<Tag>(*ip1, min_val, max_val);
}
}
else {
for (npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) {
*op1 = _NPY_CLIP<Tag>(*ip1, min_val, max_val);
}
}
}
else {
T *ip1 = args[0], *ip2 = args[1], *ip3 = args[2], *op1 = args[3];
npy_intp is1 = steps[0] / sizeof(T), is2 = steps[1] / sizeof(T),
is3 = steps[2] / sizeof(T), os1 = steps[3] / sizeof(T);
for (npy_intp i = 0; i < n;
i++, ip1 += is1, ip2 += is2, ip3 += is3, op1 += os1)
*op1 = _NPY_CLIP<Tag>(*ip1, *ip2, *ip3);
}
npy_clear_floatstatus_barrier((char *)dimensions);
}
template <class Tag>
static void
_npy_clip(char **args, npy_intp const *dimensions, npy_intp const *steps)
{
using T = typename Tag::type;
return _npy_clip_<Tag>((T **)args, dimensions, steps);
}
extern "C" {
NPY_NO_EXPORT void
BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::bool_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::byte_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::ubyte_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::short_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::ushort_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::int_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::uint_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::long_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::ulong_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::longlong_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::ulonglong_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::half_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::float_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::double_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::longdouble_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::cfloat_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::cdouble_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
CLONGDOUBLE_clip(char **args, npy_intp const *dimensions,
npy_intp const *steps, void *NPY_UNUSED(func))
{
return _npy_clip<npy::clongdouble_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::datetime_tag>(args, dimensions, steps);
}
NPY_NO_EXPORT void
TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps,
void *NPY_UNUSED(func))
{
return _npy_clip<npy::timedelta_tag>(args, dimensions, steps);
}
}
|