Cur*_*ous 10 c c++ assembly arm inline-assembly
对于我的项目,我必须使用内联汇编指令(如rdtsc)来计算某些C/C++指令的执行时间.
以下代码似乎适用于英特尔,但不适用于ARM处理器:
{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t0 = ((unsigned long)a) | (((unsigned long)d) << 32);}
//The C++ statement to measure its execution time
{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t1 = ((unsigned long)a) | (((unsigned long)d) << 32);}
time = t1-t0;
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如何在ARM处理器上编写类似于上面的内联汇编代码(计算指令的执行经过时间)?
axi*_*qia 12
对于 Arm64,系统寄存器CNTVCT_EL0可用于从用户空间检索计数器。
// SPDX-License-Identifier: GPL-2.0
u64 rdtsc(void)
{
u64 val;
/*
* According to ARM DDI 0487F.c, from Armv8.0 to Armv8.5 inclusive, the
* system counter is at least 56 bits wide; from Armv8.6, the counter
* must be 64 bits wide. So the system counter could be less than 64
* bits wide and it is attributed with the flag 'cap_user_time_short'
* is true.
*/
asm volatile("mrs %0, cntvct_el0" : "=r" (val));
return val;
}
Run Code Online (Sandbox Code Playgroud)
请参阅此补丁https://lore.kernel.org/patchwork/patch/1305380/了解更多详细信息。