1
0

powerpcmacos.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <sys/types.h>
  2. #include <sys/sysctl.h>
  3. #include <mach/mach_time.h>
  4. #define timebase mach_absolute_time
  5. static int cpumib[2] = { CTL_HW, HW_CPU_FREQ } ;
  6. static int tbmib[2] = { CTL_HW, HW_TB_FREQ } ;
  7. static long myround(double u)
  8. {
  9. long result = u;
  10. while (result + 0.5 < u) result += 1;
  11. while (result - 0.5 > u) result -= 1;
  12. return result;
  13. }
  14. static long tbcycles = 0;
  15. static void init(void)
  16. {
  17. unsigned int cpufrequency = 0; size_t cpufrequencylen = sizeof(unsigned int);
  18. unsigned int tbfrequency = 0; size_t tbfrequencylen = sizeof(unsigned int);
  19. sysctl(cpumib,2,&cpufrequency,&cpufrequencylen,0,0);
  20. sysctl(tbmib,2,&tbfrequency,&tbfrequencylen,0,0);
  21. if (tbfrequency > 0)
  22. tbcycles = myround((double) (unsigned long long) cpufrequency
  23. / (double) (unsigned long long) tbfrequency);
  24. }
  25. long long cpucycles_powerpcmacos(void)
  26. {
  27. if (!tbcycles) init();
  28. return timebase() * tbcycles;
  29. }
  30. long long cpucycles_powerpcmacos_persecond(void)
  31. {
  32. unsigned int result = 0; size_t resultlen = sizeof(unsigned int);
  33. sysctl(cpumib,2,&result,&resultlen,0,0);
  34. return (unsigned long long) result;
  35. }