Thanks to visit codestin.com
Credit goes to github.com

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 <[email protected]> Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Yan Yanchii <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 75f59bb commit ab353b3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
Lines changed: 2 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
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)