Skip to content

Commit e4dba41

Browse files
authored
gh-161127: Let compilers vectorize v_complement() (#161128)
1 parent 126f33e commit e4dba41

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎Objects/longobject.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5554,14 +5554,20 @@ _PyLong_Lshift(PyObject *a, int64_t shiftby)
55545554
static void
55555555
v_complement(digit *z, digit *a, Py_ssize_t m)
55565556
{
5557-
Py_ssize_t i;
5558-
digit carry = 1;
5559-
for (i = 0; i < m; ++i) {
5560-
carry += a[i] ^ PyLong_MASK;
5561-
z[i] = carry & PyLong_MASK;
5562-
carry >>= PyLong_SHIFT;
5557+
Py_ssize_t i = 0;
5558+
5559+
/* -a is ~a + 1, and the +1 only carries through the trailing zero
5560+
digits: they stay zero, the first nonzero digit is negated, and the
5561+
digits above it are just inverted. No carry is passed between
5562+
iterations of the last loop, so it can be vectorized. */
5563+
while (i < m && a[i] == 0) {
5564+
z[i++] = 0;
5565+
}
5566+
assert(i < m);
5567+
z[i] = (0U - a[i]) & PyLong_MASK;
5568+
for (++i; i < m; ++i) {
5569+
z[i] = a[i] ^ PyLong_MASK;
55635570
}
5564-
assert(carry == 0);
55655571
}
55665572

55675573
/* Bitwise and/xor/or operations */

0 commit comments

Comments
 (0)