Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,44 @@ def test_bitop_identities(self):
self.check_bitop_identities_2(x, y)
self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))

def test_bitwise_cancellation(self):
for ndigits in (3, 4, 8, 16):
high = BASE ** ndigits
for low in (0, 1, 256, 257, BASE - 1, BASE, BASE + 1,
BASE ** 2 - 1, BASE ** 2):
with self.subTest(ndigits=ndigits, low=low):
self.assertEqual((high + low) ^ high, low)
self.assertEqual(high ^ (high + low), low)
self.assertEqual((high + low) & (2 * high + low), low)
self.assertEqual((2 * high + low) & (high + low), low)
self.assertEqual((high + low) & (BASE * high + low), low)
self.assertEqual((BASE * high + low) & (high + low), low)

def test_bitwise_digit_boundaries(self):
values = {0, 1, -1}
for ndigits in (1, 2, 3, 4, 8):
power = BASE ** ndigits
for value in (power - 1, power, power + 1, power + BASE - 1,
power + BASE, 2 * power - 1):
values.update((value, -value))

values = sorted(values)
for x in values:
for y in values:
with self.subTest(x=x, y=y):
nbytes = (max(x.bit_length(), y.bit_length()) + 8) // 8
a = x.to_bytes(nbytes, 'little', signed=True)
b = y.to_bytes(nbytes, 'little', signed=True)
self.assertEqual(x & y, int.from_bytes(
bytes(u & v for u, v in zip(a, b)),
'little', signed=True))
self.assertEqual(x | y, int.from_bytes(
bytes(u | v for u, v in zip(a, b)),
'little', signed=True))
self.assertEqual(x ^ y, int.from_bytes(
bytes(u ^ v for u, v in zip(a, b)),
'little', signed=True))

def slow_format(self, x, base):
digits = []
sign = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce memory usage for bitwise ``&`` and ``^`` operations on non-negative
:class:`int` objects when their high bits cancel out. Patch by Shamil Abdulaev.
49 changes: 38 additions & 11 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5627,6 +5627,10 @@ long_bitwise(PyLongObject *a,
b = z;
}

const digit *a_digits = a->long_value.ob_digit;
const digit *b_digits = b->long_value.ob_digit;
int nonnegative = !nega && !negb;

/* JRH: The original logic here was to allocate the result value (z)
as the longer of the two operands. However, there are some cases
where the result is guaranteed to be shorter than that: AND of two
Expand All @@ -5638,10 +5642,21 @@ long_bitwise(PyLongObject *a,
case '^':
negz = nega ^ negb;
size_z = size_a;
if (nonnegative && size_a == size_b) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (nonnegative && size_a == size_b) {
if (!negz && size_z <= size_b) {

I think we can widen the condition a bit (also for the other branch). Needs to be checked though.

while (size_z > 0 && a_digits[size_z - 1] == b_digits[size_z - 1]) {
--size_z;
}
}
break;
case '&':
negz = nega & negb;
size_z = negb ? size_a : size_b;
if (nonnegative) {
while (size_z > 0 &&
(a_digits[size_z - 1] & b_digits[size_z - 1]) == 0) {
--size_z;
}
}
break;
case '|':
negz = nega | negb;
Expand All @@ -5657,6 +5672,13 @@ long_bitwise(PyLongObject *a,
return get_small_int(0);
}

if (nonnegative && size_z == 1 && op != '|') {
digit a0 = a_digits[0];
digit b0 = size_b ? b_digits[0] : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
digit b0 = size_b ? b_digits[0] : 0;
digit b0 = b_digits[0];

digit d = op == '&' ? a0 & b0 : a0 ^ b0;
return (PyObject *)_PyLong_FromSTwoDigits(d);
}

/* We allow an extra digit if z is negative, to make sure that
the final two's complement of z doesn't overflow. */
z = long_alloc(size_z + negz);
Expand All @@ -5667,18 +5689,20 @@ long_bitwise(PyLongObject *a,
}

/* Compute digits for overlap of a and b. */
digit *z_digits = z->long_value.ob_digit;
Py_ssize_t size_overlap = Py_MIN(size_b, size_z);
switch(op) {
case '&':
for (i = 0; i < size_b; ++i)
z->long_value.ob_digit[i] = a->long_value.ob_digit[i] & b->long_value.ob_digit[i];
for (i = 0; i < size_overlap; ++i)
z_digits[i] = a_digits[i] & b_digits[i];
break;
case '|':
for (i = 0; i < size_b; ++i)
z->long_value.ob_digit[i] = a->long_value.ob_digit[i] | b->long_value.ob_digit[i];
for (i = 0; i < size_overlap; ++i)
z_digits[i] = a_digits[i] | b_digits[i];
break;
case '^':
for (i = 0; i < size_b; ++i)
z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ b->long_value.ob_digit[i];
for (i = 0; i < size_overlap; ++i)
z_digits[i] = a_digits[i] ^ b_digits[i];
break;
default:
Py_UNREACHABLE();
Expand All @@ -5687,16 +5711,19 @@ long_bitwise(PyLongObject *a,
/* Copy any remaining digits of a, inverting if necessary. */
if (op == '^' && negb)
for (; i < size_z; ++i)
z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ PyLong_MASK;
z_digits[i] = a_digits[i] ^ PyLong_MASK;
else if (i < size_z)
memcpy(&z->long_value.ob_digit[i], &a->long_value.ob_digit[i],
(size_z-i)*sizeof(digit));
memcpy(z_digits + i, a_digits + i, (size_z-i)*sizeof(digit));

/* Complement result if negative. */
if (negz) {
_PyLong_FlipSign(z);
z->long_value.ob_digit[size_z] = PyLong_MASK;
v_complement(z->long_value.ob_digit, z->long_value.ob_digit, size_z+1);
z_digits[size_z] = PyLong_MASK;
v_complement(z_digits, z_digits, size_z+1);
}

if (nonnegative) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would perhaps drop this (and the 1-digit shortcut above). If keeping here, add an assert that maybe_small_long and long_normalize are not needed here.

return (PyObject *)z;
}

Py_XDECREF(new_a);
Expand Down
Loading