如何将这个汇编时间戳函数转换为c++
How can I convert this assembly timestamp function to C++?
我正试图将别人的项目从32位转换为64位。一切似乎都很好,除了一个函数,它使用汇编表达式,在构建x64时不支持Visual Studio:
// Returns the Read Time Stamp Counter of the CPU
// The instruction returns in registers EDX:EAX the count of ticks from processor reset.
// Added in Pentium. Opcode: 0F 31.
int64_t CDiffieHellman::GetRTSC( void )
{
int tmp1 = 0;
int tmp2 = 0;
#if defined(WIN32)
__asm
{
RDTSC; // Clock cycles since CPU started
mov tmp1, eax;
mov tmp2, edx;
}
#else
asm( "RDTSC;nt"
"movl %%eax, %0;nt"
"movl %%edx, %1;"
:"=r"(tmp1),"=r"(tmp2)
:
:
);
#endif
return ((int64_t)tmp1 * (int64_t)tmp2);
}
最有趣的是,它被用来生成随机数。asm
块都不能在x64下编译,所以玩弄ifdef
是没有帮助的。我只需要找到C/c++的替代品,以避免重写整个程序。
对于Windows分支,
#include <intrin.h>
,调用__rdtsc()
内禀函数
MSDN文档
对于Linux分支,内部文件在相同的名称下可用,但您需要一个不同的头文件:
#include <x86intrin.h>
相关文章:
- 如何使用Rcpp将R函数转换为C++函数
- C++函数转换为 C# 函数
- 将 C 函数转换为 C++ 以检查数字是否有效
- C++:从重载函数转换为 std::function
- 将 C 函数转换为C++语言
- C++隐式构造函数转换,后跟类型向上转换
- 将 lambda 函数转换为具有混合 lambda 引入器和参数列表的函子结构
- 通过 Boost Python 将 Python 函数转换为 C++,用作回调
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- 如何将函数转换为 lambda 函数
- 自定义函数转换错误?
- 将 C# 哈希函数转换为C++
- 将 lambda 函数转换为另一个编译单元中的普通函数会缩短编译时间吗?
- 加快 R 性能或将 R 函数转换为C++函数
- 将成员函数转换为指向成员函数的指针
- 将MATLAB炒作函数转换为C
- Rcpp - 用二进制函数转换数字向量?
- C lambda函数转换误差
- 此语法中的构造函数转换错误
- 将函数转换为find_if lambda