//! 320-bit addition with overflow detection.
use super::U320;

impl U320 {
    /// Computes `self + other`, returning the 320-bit result and a
    /// boolean indicating whether the addition overflowed (i.e., the
    /// true result exceeds `2^320 - 1`).
    ///
    /// Packs limb pairs `[0]+[1]` and `[2]+[3]` into `u128` for native
    /// carry chaining, then handles the top limb `[4]` separately with
    /// the final carry.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u320::U320;
    ///
    /// let a = U320::from_be_limbs([0, 0, 0, 0, 10]);
    /// let b = U320::from_be_limbs([0, 0, 0, 0, 20]);
    /// let (result, overflow) = a.overflowing_add(&b);
    /// assert_eq!(result, U320::from_be_limbs([0, 0, 0, 0, 30]));
    /// assert!(!overflow);
    /// ```
    #[inline]
    pub const fn overflowing_add(&self, other: &U320) -> (U320, bool) {
        let a_lo = ((self.0[1] as u128) << 64) | (self.0[0] as u128);
        let a_hi = ((self.0[3] as u128) << 64) | (self.0[2] as u128);
        let b_lo = ((other.0[1] as u128) << 64) | (other.0[0] as u128);
        let b_hi = ((other.0[3] as u128) << 64) | (other.0[2] as u128);

        let (sum_lo, carry1) = a_lo.overflowing_add(b_lo);
        let (sum_hi, c1) = a_hi.overflowing_add(b_hi);
        let (sum_hi, c2) = sum_hi.overflowing_add(carry1 as u128);

        let (top, t1) = self.0[4].overflowing_add(other.0[4]);
        let (top, t2) = top.overflowing_add((c1 | c2) as u64);

        (
            U320([
                sum_lo as u64,
                (sum_lo >> 64) as u64,
                sum_hi as u64,
                (sum_hi >> 64) as u64,
                top,
            ]),
            t1 | t2,
        )
    }
}

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

    /// Adding two small values produces no overflow.
    #[test]
    fn small_no_overflow() {
        let a = U320::from_be_limbs([0, 0, 0, 0, 100]);
        let b = U320::from_be_limbs([0, 0, 0, 0, 200]);
        let (result, overflow) = a.overflowing_add(&b);
        assert_eq!(result, U320::from_be_limbs([0, 0, 0, 0, 300]));
        assert!(!overflow);
    }

    /// Adding MAX to one overflows, wrapping to zero.
    #[test]
    fn max_plus_one_overflows() {
        let (result, overflow) = U320::MAX.overflowing_add(&U320::ONE);
        assert_eq!(result, U320::ZERO);
        assert!(overflow);
    }

    /// Carry propagates across all five limbs.
    #[test]
    fn carry_propagation() {
        let a = U320::from_be_limbs([0, 0, 0, 0, u64::MAX]);
        let (result, overflow) = a.overflowing_add(&U320::ONE);
        assert_eq!(result, U320::from_be_limbs([0, 0, 0, 1, 0]));
        assert!(!overflow);
    }

    /// Zero is the additive identity.
    #[test]
    fn additive_identity() {
        let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
        let (result, overflow) = a.overflowing_add(&U320::ZERO);
        assert_eq!(result, a);
        assert!(!overflow);
    }

    /// Carry propagation across the u128 pair boundary.
    #[test]
    fn cross_pair_carry() {
        let a = U320::from_be_limbs([0, 0, u64::MAX, u64::MAX, u64::MAX]);
        let (result, overflow) = a.overflowing_add(&U320::ONE);
        assert_eq!(result, U320::from_be_limbs([0, 1, 0, 0, 0]));
        assert!(!overflow);
    }

    /// Carry into the top limb without overflow.
    #[test]
    fn carry_into_top() {
        let a = U320::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
        let (result, overflow) = a.overflowing_add(&U320::ONE);
        assert_eq!(result, U320::from_be_limbs([1, 0, 0, 0, 0]));
        assert!(!overflow);
    }

    /// Both halves carry simultaneously.
    #[test]
    fn double_carry() {
        let a = U320::from_be_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
        let b = U320::from_be_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
        let (result, overflow) = a.overflowing_add(&b);
        assert_eq!(result, U320::from_be_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX - 1]));
        assert!(overflow);
    }
}
