singularity

/* The std::arch module allows using CPU-specific intrinsics,
which are functions or instructions that are directly supported by
the CPU architecture. This example specifically uses SIMD (Single
Instruction, Multiple Data) intrinsics available in x86_64
architecture. */

use std::arch::x86_64::{__m128i, _mm_add_epi32, _mm_set_epi32};
/*
_mm_add_epi32 is an intrinsic that performs addition on "packed" 32-bit integers simultaneously
_mm_set_epi32 creates a SIMD vector
_mm_add_epi32 adds corresponding elements of the two vectors 
*/

fn main() {
    #[cfg(target_arch = "x86_64")]
	// conditional compilation flag
    unsafe {
        let a = _mm_set_epi32(1, 2, 3, 4);
        let b = _mm_set_epi32(5, 6, 7, 8);
		
        let result: __m128i = _mm_add_epi32(a, b);
		
        let result_array: [i32; 4] = std::mem::transmute(result);
	/* result is a SIMD vector that needs to be converted (transmuted)
	back into an array to be used in standard Rust code. 
	the elements are in reverse order due to how _mm_set_epi32 works */
	
        println!("{:?}", result_array);
    }

    #[cfg(not(target_arch = "x86_64"))]
    println!("This code only runs on x86_64 architectures.");
}