The easy way, with multiversion
The multiversion
crate is helpful for automatically multiversioning functions.
A multiversioned function is one that’s compiled multiple times for any number of targets, with the optimal function selected at runtime.
use multiversion::multiversion;
#[multiversion(targets(
"x86_64+avx",
"x86_64+sse4.2",
"arm+neon",
))]
fn multiversioned() {
println!("This function uses whichever features are available")
}
Multiversion also supports automatically targeting all SIMD features on all architectures:
use multiversion::multiversion;
#[multiversion(targets = "simd")]
fn multiversioned() {
println!("This function automatically uses the best SIMD feature available")
}
For each target, a copy of the function is compiled with the appropriate target_feature
attributes.
At runtime, the optimal function is selected.