/**
* 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;
}