project64/Source/Common/HighResTimeStamp.cpp

63 lines
1.2 KiB
C++
Raw Permalink Normal View History

2016-09-16 20:10:40 +00:00
#include "HighResTimeStamp.h"
#include <stdio.h>
#include <string.h>
2022-10-03 08:04:42 +00:00
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#else
2016-09-16 20:10:40 +00:00
#include <math.h>
#endif
#ifdef _WIN32
bool HighResTimeStamp::m_GotFreq = false;
2022-10-03 08:04:42 +00:00
uint64_t HighResTimeStamp::m_Freq = {0};
2016-09-16 20:10:40 +00:00
#endif
HighResTimeStamp::HighResTimeStamp()
{
#ifndef _WIN32
m_time = 0;
#else
if (!m_GotFreq)
{
LARGE_INTEGER value;
QueryPerformanceFrequency(&value);
m_Freq = value.QuadPart;
}
m_time = 0;
#endif
}
HighResTimeStamp & HighResTimeStamp::SetToNow(void)
{
#ifndef _WIN32
struct timespec now;
memset(&now, 0, sizeof(now));
clock_gettime(CLOCK_MONOTONIC, &now);
m_time = ((uint64_t)now.tv_sec * (uint64_t)1000000l) + (now.tv_nsec / 1000);
#else
LARGE_INTEGER value;
QueryPerformanceCounter(&value);
m_time = value.QuadPart;
#endif
return *this;
}
uint64_t HighResTimeStamp::GetMicroSeconds(void)
{
#ifndef _WIN32
return m_time;
#else
return (m_time * 1000000) / m_Freq;
#endif
}
void HighResTimeStamp::SetMicroSeconds(uint64_t MicroSeconds)
{
#ifndef _WIN32
m_time = MicroSeconds;
#else
m_time = (MicroSeconds * m_Freq) / 1000000;
#endif
}