Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0ec07e4
Add functions to hide some internals of long object.
markshannon Jan 25, 2023
292b9d0
Add internal functions to longobject.c for setting sign and digit count.
markshannon Jan 25, 2023
5c54894
Replace Py_SIZE(x) < 0 with _PyLong_IsNegative(x) in longobject.c
markshannon Feb 28, 2023
029aaa4
Replace Py_ABS(Py_SIZE(a)) with _PyLong_DigitCount(a) in longobject.c
markshannon Feb 28, 2023
b56e6da
Remove many uses of Py_SIZE in longobject.c
markshannon Feb 28, 2023
91269fc
Remove _PyLong_AssignValue, as it is no longer used.
markshannon Feb 28, 2023
c48e825
Remove some more uses of Py_SIZE in longobject.c.
markshannon Feb 28, 2023
449c0e2
Remove a few more uses of Py_SIZE in longobject.c.
markshannon Mar 1, 2023
c5ba601
Remove some more uses of Py_SIZE, replacing with _PyLong_UnsignedDigi…
markshannon Mar 1, 2023
4b3a3e8
Replace a few Py_SIZE() with _PyLong_SameSign().
markshannon Mar 1, 2023
9ef9d2c
Remove a few more Py_SIZE() from longobject.c
markshannon Mar 1, 2023
9c408c1
Replace uses of IS_MEDIUM_VALUE macro with _PyLong_IsSingleDigit.
markshannon Mar 1, 2023
548d656
Remove most of the remaining uses of Py_SIZE in longobject.c
markshannon Mar 1, 2023
3e3fefd
Replace last remaining uses of Py_SIZE applied to longobject with _Py…
markshannon Mar 1, 2023
391fb51
Don't use _PyObject_InitVar and move a couple of inline functions to …
markshannon Mar 1, 2023
df8c7d3
Correct name of inline function.
markshannon Mar 1, 2023
bc14fa6
Eliminate all remaining uses of Py_SIZE and Py_SET_SIZE on PyLongObject.
markshannon Mar 1, 2023
54c6f1b
Change layout of size/sign bits in longobject to support future addit…
markshannon Mar 2, 2023
ce6bfb2
Test pairs of longs together on fast path of add/mul/sub.
markshannon Mar 2, 2023
4c1956b
Tidy up comment and delete commented out code.
markshannon Mar 6, 2023
301158b
Add news.
markshannon Mar 6, 2023
1aa1891
Remove debugging asserts.
markshannon Mar 6, 2023
bf2a9af
Fix storage classes.
markshannon Mar 6, 2023
169f521
Remove development debug functions.
markshannon Mar 6, 2023
90f9072
Avoid casting to smaller int.
markshannon Mar 8, 2023
f143443
Apply suggestions from code review.
markshannon Mar 8, 2023
a0d661e
Widen types to avoid data loss.
markshannon Mar 8, 2023
145a2e4
Fix syntax error.
markshannon Mar 8, 2023
638a98f
Replace 'SingleDigit' with 'Compact' as the term 'single digit' seems…
markshannon Mar 9, 2023
7f5acc0
Address review comments.
markshannon Mar 16, 2023
b06bb6f
Merge branch 'main' into long-rearrange-size-bits
markshannon Mar 16, 2023
a19b0a7
Merge branch 'main' into long-rearrange-size-bits
markshannon Mar 16, 2023
87f49b2
Fix _PyLong_Sign
markshannon Mar 16, 2023
f764aa8
Replace _PyLong_Sign(x) < 0 with _PyLong_IsNegative(x).
markshannon Mar 16, 2023
9843ac0
fix sign check
markshannon Mar 16, 2023
d6cb917
Address some review comments.
markshannon Mar 22, 2023
469d26f
Change asserts on digit counts to asserts on sign where applicable.
markshannon Mar 22, 2023
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
Prev Previous commit
Next Next commit
Replace 'SingleDigit' with 'Compact' as the term 'single digit' seems…
… to provoke endless discussion on digit size, which is irrelevant to this API.
  • Loading branch information
