Replace float/UInt64 durations by a more precise Time class (#388)

Improve Clock class with atomic RestartIfOver method and allows to choose required precision
This commit is contained in:
Jérôme Leclercq
2022-12-29 21:31:46 +01:00
committed by GitHub
parent 1de5f65536
commit dd421a6385
84 changed files with 1278 additions and 663 deletions

View File

@@ -1,51 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/ClockImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // La fréquence ne varie pas pas au cours de l'exécution
}
bool ClockImplInitializeHighPrecision()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0;
}
UInt64 ClockImplGetElapsedMicroseconds()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
//HANDLE thread = GetCurrentThread();
//DWORD oldMask = SetThreadAffinityMask(thread, 1);
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
//SetThreadAffinityMask(thread, oldMask);
return time.QuadPart*1000000ULL / s_frequency.QuadPart;
}
UInt64 ClockImplGetElapsedMilliseconds()
{
#ifdef NAZARA_PLATFORM_WINDOWS_VISTA
return GetTickCount64();
#else
return GetTickCount();
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -5,7 +5,7 @@
#include <Nazara/Core/Win32/FileImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Core/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp>

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/TimeImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // The frequency of the performance counter is fixed at system boot and is consistent across all processors
}
bool InitializeHighPrecisionTimer()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0 && s_frequency.QuadPart != 0;
}
Time GetElapsedNanosecondsImpl()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
if (s_frequency.QuadPart == 10'000'000) //< seems to be a common value
return Time::Nanoseconds(100ll * time.QuadPart);
else
{
// Compute using 128bits precisions
// https://stackoverflow.com/questions/23378063/how-can-i-use-mach-absolute-time-without-overflowing
UInt64 num = 1'000'000'000ll;
UInt64 denom = s_frequency.QuadPart;
UInt64 value = time.QuadPart;
UInt64 high = (value >> 32) * num;
UInt64 low = (value & 0xFFFFFFFFull) * num / denom;
UInt64 highRem = ((high % denom) << 32) / denom;
high /= denom;
return Time::Nanoseconds(SafeCast<Int64>((high << 32) + highRem + low));
}
}
Time GetElapsedMillisecondsImpl()
{
#ifdef NAZARA_UTILS_WINDOWS_NT6
return Time::Milliseconds(GetTickCount64());
#else
return Time::Milliseconds(GetTickCount());
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -4,16 +4,17 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#define NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#ifndef NAZARA_CORE_WIN32_TIMEIMPL_HPP
#define NAZARA_CORE_WIN32_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision();
UInt64 ClockImplGetElapsedMicroseconds();
UInt64 ClockImplGetElapsedMilliseconds();
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#endif // NAZARA_CORE_WIN32_TIMEIMPL_HPP

View File

@@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz

View File

@@ -4,8 +4,8 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_TIME_HPP
#define NAZARA_CORE_WIN32_TIME_HPP
#ifndef NAZARA_CORE_WIN32_UTILS_HPP
#define NAZARA_CORE_WIN32_UTILS_HPP
#include <Nazara/Prerequisites.hpp>
#include <ctime>
@@ -16,4 +16,4 @@ namespace Nz
time_t FileTimeToTime(FILETIME* time);
}
#endif // NAZARA_CORE_WIN32_TIME_HPP
#endif // NAZARA_CORE_WIN32_UTILS_HPP