Skip to main content

crypto_bigint/int/
bit_or.rs

1//! [`Int`] bitwise OR operations.
2
3use core::ops::{BitOr, BitOrAssign};
4
5use crate::{CtOption, Uint, Wrapping};
6
7use super::Int;
8
9impl<const LIMBS: usize> Int<LIMBS> {
10    /// Computes bitwise `a | b`.
11    #[inline(always)]
12    #[must_use]
13    pub const fn bitor(&self, rhs: &Self) -> Self {
14        Self(Uint::bitor(&self.0, &rhs.0))
15    }
16
17    /// Perform wrapping bitwise `OR`.
18    ///
19    /// There's no way wrapping could ever happen.
20    /// This function exists so that all operations are accounted for in the wrapping operations
21    #[must_use]
22    pub const fn wrapping_or(&self, rhs: &Self) -> Self {
23        self.bitor(rhs)
24    }
25
26    /// Perform checked bitwise `OR`, returning a [`CtOption`] which `is_some` always
27    #[must_use]
28    pub const fn checked_or(&self, rhs: &Self) -> CtOption<Self> {
29        CtOption::some(self.bitor(rhs))
30    }
31}
32
33impl<const LIMBS: usize> BitOr for Int<LIMBS> {
34    type Output = Self;
35
36    fn bitor(self, rhs: Self) -> Int<LIMBS> {
37        self.bitor(&rhs)
38    }
39}
40
41impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for Int<LIMBS> {
42    type Output = Int<LIMBS>;
43
44    #[allow(clippy::needless_borrow)]
45    fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
46        (&self).bitor(rhs)
47    }
48}
49
50impl<const LIMBS: usize> BitOr<Int<LIMBS>> for &Int<LIMBS> {
51    type Output = Int<LIMBS>;
52
53    fn bitor(self, rhs: Int<LIMBS>) -> Int<LIMBS> {
54        self.bitor(&rhs)
55    }
56}
57
58impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for &Int<LIMBS> {
59    type Output = Int<LIMBS>;
60
61    fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
62        self.bitor(rhs)
63    }
64}
65
66impl<const LIMBS: usize> BitOrAssign for Int<LIMBS> {
67    fn bitor_assign(&mut self, other: Self) {
68        *self = *self | other;
69    }
70}
71
72impl<const LIMBS: usize> BitOrAssign<&Int<LIMBS>> for Int<LIMBS> {
73    fn bitor_assign(&mut self, other: &Self) {
74        *self = *self | other;
75    }
76}
77
78impl<const LIMBS: usize> BitOr for Wrapping<Int<LIMBS>> {
79    type Output = Self;
80
81    fn bitor(self, rhs: Self) -> Wrapping<Int<LIMBS>> {
82        Wrapping(self.0.bitor(&rhs.0))
83    }
84}
85
86impl<const LIMBS: usize> BitOr<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
87    type Output = Wrapping<Int<LIMBS>>;
88
89    fn bitor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
90        Wrapping(self.0.bitor(&rhs.0))
91    }
92}
93
94impl<const LIMBS: usize> BitOr<Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
95    type Output = Wrapping<Int<LIMBS>>;
96
97    fn bitor(self, rhs: Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
98        Wrapping(self.0.bitor(&rhs.0))
99    }
100}
101
102impl<const LIMBS: usize> BitOr<&Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
103    type Output = Wrapping<Int<LIMBS>>;
104
105    fn bitor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> {
106        Wrapping(self.0.bitor(&rhs.0))
107    }
108}
109
110impl<const LIMBS: usize> BitOrAssign for Wrapping<Int<LIMBS>> {
111    fn bitor_assign(&mut self, other: Self) {
112        *self = *self | other;
113    }
114}
115
116impl<const LIMBS: usize> BitOrAssign<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
117    fn bitor_assign(&mut self, other: &Self) {
118        *self = *self | other;
119    }
120}
121
122#[cfg(test)]
123mod tests {
124    use crate::I128;
125
126    #[test]
127    fn checked_or_ok() {
128        assert_eq!(I128::ZERO.checked_or(&I128::ONE).unwrap(), I128::ONE);
129        assert_eq!(I128::ONE.checked_or(&I128::ONE).unwrap(), I128::ONE);
130        assert_eq!(I128::MAX.checked_or(&I128::ONE).unwrap(), I128::MAX);
131    }
132
133    #[test]
134    fn wrapping_or_ok() {
135        assert_eq!(I128::ZERO.wrapping_or(&I128::ONE), I128::ONE);
136        assert_eq!(I128::ONE.wrapping_or(&I128::ONE), I128::ONE);
137        assert_eq!(I128::MAX.wrapping_or(&I128::ONE), I128::MAX);
138    }
139}