Skip to content

Commit

Permalink
Updated zentimer.h to work on Windows 11
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Mar 18, 2022
1 parent e8a3510 commit 2d6c9b4
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions zentimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
#ifdef ENABLE_ZENTIMER

#include <stdio.h>
#ifdef WIN32
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#include <time.h>
#else
#include <sys/time.h>
#endif
Expand All @@ -51,25 +52,26 @@ extern "C" {
#endif /* __cplusplus */

#define ZTIME_USEC_PER_SEC 1000000
#define ZTIME_NSEC_PER_USEC 1000

/* ztime_t represents usec */
typedef uint64_t ztime_t;

#ifdef WIN32
static uint64_t ztimer_freq = 0;
#endif

static void
ztime (ztime_t *ztimep)
{
#ifdef WIN32
QueryPerformanceCounter ((LARGE_INTEGER *) ztimep);
#ifdef HAVE_WINDOWS_H
struct timespec ts;

timespec_get (&ts, TIME_UTC);

*ztimep = (((uint64_t) ts.tv_sec) * ZTIME_USEC_PER_SEC) + (ts.tv_nsec * ZTIME_NSEC_PER_USEC);
#else
struct timeval tv;

gettimeofday (&tv, NULL);
*ztimep = ((uint64_t) tv.tv_sec * ZTIME_USEC_PER_SEC) + tv.tv_usec;

*ztimep = (((uint64_t) tv.tv_sec) * ZTIME_USEC_PER_SEC) + tv.tv_usec;
#endif
}

Expand Down Expand Up @@ -139,17 +141,8 @@ ZenTimerResume (ztimer_t *ztimer)
static double
ZenTimerElapsed (ztimer_t *ztimer, uint64_t *usec)
{
#ifdef WIN32
static uint64_t freq = 0;
ztime_t delta, stop;

if (freq == 0)
QueryPerformanceFrequency ((LARGE_INTEGER *) &freq);
#else
static uint64_t freq = ZTIME_USEC_PER_SEC;
ztime_t delta, stop;
#endif

ztimer = ztimer ? ztimer : &__ztimer;

if (ztimer->state != ZTIMER_ACTIVE)
Expand All @@ -160,9 +153,9 @@ ZenTimerElapsed (ztimer_t *ztimer, uint64_t *usec)
delta = stop - ztimer->start;

if (usec != NULL)
*usec = (uint64_t) (delta * ((double) ZTIME_USEC_PER_SEC / (double) freq));
*usec = (uint64_t) delta;

return (double) delta / (double) freq;
return (double) delta / (double) ZTIME_USEC_PER_SEC;
}

static void
Expand Down

0 comments on commit 2d6c9b4

Please sign in to comment.