Skip to content

Instantly share code, notes, and snippets.

@danielsuo
Created April 14, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsuo/03939b547e0aca0b60012553eda2108d to your computer and use it in GitHub Desktop.
Save danielsuo/03939b547e0aca0b60012553eda2108d to your computer and use it in GitHub Desktop.
#pragma once
#include <time.h> /* for clock_gettime */
#include <error.h>
#include <stdio.h>
#include <stdlib.h> /* for exit */
#include <stdarg.h>
#include <inttypes.h>
#define MILLION 1000000L
#define BILLION 1000000000L
#define rdtsc(lo, hi) __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
#define hilo2llu(lo, hi) ((unsigned long long)lo) | (((unsigned long long)hi) << 32)
#define timediff(a, b) BILLION * (b.tv_sec - a.tv_sec) + b.tv_nsec - a.tv_nsec
// UNHOLY MACROS FOLLOW
// TODO: replace with perfect forwarding
#define get_elapsed_cycles(result, args...) \
do { \
uint32_t start_lo, start_hi, end_lo, end_hi; \
rdtsc(start_lo, start_hi); \
args \
rdtsc(end_lo, end_hi); \
uint64_t start = hilo2llu(start_lo, start_hi); \
uint64_t end = hilo2llu(end_lo, end_hi); \
result = end - start; \
} while (0)
#define get_elapsed_time(result, args...) \
do { \
struct timespec start, end;\
clock_gettime(CLOCK_MONOTONIC, &start); \
args \
clock_gettime(CLOCK_MONOTONIC, &end); \
result = timediff(start, end); \
} while (0)
void spin(uint64_t ns) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
end = start;
while (timediff(start, end) < ns) {
clock_gettime(CLOCK_MONOTONIC, &end);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment