use std::{
fmt::Debug,
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub},
};
use mck::{
concr::{self, IntoMck},
misc::CBound,
};
use crate::{traits::Ext, Bitvector, Signed};
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct Unsigned<const W: u32>(pub(super) concr::UnsignedBitvector<CBound<W>>);
impl<const W: u32> Unsigned<W> {
pub fn new(value: u64) -> Self {
Unsigned(concr::UnsignedBitvector::new(value, CBound::<W>))
}
}
impl<const W: u32> Not for Unsigned<W> {
type Output = Self;
fn not(self) -> Self::Output {
Self(!self.0)
}
}
impl<const W: u32> BitAnd<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn bitand(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl<const W: u32> BitOr<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn bitor(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl<const W: u32> BitXor<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn bitxor(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0 ^ rhs.0)
}
}
impl<const W: u32> Add<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn add(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0.add(rhs.0))
}
}
impl<const W: u32> Sub<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn sub(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0.sub(rhs.0))
}
}
impl<const W: u32> Mul<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn mul(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0.mul(rhs.0))
}
}
impl<const W: u32> Div<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn div(self, rhs: Unsigned<W>) -> Self::Output {
let panic_result = self.0.div(rhs.0);
if panic_result.panic.is_nonzero() {
panic!("attempt to divide by zero")
}
Self(panic_result.result)
}
}
impl<const W: u32> Rem<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn rem(self, rhs: Unsigned<W>) -> Self::Output {
let panic_result = self.0.rem(rhs.0);
if panic_result.panic.is_nonzero() {
panic!("attempt to calculate the remainder with a divisor of zero")
}
Self(panic_result.result)
}
}
impl<const W: u32> Shl<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn shl(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0.shl(rhs.0))
}
}
impl<const W: u32> Shr<Unsigned<W>> for Unsigned<W> {
type Output = Self;
fn shr(self, rhs: Unsigned<W>) -> Self::Output {
Self(self.0.shr(rhs.0))
}
}
impl<const W: u32, const X: u32> Ext<X> for Unsigned<W> {
type Output = Unsigned<X>;
fn ext(self) -> Self::Output {
Unsigned(self.0.ext(CBound::<X>))
}
}
impl<const W: u32> PartialOrd for Unsigned<W> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<const W: u32> Ord for Unsigned<W> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl<const W: u32> From<Bitvector<W>> for Unsigned<W> {
fn from(value: Bitvector<W>) -> Self {
Self(value.0.as_unsigned())
}
}
impl<const W: u32> From<Signed<W>> for Unsigned<W> {
fn from(value: Signed<W>) -> Self {
Self(value.0.cast_bitvector().as_unsigned())
}
}
impl<const W: u32> Debug for Unsigned<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
#[doc(hidden)]
impl<const W: u32> IntoMck for Unsigned<W> {
type Type = mck::concr::Bitvector<W>;
fn into_mck(self) -> Self::Type {
self.0.cast_bitvector()
}
}