use super::{HeaplessBigInt, zero};
use crate::MachineWord;
use const_num_traits::Personality;
use core::marker::PhantomData;
use core::ops::{BitAnd, BitOr};
impl<T: MachineWord, const CAP: usize, P: Personality> BitAnd<&HeaplessBigInt<T, CAP, P>>
for &HeaplessBigInt<T, CAP, P>
{
type Output = HeaplessBigInt<T, CAP, P>;
fn bitand(self, other: &HeaplessBigInt<T, CAP, P>) -> Self::Output {
let out_len = core::cmp::min(self.len, other.len);
let mut limbs = [zero::<T>(); CAP];
let mut i = 0;
while i < out_len as usize {
limbs[i] = self.limbs[i] & other.limbs[i];
i += 1;
}
HeaplessBigInt {
limbs,
len: out_len,
_p: PhantomData,
}
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitAnd for HeaplessBigInt<T, CAP, P> {
type Output = Self;
fn bitand(self, other: Self) -> Self {
(&self).bitand(&other)
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitAnd<&HeaplessBigInt<T, CAP, P>>
for HeaplessBigInt<T, CAP, P>
{
type Output = Self;
fn bitand(self, other: &Self) -> Self {
(&self).bitand(other)
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitAnd<HeaplessBigInt<T, CAP, P>>
for &HeaplessBigInt<T, CAP, P>
{
type Output = HeaplessBigInt<T, CAP, P>;
fn bitand(self, other: HeaplessBigInt<T, CAP, P>) -> Self::Output {
self.bitand(&other)
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitOr<&HeaplessBigInt<T, CAP, P>>
for &HeaplessBigInt<T, CAP, P>
{
type Output = HeaplessBigInt<T, CAP, P>;
fn bitor(self, other: &HeaplessBigInt<T, CAP, P>) -> Self::Output {
let out_len = core::cmp::max(self.len, other.len);
let mut limbs = [zero::<T>(); CAP];
let mut i = 0;
while i < out_len as usize {
limbs[i] = self.limbs[i] | other.limbs[i];
i += 1;
}
HeaplessBigInt {
limbs,
len: out_len,
_p: PhantomData,
}
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitOr for HeaplessBigInt<T, CAP, P> {
type Output = Self;
fn bitor(self, other: Self) -> Self {
(&self).bitor(&other)
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitOr<&HeaplessBigInt<T, CAP, P>>
for HeaplessBigInt<T, CAP, P>
{
type Output = Self;
fn bitor(self, other: &Self) -> Self {
(&self).bitor(other)
}
}
impl<T: MachineWord, const CAP: usize, P: Personality> BitOr<HeaplessBigInt<T, CAP, P>>
for &HeaplessBigInt<T, CAP, P>
{
type Output = HeaplessBigInt<T, CAP, P>;
fn bitor(self, other: HeaplessBigInt<T, CAP, P>) -> Self::Output {
self.bitor(&other)
}
}