mirror of
https://github.com/ZDoom/qzdoom.git
synced 2024-11-10 14:51:51 +00:00
RDTSC-based cycle_t for macOS
Windows and macOS now share most of related code Old implementation using mach_absolute_time() was more precise (at least in theory) but too costly
This commit is contained in:
parent
6225f60eb2
commit
1cdfcb4935
3 changed files with 22 additions and 83 deletions
|
@ -36,6 +36,7 @@
|
|||
#include <fnmatch.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include "d_ticcmd.h"
|
||||
#include "doomdef.h"
|
||||
|
@ -98,9 +99,29 @@ void SetLanguageIDs()
|
|||
void I_InitTimer();
|
||||
void I_ShutdownTimer();
|
||||
|
||||
double PerfToSec, PerfToMillisec;
|
||||
|
||||
static void CalculateCPUSpeed()
|
||||
{
|
||||
long long frequency;
|
||||
size_t size = sizeof frequency;
|
||||
|
||||
if (0 == sysctlbyname("machdep.tsc.frequency", &frequency, &size, nullptr, 0) && 0 != frequency)
|
||||
{
|
||||
PerfToSec = 1.0 / frequency;
|
||||
PerfToMillisec = 1000.0 / frequency;
|
||||
|
||||
if (!batchrun)
|
||||
{
|
||||
Printf("CPU speed: %.0f MHz\n", 0.001 / PerfToMillisec);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void I_Init(void)
|
||||
{
|
||||
CheckCPUID(&CPU);
|
||||
CalculateCPUSpeed();
|
||||
DumpCPUInfo(&CPU);
|
||||
|
||||
atterm(I_ShutdownSound);
|
||||
|
|
|
@ -42,15 +42,6 @@
|
|||
#include "m_swap.h"
|
||||
#include "sbar.h"
|
||||
|
||||
|
||||
#if defined (__APPLE__)
|
||||
|
||||
mach_timebase_info_data_t cycle_t::s_info;
|
||||
bool cycle_t::s_initialized;
|
||||
|
||||
#endif // __APPLE__
|
||||
|
||||
|
||||
FStat *FStat::FirstStat;
|
||||
|
||||
FStat::FStat (const char *name)
|
||||
|
|
75
src/stats.h
75
src/stats.h
|
@ -36,78 +36,7 @@
|
|||
|
||||
#include "zstring.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#if defined (__APPLE__)
|
||||
|
||||
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
|
||||
class cycle_t
|
||||
{
|
||||
public:
|
||||
cycle_t()
|
||||
{
|
||||
if ( !s_initialized )
|
||||
{
|
||||
mach_timebase_info( &s_info );
|
||||
s_initialized = true;
|
||||
}
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
cycle_t &operator=( const cycle_t &other )
|
||||
{
|
||||
if ( &other != this )
|
||||
{
|
||||
m_seconds = other.m_seconds;
|
||||
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Reset()
|
||||
{
|
||||
m_seconds = 0;
|
||||
}
|
||||
|
||||
void Clock()
|
||||
{
|
||||
m_seconds -= Nanoseconds() * 1e-9;
|
||||
}
|
||||
|
||||
void Unclock()
|
||||
{
|
||||
m_seconds += Nanoseconds() * 1e-9;
|
||||
}
|
||||
|
||||
double Time()
|
||||
{
|
||||
return m_seconds;
|
||||
}
|
||||
|
||||
double TimeMS()
|
||||
{
|
||||
return m_seconds * 1e3;
|
||||
}
|
||||
|
||||
private:
|
||||
double m_seconds;
|
||||
|
||||
static mach_timebase_info_data_t s_info;
|
||||
static bool s_initialized;
|
||||
|
||||
uint64_t Nanoseconds() const
|
||||
{
|
||||
return mach_absolute_time() * s_info.numer / s_info.denom;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#else // !__APPLE__
|
||||
#if !defined _WIN32 && !defined __APPLE__
|
||||
|
||||
#ifdef NO_CLOCK_GETTIME
|
||||
class cycle_t
|
||||
|
@ -171,8 +100,6 @@ private:
|
|||
|
||||
#endif
|
||||
|
||||
#endif // __APPLE__
|
||||
|
||||
#else
|
||||
|
||||
// Windows
|
||||
|
|
Loading…
Reference in a new issue