forked from wjhwsh/VideoPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmach_util.c
84 lines (65 loc) · 2.02 KB
/
mach_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* mach_util.h/.c - Helper functions for using Mach time on the Mac and iPhone platforms
* Written by jamesghurley<at>gmail.com
*/
#include "mach_util.h"
static Float64 sFrequency;
static UInt32 sMinDelta;
static UInt32 sToNanosNumerator;
static UInt32 sToNanosDenominator;
static UInt32 sFromNanosNumerator;
static UInt32 sFromNanosDenominator;
static int sIsInited = 0;
void mu_init(){
struct mach_timebase_info theTimeBaseInfo;
mach_timebase_info(&theTimeBaseInfo);
sMinDelta = 1;
sToNanosNumerator = theTimeBaseInfo.numer;
sToNanosDenominator = theTimeBaseInfo.denom;
sFromNanosNumerator = sToNanosDenominator;
sFromNanosDenominator = sToNanosNumerator;
// the frequency of that clock is: (sToNanosDenominator / sToNanosNumerator) * 10^9
sFrequency = (Float64)sToNanosDenominator / (Float64)sToNanosNumerator;
sFrequency *= 1000000000.0;
sIsInited = 1;
}
inline UInt64 mu_convertToNanos(UInt64 inHostTime)
{
if(!sIsInited)
{
mu_init();
}
Float64 thePartialAnswer = (Float64)inHostTime / (Float64)sToNanosDenominator;
Float64 theFloatAnswer = (thePartialAnswer * (Float64)sToNanosNumerator);
return (UInt64)theFloatAnswer;
}
inline UInt64 mu_convertFromNanos(UInt64 inNanos)
{
if(!sIsInited)
{
mu_init();
}
Float64 theNumerator = (Float64)sToNanosNumerator;
Float64 theDenominator = (Float64)sToNanosDenominator;
Float64 theNanos = (Float64)inNanos;
Float64 thePartialAnswer = theNanos / theNumerator;
Float64 theFloatAnswer = thePartialAnswer * theDenominator;
UInt64 theAnswer = (UInt64)theFloatAnswer;
return theAnswer;
}
inline UInt64 mu_convertToMicros(UInt64 inHostTime)
{
if(!sIsInited)
{
mu_init();
}
Float64 thePartialAnswer = (Float64)inHostTime / (Float64)sToNanosDenominator;
Float64 theFloatAnswer = (thePartialAnswer * (Float64)sToNanosNumerator) / 1000.f;
return (UInt64)theFloatAnswer;
}
inline UInt64 mu_currentTimeInNanos(){
return mu_convertToNanos(mach_absolute_time());
}
inline UInt64 mu_currentTimeInMicros() {
return mu_convertToMicros(mach_absolute_time());
}