如何将此装配时间戳功能转换为C?
发布时间:2020-12-16 09:59:18 所属栏目:百科 来源:网络整理
导读:参见英文答案 Get CPU cycle count?????????????????????????????????????4个 我试图将别人的项目从32位转换为64位.除了一个函数外,一切似乎都没问题,它使用了构建x64时Visual Studio不支持的汇编表达式: // Returns the Read Time Stamp Counter of the CP
参见英文答案 >
Get CPU cycle count?????????????????????????????????????4个
我试图将别人的项目从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()内在函数. 文件on MSDN 对于Linux分支,内在函数可以使用相同的名称,但是您需要一个不同的头文件: #include <x86intrin.h> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |