timer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "bx_p.h"
  6. #include <bx/timer.h>
  7. #if BX_PLATFORM_ANDROID
  8. # include <time.h> // clock, clock_gettime
  9. #elif BX_PLATFORM_EMSCRIPTEN
  10. # include <emscripten.h>
  11. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  12. # include <windows.h>
  13. #else
  14. # include <sys/time.h> // gettimeofday
  15. #endif // BX_PLATFORM_
  16. namespace bx
  17. {
  18. int64_t getHPCounter()
  19. {
  20. #if BX_PLATFORM_WINDOWS \
  21. || BX_PLATFORM_XBOXONE \
  22. || BX_PLATFORM_WINRT
  23. LARGE_INTEGER li;
  24. // Performance counter value may unexpectedly leap forward
  25. // http://support.microsoft.com/kb/274323
  26. QueryPerformanceCounter(&li);
  27. int64_t i64 = li.QuadPart;
  28. #elif BX_PLATFORM_ANDROID
  29. struct timespec now;
  30. clock_gettime(CLOCK_MONOTONIC, &now);
  31. int64_t i64 = now.tv_sec*INT64_C(1000000000) + now.tv_nsec;
  32. #elif BX_PLATFORM_EMSCRIPTEN
  33. int64_t i64 = int64_t(1000.0f * emscripten_get_now() );
  34. #elif !BX_PLATFORM_NONE
  35. struct timeval now;
  36. gettimeofday(&now, 0);
  37. int64_t i64 = now.tv_sec*INT64_C(1000000) + now.tv_usec;
  38. #else
  39. BX_CHECK(false, "Not implemented!");
  40. int64_t i64 = UINT64_MAX;
  41. #endif // BX_PLATFORM_
  42. return i64;
  43. }
  44. int64_t getHPFrequency()
  45. {
  46. #if BX_PLATFORM_WINDOWS \
  47. || BX_PLATFORM_XBOXONE \
  48. || BX_PLATFORM_WINRT
  49. LARGE_INTEGER li;
  50. QueryPerformanceFrequency(&li);
  51. return li.QuadPart;
  52. #elif BX_PLATFORM_ANDROID
  53. return INT64_C(1000000000);
  54. #elif BX_PLATFORM_EMSCRIPTEN
  55. return INT64_C(1000000);
  56. #else
  57. return INT64_C(1000000);
  58. #endif // BX_PLATFORM_
  59. }
  60. } // namespace bx