machine_check/types/
unsigned.rs

1use std::{
2    fmt::Debug,
3    ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub},
4};
5
6use mck::{
7    concr::{self, IntoMck},
8    forward::{Bitwise, HwArith, HwShift},
9};
10
11use crate::{traits::Ext, Bitvector, Signed};
12
13///
14/// Unsigned bitvector.
15///
16/// The number of bits is specified in the generic parameter L.
17/// Unsigned bitvectors support bitwise operations and wrapping-arithmetic operations.
18/// Logical bit extension is also possible (any new bits are zero).
19/// Signed bitvectors be converted into [`Unsigned`] or [`Bitvector`].
20#[derive(Clone, Copy, Hash, PartialEq, Eq)]
21pub struct Unsigned<const L: u32>(pub(super) concr::Bitvector<L>);
22
23impl<const L: u32> Unsigned<L> {
24    ///
25    /// Creates a new bitvector with the given value.
26    /// Panics if the value does not fit into the type.
27    ///
28    pub fn new(value: u64) -> Self {
29        Unsigned(concr::Bitvector::new(value))
30    }
31}
32// --- BITWISE OPERATIONS ---
33
34impl<const L: u32> Not for Unsigned<L> {
35    type Output = Self;
36
37    fn not(self) -> Self::Output {
38        Self(self.0.bit_not())
39    }
40}
41
42impl<const L: u32> BitAnd<Unsigned<L>> for Unsigned<L> {
43    type Output = Self;
44
45    fn bitand(self, rhs: Unsigned<L>) -> Self::Output {
46        Self(self.0.bit_and(rhs.0))
47    }
48}
49impl<const L: u32> BitOr<Unsigned<L>> for Unsigned<L> {
50    type Output = Self;
51
52    fn bitor(self, rhs: Unsigned<L>) -> Self::Output {
53        Self(self.0.bit_or(rhs.0))
54    }
55}
56impl<const L: u32> BitXor<Unsigned<L>> for Unsigned<L> {
57    type Output = Self;
58
59    fn bitxor(self, rhs: Unsigned<L>) -> Self::Output {
60        Self(self.0.bit_xor(rhs.0))
61    }
62}
63
64// --- ARITHMETIC OPERATIONS ---
65
66impl<const L: u32> Add<Unsigned<L>> for Unsigned<L> {
67    type Output = Self;
68
69    fn add(self, rhs: Unsigned<L>) -> Self::Output {
70        Self(self.0.add(rhs.0))
71    }
72}
73
74impl<const L: u32> Sub<Unsigned<L>> for Unsigned<L> {
75    type Output = Self;
76
77    fn sub(self, rhs: Unsigned<L>) -> Self::Output {
78        Self(self.0.sub(rhs.0))
79    }
80}
81
82impl<const L: u32> Mul<Unsigned<L>> for Unsigned<L> {
83    type Output = Self;
84
85    fn mul(self, rhs: Unsigned<L>) -> Self::Output {
86        Self(self.0.mul(rhs.0))
87    }
88}
89
90impl<const L: u32> Div<Unsigned<L>> for Unsigned<L> {
91    type Output = Self;
92
93    fn div(self, rhs: Unsigned<L>) -> Self::Output {
94        Self(self.0.udiv(rhs.0))
95    }
96}
97
98impl<const L: u32> Rem<Unsigned<L>> for Unsigned<L> {
99    type Output = Self;
100
101    fn rem(self, rhs: Unsigned<L>) -> Self::Output {
102        Self(self.0.urem(rhs.0))
103    }
104}
105
106impl<const L: u32> Shl<Unsigned<L>> for Unsigned<L> {
107    type Output = Self;
108
109    fn shl(self, rhs: Unsigned<L>) -> Self::Output {
110        Self(self.0.logic_shl(rhs.0))
111    }
112}
113
114impl<const L: u32> Shr<Unsigned<L>> for Unsigned<L> {
115    type Output = Self;
116
117    fn shr(self, rhs: Unsigned<L>) -> Self::Output {
118        Self(self.0.logic_shr(rhs.0))
119    }
120}
121
122// --- EXTENSION ---
123impl<const L: u32, const X: u32> Ext<X> for Unsigned<L> {
124    type Output = Unsigned<X>;
125
126    fn ext(self) -> Self::Output {
127        Unsigned::<X>(mck::forward::Ext::uext(self.0))
128    }
129}
130
131// --- ORDERING ---
132
133impl<const L: u32> PartialOrd for Unsigned<L> {
134    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
135        Some(self.cmp(other))
136    }
137}
138
139impl<const L: u32> Ord for Unsigned<L> {
140    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
141        self.0.unsigned_cmp(&other.0)
142    }
143}
144
145// --- CONVERSION ---
146
147impl<const L: u32> From<Bitvector<L>> for Unsigned<L> {
148    fn from(value: Bitvector<L>) -> Self {
149        Self(value.0)
150    }
151}
152
153impl<const L: u32> From<Signed<L>> for Unsigned<L> {
154    fn from(value: Signed<L>) -> Self {
155        Self(value.0)
156    }
157}
158
159// --- MISC ---
160
161impl<const L: u32> Debug for Unsigned<L> {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        std::fmt::Debug::fmt(&self.0, f)
164    }
165}
166
167// --- INTERNAL IMPLEMENTATIONS ---
168
169#[doc(hidden)]
170impl<const L: u32> IntoMck for Unsigned<L> {
171    type Type = mck::concr::Bitvector<L>;
172
173    fn into_mck(self) -> Self::Type {
174        self.0
175    }
176}