[Python-checkins] cpython: Refactor pytime.c

victor.stinner python-checkins at python.org
Wed Sep 2 01:40:00 CEST 2015


https://hg.python.org/cpython/rev/b367c1446af0
changeset:   97566:b367c1446af0
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Sep 02 00:49:16 2015 +0200
summary:
  Refactor pytime.c

Move code to convert double timestamp to subfunctions.

files:
  Python/pytime.c |  117 ++++++++++++++++++++---------------
  1 files changed, 67 insertions(+), 50 deletions(-)


diff --git a/Python/pytime.c b/Python/pytime.c
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -61,43 +61,51 @@
 }
 
 static int
+_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
+                            double denominator, _PyTime_round_t round)
+{
+    double intpart, err;
+    /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
+    volatile double floatpart;
+
+    floatpart = modf(d, &intpart);
+    if (floatpart < 0) {
+        floatpart = 1.0 + floatpart;
+        intpart -= 1.0;
+    }
+
+    floatpart *= denominator;
+    if (round == _PyTime_ROUND_CEILING) {
+        floatpart = ceil(floatpart);
+        if (floatpart >= denominator) {
+            floatpart = 0.0;
+            intpart += 1.0;
+        }
+    }
+    else {
+        floatpart = floor(floatpart);
+    }
+
+    *sec = (time_t)intpart;
+    err = intpart - (double)*sec;
+    if (err <= -1.0 || err >= 1.0) {
+        error_time_t_overflow();
+        return -1;
+    }
+
+    *numerator = (long)floatpart;
+    return 0;
+}
+
+static int
 _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
                             double denominator, _PyTime_round_t round)
 {
     assert(denominator <= LONG_MAX);
     if (PyFloat_Check(obj)) {
-        double d, intpart, err;
-        /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
-        volatile double floatpart;
-
-        d = PyFloat_AsDouble(obj);
-        floatpart = modf(d, &intpart);
-        if (floatpart < 0) {
-            floatpart = 1.0 + floatpart;
-            intpart -= 1.0;
-        }
-
-        floatpart *= denominator;
-        if (round == _PyTime_ROUND_CEILING) {
-            floatpart = ceil(floatpart);
-            if (floatpart >= denominator) {
-                floatpart = 0.0;
-                intpart += 1.0;
-            }
-        }
-        else {
-            floatpart = floor(floatpart);
-        }
-
-        *sec = (time_t)intpart;
-        err = intpart - (double)*sec;
-        if (err <= -1.0 || err >= 1.0) {
-            error_time_t_overflow();
-            return -1;
-        }
-
-        *numerator = (long)floatpart;
-        return 0;
+        double d = PyFloat_AsDouble(obj);
+        return _PyTime_DoubleToDenominator(d, sec, numerator,
+                                           denominator, round);
     }
     else {
         *sec = _PyLong_AsTime_t(obj);
@@ -221,29 +229,38 @@
 #endif
 
 static int
+_PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
+                        long to_nanoseconds)
+{
+    /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
+    volatile double d, err;
+
+    /* convert to a number of nanoseconds */
+    d = value;
+    d *= to_nanoseconds;
+
+    if (round == _PyTime_ROUND_CEILING)
+        d = ceil(d);
+    else
+        d = floor(d);
+
+    *t = (_PyTime_t)d;
+    err = d - (double)*t;
+    if (fabs(err) >= 1.0) {
+        _PyTime_overflow();
+        return -1;
+    }
+    return 0;
+}
+
+static int
 _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
                    long to_nanoseconds)
 {
     if (PyFloat_Check(obj)) {
-        /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
-        volatile double d, err;
-
-        /* convert to a number of nanoseconds */
+        double d;
         d = PyFloat_AsDouble(obj);
-        d *= to_nanoseconds;
-
-        if (round == _PyTime_ROUND_CEILING)
-            d = ceil(d);
-        else
-            d = floor(d);
-
-        *t = (_PyTime_t)d;
-        err = d - (double)*t;
-        if (fabs(err) >= 1.0) {
-            _PyTime_overflow();
-            return -1;
-        }
-        return 0;
+        return _PyTime_FromFloatObject(t, d, round, to_nanoseconds);
     }
     else {
 #ifdef HAVE_LONG_LONG

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list