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
gh-99761: Add _PyLong_IsPositiveSingleDigit #100064
Conversation
Honestly, I'm not sure that this code makes the code easier to read or maintain. I don't get the "_PyLong_Negative_or_multi_digit_int" name. From what I read, I understand that the optimization only works if the number is positive or zero and has a single digit. Maybe the function name should be the opposite, something like: _PyLong_IsPositiveSingleDigit(). The optimization would be disabled if |
I agree |
@gvanrossum @markshannon: What do you think of this change? Does it make the code more readable? The initial motivation was to put the (size_t) cast optimization in a function to give it a better name and make it less magic. The cast is used to implement "0 <= ndigits && ndigits <= 1" as a single test "(size_t)ndigits <= 1". See also #99761 for more context. |
Include/internal/pycore_long.h
Outdated
/* Return 1 if the argument is positive single digit int */ | ||
static inline int | ||
_PyLong_IsPositiveSingleDigit(PyObject* sub) { | ||
// this method uses the twos-complement representation |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :-)
There was a problem hiding this comment.
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
.
@vstinner Do you think this requires a news entry? If not, just add the skip news label and merge. |
I added |
Okay, @kumaraditya303 go ahead and merge. @eendebakpt Thanks for the code! |
|
Add
_PyLong_IsPositiveSingleDigit
to unify the usage of twos-complement in Python.This PR was part of #99762, but split off on suggestion of @vstinner