-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-44946: Streamline operators and creation of ints for common case of single 'digit'. #27832
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
Changes from 1 commit
da57f0b
0533a9f
9349daa
96496e2
5e4aad5
59ba476
0d3ca1d
c73333b
16d3167
f20a2a8
ab2b908
e43060a
ed2a430
1f2d47c
649c311
a69f420
47571ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…llow for 15 bit digits on 64 bit machines.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ medium_value(PyLongObject *x) | |
#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS) | ||
#define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS) | ||
|
||
/* To be valid the type of x must cover -PyLong_BASE to +PyLong_BASE. | ||
int, long, Py_ssize_t are all ok */ | ||
#define IS_MEDIUM_INT(x) (((twodigits)x)+PyLong_MASK <= 2*PyLong_MASK) | ||
|
||
static PyObject * | ||
|
@@ -195,9 +197,10 @@ _PyLong_FromLarge(stwodigits ival) | |
abs_ival = (twodigits)ival; | ||
sign = 1; | ||
} | ||
/* Loop to determine number of digits */ | ||
twodigits t = abs_ival; | ||
Py_ssize_t ndigits = 0; | ||
/* Must be at least two digits */ | ||
assert(abs_ival >> PyLong_SHIFT != 0); | ||
twodigits t = abs_ival >> (PyLong_SHIFT *2); | ||
markshannon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Py_ssize_t ndigits = 2; | ||
while (t) { | ||
++ndigits; | ||
t >>= PyLong_SHIFT; | ||
|
@@ -251,8 +254,44 @@ _PyLong_Negate(PyLongObject **x_p) | |
PyObject * | ||
PyLong_FromLong(long ival) | ||
{ | ||
Py_BUILD_ASSERT(sizeof(stwodigits) >= sizeof(long)); | ||
return _PyLong_FromSTwoDigits(ival); | ||
if (IS_SMALL_INT(ival)) { | ||
return get_small_int((sdigit)ival); | ||
} | ||
unsigned long abs_ival; | ||
int sign; | ||
if (ival < 0) { | ||
/* negate: can't write this as abs_ival = -ival since that | ||
invokes undefined behaviour when ival is LONG_MIN */ | ||
abs_ival = 0U-(twodigits)ival; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not have been changed. There's no guarantee that an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #30496. We seem to be okay on current platforms because from |
||
sign = -1; | ||
} | ||
else { | ||
abs_ival = (unsigned long)ival; | ||
sign = 1; | ||
} | ||
/* Fast path for single-digit ints */ | ||
if (!(abs_ival >> PyLong_SHIFT)) { | ||
return _PyLong_FromMedium((sdigit)ival); | ||
} | ||
/* Must be at least two digits */ | ||
unsigned long t = abs_ival >> (PyLong_SHIFT *2); | ||
Py_ssize_t ndigits = 2; | ||
while (t) { | ||
++ndigits; | ||
t >>= PyLong_SHIFT; | ||
} | ||
PyLongObject *v = _PyLong_New(ndigits); | ||
if (v != NULL) { | ||
digit *p = v->ob_digit; | ||
Py_SET_SIZE(v, ndigits * sign); | ||
t = abs_ival; | ||
while (t) { | ||
*p++ = Py_SAFE_DOWNCAST( | ||
t & PyLong_MASK, unsigned long, digit); | ||
t >>= PyLong_SHIFT; | ||
} | ||
} | ||
return (PyObject *)v; | ||
} | ||
|
||
#define PYLONG_FROM_UINT(INT_TYPE, ival) \ | ||
|
@@ -3554,7 +3593,7 @@ long_mul(PyLongObject *a, PyLongObject *b) | |
/* fast path for single-digit multiplication */ | ||
if (IS_MEDIUM_VALUE(a) && IS_MEDIUM_VALUE(b)) { | ||
stwodigits v = medium_value(a) * medium_value(b); | ||
return PyLong_FromLongLong((long long)v); | ||
return _PyLong_FromSTwoDigits(v); | ||
} | ||
|
||
z = k_mul(a, b); | ||
|
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.
It would be useful to have a comment clarifying what range of values this macro can safely be used for. I'm assuming it should be enough that it's valid for values in the range
(-PyLong_BASE**2, PyLong_BASE**2)
.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.
Sorry, I think I was unclear. The
(twodigits)x
cast potentially loses information ifx
is large enough, leading to the possibility of false positives forIS_MEDIUM_INT
. For example, that will happen on Windows with a largePy_ssize_t
value and 15-bit digits - in that case,Py_ssize_t
is much larger thanunsigned long
.So there's some restriction on the value of
x
for which this test is valid. "Fits instwodigits
" would probably be enough, but I don't think we use this macro for values outside the range(-PyLong_BASE**2, PyLong_BASE**2)
.Uh oh!
There was an error while loading. Please reload this page.
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.
More generally, C's integer-handling rules make this sort of thing horribly messy to reason about: for example in the 15-bit digit case the addition is an addition of an
unsigned long
to a (signed!)int
, since the integer promotions will promote theunsigned short
PyLong_MASK
to anint
(though even that part is not guaranteed by the standard - there's nothing preventingshort
andint
having the same precision, in which casePyLong_MASK
will be promoted tounsigned int
instead ofint
). So now we have to consult the rules for unsigned + signed addition in the "usual arithmetic conversions", which eventually say that becauselong
has greater rank thanint
(even if it has the same precision), both operands will be treated asunsigned long
for the addition.The
2 * PyLong_MASK
is another case that could end up being either signed or unsigned depending on ranks, types, etc; it's probably better spelled as2U * PyLong_MASK
; that way we can at least be sure that it's performed as an unsigned multiplication and that the final comparison is unsigned-to-unsigned.I'd suggest the addition of an extra cast around the result of the addition, just to reduce the number of mental hoops one has to jump through to establish that this really does give the right result: that is,
We should also add extra parentheses around the
x
, in case someone tries to useIS_MEDIUM_INT
on an expression more complicated than a single name.Uh oh!
There was an error while loading. Please reload this page.
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 wholeheartedly agree that C's integer handling is a pain to think about 😞
For clarity I think this is best to use an inline function that makes all casts super explicit.
That way that it makes the cast explicit (if called with something other than
stwodigits
orsdigits
, the caller is responsible.Does that seem sensible?