Last active
January 26, 2020 08:06
-
-
Save mooman219/2cd2b53389892aae19a97fbc09fa0c35 to your computer and use it in GitHub Desktop.
This file contains 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
pub fn get_bitmap(&self) -> Vec<u8> { | |
let length = self.w * self.h; | |
let aligned_length = (length + 3) & !3; | |
assert!(aligned_length <= self.a.len()); | |
let mut output = vec![0; aligned_length]; | |
unsafe { | |
// offset = Zeroed out lanes | |
let mut offset = _mm_setzero_ps(); | |
// lookup = The 4 bytes (12, 8, 4, 0) in all lanes | |
let lookup = _mm_set1_epi32(0x0c_08_04_00); | |
for i in (0..aligned_length).step_by(4) { | |
// x = Read 4 floats from self.a | |
let mut x = _mm_loadu_ps(self.a.get_unchecked(i)); | |
// x += Shift x register left by 32 bits (Padding with 0s). The casts are to | |
// satisfy the type requirements, they are otherwise nops. | |
x = _mm_add_ps(x, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(x), 4))); | |
// x += (0.0, 0.0, x[0], x[2]) | |
x = _mm_add_ps(x, _mm_shuffle_ps(_mm_setzero_ps(), x, 0x40)); | |
// x += offset | |
x = _mm_add_ps(x, offset); | |
// y = x * 255.0 | |
let y = _mm_mul_ps(x, _mm_set1_ps(255.0)); | |
// y = Convert y to i32s and truncate | |
let mut y = _mm_cvttps_epi32(y); | |
// (SSSE3) y = Take the first byte of each of the 4 values in y and pack them into | |
// the first 4 bytes of y. This produces the same value in all 4 lanes. | |
y = _mm_shuffle_epi8(y, lookup); | |
// Store the first 4 u8s from y in output. The cast again is a nop. | |
_mm_store_ss(core::mem::transmute(output.get_unchecked_mut(i)), _mm_castsi128_ps(y)); | |
// offset = (x[3], x[3], x[3], x[3]) | |
offset = _mm_shuffle_ps(x, x, 0b11_11_11_11); | |
} | |
} | |
output.truncate(length); | |
output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment