Skip to content

Commit

Permalink
Adding support for arm mrc instruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlb6740 committed Jul 24, 2020
1 parent 0cc0312 commit 7564a33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ edition = "2018"

[dependencies]
libc = "0.2"
time = "0.1.42"
chrono = "*"

[build-dependencies]
cc = "1"
Expand Down
9 changes: 9 additions & 0 deletions src/cpucounter.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include <stdint.h>

#if defined(__x86_64__) || defined(__amd64__)
uint64_t cpucounter(void)
{
uint64_t low, high;
__asm__ __volatile__ ("rdtscp" : "=a" (low), "=d" (high) : : "%ecx");
return (high << 32) | low;
}
#elif defined(__aarch64__)
uint64_t cpucounter(void)
{
uint64_t virtual_timer_value;
__asm__ __volatile__ ("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
return virtual_timer_value;
}
#endif
13 changes: 12 additions & 1 deletion src/cpucounter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use super::timestamp::*;

pub(crate) struct CPUCounter;

#[cfg(asm)]
#[inline]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe fn cpucounter() -> u64 {
let (low, high): (u64, u64);
asm!("rdtscp" : "={eax}" (low), "={edx}" (high) : : "ecx");
(high << 32) | low
}


// https://github.com/google/benchmark/blob/v1.1.0/src/cycleclock.h#L116
#[cfg(asm)]
#[inline]
#[cfg(any(target_arch = "aarch64"))]
unsafe fn cpucounter() -> u64 {
let (vtm): (u64);
asm!("mrs %0, cntvct_el0" : "=r"(vtm));
vtm
}

#[cfg(not(asm))]
extern "C" {
fn cpucounter() -> u64;
Expand Down

0 comments on commit 7564a33

Please # to comment.