//! Conversion from `u128` into [`U384`].
use super::U384;

/// Creates a [`U384`] from a `u128` value, placing the high 64 bits in
/// limb `w4` and the low 64 bits in limb `w5`, with the upper four limbs
/// set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// let v = U384::from(0x1_0000_0000_0000_0000u128);
/// assert_eq!(v, U384::from_be_limbs([0, 0, 0, 0, 1, 0]));
/// ```
impl From<u128> for U384 {
    #[inline]
    fn from(value: u128) -> Self {
        Self::from_be_limbs([0, 0, 0, 0, (value >> 64) as u64, value as u64])
    }
}

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

    /// Converting zero produces U384::ZERO.
    #[test]
    fn zero() {
        assert_eq!(U384::from(0u128), U384::ZERO);
    }

    /// Converting one produces U384::ONE.
    #[test]
    fn one() {
        assert_eq!(U384::from(1u128), U384::ONE);
    }

    /// Converting u128::MAX fills only the lowest two limbs.
    #[test]
    fn max_u128() {
        assert_eq!(
            U384::from(u128::MAX),
            U384::from_be_limbs([0, 0, 0, 0, u64::MAX, u64::MAX]),
        );
    }

    /// Value spanning both u64 halves of the u128.
    #[test]
    fn cross_limb() {
        let v = U384::from(((5u128) << 64) | 7);
        assert_eq!(v, U384::from_be_limbs([0, 0, 0, 0, 5, 7]));
    }
}
