Skip to main content

write_binary_avx2

Function write_binary_avx2 

Source
pub unsafe fn write_binary_avx2(bytes: &mut [u8], words: &[u64])
Examples found in repository?
crates/competitive/src/data_structure/bitset.rs (line 119)
92    pub fn to_binary(&self) -> String {
93        const TABLE: [[u8; 8]; 256] = {
94            let mut table = [[b'0'; 8]; 256];
95            let mut i = 0;
96            while i < 256 {
97                let mut j = 0;
98                while j < 8 {
99                    table[i][j] |= ((i >> j) & 1) as u8;
100                    j += 1;
101                }
102                i += 1;
103            }
104            table
105        };
106        let mut bytes = vec![b'0'; self.size.div_ceil(8) * 8];
107        #[cfg(target_arch = "x86_64")]
108        let end = if self.size >= 64 && avx512_enabled() && is_x86_feature_detected!("avx512bw") {
109            let end = self.size / 64 * 64;
110            // SAFETY: AVX-512BW is available; each output chunk holds one 64-bit word.
111            unsafe {
112                simd::write_binary_avx512(&mut bytes[..end], self.words());
113            }
114            end
115        } else if self.size >= 64 && is_x86_feature_detected!("avx2") {
116            let end = self.size / 64 * 64;
117            // SAFETY: AVX2 is available; each output chunk holds one complete 64-bit word.
118            unsafe {
119                simd::write_binary_avx2(&mut bytes[..end], self.words());
120            }
121            end
122        } else {
123            0
124        };
125        #[cfg(not(target_arch = "x86_64"))]
126        let end = 0;
127        for (chunk, &word) in bytes[end..].chunks_mut(64).zip(&self.words()[end / 64..]) {
128            for (i, byte) in chunk.as_chunks_mut::<8>().0.iter_mut().enumerate() {
129                byte.copy_from_slice(&TABLE[(word >> (i * 8) & 255) as usize]);
130            }
131        }
132        bytes.truncate(self.size);
133        // SAFETY: every output byte is ASCII `0` or `1`.
134        unsafe { String::from_utf8_unchecked(bytes) }
135    }