Skip to content

Commit

Permalink
gh-129149: Add fast path in PYLONG_FROM_UINT macro for compact intege…
Browse files Browse the repository at this point in the history
…rs (#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>
  • Loading branch information
6 people authored Jan 23, 2025
1 parent 75f59bb commit ab353b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add fast path for medium-size integers in :c:func:`PyLong_FromUnsignedLong`,
:c:func:`PyLong_FromUnsignedLongLong` and :c:func:`PyLong_FromSize_t`.
8 changes: 6 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,13 @@ PyLong_FromLong(long ival)
if (IS_SMALL_UINT(ival)) { \
return get_small_int((sdigit)(ival)); \
} \
if ((ival) <= PyLong_MASK) { \
return _PyLong_FromMedium((sdigit)(ival)); \
} \
/* Do shift in two steps to avoid possible undefined behavior. */ \
INT_TYPE t = (ival) >> PyLong_SHIFT >> PyLong_SHIFT; \
/* Count the number of Python digits. */ \
Py_ssize_t ndigits = 0; \
INT_TYPE t = (ival); \
Py_ssize_t ndigits = 2; \
while (t) { \
++ndigits; \
t >>= PyLong_SHIFT; \
Expand Down

0 comments on commit ab353b3

Please sign in to comment.