8000 Use unstable PyLong functions in Py3.12b1, if defined. · cython/cython@b462eec · GitHub
[go: up one dir, main page]

Skip to content

Commit b462eec

Browse files
committed
Use unstable PyLong functions in Py3.12b1, if defined.
See python/cpython#101685
1 parent 50a0353 commit b462eec

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Cython/Utility/TypeConversion.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,31 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
132132

133133
#if CYTHON_USE_PYLONG_INTERNALS
134134
#if PY_VERSION_HEX >= 0x030C00A7
135-
#define __Pyx_PyLong_Sign(x) (((PyLongObject*)x)->long_value.lv_tag & 3)
135+
#ifndef _PyLong_SIGN_MASK
136+
#define _PyLong_SIGN_MASK 3
137+
#endif
138+
#ifndef _PyLong_NON_SIZE_BITS
139+
#define _PyLong_NON_SIZE_BITS 3
140+
#endif
141+
#define __Pyx_PyLong_Sign(x) (((PyLongObject*)x)->long_value.lv_tag & _PyLong_SIGN_MASK)
136142
#define __Pyx_PyLong_IsNeg(x) ((__Pyx_PyLong_Sign(x) & 2) != 0)
137143
#define __Pyx_PyLong_IsNonNeg(x) (!__Pyx_PyLong_IsNeg(x))
138144
#define __Pyx_PyLong_IsZero(x) (__Pyx_PyLong_Sign(x) & 1)
139145
#define __Pyx_PyLong_IsPos(x) (__Pyx_PyLong_Sign(x) == 0)
140-
#define __Pyx_PyLong_IsCompact(x) (((PyLongObject*)x)->long_value.lv_tag < (2 << 3)) // (2 << NON_SIZE_BITS)
141-
#define __Pyx_PyLong_CompactValue(x) ((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_Digits(x)[0])
142146
#define __Pyx_PyLong_CompactValueUnsigned(x) (__Pyx_PyLong_Digits(x)[0])
143-
#define __Pyx_PyLong_DigitCount(x) (((PyLongObject*)x)->long_value.lv_tag >> 3) // (>> NON_SIZE_BITS)
147+
#define __Pyx_PyLong_DigitCount(x) (((PyLongObject*)x)->long_value.lv_tag >> _PyLong_NON_SIZE_BITS)
144148
#define __Pyx_PyLong_SignedDigitCount(x) \
145-
((1 - (Py_ssize_t) (((PyLongObject*)x)->long_value.lv_tag & 3)) * (Py_ssize_t) (((PyLongObject*)x)->long_value.lv_tag >> 3)) // (>> NON_SIZE_BITS)
149+
((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_DigitCount(x))
150+
151+
#if defined(PyUnstable_Long_IsCompact) && defined(PyUnstable_Long_CompactValue)
152+
#define __Pyx_PyLong_IsCompact(x) PyUnstable_Long_IsCompact(x)
153+
#define __Pyx_PyLong_CompactValue(x) PyUnstable_Long_CompactValue(x)
154+
#else
155+
#define __Pyx_PyLong_IsCompact(x) (((PyLongObject*)x)->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS))
156+
#define __Pyx_PyLong_CompactValue(x) ((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_Digits(x)[0])
157+
#endif
146158

147-
// CPython 3.12 requires C99
159+
// CPython 3.12 requires C99, which defines 'size_t' (but not 'ssize_t')
148160
typedef Py_ssize_t __Pyx_compact_pylong;
149161
typedef size_t __Pyx_compact_upylong;
150162

@@ -153,13 +165,14 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
153165
#define __Pyx_PyLong_IsNonNeg(x) (Py_SIZE(x) >= 0)
154166
#define __Pyx_PyLong_IsZero(x) (Py_SIZE(x) == 0)
155167
#define __Pyx_PyLong_IsPos(x) (Py_SIZE(x) > 0)
156-
#define __Pyx_PyLong_IsCompact(x) (Py_SIZE(x) == 0 || Py_SIZE(x) == 1 || Py_SIZE(x) == -1)
157-
#define __Pyx_PyLong_CompactValue(x) \
158-
((Py_SIZE(x) == 0) ? (sdigit) 0 : ((Py_SIZE(x) < 0) ? -(sdigit)__Pyx_PyLong_Digits(x)[0] : (sdigit)__Pyx_PyLong_Digits(x)[0]))
159168
#define __Pyx_PyLong_CompactValueUnsigned(x) ((Py_SIZE(x) == 0) ? 0 : __Pyx_PyLong_Digits(x)[0])
160169
#define __Pyx_PyLong_DigitCount(x) __Pyx_sst_abs(Py_SIZE(x))
161170
#define __Pyx_PyLong_SignedDigitCount(x) Py_SIZE(x)
162171

172+
#define __Pyx_PyLong_IsCompact(x) (Py_SIZE(x) == 0 || Py_SIZE(x) == 1 || Py_SIZE(x) == -1)
173+
#define __Pyx_PyLong_CompactValue(x) \
174+
((Py_SIZE(x) == 0) ? (sdigit) 0 : ((Py_SIZE(x) < 0) ? -(sdigit)__Pyx_PyLong_Digits(x)[0] : (sdigit)__Pyx_PyLong_Digits(x)[0]))
175+
163176
typedef sdigit __Pyx_compact_pylong;
164177
typedef digit __Pyx_compact_upylong;
165178
#endif

0 commit comments

Comments
 (0)
0