Skip to content

Instantly share code, notes, and snippets.

@kulp
Created January 4, 2013 16:38
Show Gist options
  • Save kulp/4453946 to your computer and use it in GitHub Desktop.
Save kulp/4453946 to your computer and use it in GitHub Desktop.
Intel assembly timer routines for gcc inline asm
#include <stdint.h>
// from http://download.intel.com/embedded/software/IA/324264.pdf
#define rdtscp_before() \
({ uint32_t cycles_high, cycles_low; \
__asm__ volatile ("CPUID\n\t" \
"RDTSC\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: \
"%rax", "%rbx", "%rcx", "%rdx"); \
(uint64_t)((uint64_t)cycles_high << 32) + cycles_low; })
#define rdtscp_after() \
({ uint32_t cycles_high, cycles_low; \
__asm__ volatile ( "RDTSC\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t" \
"CPUID\n\t" : "=r" (cycles_high), "=r" (cycles_low):: \
"%rax", "%rbx", "%rcx", "%rdx"); \
(uint64_t)((uint64_t)cycles_high << 32) + cycles_low; })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment