codekingpro/portable-devtools
115k
1#ifndef NUMPY_CORE_INCLUDE_NUMPY_UFUNCOBJECT_H_
2#define NUMPY_CORE_INCLUDE_NUMPY_UFUNCOBJECT_H_
3
4#include <numpy/npy_math.h>
5#include <numpy/npy_common.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11/*
12 * The legacy generic inner loop for a standard element-wise or
13 * generalized ufunc.
14 */
15typedef void (*PyUFuncGenericFunction)
16 (char **args,
17 npy_intp const *dimensions,
18 npy_intp const *strides,
19 void *innerloopdata);
20
21/*
22 * The most generic one-dimensional inner loop for
23 * a masked standard element-wise ufunc. "Masked" here means that it skips
24 * doing calculations on any items for which the maskptr array has a true
25 * value.
26 */
27typedef void (PyUFunc_MaskedStridedInnerLoopFunc)(
28 char **dataptrs, npy_intp *strides,
29 char *maskptr, npy_intp mask_stride,
30 npy_intp count,
31 NpyAuxData *innerloopdata);
32
33/* Forward declaration for the type resolver and loop selector typedefs */
34struct _tagPyUFuncObject;
35
36/*
37 * Given the operands for calling a ufunc, should determine the
38 * calculation input and output data types and return an inner loop function.
39 * This function should validate that the casting rule is being followed,
40 * and fail if it is not.
41 *
42 * For backwards compatibility, the regular type resolution function does not
43 * support auxiliary data with object semantics. The type resolution call
44 * which returns a masked generic function returns a standard NpyAuxData
45 * object, for which the NPY_AUXDATA_FREE and NPY_AUXDATA_CLONE macros
46 * work.
47 *
48 * ufunc: The ufunc object.
49 * casting: The 'casting' parameter provided to the ufunc.
50 * operands: An array of length (ufunc->nin + ufunc->nout),
51 * with the output parameters possibly NULL.
52 * type_tup: Either NULL, or the type_tup passed to the ufunc.
53 * out_dtypes: An array which should be populated with new
54 * references to (ufunc->nin + ufunc->nout) new
55 * dtypes, one for each input and output. These
56 * dtypes should all be in native-endian format.
57 *
58 * Should return 0 on success, -1 on failure (with exception set),
59 * or -2 if Py_NotImplemented should be returned.
60 */
61typedef int (PyUFunc_TypeResolutionFunc)(
62 struct _tagPyUFuncObject *ufunc,
63 NPY_CASTING casting,
64 PyArrayObject **operands,
65 PyObject *type_tup,
66 PyArray_Descr **out_dtypes);
67
68/*
69 * This is the signature for the functions that may be assigned to the
70 * `process_core_dims_func` field of the PyUFuncObject structure.
71 * Implementation of this function is optional. This function is only used
72 * by generalized ufuncs (i.e. those with the field `core_enabled` set to 1).
73 * The function is called by the ufunc during the processing of the arguments
74 * of a call of the ufunc. The function can check the core dimensions of the
75 * input and output arrays and return -1 with an exception set if any
76 * requirements are not satisfied. If the caller of the ufunc didn't provide
77 * output arrays, the core dimensions associated with the output arrays (i.e.
78 * those that are not also used in input arrays) will have the value -1 in
79 * `core_dim_sizes`. This function can replace any output core dimensions
80 * that are -1 with a value that is appropriate for the ufunc.
81 *
82 * Parameter Description
83 * --------------- ------------------------------------------------------
84 * ufunc The ufunc object
85 * core_dim_sizes An array with length `ufunc->core_num_dim_ix`.
86 * The core dimensions of the arrays passed to the ufunc
87 * will have been set. If the caller of the ufunc didn't
88 * provide the output array(s), the output-only core
89 * dimensions will have the value -1.
90 *
91 * The function must not change any element in `core_dim_sizes` that is
92 * not -1 on input. Doing so will result in incorrect output from the
93 * ufunc, and could result in a crash of the Python interpreter.
94 *
95 * The function must return 0 on success, -1 on failure (with an exception
96 * set).
97 */
98typedef int (PyUFunc_ProcessCoreDimsFunc)(
99 struct _tagPyUFuncObject *ufunc,
100 npy_intp *core_dim_sizes);
101
102typedef struct _tagPyUFuncObject {
103 PyObject_HEAD
104 /*
105 * nin: Number of inputs
106 * nout: Number of outputs
107 * nargs: Always nin + nout (Why is it stored?)
108 */
109 int nin, nout, nargs;
110
111 /*
112 * Identity for reduction, any of PyUFunc_One, PyUFunc_Zero
113 * PyUFunc_MinusOne, PyUFunc_None, PyUFunc_ReorderableNone,
114 * PyUFunc_IdentityValue.
115 */
116 int identity;
117
118 /* Array of one-dimensional core loops */
119 PyUFuncGenericFunction *functions;
120 /* Array of funcdata that gets passed into the functions */
121 void *const *data;
122 /* The number of elements in 'functions' and 'data' */
123 int ntypes;
124
125 /* Used to be unused field 'check_return' */
126 int reserved1;
127
128 /* The name of the ufunc */
129 const char *name;
130
131 /* Array of type numbers, of size ('nargs' * 'ntypes') */
132 const char *types;
133
134 /* Documentation string */
135 const char *doc;
136
137 void *ptr;
138 PyObject *obj;
139 PyObject *userloops;
140
141 /* generalized ufunc parameters */
142
143 /* 0 for scalar ufunc; 1 for generalized ufunc */
144 int core_enabled;
145 /* number of distinct dimension names in signature */
146 int core_num_dim_ix;
147
148 /*
149 * dimension indices of input/output argument k are stored in
150 * core_dim_ixs[core_offsets[k]..core_offsets[k]+core_num_dims[k]-1]
151 */
152
153 /* numbers of core dimensions of each argument */
154 int *core_num_dims;
155 /*
156 * dimension indices in a flatted form; indices
157 * are in the range of [0,core_num_dim_ix)
158 */
159 int *core_dim_ixs;
160 /*
161 * positions of 1st core dimensions of each
162 * argument in core_dim_ixs, equivalent to cumsum(core_num_dims)
163 */
164 int *core_offsets;
165 /* signature string for printing purpose */
166 char *core_signature;
167
168 /*
169 * A function which resolves the types and fills an array
170 * with the dtypes for the inputs and outputs.
171 */
172 PyUFunc_TypeResolutionFunc *type_resolver;
173
174 /* A dictionary to monkeypatch ufuncs */
175 PyObject *dict;
176
177 /*
178 * This was blocked off to be the "new" inner loop selector in 1.7,
179 * but this was never implemented. (This is also why the above
180 * selector is called the "legacy" selector.)
181 */
182 #ifndef Py_LIMITED_API
183 vectorcallfunc vectorcall;
184 #else
185 void *vectorcall;
186 #endif
187
188 /* Was previously the `PyUFunc_MaskedInnerLoopSelectionFunc` */
189 void *reserved3;
190
191 /*
192 * List of flags for each operand when ufunc is called by nditer object.
193 * These flags will be used in addition to the default flags for each
194 * operand set by nditer object.
195 */
196 npy_uint32 *op_flags;
197
198 /*
199 * List of global flags used when ufunc is called by nditer object.
200 * These flags will be used in addition to the default global flags
201 * set by nditer object.
202 */
203 npy_uint32 iter_flags;
204
205 /* New in NPY_API_VERSION 0x0000000D and above */
206 #if NPY_FEATURE_VERSION >= NPY_1_16_API_VERSION
207 /*
208 * for each core_num_dim_ix distinct dimension names,
209 * the possible "frozen" size (-1 if not frozen).
210 */
211 npy_intp *core_dim_sizes;
212
213 /*
214 * for each distinct core dimension, a set of UFUNC_CORE_DIM* flags
215 */
216 npy_uint32 *core_dim_flags;
217
218 /* Identity for reduction, when identity == PyUFunc_IdentityValue */
219 PyObject *identity_value;
220 #endif /* NPY_FEATURE_VERSION >= NPY_1_16_API_VERSION */
221
222 /* New in NPY_API_VERSION 0x0000000F and above */
223 #if NPY_FEATURE_VERSION >= NPY_1_22_API_VERSION
224 /* New private fields related to dispatching */
225 void *_dispatch_cache;
226 /* A PyListObject of `(tuple of DTypes, ArrayMethod/Promoter)` */
227 PyObject *_loops;
228 #endif
229 #if NPY_FEATURE_VERSION >= NPY_2_1_API_VERSION
230 /*
231 * Optional function to process core dimensions of a gufunc.
232 */
233 PyUFunc_ProcessCoreDimsFunc *process_core_dims_func;
234 #endif
235} PyUFuncObject;
236
237#include "arrayobject.h"
238/* Generalized ufunc; 0x0001 reserved for possible use as CORE_ENABLED */
239/* the core dimension's size will be determined by the operands. */
240#define UFUNC_CORE_DIM_SIZE_INFERRED 0x0002
241/* the core dimension may be absent */
242#define UFUNC_CORE_DIM_CAN_IGNORE 0x0004
243/* flags inferred during execution */
244#define UFUNC_CORE_DIM_MISSING 0x00040000
245
246
247#define UFUNC_OBJ_ISOBJECT 1
248#define UFUNC_OBJ_NEEDS_API 2
249
250
251#if NPY_ALLOW_THREADS
252#define NPY_LOOP_BEGIN_THREADS do {if (!(loop->obj & UFUNC_OBJ_NEEDS_API)) _save = PyEval_SaveThread();} while (0);
253#define NPY_LOOP_END_THREADS do {if (!(loop->obj & UFUNC_OBJ_NEEDS_API)) PyEval_RestoreThread(_save);} while (0);
254#else
255#define NPY_LOOP_BEGIN_THREADS
256#define NPY_LOOP_END_THREADS
257#endif
258
259/*
260 * UFunc has unit of 0, and the order of operations can be reordered
261 * This case allows reduction with multiple axes at once.
262 */
263#define PyUFunc_Zero 0
264/*
265 * UFunc has unit of 1, and the order of operations can be reordered
266 * This case allows reduction with multiple axes at once.
267 */
268#define PyUFunc_One 1
269/*
270 * UFunc has unit of -1, and the order of operations can be reordered
271 * This case allows reduction with multiple axes at once. Intended for
272 * bitwise_and reduction.
273 */
274#define PyUFunc_MinusOne 2
275/*
276 * UFunc has no unit, and the order of operations cannot be reordered.
277 * This case does not allow reduction with multiple axes at once.
278 */
279#define PyUFunc_None -1
280/*
281 * UFunc has no unit, and the order of operations can be reordered
282 * This case allows reduction with multiple axes at once.
283 */
284#define PyUFunc_ReorderableNone -2
285/*
286 * UFunc unit is an identity_value, and the order of operations can be reordered
287 * This case allows reduction with multiple axes at once.
288 */
289#define PyUFunc_IdentityValue -3
290
291
292#define UFUNC_REDUCE 0
293#define UFUNC_ACCUMULATE 1
294#define UFUNC_REDUCEAT 2
295#define UFUNC_OUTER 3
296
297
298typedef struct {
299 int nin;
300 int nout;
301 PyObject *callable;
302} PyUFunc_PyFuncData;
303
304/* A linked-list of function information for
305 user-defined 1-d loops.
306 */
307typedef struct _loop1d_info {
308 PyUFuncGenericFunction func;
309 void *data;
310 int *arg_types;
311 struct _loop1d_info *next;
312 int nargs;
313 PyArray_Descr **arg_dtypes;
314} PyUFunc_Loop1d;
315
316
317#define UFUNC_PYVALS_NAME "UFUNC_PYVALS"
318
319/* THESE MACROS ARE DEPRECATED.
320 * Use npy_set_floatstatus_* in the npymath library.
321 */
322#define UFUNC_FPE_DIVIDEBYZERO NPY_FPE_DIVIDEBYZERO
323#define UFUNC_FPE_OVERFLOW NPY_FPE_OVERFLOW
324#define UFUNC_FPE_UNDERFLOW NPY_FPE_UNDERFLOW
325#define UFUNC_FPE_INVALID NPY_FPE_INVALID
326
327/* Make sure it gets defined if it isn't already */
328#ifndef UFUNC_NOFPE
329/* Clear the floating point exception default of Borland C++ */
330#if defined(__BORLANDC__)
331#define UFUNC_NOFPE _control87(MCW_EM, MCW_EM);
332#else
333#define UFUNC_NOFPE
334#endif
335#endif
336
337#include "__ufunc_api.h"
338
339#ifdef __cplusplus
340}
341#endif
342
343#endif /* NUMPY_CORE_INCLUDE_NUMPY_UFUNCOBJECT_H_ */
344 