https://github.com/python/cpython/commit/ab353b31bb300d55e845476a8e265366455d93fc
commit: ab353b31bb300d55e845476a8e265366455d93fc
branch: main
author: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) 
<[email protected]>
committer: vstinner <[email protected]>
date: 2025-01-23T14:36:52Z
summary:

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]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-01-22-14-24-44.gh-issue-129149.wAYu43.rst
M Objects/longobject.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-01-22-14-24-44.gh-issue-129149.wAYu43.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-22-14-24-44.gh-issue-129149.wAYu43.rst
new file mode 100644
index 00000000000000..d946f6a2fea185
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-22-14-24-44.gh-issue-129149.wAYu43.rst
@@ -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`.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 89526d5a430304..b4e3a70adf2b5b 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -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; \

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to