sucds 0.6.0

Succinct data structures in Rust
Documentation
//! Broadword tools.
#![cfg(target_pointer_width = "64")]

#[cfg(feature = "intrinsics")]
use crate::intrinsics;

pub(crate) const ONES_STEP_4: usize = 0x1111111111111111;
pub(crate) const ONES_STEP_8: usize = 0x0101010101010101;
pub(crate) const ONES_STEP_9: usize =
    1 << 0 | 1 << 9 | 1 << 18 | 1 << 27 | 1 << 36 | 1 << 45 | 1 << 54;
pub(crate) const MSBS_STEP_8: usize = 0x80 * ONES_STEP_8;
pub(crate) const MSBS_STEP_9: usize = 0x100 * ONES_STEP_9;
pub(crate) const INV_COUNT_STEP_9: usize =
    1 << 54 | 2 << 45 | 3 << 36 | 4 << 27 | 5 << 18 | 6 << 9 | 7;

#[allow(dead_code)]
#[inline(always)]
pub(crate) const fn leq_step_8(x: usize, y: usize) -> usize {
    ((((y | MSBS_STEP_8) - (x & !MSBS_STEP_8)) ^ (x ^ y)) & MSBS_STEP_8) >> 7
}

#[allow(dead_code)]
#[inline(always)]
pub(crate) const fn uleq_step_8(x: usize, y: usize) -> usize {
    (((((y | MSBS_STEP_8) - (x & !MSBS_STEP_8)) ^ (x ^ y)) ^ (x & !y)) & MSBS_STEP_8) >> 7
}

#[inline(always)]
pub(crate) const fn uleq_step_9(x: usize, y: usize) -> usize {
    (((((y | MSBS_STEP_9) - (x & !MSBS_STEP_9)) | (x ^ y)) ^ (x & !y)) & MSBS_STEP_9) >> 8
}

#[inline(always)]
pub(crate) const fn byte_counts(mut x: usize) -> usize {
    x = x - ((x & (0xa * ONES_STEP_4)) >> 1);
    x = (x & (3 * ONES_STEP_4)) + ((x >> 2) & (3 * ONES_STEP_4));
    (x + (x >> 4)) & (0x0f * ONES_STEP_8)
}

#[allow(dead_code)]
#[inline(always)]
pub(crate) const fn bytes_sum(x: usize) -> usize {
    ONES_STEP_8.wrapping_mul(x) >> 56
}

/// Counts the number of set bits.
///
/// # Examples
///
/// ```
/// use sucds::broadword::popcount;
///
/// assert_eq!(popcount(0), 0);
/// assert_eq!(popcount(usize::MAX), 64);
/// assert_eq!(popcount(0b1010110011), 6);
/// ```
#[inline(always)]
pub const fn popcount(x: usize) -> usize {
    #[cfg(not(feature = "intrinsics"))]
    {
        bytes_sum(byte_counts(x))
    }
    #[cfg(feature = "intrinsics")]
    {
        intrinsics::popcount(x)
    }
}

/// Searches the position of the `k`-th bit set.
///
/// # Examples
///
/// ```
/// use sucds::broadword::select_in_word;
///
/// assert_eq!(select_in_word(0b1010011, 0), 0);
/// assert_eq!(select_in_word(0b1010011, 1), 1);
/// assert_eq!(select_in_word(0b1010011, 2), 4);
/// assert_eq!(select_in_word(0b1010011, 3), 6);
/// ```
#[inline(always)]
pub fn select_in_word(x: usize, k: usize) -> usize {
    debug_assert!(k < popcount(x));
    let byte_sums = ONES_STEP_8.wrapping_mul(byte_counts(x));
    let k_step_8 = k * ONES_STEP_8;
    let geq_k_step_8 = ((k_step_8 | MSBS_STEP_8) - byte_sums) & MSBS_STEP_8;
    let place = {
        #[cfg(feature = "intrinsics")]
        {
            popcount(geq_k_step_8) * 8
        }
        #[cfg(not(feature = "intrinsics"))]
        {
            ((geq_k_step_8 >> 7).wrapping_mul(ONES_STEP_8) >> 53) & !0x7
        }
    };
    let byte_rank = k - (((byte_sums << 8) >> place) & 0xFF);
    place + SELECT_IN_BYTE[((x >> place) & 0xFF) | (byte_rank << 8)] as usize
}

#[allow(dead_code)]
#[inline(always)]
pub(crate) fn bit_position(x: usize) -> usize {
    debug_assert!(popcount(x) == 1);
    DEBRUIJN64_MAPPING[(DEBRUIJN64.wrapping_mul(x)) >> 58] as usize
}

/// Gets the least significant bit.
///
/// # Examples
///
/// ```
/// use sucds::broadword::lsb;
///
/// assert_eq!(lsb(0b101100), Some(2));
/// assert_eq!(lsb(0b0), None);
/// ```
#[allow(clippy::missing_const_for_fn)]
#[inline(always)]
pub fn lsb(x: usize) -> Option<usize> {
    #[cfg(not(feature = "intrinsics"))]
    {
        if x == 0 {
            None
        } else {
            Some(bit_position(x & std::usize::MAX.wrapping_mul(x)))
        }
    }
    #[cfg(feature = "intrinsics")]
    {
        intrinsics::bsf64(x)
    }
}

/// Gets the most significant bit.
///
/// # Examples
///
/// ```
/// use sucds::broadword::msb;
///
/// assert_eq!(msb(0b101100), Some(5));
/// assert_eq!(msb(0b0), None);
/// ```
#[allow(clippy::missing_const_for_fn)]
#[inline(always)]
pub fn msb(x: usize) -> Option<usize> {
    #[cfg(not(feature = "intrinsics"))]
    {
        if x == 0 {
            return None;
        }
        // right-saturate the word
        let mut x = x;
        x |= x >> 1;
        x |= x >> 2;
        x |= x >> 4;
        x |= x >> 8;
        x |= x >> 16;
        x |= x >> 32;
        // isolate the MSB
        x ^= x >> 1;
        Some(bit_position(x))
    }
    #[cfg(feature = "intrinsics")]
    {
        intrinsics::bsr64(x)
    }
}

const SELECT_IN_BYTE: [u8; 2048] = [
    8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0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, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7,
];

const DEBRUIJN64_MAPPING: [u8; 64] = [
    63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55,
    30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56,
    45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5,
];

const DEBRUIJN64: usize = 0x07EDD5E59A4E28C2;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_select_in_word() {
        let x: usize = 0b0000010011000011000011100100000000010000100010000010001100010011;
        let sels = [
            0, 1, 4, 8, 9, 13, 19, 23, 28, 38, 41, 42, 43, 48, 49, 54, 55, 58,
        ];
        for (i, &k) in sels.iter().enumerate() {
            assert_eq!(select_in_word(x, i), k);
        }
    }
}