expression
Guard native SIMD with a scalar fallback
The x86-64 SSE2 integer addition intrinsic adds four 32-bit lanes with wrapping semantics. Conditional compilation excludes the native implementation on other architectures; runtime feature detection guards the call. Unaligned loads and stores still require valid full-width memory. Both paths use identical wrapping arithmetic. This candidate does not prove a speedup or actual instruction selection. Expected result (NOT RUN): [0, 7, 9, 11] Common mistake: Calling a target-feature function without its CPU precondition, assuming unaligned means unbounded, or mixing wrapping with checked arithmetic. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
#[target_feature(enable = "sse2")] and is_x86_feature_detected!("sse2")Working Example
Example Snippetrust
fn scalar(a: [u32;4], b: [u32;4]) -> [u32;4] { std::array::from_fn(|i| a[i].wrapping_add(b[i]))}#[cfg(target_arch = "x86_64")]#[target_feature(enable = "sse2")]unsafe fn native(a: [u32;4], b: [u32;4]) -> [u32;4] { use std::arch::x86_64::{__m128i, _mm_loadu_si128, _mm_add_epi32, _mm_storeu_si128}; let mut out = [0u32;4]; // SAFETY: all arrays contain 16 valid bytes; unaligned operations are allowed. // Caller guarantees SSE2 support; output storage is distinct from the inputs. unsafe { let x = _mm_loadu_si128(a.as_ptr().cast::<__m128i>()); let y = _mm_loadu_si128(b.as_ptr().cast::<__m128i>()); _mm_storeu_si128(out.as_mut_ptr().cast::<__m128i>(), _mm_add_epi32(x,y)); } out}fn add(a: [u32;4], b: [u32;4]) -> [u32;4] { #[cfg(target_arch = "x86_64")] if std::arch::is_x86_feature_detected!("sse2") { // SAFETY: the CPU feature has been checked. return unsafe { native(a,b) }; } scalar(a,b)}fn main() { let a = [u32::MAX, 2, 3, 4]; let b = [1, 5, 6, 7]; let out = add(a,b); assert_eq!(out,scalar(a,b)); println!("{out:?}");}