Initial WebM release
[profile/ivi/libvpx.git] / vpx_ports / vpx_timer.h
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license and patent
5  *  grant that can be found in the LICENSE file in the root of the source
6  *  tree. All contributing project authors may be found in the AUTHORS
7  *  file in the root of the source tree.
8  */
9
10
11 #ifndef VPX_TIMER_H
12 #define VPX_TIMER_H
13
14 #if defined(_MSC_VER)
15 /*
16  * Win32 specific includes
17  */
18 #ifndef WIN32_LEAN_AND_MEAN
19 #define WIN32_LEAN_AND_MEAN
20 #endif
21 #include <windows.h>
22 #else
23 /*
24  * POSIX specific includes
25  */
26 #include <sys/time.h>
27
28 /* timersub is not provided by msys at this time. */
29 #ifndef timersub
30 #define timersub(a, b, result) \
31     do { \
32         (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
33         (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
34         if ((result)->tv_usec < 0) { \
35             --(result)->tv_sec; \
36             (result)->tv_usec += 1000000; \
37         } \
38     } while (0)
39 #endif
40 #endif
41
42
43 struct vpx_usec_timer
44 {
45 #if defined(_MSC_VER)
46     LARGE_INTEGER  begin, end;
47 #else
48     struct timeval begin, end;
49 #endif
50 };
51
52
53 static INLINE void
54 vpx_usec_timer_start(struct vpx_usec_timer *t)
55 {
56 #if defined(_MSC_VER)
57     QueryPerformanceCounter(&t->begin);
58 #else
59     gettimeofday(&t->begin, NULL);
60 #endif
61 }
62
63
64 static INLINE void
65 vpx_usec_timer_mark(struct vpx_usec_timer *t)
66 {
67 #if defined(_MSC_VER)
68     QueryPerformanceCounter(&t->end);
69 #else
70     gettimeofday(&t->end, NULL);
71 #endif
72 }
73
74
75 static INLINE long
76 vpx_usec_timer_elapsed(struct vpx_usec_timer *t)
77 {
78 #if defined(_MSC_VER)
79     LARGE_INTEGER freq, diff;
80
81     diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
82
83     if (QueryPerformanceFrequency(&freq) && diff.QuadPart < freq.QuadPart)
84         return (long)(diff.QuadPart * 1000000 / freq.QuadPart);
85
86     return 1000000;
87 #else
88     struct timeval diff;
89
90     timersub(&t->end, &t->begin, &diff);
91     return diff.tv_sec ? 1000000 : diff.tv_usec;
92 #endif
93 }
94
95
96 #endif