时钟周期数问心无愧GCC [英] clock cycle count wth GCC
本文介绍了时钟周期数问心无愧GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Visual Studio中,我可以从处理器读出的时钟周期计数,如下所示。
我该怎么做同样的事情GCC?
的#ifdef _MSC_VER //编译器:微软的Visual Studio #IFDEF _M_IX86 //处理器:86 内联uint64_t中clockCycleCount()
{
uint64_t中℃;
__asm {
CPUID //序列处理器
RDTSC //读取时间戳计数器
MOV DWORD PTR [C + 0],EAX
MOV DWORD PTR [C + 4],EDX
}
返回℃;
} #elif指令定义(_M_X64)//处理器:64 为externC无符号__int64 __rdtsc();
内在的#pragma(__ RDTSC)
内联uint64_t中clockCycleCount()
{
返回__rdtsc();
} #万一#万一
解决方案
在最近版本的Linux gettimeofday的将纳入纳秒的时间安排。
如果你真的想叫你RDTSC可以使用下面的内联汇编:
<一个href=\"http://www.mcs.anl.gov/~kazutomo/rdtsc.html\">http://www.mcs.anl.gov/~kazutomo/rdtsc.html
#如果定义(__ i386__)静态__inline__无符号长长RDTSC(无效)
{
无符号长长整型X;
__asm__挥发性(.BYTE为0x0F,0X31:= A(X));
返回X;
}#elif指令定义(__ x86_64__)静态__inline__无符号长长RDTSC(无效)
{
无符号HI,LO;
__asm__ __volatile__(RDTSC:= A(LO),= D(HI));
返回((无符号很长很长)LO)|(((无符号很长很长)喜)LT;&LT; 32);
}#万一
With Visual Studio I can read the clock cycle count from the processor as shown below. How do I do the same thing with GCC?
#ifdef _MSC_VER // Compiler: Microsoft Visual Studio
#ifdef _M_IX86 // Processor: x86
inline uint64_t clockCycleCount()
{
uint64_t c;
__asm {
cpuid // serialize processor
rdtsc // read time stamp counter
mov dword ptr [c + 0], eax
mov dword ptr [c + 4], edx
}
return c;
}
#elif defined(_M_X64) // Processor: x64
extern "C" unsigned __int64 __rdtsc();
#pragma intrinsic(__rdtsc)
inline uint64_t clockCycleCount()
{
return __rdtsc();
}
#endif
#endif
解决方案
On recent versions of Linux gettimeofday will incorporate nanosecond timings.
If you really want to call RDTSC you can use the following inline assembly:
http://www.mcs.anl.gov/~kazutomo/rdtsc.html
#if defined(__i386__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#endif
这篇关于时钟周期数问心无愧GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文