Skip to content

Commit

Permalink
Detect CPU features at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Jan 5, 2024
1 parent 559b565 commit 570e986
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: cargo build --release

- name: Test
run: cargo test --release --lib
run: cargo test --release

build_test_x86_avx2:
name: Build & Test X86 AVX2
Expand All @@ -37,7 +37,7 @@ jobs:
run: cargo build --release

- name: Test
run: cargo test --release --lib
run: cargo test --release

build_test_arm:
name: Build & Test ARM
Expand All @@ -50,4 +50,4 @@ jobs:
run: cargo build --release

- name: Test
run: cargo test --release --lib
run: cargo test --release
15 changes: 15 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ extern crate rustc_version;
use rustc_version::{version_meta, Channel};

fn main() {

if !has_hardware_support() {
panic!("Hardware is not supported");
}

if version_meta().unwrap().channel == Channel::Nightly
&& cfg!(target_arch = "x86_64")
&& cfg!(target_feature = "avx2")
&& cfg!(target_feature = "vaes") {
println!("cargo:rustc-cfg=hybrid");
}
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn has_hardware_support() -> bool {
std::is_x86_feature_detected!("aes") && std::is_x86_feature_detected!("sse2")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn has_hardware_support() -> bool {
cfg!(all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "aes", target_feature = "neon"))
}
4 changes: 2 additions & 2 deletions src/gxhash/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "aes", target_feature = "neon"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[path = "arm.rs"]
mod platform;

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", target_feature = "sse2"))]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[path = "x86.rs"]
mod platform;

Expand Down

0 comments on commit 570e986

Please sign in to comment.