test-ticks.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "ed25519-donna-portable-identify.h"
  2. /* ticks - not tested on anything other than x86 */
  3. static uint64_t
  4. get_ticks(void) {
  5. #if defined(CPU_X86) || defined(CPU_X86_64)
  6. #if defined(COMPILER_INTEL)
  7. return _rdtsc();
  8. #elif defined(COMPILER_MSVC)
  9. return __rdtsc();
  10. #elif defined(COMPILER_GCC)
  11. uint32_t lo, hi;
  12. __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
  13. return ((uint64_t)lo | ((uint64_t)hi << 32));
  14. #else
  15. need rdtsc for this compiler
  16. #endif
  17. #elif defined(OS_SOLARIS)
  18. return (uint64_t)gethrtime();
  19. #elif defined(CPU_SPARC) && !defined(OS_OPENBSD)
  20. uint64_t t;
  21. __asm__ __volatile__("rd %%tick, %0" : "=r" (t));
  22. return t;
  23. #elif defined(CPU_PPC)
  24. uint32_t lo = 0, hi = 0;
  25. __asm__ __volatile__("mftbu %0; mftb %1" : "=r" (hi), "=r" (lo));
  26. return ((uint64_t)lo | ((uint64_t)hi << 32));
  27. #elif defined(CPU_IA64)
  28. uint64_t t;
  29. __asm__ __volatile__("mov %0=ar.itc" : "=r" (t));
  30. return t;
  31. #elif defined(OS_NIX)
  32. timeval t2;
  33. gettimeofday(&t2, NULL);
  34. t = ((uint64_t)t2.tv_usec << 32) | (uint64_t)t2.tv_sec;
  35. return t;
  36. #else
  37. need ticks for this platform
  38. #endif
  39. }
  40. #define timeit(x,minvar) \
  41. ticks = get_ticks(); \
  42. x; \
  43. ticks = get_ticks() - ticks; \
  44. if (ticks < minvar) \
  45. minvar = ticks;
  46. #define maxticks 0xffffffffffffffffull