Skip to content

Commit ab353b3

Browse files
srinivasreddyeendebakptskirpichevWolframAlphpicnixz
authored
gh-129149: Add fast path in PYLONG_FROM_UINT macro for compact integers (#129168)
Add fast path in PyLong_From*() functions for compact integers. Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Yan Yanchii <yyanchiy@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 75f59bb commit ab353b3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add fast path for medium-size integers in :c:func:`PyLong_FromUnsignedLong`,
2+
:c:func:`PyLong_FromUnsignedLongLong` and :c:func:`PyLong_FromSize_t`.

Objects/longobject.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,13 @@ PyLong_FromLong(long ival)
365365
if (IS_SMALL_UINT(ival)) { \
366366
return get_small_int((sdigit)(ival)); \
367367
} \
368+
if ((ival) <= PyLong_MASK) { \
369+
return _PyLong_FromMedium((sdigit)(ival)); \
370+
} \
371+
/* Do shift in two steps to avoid possible undefined behavior. */ \
372+
INT_TYPE t = (ival) >> PyLong_SHIFT >> PyLong_SHIFT; \
368373
/* Count the number of Python digits. */ \
369-
Py_ssize_t ndigits = 0; \
370-
INT_TYPE t = (ival); \
374+
Py_ssize_t ndigits = 2; \
371375
while (t) { \
372376
++ndigits; \
373377
t >>= PyLong_SHIFT; \

0 commit comments

Comments
 (0)