Skip to content

Commit 7564a33

Browse files
committed
Adding support for arm mrc instruction.
Timing reference is here: https://github.com/google/benchmark/blob/v1.1.0/src/cycleclock.h#L116
1 parent 0cc0312 commit 7564a33

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ edition = "2018"
1111

1212
[dependencies]
1313
libc = "0.2"
14+
time = "0.1.42"
15+
chrono = "*"
1416

1517
[build-dependencies]
1618
cc = "1"

src/cpucounter.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
#include <stdint.h>
22

3+
#if defined(__x86_64__) || defined(__amd64__)
34
uint64_t cpucounter(void)
45
{
56
uint64_t low, high;
67
__asm__ __volatile__ ("rdtscp" : "=a" (low), "=d" (high) : : "%ecx");
78
return (high << 32) | low;
89
}
10+
#elif defined(__aarch64__)
11+
uint64_t cpucounter(void)
12+
{
13+
uint64_t virtual_timer_value;
14+
__asm__ __volatile__ ("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
15+
return virtual_timer_value;
16+
}
17+
#endif

src/cpucounter.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
use super::timestamp::*;
2-
32
pub(crate) struct CPUCounter;
43

54
#[cfg(asm)]
65
#[inline]
6+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
77
unsafe fn cpucounter() -> u64 {
88
let (low, high): (u64, u64);
99
asm!("rdtscp" : "={eax}" (low), "={edx}" (high) : : "ecx");
1010
(high << 32) | low
1111
}
1212

13+
14+
// https://github.com/google/benchmark/blob/v1.1.0/src/cycleclock.h#L116
15+
#[cfg(asm)]
16+
#[inline]
17+
#[cfg(any(target_arch = "aarch64"))]
18+
unsafe fn cpucounter() -> u64 {
19+
let (vtm): (u64);
20+
asm!("mrs %0, cntvct_el0" : "=r"(vtm));
21+
vtm
22+
}
23+
1324
#[cfg(not(asm))]
1425
extern "C" {
1526
fn cpucounter() -> u64;

0 commit comments

Comments
 (0)