From 5b9eb861876fc9a3614e628a2b7bfeca8f4853d0 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Sun, 22 Jan 2023 19:26:57 +0100 Subject: [PATCH 1/5] minor: make the _runtime_select function explicitly unsafe This might require that consumers of this crate need to wrap their _runtime_select function in a safe block --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0510b85..ae4af2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -658,7 +658,7 @@ macro_rules! simd_runtime_generate { $vis unsafe fn [<$fn_name _avx2>]($($arg:$typ,)*) $(-> $rt)? { $fn_name::($($arg,)*) } - $vis fn [<$fn_name _runtime_select>]($($arg:$typ,)*) $(-> $rt)? { + $vis unsafe fn [<$fn_name _runtime_select>]($($arg:$typ,)*) $(-> $rt)? { if is_x86_feature_detected!("avx2") { unsafe { [<$fn_name _avx2>]($($arg,)*) } } else if is_x86_feature_detected!("sse4.1") { From 8f0dbdf21a6aaef86d283b4f3e81551889ff8941 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Sun, 22 Jan 2023 19:28:27 +0100 Subject: [PATCH 2/5] minor: make the _compiletime_select function explicitly unsafe This might require that consumers of this crate need to wrap their _compiletime_select function in a safe block --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae4af2a..50a779a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -686,21 +686,21 @@ macro_rules! simd_compiletime_generate { paste::item! { #[cfg(target_feature = "avx2")] - $vis fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { + $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { unsafe { $fn_name::($($arg,)*) } } #[cfg(all(target_feature = "sse4.1",not(target_feature = "avx2")))] - $vis fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { + $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { unsafe { $fn_name::($($arg,)*) } } #[cfg(all(target_feature = "sse2",not(any(target_feature="sse4.1",target_feature = "avx2"))))] - $vis fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { + $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { unsafe { $fn_name::($($arg,)*) } } #[cfg(not(any(target_feature="sse4.1",target_feature = "avx2",target_feature="sse2")))] - $vis fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { + $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { unsafe { $fn_name::($($arg,)*) } } From 903e7b6e6a04df842a414f82d816616de18398ff Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Sun, 22 Jan 2023 19:37:47 +0100 Subject: [PATCH 3/5] patch: remove redundant unsafe blocks --- src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 50a779a..3dba819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -660,13 +660,13 @@ macro_rules! simd_runtime_generate { } $vis unsafe fn [<$fn_name _runtime_select>]($($arg:$typ,)*) $(-> $rt)? { if is_x86_feature_detected!("avx2") { - unsafe { [<$fn_name _avx2>]($($arg,)*) } + [<$fn_name _avx2>]($($arg,)*) } else if is_x86_feature_detected!("sse4.1") { - unsafe { [<$fn_name _sse41>]($($arg,)*) } + [<$fn_name _sse41>]($($arg,)*) } else if is_x86_feature_detected!("sse2") { - unsafe { [<$fn_name _sse2>]($($arg,)*) } + [<$fn_name _sse2>]($($arg,)*) } else { - unsafe { [<$fn_name _scalar>]($($arg,)*) } + [<$fn_name _scalar>]($($arg,)*) } } } @@ -687,21 +687,21 @@ macro_rules! simd_compiletime_generate { paste::item! { #[cfg(target_feature = "avx2")] $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { - unsafe { $fn_name::($($arg,)*) } + $fn_name::($($arg,)*) } #[cfg(all(target_feature = "sse4.1",not(target_feature = "avx2")))] $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { - unsafe { $fn_name::($($arg,)*) } + $fn_name::($($arg,)*) } #[cfg(all(target_feature = "sse2",not(any(target_feature="sse4.1",target_feature = "avx2"))))] $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { - unsafe { $fn_name::($($arg,)*) } + $fn_name::($($arg,)*) } #[cfg(not(any(target_feature="sse4.1",target_feature = "avx2",target_feature="sse2")))] $vis unsafe fn [<$fn_name _compiletime>]($($arg:$typ,)*) $(-> $rt)? { - unsafe { $fn_name::($($arg,)*) } + $fn_name::($($arg,)*) } From 420dbff149ea62ba5625e4ce69f530ade55cd373 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Sun, 22 Jan 2023 19:40:55 +0100 Subject: [PATCH 4/5] chore: update documentation to reflect that the generated functions are unsafe --- README.md | 2 +- src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fda4474..f2270b6 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ use simdeez::*; fn main() { } ``` -This will generate 5 functions for you: +This will generate 5 unsafe functions for you: * `distance` the generic version of your function * `distance_scalar` a scalar fallback * `distance_sse2` SSE2 version diff --git a/src/lib.rs b/src/lib.rs index 3dba819..442634d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,7 +121,7 @@ //! # } //! ``` //! -//! This will generate 5 functions for you: +//! This will generate 5 unsafe functions for you: //! * `distance` the generic version of your function //! * `distance_scalar` a scalar fallback //! * `distance_sse2` SSE2 version @@ -631,7 +631,7 @@ pub trait Simd: Sync + Send { /// * SSE41 (fn_name_sse41) /// * SSE2 (fn_name_sse2) /// * Scalar fallback (fn_name_scalar) -/// Finally, it also generates a function which will select at runtime the fastest version +/// Finally, it also generates a unsafe function which will select at runtime the fastest version /// from above that the cpu supports. (fn_name_runtime_select) #[macro_export] macro_rules! simd_runtime_generate { @@ -674,7 +674,7 @@ macro_rules! simd_runtime_generate { } -/// Generates a generic version of your function (fn_name) +/// Generates a generic unsafe version of your function (fn_name) /// And the fastest version supported by your rust compilation settings /// (fn_name_compiletime) #[macro_export] From ffff2212341013d4027e9f178ae934dbe173c709 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Sun, 22 Jan 2023 19:45:25 +0100 Subject: [PATCH 5/5] chore: improve examples by showing how to call the generated functions --- README.md | 3 +++ src/lib.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index f2270b6..68fa3fc 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,9 @@ use simdeez::*; result }); fn main() { + unsafe { + let _dist = distance_runtime_select(&0.0, &0.0, &10.0, &10.0); + } } ``` This will generate 5 unsafe functions for you: diff --git a/src/lib.rs b/src/lib.rs index 442634d..2ce2ffa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,9 @@ //! result //! }); //! # fn main() { +//! unsafe { +//! let _dist = distance_runtime_select(&0.0, &0.0, &10.0, &10.0); +//! } //! # } //! ``` //!