rust-cookbook
rust-cookbook copied to clipboard
Add SIMD examples in section Processor
trafficstars
Demonstrate a very simple way to implement code to use simd in https://rust-lang-nursery.github.io/rust-cookbook/hardware/processor.html Demonstrate especially the macros and function about processor feature detection and fallback and a simple use of simd, rather that an advanced use of the feature.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "avx2"))]
fn foo() {
#[cfg(target_arch = "x86")]
use std::arch::x86::_mm256_add_epi64;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::_mm256_add_epi64;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if is_x86_feature_detected!("avx2") {
return unsafe { foo_avx2() };
}
}
References: https://github.com/rust-lang/rfcs/blob/master/text/2325-stable-simd.md https://blog.rust-lang.org/2018/06/21/Rust-1.27.html