NP2kai/np2_tickcount.c

100 lines
2.3 KiB
C
Raw Normal View History

2020-04-17 02:27:01 +00:00
#if defined(SUPPORT_NP2_TICKCOUNT)
2020-05-15 20:29:23 +00:00
#include <np2_tickcount.h>
2019-12-10 08:59:05 +00:00
#include <time.h>
#if defined(NP2_WIN)
#include <windows.h>
2020-05-15 20:29:23 +00:00
#elif defined(NP2_SDL)
2019-12-10 08:59:05 +00:00
#include <SDL.h>
2020-02-04 05:21:43 +00:00
#elif defined(__LIBRETRO__)
2020-02-03 04:18:15 +00:00
#include <features/features_cpu.h>
2019-12-10 08:59:05 +00:00
#endif
static int64_t initcount;
2020-02-03 04:18:15 +00:00
#if defined(__LIBRETRO__)
static int64_t inittime;
static int64_t lastcount;
static int64_t lasttime;
#endif
2019-12-10 08:59:05 +00:00
int64_t NP2_TickCount_GetCount(void) {
#if defined(NP2_WIN)
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return count.QuadPart;
2020-05-15 20:29:23 +00:00
#elif defined(NP2_SDL)
2020-01-14 16:23:27 +00:00
#if SDL_MAJOR_VERSION == 1
return SDL_GetTicks();
#else
2019-12-10 08:59:05 +00:00
return SDL_GetPerformanceCounter();
2020-01-14 16:23:27 +00:00
#endif
2020-02-03 04:18:15 +00:00
#elif defined(__LIBRETRO__)
lastcount = cpu_features_get_perf_counter();
lasttime = cpu_features_get_time_usec();
return lastcount;
2019-12-10 08:59:05 +00:00
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
#endif
}
int64_t NP2_TickCount_GetFrequency(void) {
#if defined(NP2_WIN)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return freq.QuadPart;
2020-05-15 20:29:23 +00:00
#elif defined(NP2_SDL)
2020-01-14 16:23:27 +00:00
#if SDL_MAJOR_VERSION == 1
return 100000000;
#else
2019-12-10 08:59:05 +00:00
return SDL_GetPerformanceFrequency();
2020-01-14 16:23:27 +00:00
#endif
2020-02-03 04:18:15 +00:00
#elif defined(__LIBRETRO__)
int64_t nowcount = cpu_features_get_perf_counter();
int64_t nowtime = cpu_features_get_time_usec();
2020-02-04 17:57:46 +00:00
int64_t ret;
2020-02-03 04:18:15 +00:00
if(nowtime > lasttime) {
2020-02-04 17:57:46 +00:00
ret = ((nowcount - lastcount) / (nowtime - lasttime)) * 1000000;
2020-02-03 04:18:15 +00:00
} else {
2020-02-04 17:57:46 +00:00
ret = 0;
2020-02-03 04:18:15 +00:00
}
2020-02-04 17:57:46 +00:00
lastcount = nowtime;
lasttime = nowtime;
return ret;
2019-12-10 08:59:05 +00:00
#else
struct timespec res;
clock_getres(CLOCK_MONOTONIC, &res);
return 1000000000 / (res.tv_sec * 1000000000 + res.tv_nsec);
#endif
}
void NP2_TickCount_Initialize(void) {
initcount = NP2_TickCount_GetCount();
2020-02-03 04:18:15 +00:00
#if defined(__LIBRETRO__)
inittime = cpu_features_get_time_usec();
lastcount = initcount;
lasttime = inittime;
#endif
2019-12-10 08:59:05 +00:00
}
int64_t NP2_TickCount_GetCountFromInit(void) {
return NP2_TickCount_GetCount() - initcount;
}
2020-04-21 23:40:44 +00:00
#if !defined(_WINDOWS) && defined(SUPPORT_NP2_TICKCOUNT)
BOOL QueryPerformanceCounter(LARGE_INTEGER* count) {
int64_t icount = NP2_TickCount_GetCount();
COPY64(count, &icount);
return TRUE;
}
BOOL QueryPerformanceFrequency(LARGE_INTEGER* freq) {
int64_t ifreq = NP2_TickCount_GetFrequency();
COPY64(freq, &ifreq);
return TRUE;
}
#endif
2020-04-17 02:27:01 +00:00
#endif // SUPPORT_NP2_TICKCOUNT