Commit e9555bb4 authored by Stefan Behnel's avatar Stefan Behnel

tweak 2**N optimisation a bit more in Py3

parent 43d26ad8
......@@ -435,6 +435,7 @@ bad:
static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace); /*proto*/
/////////////// PyNumberPow2 ///////////////
//@requires: TypeConversion.c::PyLongInternals
static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace) {
// in CPython, 1<<N is substantially faster than 2**N
......@@ -442,7 +443,17 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject
#if CYTHON_COMPILING_IN_CPYTHON
Py_ssize_t shiftby;
if (likely(PyLong_CheckExact(exp))) {
#if PY_MAJOR_VERSION >= 3 && CYTHON_USE_PYLONG_INTERNALS
switch (Py_SIZE(exp)) {
case 0: shiftby = 0; break;
case 1: shiftby = ((PyLongObject*)exp)->ob_digit[0]; break;
default:
if (unlikely(Py_SIZE(exp) < 0)) goto fallback;
shiftby = PyLong_AsSsize_t(exp); break;
}
#else
shiftby = PyLong_AsSsize_t(exp);
#endif
#if PY_MAJOR_VERSION < 3
} else if (likely(PyInt_CheckExact(exp))) {
shiftby = PyInt_AsLong(exp);
......
......@@ -150,6 +150,7 @@ bad:
#endif
/////////////// TypeConversions ///////////////
//@requires: PyLongInternals
/* Type Conversion Functions */
......@@ -279,11 +280,6 @@ static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {
return res;
}
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
Py_ssize_t ival;
PyObject *x;
......@@ -504,18 +500,23 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
}
/////////////// PyLongInternals ///////////////
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
/////////////// CIntFromPy.proto ///////////////
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
/////////////// CIntFromPy ///////////////
//@requires: CIntFromPyVerify
//@requires: PyLongInternals
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
const int is_unsigned = neg_one > const_zero;
......
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