From 7046d6288ec47329bf612df816fd75a6db64d443 Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Sun, 12 May 2024 17:42:45 +0200 Subject: [PATCH 1/2] auto-detect cpu features for sha3 crate --- libcrux-sha3/build.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libcrux-sha3/build.rs diff --git a/libcrux-sha3/build.rs b/libcrux-sha3/build.rs new file mode 100644 index 000000000..f15f3d581 --- /dev/null +++ b/libcrux-sha3/build.rs @@ -0,0 +1,26 @@ +use std::env; + +fn main() { + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + let disable_simd128 = match env::var("LIBCRUX_DISABLE_SIMD128") { + Ok(s) => s == "1" || s == "y" || s == "Y", + Err(_) => false, + }; + + let disable_simd256 = match env::var("LIBCRUX_DISABLE_SIMD256") { + Ok(s) => s == "1" || s == "y" || s == "Y", + Err(_) => false, + }; + + if target_arch == "aarch64" && !disable_simd128 { + // We enable simd128 on all aarch64 builds. + println!("cargo:rustc-cfg=feature=\"simd128\""); + } + if (target_arch == "x86" || target_arch == "x86_64") && !disable_simd256 { + // We enable simd256 on all x86 and x86_64 builds. + // Note that this doesn't mean the required CPU features are available. + // But the compiler will support them and the runtime checks ensure that + // it's only used when available. + println!("cargo:rustc-cfg=feature=\"simd256\""); + } +} From b218ed29bbc08d6716ffcdd67dd259220e8e6103 Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Sun, 12 May 2024 19:13:01 +0200 Subject: [PATCH 2/2] don't try to use simd256 on 32-bit --- libcrux-sha3/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcrux-sha3/build.rs b/libcrux-sha3/build.rs index f15f3d581..a5dce81b2 100644 --- a/libcrux-sha3/build.rs +++ b/libcrux-sha3/build.rs @@ -16,7 +16,7 @@ fn main() { // We enable simd128 on all aarch64 builds. println!("cargo:rustc-cfg=feature=\"simd128\""); } - if (target_arch == "x86" || target_arch == "x86_64") && !disable_simd256 { + if target_arch == "x86_64" && !disable_simd256 { // We enable simd256 on all x86 and x86_64 builds. // Note that this doesn't mean the required CPU features are available. // But the compiler will support them and the runtime checks ensure that