TickCounter.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. // Include for __rdtsc
  6. #if defined(JPH_PLATFORM_WINDOWS)
  7. #include <intrin.h>
  8. #elif defined(JPH_CPU_X86) && defined(JPH_COMPILER_GCC)
  9. #include <x86intrin.h>
  10. #endif
  11. JPH_NAMESPACE_BEGIN
  12. #if defined(JPH_PLATFORM_WINDOWS_UWP) || (defined(JPH_PLATFORM_WINDOWS) && defined(JPH_CPU_ARM))
  13. /// Functionality to get the processors cycle counter
  14. uint64 GetProcessorTickCount(); // Not inline to avoid having to include Windows.h
  15. #else
  16. /// Functionality to get the processors cycle counter
  17. JPH_INLINE uint64 GetProcessorTickCount()
  18. {
  19. #if defined(JPH_PLATFORM_BLUE)
  20. return JPH_PLATFORM_BLUE_GET_TICKS();
  21. #elif defined(JPH_CPU_X86)
  22. return __rdtsc();
  23. #elif defined(JPH_CPU_ARM)
  24. uint64 val;
  25. asm volatile("mrs %0, cntvct_el0" : "=r" (val));
  26. return val;
  27. #elif defined(JPH_CPU_WASM)
  28. return 0; // Not supported
  29. #else
  30. #error Undefined
  31. #endif
  32. }
  33. #endif // JPH_PLATFORM_WINDOWS_UWP || (JPH_PLATFORM_WINDOWS && JPH_CPU_ARM)
  34. /// Get the amount of ticks per second, note that this number will never be fully accurate as the amound of ticks per second may vary with CPU load, so this number is only to be used to give an indication of time for profiling purposes
  35. uint64 GetProcessorTicksPerSecond();
  36. JPH_NAMESPACE_END