Skip to content

Commit af34c3c

Browse files
Merge pull request #523 from pydata/add_funcs
Adding isnan/isfinite functions etc.
2 parents 82ebe8d + 9e901d4 commit af34c3c

File tree

12 files changed

+430
-36
lines changed

12 files changed

+430
-36
lines changed

ADDFUNCS.rst

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
Functions and Function signatures
2+
=================================
3+
4+
Adding functions
5+
----------------
6+
7+
In order to add new functions to ``numexpr``, currently it is necessary to edit several files. Consider adding a function
8+
``out_type myfunc(arg_type)``.
9+
10+
* ``numexpr/expressions.py``
11+
Add ``'myfunc': func(numpy.myfunc, out_dtype),`` to the dict of functions, ``functions = {...``. If the return type of the function is ``bool``, add
12+
the function to the list ``if opcode in ("isnan", "isfinite"):`` in the ``__init__`` function of the ``FuncNode`` class.
13+
In the future it might be nice to refactor this function since it sets the output type based on the type of the inputs in general.
14+
15+
* ``numexpr/necompiler.py``
16+
Add ``"myfunc"`` to the list of functions:
17+
18+
.. code-block:: python3
19+
20+
"floor",
21+
"isnan",
22+
"isfinite",
23+
"myfunc"
24+
]
25+
26+
* ``numexpr/functions.hpp``
27+
Find the correct function signature ``FUNC_OA`` where ``O`` is the return type, and ``A`` the argument type(s). For example, if the function
28+
is ``double myfunc(double)``, one should edit within the ``FUNC_DD`` clause. If you cannot find your function signature you will have to add it,
29+
following the template of the other functions.
30+
Most likely, you will want to add support for several function signatures (e.g. double -> bool and float -> bool) and so you will have to add the
31+
function in two clauses. If your function has a float input, you will see that there are 5 arguments in the
32+
``FUNC_OA`` macro, and you will have to add ``myfunc2`` here is order to compile on MSVC machines (i.e. Windows, see following).
33+
Example:
34+
35+
.. code-block:: cpp
36+
:emphasize-lines: 6, 20
37+
38+
#ifndef FUNC_DD
39+
#define ELIDE_FUNC_DD
40+
#define FUNC_DD(...)
41+
#endif
42+
...
43+
FUNC_DD(FUNC_MYFUNC_DD, "myfunc_dd", myfunc, vdMyfunc)
44+
FUNC_DD(FUNC_DD_LAST, NULL, NULL, NULL)
45+
#ifdef ELIDE_FUNC_DD
46+
#undef ELIDE_FUNC_DD
47+
#undef FUNC_DD
48+
#endif
49+
50+
...
51+
52+
#ifndef FUNC_FF
53+
#define ELIDE_FUNC_FF
54+
#define FUNC_FF(...)
55+
#endif
56+
...
57+
FUNC_FF(FUNC_MYFUNC_FF, "myfunc_ff", myfuncf, myfuncf2, vfMyfunc)
58+
FUNC_FF(FUNC_FF_LAST, NULL, NULL, NULL, NULL)
59+
#ifdef ELIDE_FUNC_FF
60+
#undef ELIDE_FUNC_FF
61+
#undef FUNC_FF
62+
#endif
63+
64+
* ``numexpr/msvc_function_stubs.hpp``
65+
In order to support float arguments, due to oddities of MSVC, you have to provide explicit support for your function in this file.
66+
Add ``#define myfuncf(x) ((float)floor((double)(x)))`` (if your function is float -> float) to the ``#if`` clause at the top of the file
67+
which is for old versions of MSVC which did not have support for single precision fucntions. Then in the body, add an inline function
68+
69+
.. code-block:: cpp
70+
71+
inline float myfuncf2(float x) {
72+
return myfuncf(x);
73+
}
74+
75+
This is the function that appears as the ``f_win32`` parameter in ``functions.hpp``.
76+
77+
* ``numexpr/tests/test_numexpr.py``
78+
Don't forget to add a test for your function!
79+
80+
Adding function signatures
81+
--------------------------
82+
It may so happen that you cannot find your desired function signature in ``functions.hpp``. This means you will have to add it yourself!
83+
This involves editing a few more files. In addition, there may be certain bespoke changes, specific to the function signature
84+
that you may have to make (see Notes, below)
85+
86+
* ``numexpr/functions.hpp``
87+
Firstly, add clause(s) for your function signature. For example, if the function signature is ``bool(double)`` and ``bool(float)``, add
88+
``FUNC_BD`` and ``FUNC_BF`` clauses (in the latter case you will need the macro to take 5 arguments for MSVC-compatibility.)
89+
90+
.. code-block:: cpp
91+
92+
#ifndef FUNC_BD
93+
#define ELIDE_FUNC_BD
94+
#define FUNC_BD(...)
95+
#endif
96+
...
97+
FUNC_BD(FUNC_BD_LAST, NULL, NULL, NULL)
98+
#ifdef ELIDE_FUNC_BD
99+
#undef ELIDE_FUNC_BD
100+
#undef FUNC_BD
101+
#endif
102+
103+
#ifndef FUNC_BF
104+
#define ELIDE_FUNC_BF
105+
#define FUNC_BF(...)
106+
#endif
107+
...
108+
FUNC_BF(FUNC_BF_LAST, NULL, NULL, NULL, NULL)
109+
#ifdef ELIDE_FUNC_BF
110+
#undef ELIDE_FUNC_BF
111+
#undef FUNC_BF
112+
#endif
113+
114+
The ultimate source of the functions in the macro ``FUNC_BF(...)`` are the headers included in ``numexpr/interpreter.cpp`` (in particular
115+
``numexpr/numexpr_config.hpp``, which can be used to overwrite ``<math.h>`` functions), so the functions should be available from there.
116+
117+
* ``numexpr/interp_body.cpp``
118+
Add case support for OPCODES associated to your new function signatures via e.g. ``case OP_FUNC_BFN`` and ``case OP_FUNC_BDN``, following
119+
the framework suggested by the other functions:
120+
121+
.. code-block:: cpp
122+
123+
case OP_FUNC_BFN:
124+
#ifdef USE_VML
125+
VEC_ARG1_VML(functions_bf_vml[arg2](BLOCK_SIZE,
126+
(float*)x1, (bool*)dest));
127+
#else
128+
VEC_ARG1(b_dest = functions_bf[arg2](f1));
129+
#endif
130+
131+
Note that it is important that the out variable matches the output type of the function (i.e. ``b_dest`` for bool, ``f_dest`` for float etc.)
132+
133+
* ``numexpr/interpreter.hpp``
134+
Add clauses to read the ``functions.hpp`` macros correctly
135+
136+
.. code-block:: cpp
137+
138+
enum FuncBFCodes {
139+
#define FUNC_BF(fop, ...) fop,
140+
#include "functions.hpp"
141+
#undef FUNC_BF
142+
};
143+
144+
* ``numexpr/interpreter.cpp``
145+
Add clauses to generate the FUNC_CODES from the ``functions.hpp`` header, making sure to include clauses for ``_WIN32`` and
146+
``VML`` as necessary accoridng to the framework suggested by the other functions.
147+
148+
.. code-block:: cpp
149+
150+
typedef bool (*FuncBFPtr)(float);
151+
#ifdef _WIN32
152+
FuncBFPtr functions_bf[] = {
153+
#define FUNC_BF(fop, s, f, f_win32, ...) f_win32,
154+
#include "functions.hpp"
155+
#undef FUNC_BF
156+
};
157+
#else
158+
FuncBFPtr functions_bf[] = {
159+
#define FUNC_BF(fop, s, f, ...) f,
160+
#include "functions.hpp"
161+
#undef FUNC_BF
162+
};
163+
#endif
164+
165+
#ifdef USE_VML
166+
typedef void (*FuncBFPtr_vml)(MKL_INT, const float*, bool*);
167+
FuncBFPtr_vml functions_bf_vml[] = {
168+
#define FUNC_BF(fop, s, f, f_win32, f_vml) f_vml,
169+
#include "functions.hpp"
170+
#undef FUNC_BF
171+
};
172+
#endif
173+
174+
Add case handling to the ``check_program`` function
175+
176+
.. code-block:: cpp
177+
178+
else if (op == OP_FUNC_BDN) {
179+
if (arg < 0 || arg >= FUNC_BD_LAST) {
180+
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
181+
return -1;
182+
}
183+
}
184+
else if (op == OP_FUNC_BFN) {
185+
if (arg < 0 || arg >= FUNC_BF_LAST) {
186+
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
187+
return -1;
188+
}
189+
}
190+
191+
* ``numexpr/module.cpp``
192+
Add code here to define the ``FUNC_OA`` macros you require
193+
194+
.. code-block:: cpp
195+
196+
#define FUNC_BF(name, sname, ...) add_func(name, sname);
197+
#define FUNC_BD(name, sname, ...) add_func(name, sname);
198+
...
199+
#include "functions.hpp"
200+
...
201+
#undef FUNC_BD
202+
#undef FUNC_BF
203+
204+
* ``numexpr/opcodes.hpp``
205+
Finally, add the ``OP_FUNC_BDN`` etc. codes here. It is necessary for the OPCODES in the file to be in (ascending order) with
206+
``NOOP`` as 0 and ``OP_LAST`` as the largest number. Secondly, all reduction OPCODES must appear last. Hence, after adding your
207+
function signatures (just before the reduction OPCODES) it is necessary to increment all succeeding OPCODES.
208+
209+
.. code-block:: cpp
210+
211+
OPCODE(106, OP_FUNC_BDN, "func_bdn", Tb, Td, Tn, T0)
212+
OPCODE(107, OP_FUNC_BFN, "func_bfn", Tb, Tf, Tn, T0)
213+
214+
Notes
215+
-----
216+
In many cases this process will not be very smooth since one relies on the internal C/C++ standard functions (which can be fussy, to varying degrees on different platforms). Some common gotchas are then:
217+
218+
* OPCODES are currently only supported up to 255 - if it becomes necessary to increment further, one will have to change the ``latin_1`` encoding used in ``quadrupleToString`` in ``necompiler.py``. In addition, since the OPCDE table is assumed to be of type ``unsigned char`` the ``get_return_sig`` function in ``numexpr/interpreter.cpp`` may have to be changed (possibly other changes too).
219+
220+
* Depending on the new function signature (above all if the out type is different to the input types), one may have to edit the ``__init__`` function in the ``FuncNode`` class in ``expressions.py``.
221+
222+
* Depending on MSVC support, namespace clashes, casting problems, it may be necessary to make various changes to ``numexpr/numexpr_config.hpp`` and ``numexpr/msvc_function_stubs.hpp``. For example, in PR #523, non-clashing wrappers were introduced for ``isnan`` and ``isfinite`` since the float versions ``isnanf, isfinitef`` were inconsistently defined (and output ints) - depending on how strict the platform interpreter is, the implicit cast from int to bool was acceptable or not for example. In addition, the base functions were in different namespaces or had different names across platforms.

