1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AOM_AOM_PORTS_AOM_TIMER_H_
13 #define AOM_AOM_PORTS_AOM_TIMER_H_
14
15 #include "config/aom_config.h"
16
17 #if CONFIG_OS_SUPPORT
18
19 #include <stddef.h>
20 #include <stdint.h>
21
22 #if defined(_WIN32)
23 /*
24 * Win32 specific includes
25 */
26 #undef NOMINMAX
27 #define NOMINMAX
28 #undef WIN32_LEAN_AND_MEAN
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
31 #else
32 /*
33 * POSIX specific includes
34 */
35 #include <sys/time.h>
36
37 /* timersub is not provided by msys at this time. */
38 #ifndef timersub
39 #define timersub(a, b, result) \
40 do { \
41 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
42 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
43 if ((result)->tv_usec < 0) { \
44 --(result)->tv_sec; \
45 (result)->tv_usec += 1000000; \
46 } \
47 } while (0)
48 #endif
49 #endif
50
51 struct aom_usec_timer {
52 #if defined(_WIN32)
53 LARGE_INTEGER begin, end;
54 #else
55 struct timeval begin, end;
56 #endif
57 };
58
aom_usec_timer_start(struct aom_usec_timer * t)59 static inline void aom_usec_timer_start(struct aom_usec_timer *t) {
60 #if defined(_WIN32)
61 QueryPerformanceCounter(&t->begin);
62 #else
63 gettimeofday(&t->begin, NULL);
64 #endif
65 }
66
aom_usec_timer_mark(struct aom_usec_timer * t)67 static inline void aom_usec_timer_mark(struct aom_usec_timer *t) {
68 #if defined(_WIN32)
69 QueryPerformanceCounter(&t->end);
70 #else
71 gettimeofday(&t->end, NULL);
72 #endif
73 }
74
aom_usec_timer_elapsed(struct aom_usec_timer * t)75 static inline int64_t aom_usec_timer_elapsed(struct aom_usec_timer *t) {
76 #if defined(_WIN32)
77 LARGE_INTEGER freq, diff;
78
79 diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
80
81 QueryPerformanceFrequency(&freq);
82 return diff.QuadPart * 1000000 / freq.QuadPart;
83 #else
84 struct timeval diff;
85
86 timersub(&t->end, &t->begin, &diff);
87 return ((int64_t)diff.tv_sec) * 1000000 + diff.tv_usec;
88 #endif
89 }
90
91 #else /* CONFIG_OS_SUPPORT = 0*/
92
93 /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
94 #ifndef timersub
95 #define timersub(a, b, result)
96 #endif
97
98 struct aom_usec_timer {
99 void *dummy;
100 };
101
aom_usec_timer_start(struct aom_usec_timer * t)102 static inline void aom_usec_timer_start(struct aom_usec_timer *t) { (void)t; }
103
aom_usec_timer_mark(struct aom_usec_timer * t)104 static inline void aom_usec_timer_mark(struct aom_usec_timer *t) { (void)t; }
105
aom_usec_timer_elapsed(struct aom_usec_timer * t)106 static inline int aom_usec_timer_elapsed(struct aom_usec_timer *t) {
107 (void)t;
108 return 0;
109 }
110
111 #endif /* CONFIG_OS_SUPPORT */
112
113 #endif // AOM_AOM_PORTS_AOM_TIMER_H_
114