From acf1e18e8ffa7cd841c4abbc9f19d2799fcb33e1 Mon Sep 17 00:00:00 2001 From: Aydyn Tairov Date: Thu, 27 Jul 2023 15:38:45 +0100 Subject: [PATCH 1/2] remove second ifdefs for windows timing by introducing ported version of clock_gettime --- run.c | 6 ------ win.c | 8 ++++++++ win.h | 11 ++++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/run.c b/run.c index 80b0b00c..eb344e2d 100644 --- a/run.c +++ b/run.c @@ -365,15 +365,9 @@ int argmax(float* v, int n) { // ---------------------------------------------------------------------------- long time_in_ms() { -#if defined _WIN32 - // windows specific way to get time - return GetTickCount(); -#else - // linux specific way to get time struct timespec time; clock_gettime(CLOCK_REALTIME, &time); return time.tv_sec * 1000 + time.tv_nsec / 1000000; -#endif } int main(int argc, char *argv[]) { diff --git a/win.c b/win.c index 7f872653..cba8c0ca 100644 --- a/win.c +++ b/win.c @@ -176,3 +176,11 @@ int munlock(const void *addr, size_t len) return -1; } + +// Portable clock_gettime function for Windows +int clock_gettime(int clk_id, struct timespec *tp) { + DWORD ticks = GetTickCount(); + tp->tv_sec = ticks / 1000; + tp->tv_nsec = (ticks % 1000) * 1000000; + return 0; +} diff --git a/win.h b/win.h index 1a66a837..11345d8f 100644 --- a/win.h +++ b/win.h @@ -3,6 +3,7 @@ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include +#include // Below code is originally from mman-win32 @@ -12,9 +13,9 @@ * mman-win32 */ -#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. -#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. -#endif +#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. +#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. +#endif /* All the headers include this file. */ #ifndef _MSC_VER @@ -47,12 +48,16 @@ extern "C" { #define MS_SYNC 2 #define MS_INVALIDATE 4 +/* Flags for portable clock_gettime call. */ +#define CLOCK_REALTIME 0 + void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off); int munmap(void *addr, size_t len); int mprotect(void *addr, size_t len, int prot); int msync(void *addr, size_t len, int flags); int mlock(const void *addr, size_t len); int munlock(const void *addr, size_t len); +int clock_gettime(int clk_id, struct timespec *tp); #ifdef __cplusplus }; From 343572675f502656798e1af7ff43f3c1b1594fc7 Mon Sep 17 00:00:00 2001 From: Aydyn Tairov Date: Thu, 27 Jul 2023 16:30:22 +0100 Subject: [PATCH 2/2] minor whitespaces cleanup --- win.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/win.h b/win.h index 11345d8f..12c7f398 100644 --- a/win.h +++ b/win.h @@ -13,9 +13,9 @@ * mman-win32 */ -#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. -#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. -#endif +#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. +#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. +#endif /* All the headers include this file. */ #ifndef _MSC_VER