numexpr/expressions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ def multiply(x, y):
366366
'complex': func(complex, 'complex'),
367367
'conj': func(numpy.conj, 'complex'),
368368

369+
'isnan': func(numpy.isnan, 'bool'),
370+
'isfinite': func(numpy.isfinite, 'bool'),
371+
369372
'sum': gen_reduce_axis_func('sum'),
370373
'prod': gen_reduce_axis_func('prod'),
371374
'min': gen_reduce_axis_func('min'),
@@ -521,4 +524,6 @@ class FuncNode(OpNode):
521524
def __init__(self, opcode=None, args=None, kind=None):
522525
if (kind is None) and (args is not None):
523526
kind = commonKind(args)
527+
if opcode in ("isnan", "isfinite"): # bodge for boolean return functions
528+
kind = 'bool'
524529
OpNode.__init__(self, opcode, args, kind)

numexpr/functions.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ FUNC_DD(FUNC_DD_LAST, NULL, NULL, NULL)
8686
#undef FUNC_DD
8787
#endif
8888

89+
// double -> boolean functions
90+
#ifndef FUNC_BD
91+
#define ELIDE_FUNC_BD
92+
#define FUNC_BD(...)
93+
#endif
94+
FUNC_BD(FUNC_ISNAN_BD, "isnan_bd", isnand, vdIsnan)
95+
FUNC_BD(FUNC_ISFINITE_BD, "isfinite_bd", isfinited, vdIsfinite)
96+
FUNC_BD(FUNC_BD_LAST, NULL, NULL, NULL)
97+
#ifdef ELIDE_FUNC_BD
98+
#undef ELIDE_FUNC_BD
99+
#undef FUNC_BD
100+
#endif
101+
102+
// float -> boolean functions (C99 defines the same function for all types)
103+
#ifndef FUNC_BF
104+
#define ELIDE_FUNC_BF
105+
#define FUNC_BF(...)
106+
#endif // use wrappers as there is name collision with isnanf in std
107+
FUNC_BF(FUNC_ISNAN_BF, "isnan_bf", isnanf_, isnanf2, vfIsnan)
108+
FUNC_BF(FUNC_ISFINITE_BF, "isfinite_bf", isfinitef_, isfinitef2, vfIsfinite)
109+
FUNC_BF(FUNC_BF_LAST, NULL, NULL, NULL, NULL)
110+
#ifdef ELIDE_FUNC_BF
111+
#undef ELIDE_FUNC_BF
112+
#undef FUNC_BF
113+
#endif
114+
89115
#ifndef FUNC_DDD
90116
#define ELIDE_FUNC_DDD
91117
#define FUNC_DDD(...)

