Skip to content

Commit

Permalink
Only enable simd on targets which feature SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Oct 28, 2020
1 parent 9d3752e commit cf049c4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ fn main() {
enable_libc();
}

// This adds various simd cfgs if this compiler supports it.
// This adds various simd cfgs if this compiler and target support it.
//
// This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but
// this is generally only intended for testing.
//
// On targets which don't feature SSE2, this is disabled, as LLVM wouln't know
// how to work with SSE2 operands. Enabling SSE4.2 and AVX on SSE2-only targets
// is not a problem. In that case, the fastest option will be chosen at
// runtime.
fn enable_simd_optimizations() {
if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") {
if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD")
|| !target_has_feature("sse2")
{
return;
}
println!("cargo:rustc-cfg=memchr_runtime_simd");
Expand Down Expand Up @@ -59,3 +66,9 @@ fn is_feature_set(name: &str) -> bool {
fn is_env_set(name: &str) -> bool {
env::var_os(name).is_some()
}

fn target_has_feature(feature: &str) -> bool {
env::var("CARGO_CFG_TARGET_FEATURE")
.map(|features| features.contains(feature))
.unwrap_or(false)
}

0 comments on commit cf049c4

Please sign in to comment.