From c553689ebfcb87c776d5b49034308b5cd14a1a3d Mon Sep 17 00:00:00 2001 From: Robert Christensen Date: Mon, 10 Apr 2023 10:05:24 -0600 Subject: [PATCH] Update to use standardized asm macro Signed-off-by: Robert Christensen --- build.rs | 6 ++++-- src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/build.rs b/build.rs index 06018fe..aa29497 100644 --- a/build.rs +++ b/build.rs @@ -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"); } _ => {} @@ -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""#); } diff --git a/src/lib.rs b/src/lib.rs index 3d8a6b7..4b0f4ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ #![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 { @@ -9,7 +10,7 @@ pub fn ticks() -> u64 { let high: u64; let low: u64; unsafe { - #[cfg(not(feature = "OLD_ASM"))] + #[cfg(feature = "LLVM_ASM")] { llvm_asm!("lfence;rdtsc" : "={edx}"(high), "={eax}"(low) @@ -18,7 +19,6 @@ pub fn ticks() -> u64 { : "volatile" ); } - #[cfg(feature = "OLD_ASM")] { asm!("lfence;rdtsc" @@ -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) } @@ -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) @@ -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) } @@ -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) @@ -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) } @@ -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) @@ -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; @@ -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) @@ -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; @@ -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) @@ -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;