2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 /** \file time.c Helper functions for dealing with time values. */
11 * Convert struct timeval to milliseconds.
13 * \param tv The time value value to convert.
15 * \return The number off milliseconds in \a tv.
17 long unsigned tv2ms(const struct timeval *tv)
19 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
23 * Convert milliseconds to a struct timeval.
25 * \param n The number of milliseconds.
26 * \param tv Result pointer.
28 void ms2tv(long unsigned n, struct timeval *tv)
30 tv->tv_sec = n / 1000;
31 tv->tv_usec = (n % 1000) * 1000;
35 * Convert a double to a struct timeval.
37 * \param x The value to convert.
38 * \param tv Result pointer.
40 void d2tv(double x, struct timeval *tv)
43 tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
47 * Compute the difference of two time values.
50 * \param a Subtrahend.
51 * \param diff Result pointer.
53 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
56 * \return If \a b < \a, this function returns -1, otherwise it returns 1.
58 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
62 if ((b->tv_sec < a->tv_sec) ||
63 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
64 const struct timeval *tmp = a;
71 diff->tv_sec = b->tv_sec - a->tv_sec;
72 if (b->tv_usec < a->tv_usec) {
74 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
76 diff->tv_usec = b->tv_usec - a->tv_usec;
81 * Add two time values.
83 * \param a First addend.
84 * \param b Second addend.
86 * \param \a Sum contains the sum \a + \a b on return.
88 void tv_add(const struct timeval *a, const struct timeval *b,
91 sum->tv_sec = a->tv_sec + b->tv_sec;
92 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
94 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
96 sum->tv_usec = a->tv_usec + b->tv_usec;
100 * Compute integer multiple of given struct timeval.
102 * \param mult The integer value to multiply with.
103 * \param tv The timevalue to multiply.
105 * \param result Contains \a mult * \a tv on return.
107 void tv_scale(const unsigned long mult, const struct timeval *tv,
108 struct timeval *result)
110 result->tv_sec = mult * tv->tv_sec;
111 result->tv_sec += tv->tv_usec * mult / 1000 / 1000;
112 result->tv_usec = tv->tv_usec * mult % (1000 * 1000);
116 * Compute a fraction of given struct timeval.
118 * \param divisor The integer value to divide by.
119 * \param tv The timevalue to divide.
120 * \param result Contains (1 / mult) * tv on return.
122 void tv_divide(const unsigned long divisor, const struct timeval *tv,
123 struct timeval *result)
127 q = tv->tv_usec / divisor;
128 result->tv_sec = tv->tv_sec / divisor;
129 result->tv_usec = (tv->tv_sec - result->tv_sec * divisor)
130 * 1000 * 1000 / divisor;
131 if (result->tv_usec + q >= 1000 * 1000) {
133 result->tv_usec = 1000 * 1000 - result->tv_usec - q;
135 result->tv_usec += q;
139 * Compute a convex combination of two time values.
141 * \param a The first coefiicent.
142 * \param tv1 The first time value.
143 * \param b The second coefiicent.
144 * \param tv2 The second time value.
145 * \param result Contains the convex combination upon return.
147 * compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
148 * Both \a a and \a b may be negative.
150 * \return One if \a x is positive, -1 otherwise.
152 int tv_convex_combination(const long a, const struct timeval *tv1,
153 const long b, const struct timeval *tv2,
154 struct timeval *result)
156 struct timeval tmp1, tmp2, tmp3;
157 int ret = 1, subtract = ((a > 0 && b < 0) || (a < 0 && b > 0));
158 unsigned long a1 = PARA_ABS(a), b1 = PARA_ABS(b);
160 tv_scale(a1, tv1, &tmp1);
161 tv_scale(b1, tv2, &tmp2);
163 ret = tv_diff(&tmp1, &tmp2, &tmp3);
165 tv_add(&tmp1, &tmp2, &tmp3);
167 tv_divide(a1 + b1, &tmp3, result);