Created
May 15, 2018 18:09
-
-
Save spacekookie/11b5b395a3006de2ba8a2fe83f71e6b6 to your computer and use it in GitHub Desktop.
FizzBuzz SIMD Edition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate core; | |
use core::arch::x86_64::*; | |
#[inline] | |
fn simdset(a: u32) -> __m256 { | |
unsafe { | |
_mm256_setr_ps( | |
a as f32 + 0.0, | |
a as f32 + 1.0, | |
a as f32 + 2.0, | |
a as f32 + 3.0, | |
a as f32 + 4.0, | |
a as f32 + 5.0, | |
a as f32 + 6.0, | |
a as f32 + 7.0, | |
) | |
} | |
} | |
fn main() { | |
let store: [f32; 8] = [0.0; 8]; | |
let store3p: [f32; 8] = [0.0; 8]; | |
let store5p: [f32; 8] = [0.0; 8]; | |
(0..10).for_each(|i| { | |
let simd = simdset(i); | |
let rslt3 = unsafe { _mm256_div_ps(simd, _mm256_set1_ps(3.0)) }; | |
let comp3 = unsafe { _mm256_cmp_ps(rslt3, _mm256_floor_ps(rslt3), 0) }; | |
let rslt5 = unsafe { _mm256_div_ps(simd, _mm256_set1_ps(5.0)) }; | |
let comp5 = unsafe { _mm256_cmp_ps(rslt5, _mm256_floor_ps(rslt5), 0) }; | |
unsafe { _mm256_store_ps(store3p.as_ptr(), comp3) }; | |
unsafe { _mm256_store_ps(store.as_ptr(), simd) }; | |
unsafe { _mm256_store_ps(store5p.as_ptr(), comp5) }; | |
for j in 0..8 { | |
if store3p[j] == 0.0 || store5p[j] == 0.0 { | |
println!( | |
"{}{}", | |
if store3p[j] as u32 == 0 { "fizz" } else { "" }, | |
if store5p[j] as u32 == 0 { "buzz" } else { "" } | |
); | |
continue; | |
} | |
println!("{}", store[j]); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment