qfall_math/integer/z/logic/
bit_or.rs

1// Copyright © 2025 Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains implementations of the [`BitOr`] trait.
10
11use crate::{
12    integer::Z,
13    macros::arithmetics::{
14        arithmetic_between_types, arithmetic_trait_borrowed_to_owned,
15        arithmetic_trait_mixed_borrowed_owned,
16    },
17};
18use flint_sys::fmpz::fmpz_or;
19use std::ops::BitOr;
20
21impl BitOr for &Z {
22    type Output = Z;
23
24    /// Computes the bit-wise logical `or` of `self` and `other`.
25    ///
26    /// Parameters:
27    /// - `other`: specifies the value to apply the bit-wise logical `or` action to
28    ///   apart from `self`
29    ///
30    /// Returns a new instance of [`Z`] containing the number resulting
31    /// from the bit-wise logical `or` operation performed on `self` and `other`.
32    ///
33    /// # Examples
34    /// ```
35    /// use qfall_math::integer::Z;
36    /// let a = Z::from(5);
37    /// let b = Z::from(7);
38    ///
39    /// let c: Z = &a | &b;
40    /// let d: Z = a | 1;
41    /// let e: Z = b | 15_u64;
42    /// ```
43    fn bitor(self, other: Self) -> Self::Output {
44        let mut out = Z::default();
45        unsafe { fmpz_or(&mut out.value, &self.value, &other.value) };
46        out
47    }
48}
49
50arithmetic_trait_borrowed_to_owned!(BitOr, bitor, Z, Z, Z);
51arithmetic_trait_mixed_borrowed_owned!(BitOr, bitor, Z, Z, Z);
52arithmetic_between_types!(BitOr, bitor, Z, Z, i64 i32 i16 i8 u64 u32 u16 u8);
53
54#[cfg(test)]
55mod test_bitor {
56    use super::Z;
57
58    /// Testing `bitor` for two [`Z`]
59    #[test]
60    fn bitor() {
61        let a: Z = Z::from(42);
62        let b: Z = Z::from(24);
63
64        let c: Z = a | b;
65
66        assert_eq!(Z::from(58), c);
67    }
68
69    /// Testing `bitor` for two borrowed [`Z`]
70    #[test]
71    fn bitor_borrow() {
72        let a: Z = Z::from(42);
73        let b: Z = Z::from(24);
74
75        let c: Z = &a | &b;
76
77        assert_eq!(Z::from(58), c);
78    }
79
80    /// Testing `bitor` for borrowed [`Z`] and [`Z`]
81    #[test]
82    fn bitor_first_borrowed() {
83        let a: Z = Z::from(42);
84        let b: Z = Z::from(24);
85
86        let c: Z = &a | b;
87
88        assert_eq!(Z::from(58), c);
89    }
90
91    /// Testing `bitor` for [`Z`] and borrowed [`Z`]
92    #[test]
93    fn bitor_second_borrowed() {
94        let a: Z = Z::from(42);
95        let b: Z = Z::from(24);
96
97        let c: Z = a | &b;
98
99        assert_eq!(Z::from(58), c);
100    }
101
102    /// Testing `bitor` for large numbers
103    #[test]
104    fn bitor_large_numbers() {
105        let a: Z = Z::from(u64::MAX);
106        let b: Z = Z::from(221319874);
107        let c: Z = Z::from(i64::MAX);
108
109        let d: Z = &c | b;
110        let e: Z = a | c;
111
112        assert_eq!(Z::from(i64::MAX), d);
113        assert_eq!(Z::from(u64::MAX), e);
114    }
115
116    /// Testing `bitor` between different types
117    #[test]
118    #[allow(clippy::op_ref)]
119    fn availability() {
120        let a: Z = Z::from(42);
121        let b: u64 = 1;
122        let c: u32 = 1;
123        let d: u16 = 1;
124        let e: u8 = 1;
125        let f: i64 = 1;
126        let g: i32 = 1;
127        let h: i16 = 1;
128        let i: i8 = 1;
129
130        let _: Z = &a | &b;
131        let _: Z = &a | &c;
132        let _: Z = &a | &d;
133        let _: Z = &a | &e;
134        let _: Z = &a | &f;
135        let _: Z = &a | &g;
136        let _: Z = &a | &h;
137        let _: Z = &a | &i;
138
139        let _: Z = &b | &a;
140        let _: Z = &c | &a;
141        let _: Z = &d | &a;
142        let _: Z = &e | &a;
143        let _: Z = &f | &a;
144        let _: Z = &g | &a;
145        let _: Z = &h | &a;
146        let _: Z = &i | &a;
147
148        let _: Z = &a | b;
149        let _: Z = &a | c;
150        let _: Z = &a | d;
151        let _: Z = &a | e;
152        let _: Z = &a | f;
153        let _: Z = &a | g;
154        let _: Z = &a | h;
155        let _: Z = &a | i;
156
157        let _: Z = &b | Z::from(42);
158        let _: Z = &c | Z::from(42);
159        let _: Z = &d | Z::from(42);
160        let _: Z = &e | Z::from(42);
161        let _: Z = &f | Z::from(42);
162        let _: Z = &g | Z::from(42);
163        let _: Z = &h | Z::from(42);
164        let _: Z = &i | Z::from(42);
165
166        let _: Z = Z::from(42) | &b;
167        let _: Z = Z::from(42) | &c;
168        let _: Z = Z::from(42) | &d;
169        let _: Z = Z::from(42) | &e;
170        let _: Z = Z::from(42) | &f;
171        let _: Z = Z::from(42) | &g;
172        let _: Z = Z::from(42) | &h;
173        let _: Z = Z::from(42) | &i;
174
175        let _: Z = b | &a;
176        let _: Z = c | &a;
177        let _: Z = d | &a;
178        let _: Z = e | &a;
179        let _: Z = f | &a;
180        let _: Z = g | &a;
181        let _: Z = h | &a;
182        let _: Z = i | &a;
183
184        let _: Z = Z::from(42) | b;
185        let _: Z = Z::from(42) | c;
186        let _: Z = Z::from(42) | d;
187        let _: Z = Z::from(42) | e;
188        let _: Z = Z::from(42) | f;
189        let _: Z = Z::from(42) | g;
190        let _: Z = Z::from(42) | h;
191        let _: Z = Z::from(42) | i;
192
193        let _: Z = b | Z::from(42);
194        let _: Z = c | Z::from(42);
195        let _: Z = d | Z::from(42);
196        let _: Z = e | Z::from(42);
197        let _: Z = f | Z::from(42);
198        let _: Z = g | Z::from(42);
199        let _: Z = h | Z::from(42);
200        let _: Z = i | Z::from(42);
201    }
202}