diff --git a/build.rs b/build.rs index 20a8d92..77f70fc 100644 --- a/build.rs +++ b/build.rs @@ -17,6 +17,8 @@ fn cfg() { let ok_arch = matches!(&*target_arch, "x86" | "x86_64"); let sse4_2_guaranteed = target_feature.split(',').any(|f| f == "sse4.2"); + println!("cargo::rustc-check-cfg=cfg(jetscii_sse4_2, values(\"yes\", \"maybe\", \"no\"))"); + if sse4_2_guaranteed { println!(r#"cargo:rustc-cfg=jetscii_sse4_2="yes""#); } else if ok_arch { diff --git a/src/fallback.rs b/src/fallback.rs index 0f7b21b..1fc6ecc 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -26,7 +26,7 @@ pub struct ByteSubstring<'a> { } impl<'a> ByteSubstring<'a> { - pub /* const */ fn new(needle: &'a[u8]) -> Self { + pub const fn new(needle: &'a[u8]) -> Self { ByteSubstring { needle } } diff --git a/src/lib.rs b/src/lib.rs index 7c30098..a4b9acf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -292,7 +292,7 @@ pub struct ByteSubstring<'a> { } impl<'a> ByteSubstring<'a> { - pub /* const */ fn new(needle: &'a [u8]) -> Self { + pub const fn new(needle: &'a [u8]) -> Self { ByteSubstring { #[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))] simd: simd::ByteSubstring::new(needle), diff --git a/src/simd.rs b/src/simd.rs index b03a7c0..60dd347 100644 --- a/src/simd.rs +++ b/src/simd.rs @@ -267,12 +267,18 @@ pub struct ByteSubstring<'a> { } impl<'a> ByteSubstring<'a> { - pub /* const */ fn new(needle: &'a[u8]) -> Self { - use std::cmp; - + pub const fn new(needle: &'a[u8]) -> Self { let mut simd_needle = [0; 16]; - let len = cmp::min(simd_needle.len(), needle.len()); - simd_needle[..len].copy_from_slice(&needle[..len]); + let len = if simd_needle.len() < needle.len() { + simd_needle.len() + } else { + needle.len() + }; + let mut i = 0; + while i < len { + simd_needle[i] = needle[i]; + i += 1; + } ByteSubstring { complete_needle: needle, needle: unsafe { TransmuteToSimd { bytes: simd_needle }.simd },