TickCounter.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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) && defined(JPH_USE_NEON)
  24. uint64 val;
  25. asm volatile("mrs %0, cntvct_el0" : "=r" (val));
  26. return val;
  27. #elif defined(JPH_CPU_ARM)
  28. return 0; // Not supported
  29. #elif defined(JPH_CPU_WASM)
  30. return 0; // Not supported
  31. #else
  32. #error Undefined
  33. #endif
  34. }
  35. #endif // JPH_PLATFORM_WINDOWS_UWP || (JPH_PLATFORM_WINDOWS && JPH_CPU_ARM)
  36. /// 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
  37. uint64 GetProcessorTicksPerSecond();
  38. JPH_NAMESPACE_END