Skip to content

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Dec 6, 2022

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

@vstinner
Copy link
Member

vstinner commented Dec 6, 2022

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 !_PyLong_IsPositiveSingleDigit(number).

@eendebakpt
Copy link
Contributor Author

_PyLong_IsPositiveSingleDigit

I agree _PyLong_IsPositiveSingleDigit is a better name. The method is only used in three locations (in code that I guess will only be modified by expert core devs), so if this variation does not improve the code I am fine with closing the PR.

@eendebakpt eendebakpt changed the title gh-99761: Add _PyLongNegative_or_multi_digit_int gh-99761: Add _PyLong_IsPositiveSingleDigit Dec 7, 2022
@vstinner
Copy link
Member

vstinner commented Dec 8, 2022

@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.

/* 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.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Excellent! Future generations will thank you for that comment.

@python python deleted a comment from netlify bot Dec 10, 2022
@gvanrossum
Copy link
Member

@vstinner Do you think this requires a news entry? If not, just add the skip news label and merge.