哪个内联汇编代码对于 rdtscp 是正确的? [英] Which inline assembly code is correct for rdtscp?

查看:32
本文介绍了哪个内联汇编代码对于 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);
}

我注意到这个版本中没有破坏"的东西.我不知道这是否是一个问题......我想这取决于编译器是否内联函数.使用这个版本给我带来了并不总是可重现的问题.

I notice there is no 'clobbering' stuff in this version. Whether or not this is a problem I don't know... I suppose it depends if the compiler inlines the function or not. Using this version causes me problems that aren't always reproducible.

我找到的下一个版本是

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.

相信第一个版本有问题的原因是它覆盖了以前保存函数参数的寄存器.

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++ 代码将返回 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
" : "=a" (rax), "=d" (rdx), "=c" (aux) : : );
    return (rdx << 32) + rax;
}

最好在 C++ 语句中执行 shiftadd 来合并两个 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.

这篇关于哪个内联汇编代码对于 rdtscp 是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