summaryrefslogtreecommitdiff
path: root/Python/pytime.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pytime.c')
-rw-r--r--Python/pytime.c213
1 files changed, 136 insertions, 77 deletions
diff --git a/Python/pytime.c b/Python/pytime.c
index 77db204489..9f7ee2d0ed 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -60,50 +60,70 @@ _PyLong_FromTime_t(time_t t)
#endif
}
+double
+_PyTime_RoundHalfEven(double x)
+{
+ double rounded = round(x);
+ if (fabs(x-rounded) == 0.5)
+ /* halfway case: round to even */
+ rounded = 2.0*round(x/2.0);
+ return rounded;
+}
+
static int
-_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
+_PyTime_DoubleToDenominator(double d, 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;
+ double intpart, err;
+ /* volatile avoids optimization changing how numbers are rounded */
+ volatile double floatpart;
- d = PyFloat_AsDouble(obj);
- floatpart = modf(d, &intpart);
- if (floatpart < 0) {
- floatpart = 1.0 + floatpart;
- intpart -= 1.0;
- }
+ floatpart = modf(d, &intpart);
- floatpart *= denominator;
- if (round == _PyTime_ROUND_CEILING) {
- floatpart = ceil(floatpart);
- if (floatpart >= denominator) {
- floatpart = 0.0;
- intpart += 1.0;
- }
- }
- else {
- floatpart = floor(floatpart);
- }
+ floatpart *= denominator;
+ if (round == _PyTime_ROUND_HALF_EVEN)
+ floatpart = _PyTime_RoundHalfEven(floatpart);
+ else if (round == _PyTime_ROUND_CEILING)
+ floatpart = ceil(floatpart);
+ else
+ floatpart = floor(floatpart);
+ if (floatpart >= denominator) {
+ floatpart -= denominator;
+ intpart += 1.0;
+ }
+ else if (floatpart < 0) {
+ floatpart += denominator;
+ intpart -= 1.0;
+ }
+ assert(0.0 <= floatpart && floatpart < denominator);
- *sec = (time_t)intpart;
- err = intpart - (double)*sec;
- if (err <= -1.0 || err >= 1.0) {
- error_time_t_overflow();
- return -1;
- }
+ *sec = (time_t)intpart;
+ *numerator = (long)floatpart;
- *numerator = (long)floatpart;
- return 0;
+ err = intpart - (double)*sec;
+ if (err <= -1.0 || err >= 1.0) {
+ error_time_t_overflow();
+ return -1;
+ }
+ return 0;
+}
+
+static int
+_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
+ double denominator, _PyTime_round_t round)
+{
+ assert(denominator <= (double)LONG_MAX);
+
+ if (PyFloat_Check(obj)) {
+ double d = PyFloat_AsDouble(obj);
+ return _PyTime_DoubleToDenominator(d, sec, numerator,
+ denominator, round);
}
else {
*sec = _PyLong_AsTime_t(obj);
+ *numerator = 0;
if (*sec == (time_t)-1 && PyErr_Occurred())
return -1;
- *numerator = 0;
return 0;
}
}
@@ -112,10 +132,14 @@ int
_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
{
if (PyFloat_Check(obj)) {
- double d, intpart, err;
+ double intpart, err;
+ /* volatile avoids optimization changing how numbers are rounded */
+ volatile double d;
d = PyFloat_AsDouble(obj);
- if (round == _PyTime_ROUND_CEILING)
+ if (round == _PyTime_ROUND_HALF_EVEN)
+ d = _PyTime_RoundHalfEven(d);
+ else if (round == _PyTime_ROUND_CEILING)
d = ceil(d);
else
d = floor(d);
@@ -141,14 +165,20 @@ int
_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
_PyTime_round_t round)
{
- return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
+ int res;
+ res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
+ assert(0 <= *nsec && *nsec < SEC_TO_NS);
+ return res;
}
int
_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
_PyTime_round_t round)
{
- return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
+ int res;
+ res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
+ assert(0 <= *usec && *usec < SEC_TO_US);
+ return res;
}
static void
@@ -162,12 +192,13 @@ _PyTime_t
_PyTime_FromSeconds(int seconds)
{
_PyTime_t t;
+ t = (_PyTime_t)seconds;
/* ensure that integer overflow cannot happen, int type should have 32
bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
bits). */
- assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS)
- || (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS));
- t = (_PyTime_t)seconds * SEC_TO_NS;
+ assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
+ || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
+ t *= SEC_TO_NS;
return t;
}
@@ -221,29 +252,41 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
#endif
static int
+_PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
+ long to_nanoseconds)
+{
+ double err;
+ /* volatile avoids optimization changing how numbers are rounded */
+ volatile double d;
+
+ /* convert to a number of nanoseconds */
+ d = value;
+ d *= to_nanoseconds;
+
+ if (round == _PyTime_ROUND_HALF_EVEN)
+ d = _PyTime_RoundHalfEven(d);
+ else 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
@@ -305,17 +348,35 @@ _PyTime_AsNanosecondsObject(_PyTime_t t)
}
static _PyTime_t
-_PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round)
+_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
+ const _PyTime_round_t round)
{
assert(k > 1);
- if (round == _PyTime_ROUND_CEILING) {
+ if (round == _PyTime_ROUND_HALF_EVEN) {
+ _PyTime_t x, r, abs_r;
+ x = t / k;
+ r = t % k;
+ abs_r = Py_ABS(r);
+ if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
+ if (t >= 0)
+ x++;
+ else
+ x--;
+ }
+ return x;
+ }
+ else if (round == _PyTime_ROUND_CEILING) {
if (t >= 0)
return (t + k - 1) / k;
else
+ return t / k;
+ }
+ else {
+ if (t >= 0)
+ return t / k;
+ else
return (t - (k - 1)) / k;
}
- else
- return t / k;
}
_PyTime_t
@@ -336,13 +397,10 @@ _PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
{
_PyTime_t secs, ns;
int res = 0;
+ int usec;
secs = t / SEC_TO_NS;
ns = t % SEC_TO_NS;
- if (ns < 0) {
- ns += SEC_TO_NS;
- secs -= 1;
- }
#ifdef MS_WINDOWS
/* On Windows, timeval.tv_sec is a long (32 bit),
@@ -367,20 +425,21 @@ _PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
res = -1;
#endif
- if (round == _PyTime_ROUND_CEILING)
- tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
- else
- tv->tv_usec = (int)(ns / US_TO_NS);
-
- if (tv->tv_usec >= SEC_TO_US) {
- tv->tv_usec -= SEC_TO_US;
+ usec = (int)_PyTime_Divide(ns, US_TO_NS, round);
+ if (usec < 0) {
+ usec += SEC_TO_US;
+ tv->tv_sec -= 1;
+ }
+ else if (usec >= SEC_TO_US) {
+ usec -= SEC_TO_US;
tv->tv_sec += 1;
}
+ assert(0 <= usec && usec < SEC_TO_US);
+ tv->tv_usec = usec;
+
if (res && raise)
_PyTime_overflow();
-
- assert(0 <= tv->tv_usec && tv->tv_usec <= 999999);
return res;
}
@@ -409,13 +468,13 @@ _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
secs -= 1;
}
ts->tv_sec = (time_t)secs;
+ assert(0 <= nsec && nsec < SEC_TO_NS);
+ ts->tv_nsec = nsec;
+
if ((_PyTime_t)ts->tv_sec != secs) {
_PyTime_overflow();
return -1;
}
- ts->tv_nsec = nsec;
-
- assert(0 <= ts->tv_nsec && ts->tv_nsec <= 999999999);
return 0;
}
#endif