diff --git a/Objects/longobject.c b/Objects/longobject.c index 4e2cc41..c48502c 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -2211,16 +2211,17 @@ long_compare(PyLongObject *a, PyLongObject *b) } else { Py_ssize_t i = PyLong_NDIGITS(a); - while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) - ; - if (i < 0) - sign = 0; - else { - sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; - if (Py_SIZE(a) < 0) + if (i == 0) + return 0; + do { + --i; + sign = (sdigit)a->ob_digit[i] - b->ob_digit[i]; + if (sign != 0) + break; + } while (i != 0); + if (PyLong_SIGN(a) < 0) sign = -sign; } - } return sign < 0 ? -1 : sign > 0 ? 1 : 0; } @@ -3256,6 +3257,21 @@ long_rshift(PyLongObject *a, PyLongObject *b) CHECK_BINOP(a, b); + if (PyLong_FITS_INT(a) && PyLong_FITS_INT(b)) { + sdigit aa = PyLong_GET_INT(a); + sdigit bb = PyLong_GET_INT(b); + if (bb < 0) { + PyErr_SetString(PyExc_ValueError, + "negative shift count"); + goto rshift_error; + } + if (bb < sizeof(aa)*8) + aa >>= bb; + else + aa = 0; + return PyLong_FromLong(aa); + } + if (Py_SIZE(a) < 0) { /* Right shifting negative numbers is harder */ PyLongObject *a1, *a2; @@ -3330,6 +3346,15 @@ long_lshift(PyObject *v, PyObject *w) "outrageous left shift count"); goto lshift_error; } + + if ((shiftby < (sdigit)8 * sizeof(sdigit)) && PyLong_FITS_INT(a)) { + sdigit aa = PyLong_GET_INT(a); + sdigit bb = (sdigit)shiftby; + sdigit cc = aa << bb; + if ((cc >> bb) == aa) + return PyLong_FromLong(cc); + } + /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ wordshift = (int)shiftby / PyLong_SHIFT; remshift = (int)shiftby - wordshift * PyLong_SHIFT;