Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-99761: Add _PyLong_IsPositiveSingleDigit #100064

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
int base,
int alternate);

/* Return 1 if the argument is positive single digit int */
static inline int
_PyLong_IsPositiveSingleDigit(PyObject* sub) {
// this method uses the twos-complement representation
Copy link
Member

Choose a reason for hiding this comment

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

This comment feels disconnected (two's complement is not unique to this function, it is assumed everywhere).

What requires an explanation is the trick of casting a signed value to an unsigned value and then checking whether the result is <= 1. The clever bit here is that this cast makes all negative numbers be considered very large positive numbers.

I'm also not keen on 'signed_magnitude' as the name for the variable. It makes me think of the "sign + magnitude" representation of numbers which is actually how I'd describe one's complement (!). I suggest renaming it to 'signed_size' which is just a reminder of what Py_SIZE() of a PyLong represents the size, negated if the sign of the overall number is negative. (The clever bit there is that the value zero has size 0 which is invariant if negated.)

Copy link
Member

Choose a reason for hiding this comment

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

I asked to mention two's complement in this function, even if I'm not sure that this optimization relies on it.

This change motivated me to create issue #100008 to require two's complement integer representation to build Python.

Copy link
Member

Choose a reason for hiding this comment

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

What's a cast from int to uint expected to do for negative values in one's complement? Thinking about it more, it probably still converts all negative numbers to very large positive ones, so it would still work, except for -0. Or maybe even in that case, because that's not a positive int.

So I'm still not sure that two's complement deserves being called out here.

(I do agree that we should stop believing we might support one's complement. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that mentioning twos complement here is not relevant. I updated the description and included the link that was already present in the valid_index method.

I also updated the name to signed_size.

assert(PyLong_CheckExact(sub));
Py_ssize_t signed_magnitude = Py_SIZE(sub);
return ((size_t)signed_magnitude) <= 1;
}

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 3 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ dummy_func(
DEOPT_IF(!PyList_CheckExact(list), BINARY_SUBSCR);

// Deopt unless 0 <= sub < PyList_Size(list)
Py_ssize_t signed_magnitude = Py_SIZE(sub);
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
DEOPT_IF(!_PyLong_IsPositiveSingleDigit(sub), BINARY_SUBSCR);
assert(((PyLongObject *)_PyLong_GetZero())->ob_digit[0] == 0);
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyList_GET_SIZE(list), BINARY_SUBSCR);
Expand All @@ -402,8 +401,7 @@ dummy_func(
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);

// Deopt unless 0 <= sub < PyTuple_Size(list)
Py_ssize_t signed_magnitude = Py_SIZE(sub);
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
DEOPT_IF(!_PyLong_IsPositiveSingleDigit(sub), BINARY_SUBSCR);
assert(((PyLongObject *)_PyLong_GetZero())->ob_digit[0] == 0);
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyTuple_GET_SIZE(tuple), BINARY_SUBSCR);
Expand Down Expand Up @@ -507,7 +505,7 @@ dummy_func(
DEOPT_IF(!PyList_CheckExact(list), STORE_SUBSCR);

// Ensure nonnegative, zero-or-one-digit ints.
DEOPT_IF(((size_t)Py_SIZE(sub)) > 1, STORE_SUBSCR);
DEOPT_IF(!_PyLong_IsPositiveSingleDigit(sub), STORE_SUBSCR);
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
// Ensure index < len(list)
DEOPT_IF(index >= PyList_GET_SIZE(list), STORE_SUBSCR);
Expand Down
8 changes: 3 additions & 5 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.