Skip to content

Commit 42acb7b

Browse files
hongweipengmethane
authored andcommitted
bpo-35696: Simplify long_compare() (GH-16146)
1 parent d299b8b commit 42acb7b

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Objects/longobject.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,33 +3028,32 @@ PyLong_AsDouble(PyObject *v)
30283028

30293029
/* Methods */
30303030

3031-
static int
3031+
/* if a < b, return a negative number
3032+
if a == b, return 0
3033+
if a > b, return a positive number */
3034+
3035+
static Py_ssize_t
30323036
long_compare(PyLongObject *a, PyLongObject *b)
30333037
{
3034-
Py_ssize_t sign;
3035-
3036-
if (Py_SIZE(a) != Py_SIZE(b)) {
3037-
sign = Py_SIZE(a) - Py_SIZE(b);
3038-
}
3039-
else {
3038+
Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b);
3039+
if (sign == 0) {
30403040
Py_ssize_t i = Py_ABS(Py_SIZE(a));
3041-
while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
3042-
;
3043-
if (i < 0)
3044-
sign = 0;
3045-
else {
3046-
sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
3047-
if (Py_SIZE(a) < 0)
3048-
sign = -sign;
3041+
sdigit diff = 0;
3042+
while (--i >= 0) {
3043+
diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i];
3044+
if (diff) {
3045+
break;
3046+
}
30493047
}
3048+
sign = Py_SIZE(a) < 0 ? -diff : diff;
30503049
}
3051-
return sign < 0 ? -1 : sign > 0 ? 1 : 0;
3050+
return sign;
30523051
}
30533052

30543053
static PyObject *
30553054
long_richcompare(PyObject *self, PyObject *other, int op)
30563055
{
3057-
int result;
3056+
Py_ssize_t result;
30583057
CHECK_BINOP(self, other);
30593058
if (self == other)
30603059
result = 0;
@@ -5210,7 +5209,8 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
52105209
{
52115210
PyLongObject *quo = NULL, *rem = NULL;
52125211
PyObject *twice_rem, *result, *temp;
5213-
int cmp, quo_is_odd, quo_is_neg;
5212+
int quo_is_odd, quo_is_neg;
5213+
Py_ssize_t cmp;
52145214

52155215
/* Equivalent Python code:
52165216

0 commit comments

Comments
 (0)