//! Byte-level leading zero count for [`U320`].
use super::U320;

impl U320 {
    /// Returns the number of leading zero bytes in the 320-bit value.
    ///
    /// Equivalent to `leading_zeros() / 8`, counting complete zero bytes
    /// from the most significant position. Returns 0-40.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u320::U320;
    ///
    /// assert_eq!(U320::ZERO.leading_zero_bytes(), 40);
    /// assert_eq!(U320::ONE.leading_zero_bytes(), 39);
    /// assert_eq!(U320::MAX.leading_zero_bytes(), 0);
    /// ```
    #[inline]
    pub const fn leading_zero_bytes(&self) -> u32 {
        self.leading_zeros() / 8
    }
}

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

    /// Zero has 40 leading zero bytes.
    #[test]
    fn zero() {
        assert_eq!(U320::ZERO.leading_zero_bytes(), 40);
    }

    /// One has 39 leading zero bytes.
    #[test]
    fn one() {
        assert_eq!(U320::ONE.leading_zero_bytes(), 39);
    }

    /// MAX has 0 leading zero bytes.
    #[test]
    fn max() {
        assert_eq!(U320::MAX.leading_zero_bytes(), 0);
    }

    /// 0xFF in the LSB has 39 leading zero bytes.
    #[test]
    fn single_byte() {
        assert_eq!(U320::from_be_limbs([0, 0, 0, 0, 0xFF]).leading_zero_bytes(), 39);
    }

    /// 0x100 spans into a second byte, so 38 leading zero bytes.
    #[test]
    fn two_byte_value() {
        assert_eq!(U320::from_be_limbs([0, 0, 0, 0, 0x100]).leading_zero_bytes(), 38);
    }

    /// Full first limb means 32 leading zero bytes.
    #[test]
    fn full_lsb_limb() {
        assert_eq!(U320::from_be_limbs([0, 0, 0, 0, u64::MAX]).leading_zero_bytes(), 32);
    }

    /// Value in the MSB limb.
    #[test]
    fn msb_limb() {
        assert_eq!(U320::from_be_limbs([1, 0, 0, 0, 0]).leading_zero_bytes(), 7);
    }
}
