bpo-42161: Add _PyLong_GetZero() and _PyLong_GetOne() (GH-22993)

Add _PyLong_GetZero() and _PyLong_GetOne() functions and a new
internal pycore_long.h header file.

Python cannot be built without small integer singletons anymore.
This commit is contained in:
Victor Stinner 2020-10-27 00:00:03 +01:00 committed by GitHub
parent bca7014032
commit 8e3b9f9283
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 25 deletions

View file

@ -5,6 +5,7 @@
#include "Python.h"
#include "pycore_bitutils.h" // _Py_popcount32()
#include "pycore_interp.h" // _PY_NSMALLPOSINTS
#include "pycore_long.h" // __PyLong_GetSmallInt_internal()
#include "pycore_object.h" // _PyObject_InitVar()
#include "pycore_pystate.h" // _Py_IsMainInterpreter()
#include "longintrepr.h"
@ -19,8 +20,8 @@ class int "PyObject *" "&PyLong_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/
#define NSMALLPOSINTS _PY_NSMALLPOSINTS
#define NSMALLNEGINTS _PY_NSMALLNEGINTS
#define NSMALLPOSINTS _PY_NSMALLPOSINTS
_Py_IDENTIFIER(little);
_Py_IDENTIFIER(big);
@ -34,7 +35,6 @@ _Py_IDENTIFIER(big);
PyObject *_PyLong_Zero = NULL;
PyObject *_PyLong_One = NULL;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)
#define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS)
@ -42,8 +42,7 @@ static PyObject *
get_small_int(sdigit ival)
{
assert(IS_SMALL_INT(ival));
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *v = (PyObject*)interp->small_ints[ival + NSMALLNEGINTS];
PyObject *v = __PyLong_GetSmallInt_internal(ival);
Py_INCREF(v);
return v;
}
@ -60,12 +59,6 @@ maybe_small_long(PyLongObject *v)
}
return v;
}
#else
#define IS_SMALL_INT(ival) 0
#define IS_SMALL_UINT(ival) 0
#define get_small_int(ival) (Py_UNREACHABLE(), NULL)
#define maybe_small_long(val) (val)
#endif
/* If a freshly-allocated int is already shared, it must
be a small integer, so negating it must go to PyLong_FromLong */
@ -2559,8 +2552,9 @@ long_divrem(PyLongObject *a, PyLongObject *b,
if (*prem == NULL) {
return -1;
}
Py_INCREF(_PyLong_Zero);
*pdiv = (PyLongObject*)_PyLong_Zero;
PyObject *zero = _PyLong_GetZero();
Py_INCREF(zero);
*pdiv = (PyLongObject*)zero;
return 0;
}
if (size_b == 1) {
@ -3669,7 +3663,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
Py_DECREF(div);
return -1;
}
temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One);
temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_GetOne());
if (temp == NULL) {
Py_DECREF(mod);
Py_DECREF(div);
@ -4078,7 +4072,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
Py_DECREF(c);
Py_DECREF(n);
if (long_compare(a, (PyLongObject *)_PyLong_One)) {
if (long_compare(a, (PyLongObject *)_PyLong_GetOne())) {
/* a != 1; we don't have an inverse. */
Py_DECREF(a);
Py_DECREF(b);
@ -4313,7 +4307,7 @@ long_invert(PyLongObject *v)
PyLongObject *x;
if (Py_ABS(Py_SIZE(v)) <=1)
return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One);
x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_GetOne());
if (x == NULL)
return NULL;
_PyLong_Negate(&x);
@ -5105,7 +5099,8 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
/* compare twice the remainder with the divisor, to see
if we need to adjust the quotient and remainder */
twice_rem = long_lshift((PyObject *)rem, _PyLong_One);
PyObject *one = _PyLong_GetOne(); // borrowed reference
twice_rem = long_lshift((PyObject *)rem, one);
if (twice_rem == NULL)
goto error;
if (quo_is_neg) {
@ -5122,9 +5117,9 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
/* fix up quotient */
if (quo_is_neg)
temp = long_sub(quo, (PyLongObject *)_PyLong_One);
temp = long_sub(quo, (PyLongObject *)one);
else
temp = long_add(quo, (PyLongObject *)_PyLong_One);
temp = long_add(quo, (PyLongObject *)one);
Py_DECREF(quo);
quo = (PyLongObject *)temp;
if (quo == NULL)
@ -5406,7 +5401,7 @@ int_as_integer_ratio_impl(PyObject *self)
if (numerator == NULL) {
return NULL;
}
ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One);
ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_GetOne());
Py_DECREF(numerator);
return ratio_tuple;
}
@ -5712,7 +5707,6 @@ PyLong_GetInfo(void)
int
_PyLong_Init(PyThreadState *tstate)
{
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
sdigit ival = (sdigit)i - NSMALLNEGINTS;
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
@ -5727,7 +5721,6 @@ _PyLong_Init(PyThreadState *tstate)
tstate->interp->small_ints[i] = v;
}
#endif
if (_Py_IsMainInterpreter(tstate)) {
_PyLong_Zero = PyLong_FromLong(0);
@ -5759,9 +5752,7 @@ _PyLong_Fini(PyThreadState *tstate)
Py_CLEAR(_PyLong_Zero);
}
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
Py_CLEAR(tstate->interp->small_ints[i]);
}
#endif
}