X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=time.c;h=a591e8cd27c1fbeebf8a17a15d6b5aaf96e50bea;hp=0cb9babe2ae8de65be0fdfdc4e65ee6a626688e8;hb=80b5eac6e7363199f62fc39e583a28ce898a0b2b;hpb=ef81b9f4f0fa6a26043c68d429c0deeb7c949351;ds=sidebyside diff --git a/time.c b/time.c index 0cb9babe..a591e8cd 100644 --- a/time.c +++ b/time.c @@ -131,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; }