在ARM中是否有与rdtsc等效的指令? [英] Is there an equivalent instruction to rdtsc in ARM?

查看:958
本文介绍了在ARM中是否有与rdtsc等效的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的项目我必须使用内联汇编指令(例如 rdtsc )来计算某些C/C ++指令的执行时间.

For my project I must use inline assembly instructions such as rdtsc to calculate the execution time of some C/C++ instructions.

以下代码似乎可以在Intel上运行,但不能在ARM处理器上运行:

The following code seems to work on Intel but not on ARM processors:

{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处理器上工作?

How to write an inline assembly code similar to the above (to calculate the execution elapsed time of an instruction) to work on ARM processors?

推荐答案

您应该阅读协处理器p15PMCCNTR寄存器(不是实际的协处理器,只是CPU功能的入口点),获取周期数.请注意,只有在以下情况下,它才可用于非特权应用程序:

You should read the PMCCNTR register of a co-processor p15 (not an actual co-processor, just an entry point for CPU functions) to obtain a cycle count. Note that it is available to an unprivileged app only if:

  1. 允许非特权PMCCNTR读取:

PMUSERENR寄存器的位0必须设置为1(

Bit 0 of PMUSERENR register must be set to 1 (official docs)

PMCCNTR实际上是在计算周期:

PMCCNTR is actually counting cycles:

PMCNTENSET寄存器的第31位必须设置为1(

Bit 31 of PMCNTENSET register must be set to 1 (official docs)

这是真实示例如何完成.

这篇关于在ARM中是否有与rdtsc等效的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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