diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2019-01-20 15:50:42 +0100 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2019-01-20 15:50:42 +0100 |
commit | 6d9daa08142e8a61d1f098fa0039b642537e614c (patch) | |
tree | 2663a75fa8154e0e83ab47d99181dc159c2b50bd | |
parent | 81c44b70bc67698b7e1040ca2f6942c2c68b5887 (diff) | |
download | cython-6d9daa08142e8a61d1f098fa0039b642537e614c.tar.gz |
Speed up &-ing multi-digit PyLongs with known single digit integers, e.g. "bigx & 255" or "bigx & 1".
-rw-r--r-- | Cython/Utility/Optimize.c | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/Cython/Utility/Optimize.c b/Cython/Utility/Optimize.c index a8287f79e..441f3780a 100644 --- a/Cython/Utility/Optimize.c +++ b/Cython/Utility/Optimize.c @@ -887,6 +887,12 @@ static {{c_ret_type}} __Pyx_PyInt_{{'' if ret_type.is_pyobject else 'Bool'}}{{op {{endif}} const digit* digits = ((PyLongObject*){{pyval}})->ob_digit; const Py_ssize_t size = Py_SIZE({{pyval}}); + {{if c_op == '&'}} + // special case for &-ing arbitrarily large numbers with known single digit operands + if ((intval & PyLong_MASK) == intval) { + return PyLong_FromLong(likely(size) ? digits[0] & intval : 0); + } else + {{endif}} // handle most common case first to avoid indirect branch and optimise branch prediction if (likely(__Pyx_sst_abs(size) <= 1)) { {{ival}} = likely(size) ? digits[0] : 0; |