Skip to content

Commit c9f2bce

Browse files
authored
Update inline assembly to support new syntax (#2)
* 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
1 parent 027dcea commit c9f2bce

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/cpucounter.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
#include <stdint.h>
22

3+
#if defined(__x86_64__) || defined(__amd64__)
34
uint64_t cpucounter(void)
45
{
56
uint64_t low, high;
6-
__asm__ __volatile__ ("rdtscp" : "=a" (low), "=d" (high) : : "%ecx");
7+
__asm__ __volatile__("rdtscp"
8+
: "=a"(low), "=d"(high)
9+
:
10+
: "%ecx");
711
return (high << 32) | low;
812
}
13+
#elif defined(__aarch64__)
14+
uint64_t cpucounter(void)
15+
{
16+
uint64_t virtual_timer_value;
17+
__asm__ __volatile__("mrs %0, cntvct_el0"
18+
: "=r"(virtual_timer_value));
19+
return virtual_timer_value;
20+
}
21+
#endif

src/cpucounter.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ pub(crate) struct CPUCounter;
66

77
#[cfg(asm)]
88
#[inline]
9+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
910
unsafe fn cpucounter() -> u64 {
1011
let (low, high): (u64, u64);
11-
llvm_asm!("rdtscp" : "={eax}" (low), "={edx}" (high) : : "ecx");
12+
asm!("rdtscp", out("eax") low, out("edx") high, out("ecx") _);
1213
(high << 32) | low
1314
}
1415

16+
#[cfg(asm)]
17+
#[inline]
18+
#[cfg(any(target_arch = "aarch64"))]
19+
unsafe fn cpucounter() -> u64 {
20+
let (vtm): (u64);
21+
asm!("mrs {}, cntvct_el0", out(reg) vtm);
22+
vtm
23+
}
24+
1525
#[cfg(not(asm))]
1626
extern "C" {
1727
fn cpucounter() -> u64;

0 commit comments

Comments
 (0)