machine_check/types/
bitvector.rs1use std::{
2 fmt::Debug,
3 ops::{Add, BitAnd, BitOr, BitXor, Mul, Not, Shl, Sub},
4};
5
6use mck::{
7 concr::{self, IntoMck},
8 forward::{Bitwise, HwArith, HwShift},
9 misc::CBound,
10};
11
12use crate::{Signed, Unsigned};
13
14#[derive(Clone, Copy, Hash, PartialEq, Eq)]
22pub struct Bitvector<const W: u32>(pub(super) concr::Bitvector<W>);
23
24impl<const W: u32> Bitvector<W> {
25 pub fn new(value: u64) -> Self {
29 Bitvector(concr::Bitvector::new(value, CBound::<W>))
30 }
31}
32impl<const W: u32> Not for Bitvector<W> {
35 type Output = Self;
36
37 fn not(self) -> Self::Output {
39 Self(self.0.bit_not())
40 }
41}
42
43impl<const W: u32> BitAnd<Bitvector<W>> for Bitvector<W> {
44 type Output = Self;
45
46 fn bitand(self, rhs: Bitvector<W>) -> Self::Output {
48 Self(self.0.bit_and(rhs.0))
49 }
50}
51impl<const W: u32> BitOr<Bitvector<W>> for Bitvector<W> {
52 type Output = Self;
53
54 fn bitor(self, rhs: Bitvector<W>) -> Self::Output {
56 Self(self.0.bit_or(rhs.0))
57 }
58}
59impl<const W: u32> BitXor<Bitvector<W>> for Bitvector<W> {
60 type Output = Self;
61
62 fn bitxor(self, rhs: Bitvector<W>) -> Self::Output {
64 Self(self.0.bit_xor(rhs.0))
65 }
66}
67
68impl<const W: u32> Add<Bitvector<W>> for Bitvector<W> {
71 type Output = Self;
72
73 fn add(self, rhs: Bitvector<W>) -> Self::Output {
75 Self(self.0.add(rhs.0))
76 }
77}
78
79impl<const W: u32> Sub<Bitvector<W>> for Bitvector<W> {
80 type Output = Self;
81
82 fn sub(self, rhs: Bitvector<W>) -> Self::Output {
84 Self(self.0.sub(rhs.0))
85 }
86}
87
88impl<const W: u32> Mul<Bitvector<W>> for Bitvector<W> {
89 type Output = Self;
90
91 fn mul(self, rhs: Bitvector<W>) -> Self::Output {
93 Self(self.0.mul(rhs.0))
94 }
95}
96
97impl<const W: u32> Shl<Bitvector<W>> for Bitvector<W> {
99 type Output = Self;
100
101 fn shl(self, rhs: Bitvector<W>) -> Self::Output {
112 Self(self.0.logic_shl(rhs.0))
113 }
114}
115
116impl<const W: u32> From<Unsigned<W>> for Bitvector<W> {
118 fn from(value: Unsigned<W>) -> Self {
120 Self(value.0.cast_bitvector())
121 }
122}
123
124impl<const W: u32> From<Signed<W>> for Bitvector<W> {
125 fn from(value: Signed<W>) -> Self {
127 Self(value.0.cast_bitvector())
128 }
129}
130
131impl<const W: u32> Debug for Bitvector<W> {
132 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 std::fmt::Debug::fmt(&self.0, f)
134 }
135}
136
137#[doc(hidden)]
140impl<const W: u32> IntoMck for Bitvector<W> {
141 type Type = mck::concr::Bitvector<W>;
142
143 fn into_mck(self) -> Self::Type {
144 self.0
145 }
146}