Created
October 15, 2020 01:42
-
-
Save mayeths/77f7acc850b7032982e94acbbc6df654 to your computer and use it in GitHub Desktop.
C header including Timer, Unroll, etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdint.h> | |
/* -------------------- UNROLL ---------------------- */ | |
/* DO NOT use unroll without braces. */ | |
#define X5(inst) inst inst inst inst inst | |
#define X10(inst) X5(inst) X5(inst) | |
#define X50(inst) X10(inst) X10(inst) X10(inst) X10(inst) X10(inst) | |
#define X100(inst) X50(inst) X50(inst) | |
#define X500(inst) X100(inst) X100(inst) X100(inst) X100(inst) X100(inst) | |
#define X1000(inst) X500(inst) X500(inst) | |
#define X2(inst) inst inst | |
#define X4(inst) X2(inst) X2(inst) | |
#define X8(inst) X4(inst) X4(inst) | |
/* --------------------- TIMER ----------------------- */ | |
/* Use TICK TOCK macro to count. */ | |
#define TICK(v) { v = high_resolution_timer_ns(); } | |
#define TOCK(v) { uint64_t w; w = high_resolution_timer_ns(); v = w - v; } | |
#if defined(__linux) | |
#define HAVE_POSIX_TIMER | |
#include <time.h> | |
#ifdef CLOCK_MONOTONIC | |
#define CLOCKID CLOCK_MONOTONIC | |
#else | |
#define CLOCKID CLOCK_REALTIME | |
#endif | |
#elif defined(__APPLE__) | |
#define HAVE_MACH_TIMER | |
#include <mach/mach_time.h> | |
#elif defined(_WIN32) | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#endif | |
static uint64_t high_resolution_timer_ns() { | |
static uint64_t is_init = 0; | |
#if defined(__linux) | |
static struct timespec linux_rate; | |
if (0 == is_init) { | |
clock_getres(CLOCKID, &linux_rate); | |
is_init = 1; | |
} | |
uint64_t now; | |
struct timespec spec; | |
clock_gettime(CLOCKID, &spec); | |
now = spec.tv_sec * 1.0e9 + spec.tv_nsec; | |
return now; | |
#elif defined(__APPLE__) | |
static mach_timebase_info_data_t info; | |
if (0 == is_init) { | |
mach_timebase_info(&info); | |
is_init = 1; | |
} | |
uint64_t now; | |
now = mach_absolute_time(); | |
now *= info.numer; | |
now /= info.denom; | |
return now; | |
#elif defined(_WIN32) | |
static LARGE_INTEGER win_frequency; | |
if (0 == is_init) { | |
QueryPerformanceFrequency(&win_frequency); | |
is_init = 1; | |
} | |
LARGE_INTEGER now; | |
QueryPerformanceCounter(&now); | |
return (uint64_t) ((1e9 * now.QuadPart) / win_frequency.QuadPart); | |
#endif | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment