[Python-checkins] On path with known exact float, extract the double with the fast macro. (GH-21072)

Raymond Hettinger webhook-mailer at python.org
Tue Jun 23 14:45:34 EDT 2020


https://github.com/python/cpython/commit/930f4518aea7f3f0f914ce93c3fb92831a7e1d2a
commit: 930f4518aea7f3f0f914ce93c3fb92831a7e1d2a
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-06-23T11:45:25-07:00
summary:

On path with known exact float, extract the double with the fast macro. (GH-21072)

files:
M Modules/mathmodule.c

diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 4450ce1894102..411c6eb1935fa 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -1256,9 +1256,15 @@ static PyObject *
 math_floor(PyObject *module, PyObject *number)
 /*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
 {
+    double x;
+
     _Py_IDENTIFIER(__floor__);
 
-    if (!PyFloat_CheckExact(number)) {
+    if (PyFloat_CheckExact(number)) {
+        x = PyFloat_AS_DOUBLE(number);
+    }
+    else
+    {
         PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__);
         if (method != NULL) {
             PyObject *result = _PyObject_CallNoArg(method);
@@ -1267,11 +1273,10 @@ math_floor(PyObject *module, PyObject *number)
         }
         if (PyErr_Occurred())
             return NULL;
+        x = PyFloat_AsDouble(number);
+        if (x == -1.0 && PyErr_Occurred())
+            return NULL;
     }
-    double x = PyFloat_AsDouble(number);
-    if (x == -1.0 && PyErr_Occurred())
-        return NULL;
-
     return PyLong_FromDouble(floor(x));
 }
 



More information about the Python-checkins mailing list