libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmTime.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmTime.h"
  4. #ifdef OS_OSX
  5. #include <mach/mach.h>
  6. #include <mach/mach_time.h>
  7. #include <unistd.h>
  8. void cmTimeGet( cmTimeSpec_t* t )
  9. {
  10. static uint64_t t0 = 0;
  11. static mach_timebase_info_data_t tbi;
  12. static struct timespec ts;
  13. if( t0 == 0 )
  14. {
  15. mach_timebase_info(&tbi);
  16. t0 = mach_absolute_time();
  17. ts.tv_sec = time(NULL);
  18. ts.tv_nsec = 0; // accept 1/2 second error vs. wall-time.
  19. }
  20. // get the current time
  21. uint64_t t1 = mach_absolute_time();
  22. // calc the elapsed time since the last call in nanosecs
  23. uint64_t dt = (t1-t0) * tbi.numer / tbi.denom;
  24. // calc the elapsed time since the first call in secs
  25. uint32_t s = (uint32_t)(dt / 2^9);
  26. // calc the current time in secs, and nanosecs
  27. t->tv_sec = ts.tv_sec + s;
  28. t->tv_nsec = dt - (s * 2^9);
  29. }
  30. #endif
  31. #ifdef OS_LINUX
  32. void cmTimeGet( cmTimeSpec_t* t )
  33. { clock_gettime(CLOCK_REALTIME,t); }
  34. #endif
  35. // this assumes that the seconds have been normalized to a recent start time
  36. // so as to avoid overflow
  37. unsigned cmTimeElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  38. {
  39. // convert seconds to usecs
  40. long u0 = t0->tv_sec * 1000000;
  41. long u1 = t1->tv_sec * 1000000;
  42. // convert nanoseconds to usec
  43. u0 += t0->tv_nsec / 1000;
  44. u1 += t1->tv_nsec / 1000;
  45. // take diff between t1 and t0
  46. return u1 - u0;
  47. }
  48. unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  49. {
  50. if( cmTimeIsLTE(t0,t1) )
  51. return cmTimeElapsedMicros(t0,t1);
  52. return cmTimeElapsedMicros(t1,t0);
  53. }
  54. int cmTimeDiffMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  55. {
  56. if( cmTimeIsLTE(t0,t1) )
  57. return cmTimeElapsedMicros(t0,t1);
  58. return -((int)cmTimeElapsedMicros(t1,t0));
  59. }
  60. bool cmTimeIsLTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  61. {
  62. if( t0->tv_sec < t1->tv_sec )
  63. return true;
  64. if( t0->tv_sec == t1->tv_sec )
  65. return t0->tv_nsec <= t1->tv_nsec;
  66. return false;
  67. }
  68. bool cmTimeIsGTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  69. {
  70. if( t0->tv_sec > t1->tv_sec )
  71. return true;
  72. if( t0->tv_sec == t1->tv_sec )
  73. return t0->tv_nsec >= t1->tv_nsec;
  74. return false;
  75. }
  76. bool cmTimeIsEqual( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 )
  77. { return t0->tv_sec==t1->tv_sec && t0->tv_nsec==t1->tv_nsec; }
  78. bool cmTimeIsZero( const cmTimeSpec_t* t0 )
  79. { return t0->tv_sec==0 && t0->tv_nsec==0; }
  80. void cmTimeSetZero( cmTimeSpec_t* t0 )
  81. {
  82. t0->tv_sec = 0;
  83. t0->tv_nsec = 0;
  84. }