numexpr/interp_body.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,24 @@
451451
case OP_COMPLEX_CDD: VEC_ARG2(cr_dest = d1;
452452
ci_dest = d2);
453453

454+
// Boolean return types
455+
case OP_FUNC_BFN:
456+
#ifdef USE_VML
457+
VEC_ARG1_VML(functions_bf_vml[arg2](BLOCK_SIZE,
458+
(float*)x1, (bool*)dest));
459+
#else
460+
VEC_ARG1(b_dest = functions_bf[arg2](f1));
461+
#endif
462+
463+
464+
case OP_FUNC_BDN:
465+
#ifdef USE_VML
466+
VEC_ARG1_VML(functions_bd_vml[arg2](BLOCK_SIZE,
467+
(double*)x1, (bool*)dest));
468+
#else
469+
VEC_ARG1(b_dest = functions_bd[arg2](d1));
470+
#endif
471+
454472
/* Reductions */
455473
case OP_SUM_IIN: VEC_ARG1(i_reduce += i1);
456474
case OP_SUM_LLN: VEC_ARG1(l_reduce += l1);

numexpr/interpreter.cpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,47 @@ FuncDDPtr functions_dd[] = {
204204
#undef FUNC_DD
205205
};
206206

207+
// Boolean output functions
208+
typedef bool (*FuncBFPtr)(float);
209+
#ifdef _WIN32
210+
FuncBFPtr functions_bf[] = {
211+
#define FUNC_BF(fop, s, f, f_win32, ...) f_win32,
212+
#include "functions.hpp"
213+
#undef FUNC_BF
214+
};
215+
#else
216+
FuncBFPtr functions_bf[] = {
217+
#define FUNC_BF(fop, s, f, ...) f,
218+
#include "functions.hpp"
219+
#undef FUNC_BF
220+
};
221+
#endif
222+
223+
#ifdef USE_VML
224+
typedef void (*FuncBFPtr_vml)(MKL_INT, const float*, bool*);
225+
FuncBFPtr_vml functions_bf_vml[] = {
226+
#define FUNC_BF(fop, s, f, f_win32, f_vml) f_vml,
227+
#include "functions.hpp"
228+
#undef FUNC_BF
229+
};
230+
#endif
231+
232+
typedef bool (*FuncBDPtr)(double);
233+
FuncBDPtr functions_bd[] = {
234+
#define FUNC_BD(fop, s, f, ...) f,
235+
#include "functions.hpp"
236+
#undef FUNC_BD
237+
};
238+
239+
#ifdef USE_VML
240+
typedef void (*FuncBDPtr_vml)(MKL_INT, const double*, bool*);
241+
FuncBDPtr_vml functions_bd_vml[] = {
242+
#define FUNC_BD(fop, s, f, f_vml) f_vml,
243+
#include "functions.hpp"
244+
#undef FUNC_BD
245+
};
246+
#endif
247+
207248
#ifdef USE_VML
208249
/* Fake vdConj function just for casting purposes inside numexpr */
209250
static void vdConj(MKL_INT n, const double* x1, double* dest)
@@ -312,11 +353,11 @@ FuncCCCPtr functions_ccc[] = {
312353

313354
char
314355
get_return_sig(PyObject* program)
315-
{
356+
{ // use unsigned chars to match OPCODE table and allow OPCODE > 127
316357
int sig;
317-
char last_opcode;
358+
unsigned char last_opcode;
318359
Py_ssize_t end = PyBytes_Size(program);
319-
char *program_str = PyBytes_AS_STRING(program);
360+
unsigned char *program_str = (unsigned char *)PyBytes_AS_STRING(program);
320361

321362
do {
322363
end -= 4;
@@ -464,6 +505,18 @@ check_program(NumExprObject *self)
464505
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
465506
return -1;
466507
}
508+
}
509+
else if (op == OP_FUNC_BDN) {
510+
if (arg < 0 || arg >= FUNC_BD_LAST) {
511+
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
512+
return -1;
513+
}
514+
}
515+
else if (op == OP_FUNC_BFN) {
516+
if (arg < 0 || arg >= FUNC_BF_LAST) {
517+
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
518+
return -1;
519+
}
467520
} else if (op >= OP_REDUCTION) {
468521
;
469522
} else {

0 commit comments

Comments
 (0)