1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::{
    fmt::Debug,
    ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub},
};

use mck::{
    concr::{self, IntoMck},
    forward::{Bitwise, HwArith, HwShift},
};

use crate::{traits::Ext, Bitvector, Unsigned};

/// Signed bitvector.
///
/// The number of bits is specified in the generic parameter L.
/// Signed bitvectors support bitwise operations and wrapping-arithmetic operations.
/// Arithmetic bit extension is also possible (the sign bit is copied into any bits above it).
/// Signed bitvectors be converted into [`Unsigned`] or [`Bitvector`].
///
/// Currently, it is not possible to create signed bitvectors directly, only convert into them.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct Signed<const L: u32>(pub(super) concr::Bitvector<L>);

// --- BITWISE OPERATIONS ---

impl<const L: u32> Not for Signed<L> {
    type Output = Self;

    fn not(self) -> Self::Output {
        Self(self.0.bit_not())
    }
}

impl<const L: u32> BitAnd<Signed<L>> for Signed<L> {
    type Output = Self;

    fn bitand(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.bit_and(rhs.0))
    }
}
impl<const L: u32> BitOr<Signed<L>> for Signed<L> {
    type Output = Self;

    fn bitor(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.bit_or(rhs.0))
    }
}
impl<const L: u32> BitXor<Signed<L>> for Signed<L> {
    type Output = Self;

    fn bitxor(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.bit_xor(rhs.0))
    }
}

// --- ARITHMETIC OPERATIONS ---

impl<const L: u32> Add<Signed<L>> for Signed<L> {
    type Output = Self;

    fn add(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.add(rhs.0))
    }
}

impl<const L: u32> Sub<Signed<L>> for Signed<L> {
    type Output = Self;

    fn sub(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.sub(rhs.0))
    }
}

impl<const L: u32> Mul<Signed<L>> for Signed<L> {
    type Output = Self;

    fn mul(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.mul(rhs.0))
    }
}

impl<const L: u32> Div<Signed<L>> for Signed<L> {
    type Output = Self;

    fn div(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.sdiv(rhs.0))
    }
}

impl<const L: u32> Rem<Signed<L>> for Signed<L> {
    type Output = Self;

    fn rem(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.srem(rhs.0))
    }
}

impl<const L: u32> Shl<Signed<L>> for Signed<L> {
    type Output = Self;

    fn shl(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.logic_shl(rhs.0))
    }
}

impl<const L: u32> Shr<Signed<L>> for Signed<L> {
    type Output = Self;

    fn shr(self, rhs: Signed<L>) -> Self::Output {
        Self(self.0.arith_shr(rhs.0))
    }
}

// --- EXTENSION ---
impl<const L: u32, const X: u32> Ext<X> for Signed<L> {
    type Output = Signed<X>;

    fn ext(self) -> Self::Output {
        Signed::<X>(mck::forward::Ext::sext(self.0))
    }
}

// --- ORDERING ---

impl<const L: u32> PartialOrd for Signed<L> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<const L: u32> Ord for Signed<L> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.signed_cmp(&other.0)
    }
}

// --- CONVERSION ---
impl<const L: u32> From<Unsigned<L>> for Signed<L> {
    fn from(value: Unsigned<L>) -> Self {
        Self(value.0)
    }
}

impl<const L: u32> From<Bitvector<L>> for Signed<L> {
    fn from(value: Bitvector<L>) -> Self {
        Self(value.0)
    }
}

// --- MISC ---

impl<const L: u32> Debug for Signed<L> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.0, f)
    }
}

// --- INTERNAL IMPLEMENTATIONS ---

#[doc(hidden)]
impl<const L: u32> IntoMck for Signed<L> {
    type Type = mck::concr::Bitvector<L>;

    fn into_mck(self) -> Self::Type {
        self.0
    }
}