markshannon committed Mar 9, 2023
commit 638a98f44c9a649d6b51143f8c9775fe11d87d1e
10 changes: 5 additions & 5 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(

/* Return 1 if the argument is positive single digit int */
static inline int
_PyLong_IsNonNegativeSingleDigit(const PyLongObject* op) {
_PyLong_IsNonNegativeCompact(const PyLongObject* op) {
assert(PyLong_Check(op));
return op->long_value.lv_tag <= (1 << NON_SIZE_BITS);
Comment thread
gvanrossum marked this conversation as resolved.

@eduardo-elizondo eduardo-elizondo Apr 7, 2023 •

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.

This doesn't work if we set the second (immortal/static) bit, i.e: the immortal small int 1 since it will have an lv_tag of 1100 and return an incorrect value here.

I'll create a new PR to restructure this a bit to make it work with the new bit flag.

cc @ericsnowcurrently

}

static inline int
_PyLong_IsSingleDigit(const PyLongObject* op) {
_PyLong_IsCompact(const PyLongObject* op) {
assert(PyLong_Check(op));
return op->long_value.lv_tag < (2 << NON_SIZE_BITS);
}

static inline int
_PyLong_BothAreSingleDigit(const PyLongObject* a, const PyLongObject* b) {
_PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
assert(PyLong_Check(a));
assert(PyLong_Check(b));
return (a->long_value.lv_tag | b->long_value.lv_tag) < (2 << NON_SIZE_BITS);
Expand All @@ -147,10 +147,10 @@ _PyLong_BothAreSingleDigit(const PyLongObject* a, const PyLongObject* b) {
* without risk of overflow.
*/
static inline Py_ssize_t
_PyLong_SingleDigitValue(const PyLongObject *op)
_PyLong_CompactValue(const PyLongObject *op)
{
assert(PyLong_Check(op));
assert(_PyLong_IsSingleDigit(op));
assert(_PyLong_IsCompact(op));
Py_ssize_t sign = 1 - (op->long_value.lv_tag & SIGN_MASK);
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ dec_from_long(PyTypeObject *type, PyObject *v,

uint8_t sign = _PyLong_IsNegative(l) ? MPD_NEG : MPD_POS;

if (_PyLong_IsSingleDigit(l)) {
if (_PyLong_IsCompact(l)) {
_dec_settriple(dec, sign, l->long_value.ob_digit[0], 0);
mpd_qfinalize(MPD(dec), ctx, status);
return dec;
Expand Down
10 changes: 5 additions & 5 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2152,14 +2152,14 @@ unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
/* Modified from Objects/longobject.c:long_compare, assuming: */
assert(Py_IS_TYPE(v, &PyLong_Type));
assert(Py_IS_TYPE(w, &PyLong_Type));
assert(_PyLong_IsSingleDigit((PyLongObject *)v));
assert(_PyLong_IsSingleDigit((PyLongObject *)w));
assert(_PyLong_IsCompact((PyLongObject *)v));
assert(_PyLong_IsCompact((PyLongObject *)w));

vl = (PyLongObject*)v;
wl = (PyLongObject*)w;

v0 = _PyLong_SingleDigitValue(vl);
w0 = _PyLong_SingleDigitValue(wl);
v0 = _PyLong_CompactValue(vl);
w0 = _PyLong_CompactValue(wl);

res = v0 < w0;
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
Expand Down Expand Up @@ -2357,7 +2357,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
if (keys_are_all_same_type) {
if (key_type == &PyLong_Type &&
ints_are_bounded &&
!_PyLong_IsSingleDigit((PyLongObject *)key)) {
!_PyLong_IsCompact((PyLongObject *)key)) {

ints_are_bounded = 0;
}
Expand Down
80 changes: 40 additions & 40 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class int "PyObject *" "&PyLong_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/

#define medium_value(x) ((stwodigits)_PyLong_SingleDigitValue(x))
#define medium_value(x) ((stwodigits)_PyLong_CompactValue(x))

#define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
#define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)
Expand Down Expand Up @@ -59,7 +59,7 @@ get_small_int(sdigit ival)
static PyLongObject *
maybe_small_long(PyLongObject *v)
{
if (v && _PyLong_IsSingleDigit(v)) {
if (v && _PyLong_IsCompact(v)) {
stwodigits ival = medium_value(v);
if (IS_SMALL_INT(ival)) {
_Py_DECREF_INT(v);
Expand Down Expand Up @@ -190,7 +190,7 @@ _PyLong_Copy(PyLongObject *src)
{
assert(src != NULL);

if (_PyLong_IsSingleDigit(src)) {
if (_PyLong_IsCompact(src)) {
stwodigits ival = medium_value(src);
if (IS_SMALL_INT(ival)) {
return get_small_int((sdigit)ival);
Expand Down Expand Up @@ -482,15 +482,15 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
return -1;
do_decref = 1;
}
if (_PyLong_IsSingleDigit(v)) {
if (_PyLong_IsCompact(v)) {
#if SIZEOF_LONG < SIZEOF_VOID_P
intptr_t tmp = _PyLong_SingleDigitValue(v);
intptr_t tmp = _PyLong_CompactValue(v);
res = (long)tmp;
if (res != tmp) {
*overflow = tmp < 0 ? -1 : 1;
}
#else
res = _PyLong_SingleDigitValue(v);
res = _PyLong_CompactValue(v);
#endif
}
else {
Expand Down Expand Up @@ -582,8 +582,8 @@ PyLong_AsSsize_t(PyObject *vv) {
}

v = (PyLongObject *)vv;
if (_PyLong_IsSingleDigit(v)) {
return _PyLong_SingleDigitValue(v);
if (_PyLong_IsCompact(v)) {
return _PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
Expand Down Expand Up @@ -631,15 +631,15 @@ PyLong_AsUnsignedLong(PyObject *vv)
}

v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeSingleDigit(v)) {
if (_PyLong_IsNonNegativeCompact(v)) {
#if SIZEOF_LONG < SIZEOF_VOID_P
intptr_t tmp = _PyLong_SingleDigitValue(v);
intptr_t tmp = _PyLong_CompactValue(v);
unsigned long res = (unsigned long)tmp;
if (res != tmp) {
goto overflow;
}
#else
return _PyLong_SingleDigitValue(v);
return _PyLong_CompactValue(v);
#endif
}
if (_PyLong_IsNegative(v)) {
Expand Down Expand Up @@ -684,8 +684,8 @@ PyLong_AsSize_t(PyObject *vv)
}

v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeSingleDigit(v)) {
return _PyLong_SingleDigitValue(v);
if (_PyLong_IsNonNegativeCompact(v)) {
return _PyLong_CompactValue(v);
}
if (_PyLong_IsNegative(v)) {
PyErr_SetString(PyExc_OverflowError,
Expand Down Expand Up @@ -721,8 +721,8 @@ _PyLong_AsUnsignedLongMask(PyObject *vv)
return (unsigned long) -1;
}
v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeSingleDigit(v)) {
return (unsigned long)_PyLong_SingleDigitValue(v);
if (_PyLong_IsNonNegativeCompact(v)) {
return (unsigned long)_PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
int sign = _PyLong_NonZeroSign(v);
Expand Down Expand Up @@ -1218,9 +1218,9 @@ PyLong_AsLongLong(PyObject *vv)
do_decref = 1;
}

if (_PyLong_IsSingleDigit(v)) {
if (_PyLong_IsCompact(v)) {
res = 0;
bytes = _PyLong_SingleDigitValue(v);
bytes = _PyLong_CompactValue(v);
}
else {
res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes,
Expand Down Expand Up @@ -1257,9 +1257,9 @@ PyLong_AsUnsignedLongLong(PyObject *vv)
}

v = (PyLongObject*)vv;
if (_PyLong_IsNonNegativeSingleDigit(v)) {
if (_PyLong_IsNonNegativeCompact(v)) {
res = 0;
bytes = _PyLong_SingleDigitValue(v);
bytes = _PyLong_CompactValue(v);
}
else {
res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
Expand Down Expand Up @@ -1289,8 +1289,8 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv)
return (unsigned long long) -1;
}
v = (PyLongObject *)vv;
if (_PyLong_IsNonNegativeSingleDigit(v)) {
return _PyLong_SingleDigitValue(v);
if (_PyLong_IsNonNegativeCompact(v)) {
return _PyLong_CompactValue(v);
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonZeroSign(v);
Expand Down Expand Up @@ -1361,8 +1361,8 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
return -1;
do_decref = 1;
}
if (_PyLong_IsSingleDigit(v)) {
res = _PyLong_SingleDigitValue(v);
if (_PyLong_IsCompact(v)) {
res = _PyLong_CompactValue(v);
}
else {
i = _PyLong_DigitCount(v);
Expand Down Expand Up @@ -3209,7 +3209,7 @@ PyLong_AsDouble(PyObject *v)
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1.0;
}
if (_PyLong_IsSingleDigit((PyLongObject *)v)) {
if (_PyLong_IsCompact((PyLongObject *)v)) {
/* Fast path; single digit long (31 bits) will cast safely
to double. This improves performance of FP/long operations
by 20%.
Expand All @@ -3234,8 +3234,8 @@ PyLong_AsDouble(PyObject *v)
static Py_ssize_t
long_compare(PyLongObject *a, PyLongObject *b)
{
if (_PyLong_BothAreSingleDigit(a, b)) {
return _PyLong_SingleDigitValue(a) - _PyLong_SingleDigitValue(b);
if (_PyLong_BothAreCompact(a, b)) {
return _PyLong_CompactValue(a) - _PyLong_CompactValue(b);
}
Py_ssize_t sign = _PyLong_SignedDigitCount(a) - _PyLong_SignedDigitCount(b);
Comment thread
markshannon marked this conversation as resolved.
if (sign == 0) {
Expand Down Expand Up @@ -3271,8 +3271,8 @@ long_hash(PyLongObject *v)
Py_ssize_t i;
int sign;

if (_PyLong_IsSingleDigit(v)) {
x = _PyLong_SingleDigitValue(v);
if (_PyLong_IsCompact(v)) {
x = _PyLong_CompactValue(v);
if (x == (Py_uhash_t)-1) {
x = (Py_uhash_t)-2;
}
Expand Down Expand Up @@ -3413,7 +3413,7 @@ x_sub(PyLongObject *a, PyLongObject *b)
PyObject *
_PyLong_Add(PyLongObject *a, PyLongObject *b)
{
if (_PyLong_BothAreSingleDigit(a, b)) {
if (_PyLong_BothAreCompact(a, b)) {
return _PyLong_FromSTwoDigits(medium_value(a) + medium_value(b));
}

Expand Down Expand Up @@ -3454,7 +3454,7 @@ _PyLong_Subtract(PyLongObject *a, PyLongObject *b)
{
PyLongObject *z;

if (_PyLong_BothAreSingleDigit(a, b)) {
if (_PyLong_BothAreCompact(a, b)) {
return _PyLong_FromSTwoDigits(medium_value(a) - medium_value(b));
}
if (_PyLong_IsNegative(a)) {
Expand Down Expand Up @@ -3906,7 +3906,7 @@ _PyLong_Multiply(PyLongObject *a, PyLongObject *b)
PyLongObject *z;

/* fast path for single-digit multiplication */
if (_PyLong_BothAreSingleDigit(a, b)) {
if (_PyLong_BothAreCompact(a, b)) {
stwodigits v = medium_value(a) * medium_value(b);
return _PyLong_FromSTwoDigits(v);
}
Expand Down Expand Up @@ -4614,7 +4614,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)

/* if modulus == 1:
return 0 */
if (_PyLong_IsNonNegativeSingleDigit(c) && (c->long_value.ob_digit[0] == 1)) {
if (_PyLong_IsNonNegativeCompact(c) && (c->long_value.ob_digit[0] == 1)) {
z = (PyLongObject *)PyLong_FromLong(0L);
goto Done;
}
Expand Down Expand Up @@ -4827,22 +4827,22 @@ long_invert(PyLongObject *v)
{
/* Implement ~x as -(x+1) */
PyLongObject *x;
if (_PyLong_IsSingleDigit(v))
if (_PyLong_IsCompact(v))
return _PyLong_FromSTwoDigits(~medium_value(v));
x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_GetOne());
if (x == NULL)
return NULL;
_PyLong_Negate(&x);
/* No need for maybe_small_long here, since any small longs
will have been caught in the _PyLong_IsSingleDigit() fast path. */
will have been caught in the _PyLong_IsCompact() fast path. */
return (PyObject *)x;
}

static PyObject *
long_neg(PyLongObject *v)
{
PyLongObject *z;
if (_PyLong_IsSingleDigit(v))
if (_PyLong_IsCompact(v))
return _PyLong_FromSTwoDigits(-medium_value(v));
z = (PyLongObject *)_PyLong_Copy(v);
if (z != NULL)
Expand Down Expand Up @@ -4915,7 +4915,7 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
assert(remshift < PyLong_SHIFT);

/* Fast path for small a. */
if (_PyLong_IsSingleDigit(a)) {
if (_PyLong_IsCompact(a)) {
stwodigits m, x;
digit shift;
m = medium_value(a);
Expand Down Expand Up @@ -5031,7 +5031,7 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
Py_ssize_t oldsize, newsize, i, j;
twodigits accum;

if (wordshift == 0 && _PyLong_IsSingleDigit(a)) {
if (wordshift == 0 && _PyLong_IsCompact(a)) {
stwodigits m = medium_value(a);
// bypass undefined shift operator behavior
stwodigits x = m < 0 ? -(-m << remshift) : m << remshift;
Expand Down Expand Up @@ -5247,7 +5247,7 @@ long_and(PyObject *a, PyObject *b)
CHECK_BINOP(a, b);
PyLongObject *x = (PyLongObject*)a;
PyLongObject *y = (PyLongObject*)b;
if (_PyLong_IsSingleDigit(x) && _PyLong_IsSingleDigit(y)) {
if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
return _PyLong_FromSTwoDigits(medium_value(x) & medium_value(y));
}
return long_bitwise(x, '&', y);
Expand All @@ -5259,7 +5259,7 @@ long_xor(PyObject *a, PyObject *b)
CHECK_BINOP(a, b);
PyLongObject *x = (PyLongObject*)a;
PyLongObject *y = (PyLongObject*)b;
if (_PyLong_IsSingleDigit(x) && _PyLong_IsSingleDigit(y)) {
if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
return _PyLong_FromSTwoDigits(medium_value(x) ^ medium_value(y));
}
return long_bitwise(x, '^', y);
Expand All @@ -5271,7 +5271,7 @@ long_or(PyObject *a, PyObject *b)
CHECK_BINOP(a, b);
PyLongObject *x = (PyLongObject*)a;
PyLongObject *y = (PyLongObject*)b;
if (_PyLong_IsSingleDigit(x) && _PyLong_IsSingleDigit(y)) {
if (_PyLong_IsCompact(x) && _PyLong_IsCompact(y)) {
return _PyLong_FromSTwoDigits(medium_value(x) | medium_value(y));
}
return long_bitwise(x, '|', y);
Expand Down
6 changes: 3 additions & 3 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "pycore_ast.h" // _PyAST_Validate()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_compile.h" // _PyAST_Compile()
#include "pycore_long.h" // _PyLong_SingleDigitValue
#include "pycore_long.h" // _PyLong_CompactValue
#include "pycore_object.h" // _Py_AddToAllObjects()
#include "pycore_pyerrors.h" // _PyErr_NoMemory()
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down Expand Up @@ -2507,8 +2507,8 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Py_ssize_t b;
overflow = 0;
/* Single digits are common, fast, and cannot overflow on unpacking. */
if (_PyLong_IsSingleDigit((PyLongObject *)item)) {
b = _PyLong_SingleDigitValue((PyLongObject *)item);
if (_PyLong_IsCompact((PyLongObject *)item)) {
b = _PyLong_CompactValue((PyLongObject *)item);
}
else {
b = PyLong_AsLongAndOverflow(item, &overflow);
Expand Down
Loading