Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 25, 2019 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/44581c76685e1336b11869bfafead518 to your computer and use it in GitHub Desktop.
Save rust-play/44581c76685e1336b11869bfafead518 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::arch::x86_64::{__m256i, _mm256_set_epi32};
use std::arch::x86_64::_mm256_or_si256 as op_or;
use std::arch::x86_64::_mm256_srli_si256 as op_shift;
use std::arch::x86_64::_mm256_extract_epi32 as extract_i32;
#[allow(non_snake_case)]
unsafe fn or_collapse_to_u32(accumulator: __m256i) -> u32 {
let a___b___c___d___ = accumulator;
let ________a___b___ = op_shift(a___b___c___d___, 8);
let a___b___ac__bd__ = op_or(a___b___c___d___, ________a___b___);
let ____a___b___ac__ = op_shift(a___b___ac__bd__, 4);
let a___ab__abc_abcd = op_or(a___b___ac__bd__,____a___b___ac__);
let abcd = extract_i32(a___ab__abc_abcd, 0);
let efgh = extract_i32(a___ab__abc_abcd, 4);
(abcd|efgh) as u32
}
fn main() {
unsafe {
let m256 = _mm256_set_epi32(8,128,1,64,32,4,16,2);
assert_eq!(or_collapse_to_u32(m256), 255);
let m256 = _mm256_set_epi32(8,128,1,64,0,4,16,2);
assert_eq!(or_collapse_to_u32(m256), 255-32);
let a = [0,0,0,0,0,0,0,1,0,0,0,0,0,0,0];
for i in 0..8 {
for j in 0..32 {
let a = a[i..i+8].into_iter().map(|v| v<<j).collect::<Vec<i32>>();
let m256 = _mm256_set_epi32(a[0],a[1],a[2],a[3],
a[4],a[5],a[6],a[7]);
assert_eq!(or_collapse_to_u32(m256), 1<<j);
let m256 = _mm256_set_epi32(a[0] | 18_000_000,a[1],a[2] | 400_000_000,a[3],
a[4],a[5] | 1_000_000_000,a[6],a[7] | 19_000);
assert_eq!(or_collapse_to_u32(m256),
1<<j | 18_000_000 | 400_000_000 | 1_000_000_000 | 19_000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment