Skip to content

bpo-38015: replace inline function is_small_int with a macro version #15710

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

Merged
merged 1 commit into from Sep 6, 2019
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
The ``CHECK_SMALL_INT`` macro used inside :file:`Object/longobject.c` has
been replaced with an explicit ``return`` at each call site, conditioned on
a ``static inline`` function ``is_small_int``.
been replaced with an explicit ``return`` at each call site.
22 changes: 9 additions & 13 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ PyObject *_PyLong_One = NULL;
*/
static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];

static inline int
is_small_int(long long ival)
{
return -NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS;
}
#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)

#ifdef COUNT_ALLOCS
Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs;
Expand All @@ -56,7 +52,7 @@ static PyObject *
get_small_int(sdigit ival)
{
PyObject *v;
assert(is_small_int(ival));
assert(IS_SMALL_INT(ival));
v = (PyObject *)&small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);
#ifdef COUNT_ALLOCS
Expand All @@ -73,16 +69,16 @@ maybe_small_long(PyLongObject *v)
{
if (v && Py_ABS(Py_SIZE(v)) <= 1) {
sdigit ival = MEDIUM_VALUE(v);
if (is_small_int(ival)) {
if (IS_SMALL_INT(ival)) {
Py_DECREF(v);
return (PyLongObject *)get_small_int(ival);
}
}
return v;
}
#else
#define is_small_int(ival) 0
#define get_small_int(ival) (assert(0), NULL)
#define IS_SMALL_INT(ival) 0
#define get_small_int(ival) (Py_UNREACHABLE(), NULL)
#define maybe_small_long(val) (val)
#endif

Expand Down Expand Up @@ -297,7 +293,7 @@ _PyLong_Copy(PyLongObject *src)
i = -(i);
if (i < 2) {
sdigit ival = MEDIUM_VALUE(src);
if (is_small_int(ival)) {
if (IS_SMALL_INT(ival)) {
return get_small_int(ival);
}
}
Expand All @@ -321,7 +317,7 @@ PyLong_FromLong(long ival)
int ndigits = 0;
int sign;

if (is_small_int(ival)) {
if (IS_SMALL_INT(ival)) {
return get_small_int((sdigit)ival);
}

Expand Down Expand Up @@ -1154,7 +1150,7 @@ PyLong_FromLongLong(long long ival)
int ndigits = 0;
int negative = 0;

if (is_small_int(ival)) {
if (IS_SMALL_INT(ival)) {
return get_small_int((sdigit)ival);
}

Expand Down Expand Up @@ -1229,7 +1225,7 @@ PyLong_FromSsize_t(Py_ssize_t ival)
int ndigits = 0;
int negative = 0;

if (is_small_int(ival)) {
if (IS_SMALL_INT(ival)) {
return get_small_int((sdigit)ival);
}

Expand Down