Skip to content

Commit 218af9e

Browse files
committed
Add low level unstable API for fast access to 'small' ints.
1 parent 45a9e38 commit 218af9e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Include/cpython/longobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
9393

9494
PyAPI_FUNC(PyObject *) _PyLong_Rshift(PyObject *, size_t);
9595
PyAPI_FUNC(PyObject *) _PyLong_Lshift(PyObject *, size_t);
96+
97+
98+
PyAPI_FUNC(int) PyUnstable_Int_IsCompact(const PyLongObject* op);
99+
PyAPI_FUNC(Py_ssize_t) PyUnstable_Int_CompactValue(const PyLongObject* op);
100+

Include/internal/pycore_long.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ _PyLong_IsCompact(const PyLongObject* op) {
137137
return op->long_value.lv_tag < (2 << NON_SIZE_BITS);
138138
}
139139

140+
#define PyUnstable_Int_IsCompact _PyLong_IsCompact
141+
140142
static inline int
141143
_PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
142144
assert(PyLong_Check(a));
@@ -159,6 +161,8 @@ _PyLong_CompactValue(const PyLongObject *op)
159161
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
160162
}
161163

164+
#define PyUnstable_Int_CompactValue _PyLong_CompactValue
165+
162166
static inline bool
163167
_PyLong_IsZero(const PyLongObject *op)
164168
{

Objects/longobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6366,3 +6366,17 @@ _PyLong_FiniTypes(PyInterpreterState *interp)
63666366
{
63676367
_PyStructSequence_FiniBuiltin(interp, &Int_InfoType);
63686368
}
6369+
6370+
#undef PyUnstable_Int_IsCompact
6371+
6372+
int
6373+
PyUnstable_Int_IsCompact(const PyLongObject* op) {
6374+
return _PyLong_IsCompact(op);
6375+
}
6376+
6377+
#undef PyUnstable_Int_CompactValue
6378+
6379+
Py_ssize_t
6380+
PyUnstable_Int_CompactValue(const PyLongObject* op) {
6381+
return _PyLong_CompactValue(op);
6382+
}

0 commit comments

Comments
 (0)