1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 
12 #ifndef VPX_PORTS_VPX_TIMER_H_
13 #define VPX_PORTS_VPX_TIMER_H_
14 #include "vpx/vpx_integer.h"
15 
16 #if CONFIG_OS_SUPPORT
17 
18 #if defined(_WIN32)
19 /*
20  * Win32 specific includes
21  */
22 #ifndef WIN32_LEAN_AND_MEAN
23 #define WIN32_LEAN_AND_MEAN
24 #endif
25 #include <windows.h>
26 #else
27 /*
28  * POSIX specific includes
29  */
30 #include <sys/time.h>
31 
32 /* timersub is not provided by msys at this time. */
33 #ifndef timersub
34 #define timersub(a, b, result) \
35   do { \
36     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
37     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
38     if ((result)->tv_usec < 0) { \
39       --(result)->tv_sec; \
40       (result)->tv_usec += 1000000; \
41     } \
42   } while (0)
43 #endif
44 #endif
45 
46 
47 struct vpx_usec_timer {
48 #if defined(_WIN32)
49   LARGE_INTEGER  begin, end;
50 #else
51   struct timeval begin, end;
52 #endif
53 };
54 
55 
56 static void
vpx_usec_timer_start(struct vpx_usec_timer * t)57 vpx_usec_timer_start(struct vpx_usec_timer *t) {
58 #if defined(_WIN32)
59   QueryPerformanceCounter(&t->begin);
60 #else
61   gettimeofday(&t->begin, NULL);
62 #endif
63 }
64 
65 
66 static void
vpx_usec_timer_mark(struct vpx_usec_timer * t)67 vpx_usec_timer_mark(struct vpx_usec_timer *t) {
68 #if defined(_WIN32)
69   QueryPerformanceCounter(&t->end);
70 #else
71   gettimeofday(&t->end, NULL);
72 #endif
73 }
74 
75 
76 static int64_t
vpx_usec_timer_elapsed(struct vpx_usec_timer * t)77 vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
78 #if defined(_WIN32)
79   LARGE_INTEGER freq, diff;
80 
81   diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
82 
83   QueryPerformanceFrequency(&freq);
84   return diff.QuadPart * 1000000 / freq.QuadPart;
85 #else
86   struct timeval diff;
87 
88   timersub(&t->end, &t->begin, &diff);
89   return diff.tv_sec * 1000000 + diff.tv_usec;
90 #endif
91 }
92 
93 #else /* CONFIG_OS_SUPPORT = 0*/
94 
95 /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
96 #ifndef timersub
97 #define timersub(a, b, result)
98 #endif
99 
100 struct vpx_usec_timer {
101   void *dummy;
102 };
103 
104 static void
vpx_usec_timer_start(struct vpx_usec_timer * t)105 vpx_usec_timer_start(struct vpx_usec_timer *t) { }
106 
107 static void
vpx_usec_timer_mark(struct vpx_usec_timer * t)108 vpx_usec_timer_mark(struct vpx_usec_timer *t) { }
109 
110 static long
vpx_usec_timer_elapsed(struct vpx_usec_timer * t)111 vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
112   return 0;
113 }
114 
115 #endif /* CONFIG_OS_SUPPORT */
116 
117 #endif  // VPX_PORTS_VPX_TIMER_H_
118