5
5
#include "Python.h"
6
6
#include "pycore_bitutils.h" // _Py_popcount32()
7
7
#include "pycore_interp.h" // _PY_NSMALLPOSINTS
8
+ #include "pycore_long.h" // __PyLong_GetSmallInt_internal()
8
9
#include "pycore_object.h" // _PyObject_InitVar()
9
10
#include "pycore_pystate.h" // _Py_IsMainInterpreter()
10
11
#include "longintrepr.h"
@@ -19,8 +20,8 @@ class int "PyObject *" "&PyLong_Type"
19
20
[clinic start generated code]*/
20
21
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/
21
22
22
- #define NSMALLPOSINTS _PY_NSMALLPOSINTS
23
23
#define NSMALLNEGINTS _PY_NSMALLNEGINTS
24
+ #define NSMALLPOSINTS _PY_NSMALLPOSINTS
24
25
25
26
_Py_IDENTIFIER (little );
26
27
_Py_IDENTIFIER (big );
@@ -34,16 +35,14 @@ _Py_IDENTIFIER(big);
34
35
PyObject * _PyLong_Zero = NULL ;
35
36
PyObject * _PyLong_One = NULL ;
36
37
37
- #if NSMALLNEGINTS + NSMALLPOSINTS > 0
38
38
#define IS_SMALL_INT (ival ) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)
39
39
#define IS_SMALL_UINT (ival ) ((ival) < NSMALLPOSINTS)
40
40
41
41
static PyObject *
42
42
get_small_int (sdigit ival )
43
43
{
44
44
assert (IS_SMALL_INT (ival ));
45
- PyInterpreterState * interp = _PyInterpreterState_GET ();
46
- PyObject * v = (PyObject * )interp -> small_ints [ival + NSMALLNEGINTS ];
45
+ PyObject * v = __PyLong_GetSmallInt_internal (ival );
47
46
Py_INCREF (v );
48
47
return v ;
49
48
}
@@ -60,12 +59,6 @@ maybe_small_long(PyLongObject *v)
60
59
}
61
60
return v ;
62
61
}
63
- #else
64
- #define IS_SMALL_INT (ival ) 0
65
- #define IS_SMALL_UINT (ival ) 0
66
- #define get_small_int (ival ) (Py_UNREACHABLE(), NULL)
67
- #define maybe_small_long (val ) (val)
68
- #endif
69
62
70
63
/* If a freshly-allocated int is already shared, it must
71
64
be a small integer, so negating it must go to PyLong_FromLong */
@@ -2559,8 +2552,9 @@ long_divrem(PyLongObject *a, PyLongObject *b,
2559
2552
if (* prem == NULL ) {
2560
2553
return -1 ;
2561
2554
}
2562
- Py_INCREF (_PyLong_Zero );
2563
- * pdiv = (PyLongObject * )_PyLong_Zero ;
2555
+ PyObject * zero = _PyLong_GetZero ();
2556
+ Py_INCREF (zero );
2557
+ * pdiv = (PyLongObject * )zero ;
2564
2558
return 0 ;
2565
2559
}
2566
2560
if (size_b == 1 ) {
@@ -3669,7 +3663,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
3669
3663
Py_DECREF (div );
3670
3664
return -1 ;
3671
3665
}
3672
- temp = (PyLongObject * ) long_sub (div , (PyLongObject * )_PyLong_One );
3666
+ temp = (PyLongObject * ) long_sub (div , (PyLongObject * )_PyLong_GetOne () );
3673
3667
if (temp == NULL ) {
3674
3668
Py_DECREF (mod );
3675
3669
Py_DECREF (div );
@@ -4078,7 +4072,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
4078
4072
4079
4073
Py_DECREF (c );
4080
4074
Py_DECREF (n );
4081
- if (long_compare (a , (PyLongObject * )_PyLong_One )) {
4075
+ if (long_compare (a , (PyLongObject * )_PyLong_GetOne () )) {
4082
4076
/* a != 1; we don't have an inverse. */
4083
4077
Py_DECREF (a );
4084
4078
Py_DECREF (b );
@@ -4313,7 +4307,7 @@ long_invert(PyLongObject *v)
4313
4307
PyLongObject * x ;
4314
4308
if (Py_ABS (Py_SIZE (v )) <=1 )
4315
4309
return PyLong_FromLong (- (MEDIUM_VALUE (v )+ 1 ));
4316
- x = (PyLongObject * ) long_add (v , (PyLongObject * )_PyLong_One );
4310
+ x = (PyLongObject * ) long_add (v , (PyLongObject * )_PyLong_GetOne () );
4317
4311
if (x == NULL )
4318
4312
return NULL ;
4319
4313
_PyLong_Negate (& x );
@@ -5105,7 +5099,8 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
5105
5099
5106
5100
/* compare twice the remainder with the divisor, to see
5107
5101
if we need to adjust the quotient and remainder */
5108
- twice_rem = long_lshift ((PyObject * )rem , _PyLong_One );
5102
+ PyObject * one = _PyLong_GetOne (); // borrowed reference
5103
+ twice_rem = long_lshift ((PyObject * )rem , one );
5109
5104
if (twice_rem == NULL )
5110
5105
goto error ;
5111
5106
if (quo_is_neg ) {
@@ -5122,9 +5117,9 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
5122
5117
if ((Py_SIZE (b ) < 0 ? cmp < 0 : cmp > 0 ) || (cmp == 0 && quo_is_odd )) {
5123
5118
/* fix up quotient */
5124
5119
if (quo_is_neg )
5125
- temp = long_sub (quo , (PyLongObject * )_PyLong_One );
5120
+ temp = long_sub (quo , (PyLongObject * )one );
5126
5121
else
5127
- temp = long_add (quo , (PyLongObject * )_PyLong_One );
5122
+ temp = long_add (quo , (PyLongObject * )one );
5128
5123
Py_DECREF (quo );
5129
5124
quo = (PyLongObject * )temp ;
5130
5125
if (quo == NULL )
@@ -5406,7 +5401,7 @@ int_as_integer_ratio_impl(PyObject *self)
5406
5401
if (numerator == NULL ) {
5407
5402
return NULL ;
5408
5403
}
5409
- ratio_tuple = PyTuple_Pack (2 , numerator , _PyLong_One );
5404
+ ratio_tuple = PyTuple_Pack (2 , numerator , _PyLong_GetOne () );
5410
5405
Py_DECREF (numerator );
5411
5406
return ratio_tuple ;
5412
5407
}
@@ -5712,7 +5707,6 @@ PyLong_GetInfo(void)
5712
5707
int
5713
5708
_PyLong_Init (PyThreadState * tstate )
5714
5709
{
5715
- #if NSMALLNEGINTS + NSMALLPOSINTS > 0
5716
5710
for (Py_ssize_t i = 0 ; i < NSMALLNEGINTS + NSMALLPOSINTS ; i ++ ) {
5717
5711
sdigit ival = (sdigit )i - NSMALLNEGINTS ;
5718
5712
int size = (ival < 0 ) ? -1 : ((ival == 0 ) ? 0 : 1 );
@@ -5727,7 +5721,6 @@ _PyLong_Init(PyThreadState *tstate)
5727
5721
5728
5722
tstate -> interp -> small_ints [i ] = v ;
5729
5723
}
5730
- #endif
5731
5724
5732
5725
if (_Py_IsMainInterpreter (tstate )) {
5733
5726
_PyLong_Zero = PyLong_FromLong (0 );
@@ -5759,9 +5752,7 @@ _PyLong_Fini(PyThreadState *tstate)
5759
5752
Py_CLEAR (_PyLong_Zero );
5760
5753
}
5761
5754
5762
- #if NSMALLNEGINTS + NSMALLPOSINTS > 0
5763
5755
for (Py_ssize_t i = 0 ; i < NSMALLNEGINTS + NSMALLPOSINTS ; i ++ ) {
5764
5756
Py_CLEAR (tstate -> interp -> small_ints [i ]);
5765
5757
}
5766
- #endif
5767
5758
}
0 commit comments