Commit 2fa0e9f6 authored by Stefan Behnel's avatar Stefan Behnel

Inline fast special cases for "abs(PyLong)" when the argument is non-negative or small.

parent e4e2be30
......@@ -5,6 +5,11 @@ Cython Changelog
0.28.2 (2018-??-??)
===================
Features added
--------------
* ``abs()`` is faster for Python long objects.
Bugs fixed
----------
......
......@@ -124,7 +124,8 @@ builtin_function_table = [
PyrexTypes.c_double_complex_type,
PyrexTypes.c_longdouble_complex_type)
) + [
BuiltinFunction('abs', "O", "O", "PyNumber_Absolute"),
BuiltinFunction('abs', "O", "O", "__Pyx_PyNumber_Absolute",
utility_code=UtilityCode.load("py_abs", "Builtins.c")),
#('all', "", "", ""),
#('any', "", "", ""),
#('ascii', "", "", ""),
......
......@@ -253,6 +253,33 @@ static CYTHON_INLINE PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) {
}
//////////////////// py_abs.proto ////////////////////
#if CYTHON_USE_PYLONG_INTERNALS
static PyObject *__Pyx_PyLong_AbsNeg(PyObject *num);/*proto*/
#define __Pyx_PyNumber_Absolute(x) \
((likely(PyLong_CheckExact(x))) ? \
(likely(Py_SIZE(x) >= 0) ? (Py_INCREF(x), (x)) : __Pyx_PyLong_AbsNeg(x)) : \
PyNumber_Absolute(x))
#else
#define __Pyx_PyNumber_Absolute(x) PyNumber_Absolute(x)
#endif
//////////////////// py_abs ////////////////////
#if CYTHON_USE_PYLONG_INTERNALS
static PyObject *__Pyx_PyLong_AbsNeg(PyObject *n) {
if (likely(Py_SIZE(n) == -1)) {
// digits are unsigned
return PyLong_FromLong(((PyLongObject*)n)->ob_digit[0]);
}
return ((PyTypeObject*)Py_TYPE(n))->tp_as_number->nb_negative(n);
}
#endif
//////////////////// pow2.proto ////////////////////
#define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment