diff options
Diffstat (limited to 'nacl/cpucycles/powerpcmacos.c')
-rw-r--r-- | nacl/cpucycles/powerpcmacos.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/nacl/cpucycles/powerpcmacos.c b/nacl/cpucycles/powerpcmacos.c new file mode 100644 index 00000000..ab0be1ea --- /dev/null +++ b/nacl/cpucycles/powerpcmacos.c | |||
@@ -0,0 +1,42 @@ | |||
1 | #include <sys/types.h> | ||
2 | #include <sys/sysctl.h> | ||
3 | #include <mach/mach_time.h> | ||
4 | |||
5 | #define timebase mach_absolute_time | ||
6 | |||
7 | static int cpumib[2] = { CTL_HW, HW_CPU_FREQ } ; | ||
8 | static int tbmib[2] = { CTL_HW, HW_TB_FREQ } ; | ||
9 | |||
10 | static long myround(double u) | ||
11 | { | ||
12 | long result = u; | ||
13 | while (result + 0.5 < u) result += 1; | ||
14 | while (result - 0.5 > u) result -= 1; | ||
15 | return result; | ||
16 | } | ||
17 | |||
18 | static long tbcycles = 0; | ||
19 | |||
20 | static void init(void) | ||
21 | { | ||
22 | unsigned int cpufrequency = 0; size_t cpufrequencylen = sizeof(unsigned int); | ||
23 | unsigned int tbfrequency = 0; size_t tbfrequencylen = sizeof(unsigned int); | ||
24 | sysctl(cpumib,2,&cpufrequency,&cpufrequencylen,0,0); | ||
25 | sysctl(tbmib,2,&tbfrequency,&tbfrequencylen,0,0); | ||
26 | if (tbfrequency > 0) | ||
27 | tbcycles = myround((double) (unsigned long long) cpufrequency | ||
28 | / (double) (unsigned long long) tbfrequency); | ||
29 | } | ||
30 | |||
31 | long long cpucycles_powerpcmacos(void) | ||
32 | { | ||
33 | if (!tbcycles) init(); | ||
34 | return timebase() * tbcycles; | ||
35 | } | ||
36 | |||
37 | long long cpucycles_powerpcmacos_persecond(void) | ||
38 | { | ||
39 | unsigned int result = 0; size_t resultlen = sizeof(unsigned int); | ||
40 | sysctl(cpumib,2,&result,&resultlen,0,0); | ||
41 | return (unsigned long long) result; | ||
42 | } | ||