Skip to content

Commit

Permalink
Update to use standardized asm macro
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Christensen <robert.christensen@gmail.com>
  • Loading branch information
robertchristensen committed Apr 10, 2023
1 parent bd6bcb5 commit c553689
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
6 changes: 4 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
};

match version_info.channel {
Channel::Beta | Channel::Stable => {
Channel::Beta | Channel::Stable if version_info.semver.minor < 59 => {
panic!("this crate is not supported on the stable, or beta versions");
}
_ => {}
Expand All @@ -18,8 +18,10 @@ fn main() {
// determine the kind of asm to use
if version_info.semver.major > 1 {
panic!("please update this crate with the breaking rustc 2.0 changes.")
} else if version_info.semver.minor >= 59 {
// nothing to do. asm macro stabalized in version 1.59
} else if version_info.semver.minor >= 46 {
// nothing to do
println!(r#"cargo:rustc-cfg=feature="LLVM_ASM""#);
} else {
println!(r#"cargo:rustc-cfg=feature="OLD_ASM""#);
}
Expand Down
52 changes: 44 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "OLD_ASM"), feature(llvm_asm))]
#![cfg_attr(feature = "LLVM_ASM", feature(llvm_asm))]
#![cfg_attr(feature = "OLD_ASM", feature(asm))]


#[inline(never)]
#[cfg(target_arch = "x86_64")]
pub fn ticks() -> u64 {
let mask = 0xFFFFFFFFu64;
let high: u64;
let low: u64;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("lfence;rdtsc"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -18,7 +19,6 @@ pub fn ticks() -> u64 {
: "volatile"
);
}

#[cfg(feature = "OLD_ASM")]
{
asm!("lfence;rdtsc"
Expand All @@ -28,6 +28,12 @@ pub fn ticks() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("lfence;rdtsc",
out("edx") high,
out("eax") low)
}
}
((high) << 32) | (mask & low)
}
Expand All @@ -52,7 +58,7 @@ pub fn ticks_amd() -> u64 {
let high: u64;
let low: u64;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("mfence;rdtsc"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -70,6 +76,12 @@ pub fn ticks_amd() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("mfence;rdtsc",
out("edx") high,
out("eax") low)
}
}
((high) << 32) | (mask & low)
}
Expand All @@ -91,7 +103,7 @@ pub fn ticks_modern() -> u64 {
let high: u64;
let low: u64;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("rdtscp"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -109,6 +121,12 @@ pub fn ticks_modern() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("rdtscp",
out("edx") high,
out("eax") low)
}
}
((high) << 32) | (mask & low)
}
Expand All @@ -119,7 +137,7 @@ pub fn ticks() -> u64 {
let high: u32;
let low: u32;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("lfence;rdtsc"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -137,6 +155,12 @@ pub fn ticks() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("rdtscp",
out("edx") high,
out("eax") low)
}
}
let high_val = (high as u64) << 32;
let low_val = low as u64;
Expand All @@ -156,7 +180,7 @@ pub fn ticks_amd() -> u64 {
let high: u32;
let low: u32;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("mfence;rdtsc"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -174,6 +198,12 @@ pub fn ticks_amd() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("mfence;rdtsc",
out("edx") high,
out("eax") low)
}
}
let high_val = (high as u64) << 32;
let low_val = low as u64;
Expand All @@ -197,7 +227,7 @@ pub fn ticks_modern() -> u64 {
let high: u32;
let low: u32;
unsafe {
#[cfg(not(feature = "OLD_ASM"))]
#[cfg(feature = "LLVM_ASM")]
{
llvm_asm!("rdtscp"
: "={edx}"(high), "={eax}"(low)
Expand All @@ -215,6 +245,12 @@ pub fn ticks_modern() -> u64 {
: "volatile"
);
}
#[cfg(all(not(feature = "LLVM_ASM"), not(feature = "OLD_ASM")))]
{
core::arch::asm!("rdtscp",
out("edx") high,
out("eax") low)
}
}
let high_val = (high as u64) << 32;
let low_val = low as u64;
Expand Down

0 comments on commit c553689

Please # to comment.