Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Changes from 2.14.2 to 2.14.3
-----------------------------

* **Under development.**
* ``abs()`` now returns integer results for integer inputs, as documented,
instead of casting them to ``double``. This also keeps int64 magnitudes
above 2**53 exact.
* Replaced expression-string blacklist filtering with AST validation before
evaluation, and disabled Python builtins while sanitization is enabled.
This closes sanitizer bypasses in both cached and ``disable_cache=True``
Expand Down
28 changes: 18 additions & 10 deletions numexpr/bespoke_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <string.h>
#include <assert.h>
#include <vector>
#include <limits>
#include "numexpr_config.hpp" // isnan definitions

// Generic sign function
inline int signi(int x) {return (0 < x) - (x < 0);}
inline long signl(long x) {return (0 < x) - (x < 0);}
inline long long signl(long long x) {return (0 < x) - (x < 0);}
inline double sign(double x){
// Floats: -1.0, 0.0, +1.0, NaN stays NaN
if (isnand(x)) {return NAN;}
Expand All @@ -25,10 +26,17 @@ inline float signf(float x){

// round function for ints
inline int rinti(int x) {return x;}
inline long rintl(long x) {return x;}
inline long long rintl(long long x) {return x;}
// abs function for ints
inline int fabsi(int x) {return x<0 ? -x: x;}
inline long fabsl(long x) {return x<0 ? -x: x;}
inline int fabsi(int x) {
// Match NumPy's wraparound result without negating the minimum integer.
if (x == std::numeric_limits<int>::min()) {return x;}
return x < 0 ? -x : x;
}
inline long long fabsl(long long x) {
if (x == std::numeric_limits<long long>::min()) {return x;}
return x < 0 ? -x : x;
}
// fmod function for ints
// TODO: Have to add FUNC_III, FUNC_LLL signatures to functions.hpp to enable these
// inline int fmodi(int x, int y) {return (int)fmodf((float)x, (float)y);}
Expand Down Expand Up @@ -85,24 +93,24 @@ static void viRint(MKL_INT n, const int* x, int* dest)
memcpy(dest, x, n * sizeof(int)); // just copy x1 which is already int
};

static void vlRint(MKL_INT n, const long* x, long* dest)
static void vlRint(MKL_INT n, const long long* x, long long* dest)
{
memcpy(dest, x, n * sizeof(long)); // just copy x1 which is already int
memcpy(dest, x, n * sizeof(long long)); // just copy x1 which is already int
};

static void viFabs(MKL_INT n, const int* x, int* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j] = x[j] < 0 ? -x[j]: x[j];
dest[j] = fabsi(x[j]);
};
};

static void vlFabs(MKL_INT n, const long* x, long* dest)
static void vlFabs(MKL_INT n, const long long* x, long long* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j] = x[j] < 0 ? -x[j]: x[j];
dest[j] = fabsl(x[j]);
};
};

Expand Down Expand Up @@ -309,7 +317,7 @@ static void viSign(MKL_INT n, const int* x1, int* dest)
dest[j] = signi(x1[j]);
};
};
static void vlSign(MKL_INT n, const long* x1, long* dest)
static void vlSign(MKL_INT n, const long long* x1, long long* dest)
{
MKL_INT j;
for(j=0; j < n; j++) {
Expand Down
2 changes: 1 addition & 1 deletion numexpr/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def function(*args):
return ConstantNode(func(*[x.value for x in args]))
kind = commonKind(args)
if kind in ('int', 'long'):
if func.__name__ not in ('copy', 'abs', 'ones_like', 'round', 'sign'):
if func.__name__ not in ('copy', 'absolute', 'ones_like', 'round', 'sign'):
# except for these special functions (which return ints for int inputs in NumPy)
# just do a cast to double
# FIXME: 'fmod' outputs ints for NumPy when inputs are ints, but need to
Expand Down
2 changes: 1 addition & 1 deletion numexpr/interp_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@
case OP_FUNC_LLN:
#ifdef USE_VML
VEC_ARG1_VML(functions_ll_vml[arg2](BLOCK_SIZE,
(long*)x1, (long*)dest));
(long long*)x1, (long long*)dest));
#else
VEC_ARG1(l_dest = functions_ll[arg2](l1));
#endif
Expand Down
4 changes: 2 additions & 2 deletions numexpr/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ FuncIIPtr_vml functions_ii_vml[] = {
};
#endif

typedef long (*FuncLLPtr)(long);
typedef long long (*FuncLLPtr)(long long);
FuncLLPtr functions_ll[] = {
#define FUNC_LL(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_LL
};

#ifdef USE_VML
typedef void (*FuncLLPtr_vml)(MKL_INT, const long*, long*);
typedef void (*FuncLLPtr_vml)(MKL_INT, const long long*, long long*);
FuncLLPtr_vml functions_ll_vml[] = {
#define FUNC_LL(fop, s, f, f_vml) f_vml,
#include "functions.hpp"
Expand Down
8 changes: 8 additions & 0 deletions numexpr/tests/test_numexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,14 @@ def test_sign_round(self):
assert evaluate("sign(r)").dtype == sign(r).dtype
assert_array_equal(evaluate("sign(r)"), sign(r))
assert_array_equal(evaluate("round(r)"), round(r))
if np.issubdtype(dtype, np.integer):
assert evaluate("abs(r)").dtype == abs(r).dtype
assert_array_equal(evaluate("abs(r)"), abs(r))
lowest = np.array([np.iinfo(dtype).min], dtype=dtype)
assert evaluate("abs(lowest)").tolist() == abs(lowest).tolist()
# int64 magnitudes beyond 2**53 are not exactly representable as double
r = np.array([-(2**53 + 1), 2**62 + 1], dtype=np.int64)
assert evaluate("abs(r)").tolist() == abs(r).tolist()

def test_rational_expr(self):
a = arange(1e6)
Expand Down