aom_timer.h 2.69 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10
11
 */

Adrian Grange's avatar
Adrian Grange committed
12
13
#ifndef AOM_PORTS_AOM_TIMER_H_
#define AOM_PORTS_AOM_TIMER_H_
Johann's avatar
Johann committed
14

Adrian Grange's avatar
Adrian Grange committed
15
#include "./aom_config.h"
Johann's avatar
Johann committed
16

Adrian Grange's avatar
Adrian Grange committed
17
#include "aom/aom_integer.h"
John Koleszar's avatar
John Koleszar committed
18

19
20
#if CONFIG_OS_SUPPORT

21
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Win32 specific includes
 */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#else
/*
 * POSIX specific includes
 */
#include <sys/time.h>

/* timersub is not provided by msys at this time. */
#ifndef timersub
clang-format's avatar
clang-format committed
37
38
39
#define timersub(a, b, result)                       \
  do {                                               \
    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
John Koleszar's avatar
John Koleszar committed
40
    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
clang-format's avatar
clang-format committed
41
42
43
44
    if ((result)->tv_usec < 0) {                     \
      --(result)->tv_sec;                            \
      (result)->tv_usec += 1000000;                  \
    }                                                \
John Koleszar's avatar
John Koleszar committed
45
  } while (0)
John Koleszar's avatar
John Koleszar committed
46
47
48
#endif
#endif

Adrian Grange's avatar
Adrian Grange committed
49
struct aom_usec_timer {
50
#if defined(_WIN32)
clang-format's avatar
clang-format committed
51
  LARGE_INTEGER begin, end;
John Koleszar's avatar
John Koleszar committed
52
#else
John Koleszar's avatar
John Koleszar committed
53
  struct timeval begin, end;
John Koleszar's avatar
John Koleszar committed
54
55
56
#endif
};

Adrian Grange's avatar
Adrian Grange committed
57
static INLINE void aom_usec_timer_start(struct aom_usec_timer *t) {
58
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
59
  QueryPerformanceCounter(&t->begin);
John Koleszar's avatar
John Koleszar committed
60
#else
John Koleszar's avatar
John Koleszar committed
61
  gettimeofday(&t->begin, NULL);
John Koleszar's avatar
John Koleszar committed
62
63
64
#endif
}

Adrian Grange's avatar
Adrian Grange committed
65
static INLINE void aom_usec_timer_mark(struct aom_usec_timer *t) {
66
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
67
  QueryPerformanceCounter(&t->end);
John Koleszar's avatar
John Koleszar committed
68
#else
John Koleszar's avatar
John Koleszar committed
69
  gettimeofday(&t->end, NULL);
John Koleszar's avatar
John Koleszar committed
70
71
72
#endif
}

Adrian Grange's avatar
Adrian Grange committed
73
static INLINE int64_t aom_usec_timer_elapsed(struct aom_usec_timer *t) {
74
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
75
  LARGE_INTEGER freq, diff;
John Koleszar's avatar
John Koleszar committed
76

John Koleszar's avatar
John Koleszar committed
77
  diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
John Koleszar's avatar
John Koleszar committed
78

John Koleszar's avatar
John Koleszar committed
79
80
  QueryPerformanceFrequency(&freq);
  return diff.QuadPart * 1000000 / freq.QuadPart;
John Koleszar's avatar
John Koleszar committed
81
#else
John Koleszar's avatar
John Koleszar committed
82
  struct timeval diff;
John Koleszar's avatar
John Koleszar committed
83

John Koleszar's avatar
John Koleszar committed
84
  timersub(&t->end, &t->begin, &diff);
John Koleszar's avatar
John Koleszar committed
85
  return diff.tv_sec * 1000000 + diff.tv_usec;
John Koleszar's avatar
John Koleszar committed
86
87
88
#endif
}

89
90
91
92
93
94
95
#else /* CONFIG_OS_SUPPORT = 0*/

/* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
#ifndef timersub
#define timersub(a, b, result)
#endif

Adrian Grange's avatar
Adrian Grange committed
96
struct aom_usec_timer {
John Koleszar's avatar
John Koleszar committed
97
  void *dummy;
98
99
};

100
static INLINE void aom_usec_timer_start(struct aom_usec_timer * /*t*/) {}
101

102
static INLINE void aom_usec_timer_mark(struct aom_usec_timer * /*t*/) {}
103

104
105
106
static INLINE int aom_usec_timer_elapsed(struct aom_usec_timer * /*t*/) {
  return 0;
}
107
108

#endif /* CONFIG_OS_SUPPORT */
John Koleszar's avatar
John Koleszar committed
109

Adrian Grange's avatar
Adrian Grange committed
110
#endif  // AOM_PORTS_AOM_TIMER_H_