TickCounter.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Include for __rdtsc
  5. #if defined(JPH_PLATFORM_WINDOWS)
  6. #include <intrin.h>
  7. #elif defined(JPH_CPU_X86) && defined(JPH_COMPILER_GCC)
  8. #include <x86intrin.h>
  9. #endif
  10. JPH_NAMESPACE_BEGIN
  11. #if defined(JPH_PLATFORM_WINDOWS_UWP) || (defined(JPH_PLATFORM_WINDOWS) && defined(JPH_CPU_ARM))
  12. /// Functionality to get the processors cycle counter
  13. uint64 GetProcessorTickCount(); // Not inline to avoid having to include Windows.h
  14. #else
  15. /// Functionality to get the processors cycle counter
  16. JPH_INLINE uint64 GetProcessorTickCount()
  17. {
  18. #if defined(JPH_PLATFORM_BLUE)
  19. return JPH_PLATFORM_BLUE_GET_TICKS();
  20. #elif defined(JPH_CPU_X86)
  21. return __rdtsc();
  22. #elif defined(JPH_CPU_ARM)
  23. uint64 val;
  24. asm volatile("mrs %0, cntvct_el0" : "=r" (val));
  25. return val;
  26. #elif defined(JPH_CPU_WASM)
  27. return 0; // Not supported
  28. #else
  29. #error Undefined
  30. #endif
  31. }
  32. #endif // JPH_PLATFORM_WINDOWS_UWP || (JPH_PLATFORM_WINDOWS && JPH_CPU_ARM)
  33. /// 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
  34. uint64 GetProcessorTicksPerSecond();
  35. JPH_NAMESPACE_END