Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect CPU features at build time #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@ extern crate rustc_version;
use rustc_version::{version_meta, Channel};

fn main() {

if let Err(e) = check_hardware_support() {
panic!("Gxhash build failed: {}", e);
}

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 check_hardware_support() -> Result<(), Box<dyn std::error::Error>> {
if !std::is_x86_feature_detected!("aes") {
return Result::Err("CPU feature 'aes' is required")?;
}
if !std::is_x86_feature_detected!("sse2") {
return Result::Err("CPU feature 'sse2' is required")?;
}
return Ok(());
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn check_hardware_support() -> Result<(), Box<dyn std::error::Error>> {
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) {
if cfg!(not(target_feature = "aes")) {
return Result::Err("CPU feature 'aes' is required")?;
}
if cfg!(not(target_feature = "neon")) {
return Result::Err("CPU feature 'neon' is required")?;
}
return Ok(());
}
return Result::Err(format!("Target arch '{}' is not supported", std::env::consts::ARCH))?;
}
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