Skip to main content

nanvm_lib/vm/bigint/
bitor.rs

1use core::ops::BitOr;
2
3use crate::vm::{BigInt, IVm};
4
5impl<A: IVm> BitOr for BigInt<A> {
6    type Output = Self;
7    fn bitor(self, rhs: Self) -> Self::Output {
8        self.bitwise_op(rhs, |a, b| a | b, |a_neg, b_neg| a_neg || b_neg)
9    }
10}
11
12#[cfg(test)]
13mod tests {
14    use crate::vm::bigint::test_util::{int, neg};
15
16    #[test]
17    fn both_positive() {
18        assert_eq!(int(0b1100) | int(0b1010), int(0b1110));
19    }
20
21    #[test]
22    fn both_negative() {
23        // -2 | -3 == -1: 0b...110 | 0b...101 == 0b...111.
24        assert_eq!(int(-2) | int(-3), int(-1));
25    }
26
27    #[test]
28    fn mixed_signs() {
29        // 5 | -1 == -1 (OR with all-ones is all-ones).
30        assert_eq!(int(5) | int(-1), int(-1));
31        assert_eq!(int(-1) | int(5), int(-1));
32    }
33
34    #[test]
35    fn with_zero() {
36        assert_eq!(int(0) | int(0), int(0));
37        assert_eq!(int(12_345) | int(0), int(12_345));
38        assert_eq!(int(-12_345) | int(0), int(-12_345));
39    }
40
41    #[test]
42    fn different_word_lengths() {
43        // x = -(2^64), a two-word magnitude; y = -1, a one-word magnitude
44        // zero-extended to match. -1's all-ones two's-complement word ORs
45        // with anything to stay all-ones, so the result is -1.
46        let x = neg([0u64, 1u64]);
47        let y = int(-1);
48        let expected = int(-1);
49        assert_eq!(x | y, expected);
50    }
51}