source: trunk/lib/perflib/i386_timer.h @ 1218

Last change on this file since 1218 was 462, checked in by cameron, 9 years ago

perflib and block_carry.h

File size: 967 bytes
Line 
1#ifndef I386_TIMER_H
2#define I386_TIMER_H
3
4#ifdef _MSC_VER
5#include <intrin.h>
6#pragma intrinsic(__rdtsc)
7#pragma intrinsic(_BitScanReverse)
8#endif
9
10#define cycle_counter_units "cyc"
11
12typedef unsigned long long int timestamp_t;
13
14static inline timestamp_t read_cycle_counter () {
15#ifdef __GNUC__
16  timestamp_t ts;
17#ifdef __x86_64__
18  unsigned int eax, edx;
19  asm volatile("rdtsc" : "=a" (eax), "=d" (edx));
20  ts = ((timestamp_t) eax) | (((timestamp_t) edx) << 32);
21#else
22  asm volatile("rdtsc\n" : "=A" (ts)); 
23#endif
24  return(ts);
25#endif
26#ifdef _MSC_VER
27  return __rdtsc();
28#endif
29}
30
31static inline int binary_order_of_magnitude(timestamp_t ts) {
32#if __GNUC__
33  return 64 - __builtin_clzll(ts);
34#endif
35#ifdef _MSC_VER
36   unsigned long index;
37   if (_BitScanReverse(&index, (uint32_t)(ts&0xFFFFFFFF)))
38        return index;
39   else if(_BitScanReverse(&index, (uint32_t)(ts>>32)))
40        return 32 + index;
41   else return 64;
42#endif
43}
44
45#endif
46
Note: See TracBrowser for help on using the repository browser.