//! Bitwise OR via the [`BitOr`] trait.
use super::U512;
use core::ops::BitOr;

/// Computes the bitwise OR of two 512-bit integers, producing a
/// result where each bit is set if either corresponding input bit
/// is set.
///
/// Applied independently to each of the eight `u64` limbs.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// let a = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0xF0]);
/// let b = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0x0F]);
/// assert_eq!(a | b, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0xFF]));
/// ```
impl BitOr for U512 {
    type Output = U512;

    #[inline]
    fn bitor(self, rhs: U512) -> U512 {
        U512([
            self.0[0] | rhs.0[0],
            self.0[1] | rhs.0[1],
            self.0[2] | rhs.0[2],
            self.0[3] | rhs.0[3],
            self.0[4] | rhs.0[4],
            self.0[5] | rhs.0[5],
            self.0[6] | rhs.0[6],
            self.0[7] | rhs.0[7],
        ])
    }
}

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

    /// OR with self is identity.
    #[test]
    fn self_identity() {
        let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
        assert_eq!(a | a, a);
    }

    /// OR with zero is identity.
    #[test]
    fn or_zero() {
        let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
        assert_eq!(a | U512::ZERO, a);
    }

    /// OR with MAX is MAX.
    #[test]
    fn or_max() {
        let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
        assert_eq!(a | U512::MAX, U512::MAX);
    }

    /// OR combines disjoint bits.
    #[test]
    fn disjoint_bits() {
        let a = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0xF0]);
        let b = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0x0F]);
        assert_eq!(a | b, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0xFF]));
    }

    /// OR is commutative.
    #[test]
    fn commutative() {
        let a = U512::from_be_limbs([1, 2, 3, 4, 5, 6, 7, 8]);
        let b = U512::from_be_limbs([8, 7, 6, 5, 4, 3, 2, 1]);
        assert_eq!(a | b, b | a);
    }
}
