Skip to content

Commit

Permalink
Update inline assembly to support new syntax (#2)
Browse files Browse the repository at this point in the history
* Update inline assembly for x86 to support new syntax

https://github.com/rust-lang/rfcs/blob/master/text/2843-llvm-asm.md
rust-lang/rfcs#2873

* Add support for Arm
  • Loading branch information
jlb6740 authored Jul 26, 2020
1 parent 027dcea commit c9f2bce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/cpucounter.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#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");
__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
12 changes: 11 additions & 1 deletion src/cpucounter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ 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);
llvm_asm!("rdtscp" : "={eax}" (low), "={edx}" (high) : : "ecx");
asm!("rdtscp", out("eax") low, out("edx") high, out("ecx") _);
(high << 32) | low
}

#[cfg(asm)]
#[inline]
#[cfg(any(target_arch = "aarch64"))]
unsafe fn cpucounter() -> u64 {
let (vtm): (u64);
asm!("mrs {}, cntvct_el0", out(reg) vtm);
vtm
}

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

0 comments on commit c9f2bce

Please sign in to comment.