build: Combine rules for object files.
[paraslash.git] / time.c
1 /*
2  * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 #include "para.h"
8 /** \file time.c Helper functions for dealing with time values. */
9
10 /**
11  * Convert struct timeval to milliseconds.
12  *
13  * \param tv The time value value to convert.
14  *
15  * \return The number of milliseconds in \a tv.
16  */
17 long unsigned tv2ms(const struct timeval *tv)
18 {
19         return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
20 }
21
22 /**
23  * Convert milliseconds to a struct timeval.
24  *
25  * \param n The number of milliseconds.
26  * \param tv Result pointer.
27  */
28 void ms2tv(long unsigned n, struct timeval *tv)
29 {
30         tv->tv_sec = n / 1000;
31         tv->tv_usec = (n % 1000) * 1000;
32 }
33
34 /**
35  * Convert a double to a struct timeval.
36  *
37  * \param x The value to convert.
38  * \param tv Result pointer.
39  */
40 void d2tv(double x, struct timeval *tv)
41 {
42         tv->tv_sec = x;
43         tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
44 }
45
46 /**
47  * Compute the difference of two time values.
48  *
49  * \param b Minuend.
50  * \param a Subtrahend.
51  * \param diff Result pointer.
52  *
53  * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
54  * return.
55  *
56  * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
57  */
58 int tv_diff(const struct timeval *b, const struct timeval *a,
59                 struct timeval *diff)
60 {
61         int ret = 1;
62
63         if ((b->tv_sec < a->tv_sec) ||
64                 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
65                 const struct timeval *tmp = a;
66                 a = b;
67                 b = tmp;
68                 ret = -1;
69         }
70         if (!diff)
71                 return ret;
72         diff->tv_sec = b->tv_sec - a->tv_sec;
73         if (b->tv_usec < a->tv_usec) {
74                 diff->tv_sec--;
75                 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
76         } else
77                 diff->tv_usec = b->tv_usec - a->tv_usec;
78         return ret;
79 }
80
81 /**
82  * Add two time values.
83  *
84  * \param a First addend.
85  * \param b Second addend.
86  * \param sum Contains the sum \a a + \a b on return.
87  */
88 void tv_add(const struct timeval *a, const struct timeval *b,
89                 struct timeval *sum)
90 {
91         sum->tv_sec = a->tv_sec + b->tv_sec;
92         if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
93                 sum->tv_sec++;
94                 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
95         } else
96                 sum->tv_usec = a->tv_usec + b->tv_usec;
97 }
98
99 /**
100  * Compute integer multiple of given struct timeval.
101  *
102  * \param mult The integer value to multiply with.
103  * \param tv The timevalue to multiply.
104  *
105  * \param result Contains \a mult * \a tv on return.
106  */
107 void tv_scale(const unsigned long mult, const struct timeval *tv,
108                 struct timeval *result)
109 {
110         uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;
111
112         result->tv_sec = x / 1000 / 1000;
113         result->tv_usec = x % (1000 * 1000);
114 }
115
116 /**
117  * Compute a fraction of given struct timeval.
118  *
119  * \param divisor The integer value to divide by.
120  * \param tv The timevalue to divide.
121  * \param result Contains (1 / mult) * tv on return.
122  */
123 void tv_divide(const unsigned long divisor, const struct timeval *tv,
124                 struct timeval *result)
125 {
126         uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
127
128         result->tv_sec = x / 1000 / 1000;
129         result->tv_usec = x % (1000 * 1000);
130 }
131
132 /**
133  * Compute a convex combination of two time values.
134  *
135  * \param a The first coefficient.
136  * \param tv1 The first time value.
137  * \param b The second coefficient.
138  * \param tv2 The second time value.
139  * \param result Contains the convex combination upon return.
140  *
141  * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
142  * Both \a a and \a b may be negative.
143  *
144  * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively.
145  */
146 int tv_convex_combination(const long a, const struct timeval *tv1,
147                 const long b, const struct timeval *tv2,
148                 struct timeval *result)
149 {
150         struct timeval tmp1, tmp2, tmp3;
151         int ret = 1;
152         unsigned long a1, b1;
153
154         if (a == 0 && b == 0) {
155                 result->tv_sec = 0;
156                 result->tv_usec = 0;
157                 return 0;
158         }
159         a1 = PARA_ABS(a);
160         b1 = PARA_ABS(b);
161         tv_scale(a1, tv1, &tmp1);
162         tv_scale(b1, tv2, &tmp2);
163         if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */
164                 ret = tv_diff(&tmp1, &tmp2, &tmp3);
165         else
166                 tv_add(&tmp1, &tmp2, &tmp3);
167         tv_divide(a1 + b1, &tmp3, result);
168         if (!a || !b) {
169                 if (a + b < 0)
170                         ret = -1;
171         } else if (a < 0)
172                 ret = -ret;
173         return ret;
174 }
175
176 /**
177  * Compute when to send a chunk of an audio file.
178  *
179  * \param chunk_num The number of the chunk.
180  * \param chunk_tv The duration of one chunk.
181  * \param stream_start When the first chunk was sent.
182  * \param result The time when to send chunk number \a chunk_num.
183  *
184  * This function computes \a stream_start + \a chunk_num * \a chunk_time.
185  */
186 void compute_chunk_time(long unsigned chunk_num,
187                 struct timeval *chunk_tv, struct timeval *stream_start,
188                 struct timeval *result)
189 {
190         struct timeval tmp;
191
192         tv_scale(chunk_num, chunk_tv, &tmp);
193         tv_add(&tmp, stream_start, result);
194 }
195
196 /**
197  * Retrieve the time of the realtime clock.
198  *
199  * \param tv Where to store the result.
200  *
201  * Gets the current value of the system-wide real-time clock (identified by id
202  * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
203  * buffer, otherwise it is stored at the location given by \a tv.
204  *
205  * \return This function aborts on errors. On success it returns a pointer to
206  * memory containing the current time.
207  *
208  * \sa clock_gettime(2), gettimeofday(2).
209  */
210 struct timeval *clock_get_realtime(struct timeval *tv)
211 {
212         static struct timeval user_friendly;
213
214         if (!tv)
215                 tv = &user_friendly;
216 #ifdef HAVE_CLOCK_GETTIME
217         {
218                 struct timespec t;
219                 int ret;
220
221                 ret = clock_gettime(CLOCK_REALTIME, &t);
222                 assert(ret == 0);
223                 tv->tv_sec = t.tv_sec;
224                 tv->tv_usec = t.tv_nsec / 1000;
225         }
226 #else
227         #include <sys/time.h>
228         gettimeofday(tv, NULL);
229 #endif /* HAVE_CLOCK_GETTIME */
230         return tv;
231 }