1#include <mach/mach_time.h>
2#include <stdint.h>
3#include <stdlib.h>
4
5double intervalInCycles( uint64_t startTime, uint64_t endTime )
6{
7 uint64_t rawTime = endTime - startTime;
8 static double conversion = 0.0;
9
10 if( 0.0 == conversion )
11 {
12 mach_timebase_info_data_t info;
13 kern_return_t err = mach_timebase_info( &info );
14 if( 0 != err )
15 return 0;
16
17 uint64_t freq = 0;
18 size_t freqSize = sizeof( freq );
19 int err2 = sysctlbyname( "hw.cpufrequency", &freq, &freqSize, NULL, 0L );
20 if( 0 != err2 )
21 return 0;
22
23 conversion = (double) freq * (1e-9 * (double) info.numer / (double) info.denom);
24 }
25
26 return (double) rawTime * conversion;
27}
28
29

source code of compiler-rt/test/builtins/timing/timing.h