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-101291: Refactor the PyLongObject struct #101292

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 7 additions & 2 deletions Include/cpython/longintrepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ typedef long stwodigits; /* signed variant of twodigits */
aware that ints abuse ob_size's sign bit.
*/

struct _longobject {
PyObject_VAR_HEAD
typedef struct _PyLongValue {
Py_ssize_t ob_size; /* Number of items in variable part */
digit ob_digit[1];
} _PyLongValue;

struct _longobject {
PyObject_HEAD
_PyLongValue long_value;
};

PyAPI_FUNC(PyLongObject *) _PyLong_New(Py_ssize_t);
Expand Down
8 changes: 5 additions & 3 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ extern "C" {

#define _PyLong_DIGIT_INIT(val) \
{ \
_PyVarObject_IMMORTAL_INIT(&PyLong_Type, \
((val) == 0 ? 0 : ((val) > 0 ? 1 : -1))), \
.ob_digit = { ((val) >= 0 ? (val) : -(val)) }, \
.ob_base = _PyObject_IMMORTAL_INIT(&PyLong_Type), \
.long_value = { \
((val) == 0 ? 0 : ((val) > 0 ? 1 : -1)), \
{ ((val) >= 0 ? (val) : -(val)) }, \
} \
}

#define _PyBytes_SIMPLE_INIT(CH, LEN) \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Refactor the ``PyLongObject`` struct into a normal Python object header and
a ``PyLongValue`` struct.
10 changes: 5 additions & 5 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2171,16 +2171,16 @@ dec_from_long(PyTypeObject *type, PyObject *v,
}

if (len == 1) {
_dec_settriple(dec, sign, *l->ob_digit, 0);
_dec_settriple(dec, sign, *l->long_value.ob_digit, 0);
mpd_qfinalize(MPD(dec), ctx, status);
return dec;
}

#if PYLONG_BITS_IN_DIGIT == 30
mpd_qimport_u32(MPD(dec), l->ob_digit, len, sign, PyLong_BASE,
mpd_qimport_u32(MPD(dec), l->long_value.ob_digit, len, sign, PyLong_BASE,
ctx, status);
#elif PYLONG_BITS_IN_DIGIT == 15
mpd_qimport_u16(MPD(dec), l->ob_digit, len, sign, PyLong_BASE,
mpd_qimport_u16(MPD(dec), l->long_value.ob_digit, len, sign, PyLong_BASE,
ctx, status);
#else
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
Expand Down Expand Up @@ -3543,11 +3543,11 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
return NULL;
}

memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit));
memcpy(pylong->long_value.ob_digit, ob_digit, n * sizeof(digit));
mpd_free(ob_digit);

i = n;
while ((i > 0) && (pylong->ob_digit[i-1] == 0)) {
while ((i > 0) && (pylong->long_value.ob_digit[i-1] == 0)) {
i--;
}

Expand Down
8 changes: 4 additions & 4 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ PyTypeObject PyBool_Type = {
/* The objects representing bool values False and True */

struct _longobject _Py_FalseStruct = {
PyVarObject_HEAD_INIT(&PyBool_Type, 0)
{ 0 }
PyObject_HEAD_INIT(&PyBool_Type)
{ 0, { 0 } }
};

struct _longobject _Py_TrueStruct = {
PyVarObject_HEAD_INIT(&PyBool_Type, 1)
{ 1 }
PyObject_HEAD_INIT(&PyBool_Type)
{ 1, { 1 } }
};
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,8 +2155,8 @@ unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
vl = (PyLongObject*)v;
wl = (PyLongObject*)w;

v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->long_value.ob_digit[0];
w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->long_value.ob_digit[0];

if (Py_SIZE(vl) < 0)
v0 = -v0;
Expand Down
Loading