X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=time.c;h=d7d803d932efd704642f9098989f6661f2731377;hp=0ebc6ec0c8f3f10e66f77ffe18d0f8fe05d7210a;hb=e65bdaa6b207d5cd409acaa5d0d33fe85dd7a28d;hpb=591b16bbe13ca336c4cef00e8f9f808c1bb4c9a0 diff --git a/time.c b/time.c index 0ebc6ec0..d7d803d9 100644 --- a/time.c +++ b/time.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2008 Andre Noll + * Copyright (C) 2005-2011 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -12,7 +12,7 @@ * * \param tv The time value value to convert. * - * \return The number off milliseconds in \a tv. + * \return The number of milliseconds in \a tv. */ long unsigned tv2ms(const struct timeval *tv) { @@ -82,7 +82,7 @@ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *di * * \param a First addend. * \param b Second addend. - * \param sum Contains the sum \a + \a b on return. + * \param sum Contains the sum \a a + \a b on return. */ void tv_add(const struct timeval *a, const struct timeval *b, struct timeval *sum) @@ -106,9 +106,10 @@ void tv_add(const struct timeval *a, const struct timeval *b, void tv_scale(const unsigned long mult, const struct timeval *tv, struct timeval *result) { - result->tv_sec = mult * tv->tv_sec; - result->tv_sec += tv->tv_usec * mult / 1000 / 1000; - result->tv_usec = tv->tv_usec * mult % (1000 * 1000); + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult; + + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /** @@ -130,43 +131,44 @@ void tv_divide(const unsigned long divisor, const struct timeval *tv, /** * Compute a convex combination of two time values. * - * \param a The first coefiicent. + * \param a The first coefficient. * \param tv1 The first time value. - * \param b The second coefiicent. + * \param b The second coefficient. * \param tv2 The second time value. * \param result Contains the convex combination upon return. * - * compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result. + * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result. * Both \a a and \a b may be negative. * - * \return One if \a x is positive, -1 otherwise. + * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively. */ int tv_convex_combination(const long a, const struct timeval *tv1, const long b, const struct timeval *tv2, struct timeval *result) { struct timeval tmp1, tmp2, tmp3; - int ret = 1, subtract = ((a > 0 && b < 0) || (a < 0 && b > 0)); - unsigned long a1 = PARA_ABS(a), b1 = PARA_ABS(b); + int ret = 1; + unsigned long a1, b1; + if (a == 0 && b == 0) { + result->tv_sec = 0; + result->tv_usec = 0; + return 0; + } + a1 = PARA_ABS(a); + b1 = PARA_ABS(b); tv_scale(a1, tv1, &tmp1); tv_scale(b1, tv2, &tmp2); - if (subtract) + if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */ ret = tv_diff(&tmp1, &tmp2, &tmp3); else tv_add(&tmp1, &tmp2, &tmp3); - if (a1 + b1) - tv_divide(a1 + b1, &tmp3, result); - else { - result->tv_sec = 0; - result->tv_usec = 0; - } + tv_divide(a1 + b1, &tmp3, result); if (!a || !b) { if (a + b < 0) ret = -1; - } else - if (a < 0) - ret = -ret; + } else if (a < 0) + ret = -ret; return ret; } @@ -178,7 +180,7 @@ int tv_convex_combination(const long a, const struct timeval *tv1, * \param stream_start When the first chunk was sent. * \param result The time when to send chunk number \a chunk_num. * - * This function computes stream_start + chunk_num * chunk_time. + * This function computes \a stream_start + \a chunk_num * \a chunk_time. */ void compute_chunk_time(long unsigned chunk_num, struct timeval *chunk_tv, struct timeval *stream_start,