-
Notifications
You must be signed in to change notification settings - Fork 3
/
SignedBitset.hpp
239 lines (214 loc) · 7.16 KB
/
SignedBitset.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef SIGNEDBITSET_HPP
#define SIGNEDBITSET_HPP
#include <ostream>
#include <bitset>
#include "Exceptions.hpp"
namespace std {
/**
* \brief **SignedBitset** : Encapsulate bitset to provide extended functionallity with signs and operators.
*
* \tparam bit_width
* This template argument specifies the width of the in and output connections.
*/
template <size_t bit_width>
class SignedBitset : public std::bitset<bit_width> {
public:
/**
* Default constructor
*/
SignedBitset() : std::bitset<bit_width>() {}
/**
* Constructor with default value.
*/
SignedBitset(long long __val) : std::bitset<bit_width>(__val) {}
/**
* Default destructor
*/
~SignedBitset() {}
/** \brief Get the sign of the SignedBitset.
*
* \return int
* Returns the sign: -1 for negative, 0 for zero or 1 for positive.
*/
int sign() const {
//return this->to_llong() < 0 ? -1 : this->to_llong() == 0 ? 0 : 1;
return this->test(bit_width - 1) ? -1 : this->none() ? 0 : 1;
}
/** \brief Return the value of the bitset cast to long (with proper sign).
*/
long to_long(void) const {
struct {long x:bit_width;} s;
return s.x = this->to_ulong();
}
/** \brief Return the value of the bitset cast to long long (with proper sign).
*/
long long to_llong(void) const {
struct {long long x:bit_width;} s;
return s.x = this->to_ullong();
}
/** \brief Operator+ : Add 2 SignedBitsets and return a new SignedBitsets.
*
* \param __rhs
* The bitset to add to this bitset.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the sum.
*/
SignedBitset<bit_width> operator+(const SignedBitset<bit_width>& __rhs) const {
// std::cout << "addition: " << this->to_llong() << " + "
// << __rhs.to_llong() << " = "
// << (this->to_llong() + __rhs.to_llong()) << std::endl;
return SignedBitset<bit_width>(this->to_llong() + __rhs.to_llong());
}
/** \brief Operator+= : Add given SignedBitsets to this one.
*
* \param __rhs
* The bitset to add to this bitset.
*
* \return SignedBitset<bit_width>
* This bitset.
*/
SignedBitset<bit_width>& operator+=(const SignedBitset<bit_width>& __rhs) {
return *this = *this + __rhs;
}
/** \brief Operator- : Negate this bitset.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the negated value of this bitset.
*/
SignedBitset<bit_width> operator-(void) const {
return SignedBitset<bit_width>(-this->to_llong());
}
/** \brief Operator- : Subtract given SignedBitsets from this one by making the sum of the negation.
*
* \param __rhs
* The bitset to subtract from this bitset.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the subtraction.
*/
SignedBitset<bit_width> operator-(const SignedBitset<bit_width>& __rhs) const {
return -(-*this + __rhs);
}
/** \brief Operator-= : Subtract given SignedBitsets from this one.
*
* \param __rhs
* The bitset to subtract from this bitset.
*
* \return SignedBitset<bit_width>
* This bitset.
*/
SignedBitset<bit_width>& operator-=(const SignedBitset<bit_width>& __rhs) {
return *this = *this - __rhs;
}
/** \brief Operator* : Multiply given SignedBitsets with this one.
*
* \param __rhs
* The bitset to multiply with this bitset.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the multiplication.
*/
SignedBitset<bit_width> operator*(const SignedBitset<bit_width>& __rhs) const {
return SignedBitset<bit_width>(this->to_llong() * __rhs.to_llong());
}
/** \brief Operator*= : Multiply given SignedBitsets with this one.
*
* \param __rhs
* The bitset to multiply with this bitset.
*
* \return SignedBitset<bit_width>
* This bitset.
*/
SignedBitset<bit_width>& operator*=(const SignedBitset<bit_width>& __rhs) {
return *this = *this * __rhs;
}
/** \brief Operator/ : Divide this SignedBitsets by the given one (no floating point deivision).
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the division.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
SignedBitset<bit_width> operator/(const SignedBitset<bit_width>& __rhs) const {
#ifdef THROW_EXCEPTIONS
if (__rhs.none())
throw Exceptions::DivideByZeroException("SignedBitset");
#endif
return SignedBitset<bit_width>(this->to_llong() / __rhs.to_llong());
}
/** \brief Operator/= : Divide this SignedBitsets by the given one (no floating point deivision).
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return SignedBitset<bit_width>
* This bitset.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
SignedBitset<bit_width>& operator/=(const SignedBitset<bit_width>& __rhs) {
return *this = *this / __rhs;
}
/** \brief Operator% : Get the Modulo from dividing this SignedBitsets by the given one.
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return SignedBitset<bit_width>
* A new SignedBitset with the division.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
SignedBitset<bit_width> operator%(const SignedBitset<bit_width>& __rhs) const {
#ifdef THROW_EXCEPTIONS
if (__rhs.none())
throw Exceptions::DivideByZeroException("SignedBitset");
#endif
return SignedBitset<bit_width>(this->to_llong() % __rhs.to_llong());
}
/** \brief Operator% : Get the Modulo from dividing this SignedBitsets by the given one.
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return SignedBitset<bit_width>
* This bitset.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
SignedBitset<bit_width>& operator%=(const SignedBitset<bit_width>& __rhs) {
return *this = *this % __rhs;
}
/** \brief Compare this bitset to the given one.
*
* \param __rhs
* The bitset to compare to.
*
* \return SignedBitset<bit_width>
* The result as given by the sign of this subtracted by the given bitset.
*/
SignedBitset<bit_width> compareTo(const SignedBitset<bit_width>& __rhs) const {
return SignedBitset<bit_width>((*this - __rhs).sign());
}
/** \brief Add the signed value to a given stream (`os << SignedBitset`).
*/
friend std::ostream& operator<<(std::ostream &os, SignedBitset &s) {
os << s.to_string() << " ==> " << s.to_llong();
return os;
}
/** \brief Add the signed value to a given stream (`os << SignedBitset`).
*/
friend std::ostream& operator<<(std::ostream &os, SignedBitset *s) {
return os << *s;
}
};
}
#endif // SIGNEDBITSET_HPP