2009-07-06 08:57:36 +00:00
|
|
|
// This code is in the public domain -- castano@gmail.com
|
|
|
|
|
2010-05-27 23:18:08 +00:00
|
|
|
#pragma once
|
2009-07-06 08:57:36 +00:00
|
|
|
#ifndef NV_CORE_TIMER_H
|
|
|
|
#define NV_CORE_TIMER_H
|
|
|
|
|
2010-03-16 22:35:20 +00:00
|
|
|
#include "nvcore.h"
|
2009-07-06 08:57:36 +00:00
|
|
|
|
2012-03-09 07:08:16 +00:00
|
|
|
#if NV_CC_MSVC
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2010-07-22 10:25:14 +00:00
|
|
|
namespace nv {
|
2010-05-27 23:18:08 +00:00
|
|
|
|
2012-03-01 03:48:05 +00:00
|
|
|
#if NV_CC_MSVC
|
2012-03-09 07:08:16 +00:00
|
|
|
NV_FORCEINLINE uint64 fastCpuClock() { return __rdtsc(); }
|
2012-03-01 03:48:05 +00:00
|
|
|
#elif NV_CC_GNUC && NV_CPU_X86
|
|
|
|
NV_FORCEINLINE uint64 fastCpuClock() {
|
|
|
|
uint64 val;
|
|
|
|
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
#elif NV_CC_GNUC && NV_CPU_X86_64
|
|
|
|
NV_FORCEINLINE uint64 fastCpuClock() {
|
|
|
|
uint hi, lo;
|
|
|
|
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
|
|
|
|
return uint64(lo) | (uint64(hi) << 32);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
NV_FORCEINLINE uint64 fastCpuClock() { return 0; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint64 systemClockFrequency();
|
|
|
|
uint64 systemClock();
|
|
|
|
|
2010-07-22 10:25:14 +00:00
|
|
|
class NVCORE_CLASS Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Timer() {}
|
2010-05-27 23:18:08 +00:00
|
|
|
|
2012-03-01 03:48:05 +00:00
|
|
|
void start() { m_start = systemClock(); }
|
|
|
|
void stop() { m_stop = systemClock(); }
|
2009-07-06 08:57:36 +00:00
|
|
|
|
2012-03-01 03:48:05 +00:00
|
|
|
float elapsed() const { return float(m_stop - m_start) / systemClockFrequency(); }
|
2010-07-22 10:25:14 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-01 03:48:05 +00:00
|
|
|
uint64 m_start;
|
|
|
|
uint64 m_stop;
|
2010-07-22 10:25:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // nv namespace
|
2009-07-06 08:57:36 +00:00
|
|
|
|
|
|
|
#endif // NV_CORE_TIMER_H
|