Skip to main content

shift_right_avx2

Function shift_right_avx2 

Source
pub unsafe fn shift_right_avx2<const OR_ASSIGN: bool>(
    bits: &mut [u64],
    rhs: usize,
)
Examples found in repository?
crates/competitive/src/data_structure/bitset.rs (line 456)
434    fn shift_right<const OR_ASSIGN: bool>(&mut self, rhs: usize) {
435        if rhs == 0 {
436            return;
437        }
438        if rhs >= self.size {
439            if !OR_ASSIGN {
440                self.reset();
441            }
442            return;
443        }
444        #[cfg(target_arch = "x86_64")]
445        if self.bits.len() >= SIMD_MIN_BLOCKS {
446            if avx512_enabled()
447                && is_x86_feature_detected!("avx512f")
448                && is_x86_feature_detected!("avx512vbmi2")
449            {
450                // SAFETY: blocks are aligned and feature detection checked AVX-512F and VBMI2.
451                unsafe { simd::shift_right_avx512::<OR_ASSIGN>(&mut self.bits, rhs) };
452                return;
453            }
454            if is_x86_feature_detected!("avx2") {
455                // SAFETY: feature detection checked AVX2 support.
456                unsafe { simd::shift_right_avx2::<OR_ASSIGN>(self.words_mut(), rhs) };
457                return;
458            }
459        }
460
461        let bits = self.words_mut();
462        let word_shift = rhs >> 6;
463        let bit_shift = rhs & 63;
464        if bit_shift == 0 {
465            for i in word_shift..bits.len() {
466                if OR_ASSIGN {
467                    bits[i - word_shift] |= bits[i];
468                } else {
469                    bits[i - word_shift] = bits[i];
470                }
471            }
472        } else {
473            for i in word_shift..bits.len() - 1 {
474                let value = (bits[i] >> bit_shift) | (bits[i + 1] << (64 - bit_shift));
475                if OR_ASSIGN {
476                    bits[i - word_shift] |= value;
477                } else {
478                    bits[i - word_shift] = value;
479                }
480            }
481            if OR_ASSIGN {
482                bits[bits.len() - word_shift - 1] |= bits[bits.len() - 1] >> bit_shift;
483            } else {
484                bits[bits.len() - word_shift - 1] = bits[bits.len() - 1] >> bit_shift;
485            }
486        }
487        if !OR_ASSIGN {
488            let end = bits.len() - word_shift;
489            bits[end..].fill(0);
490        }
491    }