其中内联汇编code是rdtscp正确的? [英] Which inline assembly code is correct for rdtscp?
问题描述
免责声明:无法用言语形容我是多么讨厌AT& T公司风格的语法
Disclaimer: Words cannot describe how much I detest AT&T style syntax
我有,我希望是由寄存器重挫造成的一个问题。如果不是这样,我有一个更大的问题。
I have a problem that I hope is caused by register clobbering. If not, I have a much bigger problem.
我用的第一个版本是
static unsigned long long rdtscp(void)
{
unsigned int hi, lo;
__asm__ __volatile__("rdtscp" : "=a"(lo), "=d"(hi));
return (unsigned long long)lo | ((unsigned long long)hi << 32);
}
我注意到没有'重挫'的东西,在这个版本。不管这是我不知道的一个问题...我想这取决于如果编译器内联函数或不。使用此版本使我的问题的并不总是重现性的
下一个版本,我发现是
static unsigned long long rdtscp(void)
{
unsigned long long tsc;
__asm__ __volatile__(
"rdtscp;"
"shl $32, %%rdx;"
"or %%rdx, %%rax"
: "=a"(tsc)
:
: "%rcx", "%rdx");
return tsc;
}
这是令人放心的读取和官方政策,但就像我说我的问题并不总是重复性的,所以我只是想排除我的问题的一个可能的原因。
This is reassuringly unreadable and official looking, but like I said my issue isn't always reproducible so I'm merely trying to rule out one possible cause of my problem.
我的认为其原因的第一个版本是一个问题是,它是一个覆盖寄存器previously举行的函数参数。
The reason I believe the first version is a problem is that it is overwriting a register that previously held a function parameter.
什么是正确的...... 1版本,或版本2,或两者兼而有之?
What's correct... version 1, or version 2, or both?
推荐答案
下面是C ++ code,它会返回TSC和存储辅助32位到基准参数
Here's C++ code that will return the TSC and store the auxiliary 32-bits into the reference parameter
static inline uint64_t rdtscp( uint32_t & aux )
{
uint64_t rax,rdx;
asm volatile ( "rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : : );
return (rdx << 32) + rax;
}
这是更好地做移
和添加
合并使用C 32位半++语句,而不是内联,这使编译器来安排这些指令,因为它认为合适的。
It is better to do the shift
and add
to merge both 32-bit halves in C++ statement rather than inline, this allows the compiler to schedule those instructions as it sees fit.
这篇关于其中内联汇编code是rdtscp正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!