在ARM中有与rdtsc等价的指令吗?
Is there an equivalent instruction to rdtsc in ARM?
对于我的项目,我必须使用内联汇编指令,如rdtsc来计算一些C/c++指令的执行时间。
下面的代码似乎可以在Intel处理器上工作,但不能在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;
我的问题是
如何编写与上面的(计算指令的执行时间)相似的内联汇编代码以在ARM处理器上工作?
您应该读取协处理器p15
(不是实际的协处理器,只是CPU函数的入口点)的PMCCNTR
寄存器以获得周期计数。请注意,只有当
-
允许非特权
PMCCNTR
读取:PMUSERENR
寄存器0位必须设置为1(官方文档) -
PMCCNTR
实际上在计算周期:PMCNTENSET
寄存器的第31位必须设置为1(官方文档)
这是一个真实世界的例子。
对于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;
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++:对不存在的命名空间使用命名空间指令
- 函数名是c中该函数的第一条指令的地址吗
- 使用rdtsc进行基准测试的缺点是什么
- 错误:无效的预处理指令 #i 的意思是 #if?
- 组装指令中乘法的下部和上部是什么
- OpenMP 与有序和关键指令并行
- C++中的移动分配出现问题.非法指令: 4.
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- C++CMake编译指令与
- 使用宏扩展的泛型:为什么指令缓存使用不当?
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- AVX 指令中寄存器和指针之间的客观差异
- while 循环 c++ 中的非法指令
- 如何在编译时定义C++预处理器指令的值?
- 存储指令是否会阻止缓存未命中的后续指令?
- 保证编译器指令在C++中重新排序
- VS2008中的AVX-512指令库
- 在ARM中有与rdtsc等价的指令吗?