feat(bit): add a demo and add some functions for the bit class
This commit is contained in:
39
demo/bit.cpp
Normal file
39
demo/bit.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include "../include/tools"
|
||||||
|
|
||||||
|
signed main() {
|
||||||
|
[[maybe_unused]]
|
||||||
|
Bit <1> a; // Create a 1-bit object, default to 0
|
||||||
|
|
||||||
|
Bit <10> b(10); // Create a 10-bit object
|
||||||
|
|
||||||
|
auto ref = b.slice <4, 2> (); // A copy of [2, 3, 4] bit
|
||||||
|
|
||||||
|
std::cout << b << std::endl; // 10
|
||||||
|
|
||||||
|
b.set <3, 1> (ref); // Set [1, 2, 3] bit to [2, 3, 4] bit
|
||||||
|
|
||||||
|
std::cout << b << std::endl; // 4
|
||||||
|
|
||||||
|
b.set <0> (~a); // Set the 0-th bit to ~a (= 1 in this case)
|
||||||
|
|
||||||
|
std::cout << b << std::endl; // 5
|
||||||
|
|
||||||
|
auto sec = b.slice <2> (); // A copy of the 2-th bit (= 1 in this case)
|
||||||
|
|
||||||
|
auto d = sec.zero_extend(); // Zero extend (default to 32-bit)
|
||||||
|
|
||||||
|
std::cout << d << std::endl; // 1
|
||||||
|
|
||||||
|
auto c = sec.sign_extend <3> (); // Sign extend (default to 32-bit)
|
||||||
|
|
||||||
|
std::cout << c << std::endl; // 7
|
||||||
|
|
||||||
|
// c + d; // Error: different size
|
||||||
|
// a += 1; // Error: no assignment-operation operator
|
||||||
|
|
||||||
|
auto e = b - 1; // normal integer can be assumed as any size
|
||||||
|
|
||||||
|
std::cout << e << std::endl; // 4
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -19,9 +19,7 @@ struct MyModule : Input, Output, private Content {
|
|||||||
using Tags = SyncTags <Input, Output, Content>;
|
using Tags = SyncTags <Input, Output, Content>;
|
||||||
friend class Visitor;
|
friend class Visitor;
|
||||||
|
|
||||||
void demo() {
|
void demo() { this->d <= this->a + this->b; }
|
||||||
this->d <= this->a + this->b;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
signed main() {
|
signed main() {
|
@ -26,7 +26,7 @@ struct Bit {
|
|||||||
public:
|
public:
|
||||||
static constexpr std::size_t _Bit_Len = _Nm;
|
static constexpr std::size_t _Bit_Len = _Nm;
|
||||||
|
|
||||||
constexpr Bit(target_size_t data = 0) : _M_data(data) {}
|
constexpr Bit(target_size_t data = {}) : _M_data(data) {}
|
||||||
|
|
||||||
template <std::size_t ..._Lens>
|
template <std::size_t ..._Lens>
|
||||||
constexpr Bit(Bit<_Lens> ...args) requires ((_Lens + ...) == _Nm) :
|
constexpr Bit(Bit<_Lens> ...args) requires ((_Lens + ...) == _Nm) :
|
||||||
@ -54,6 +54,16 @@ struct Bit {
|
|||||||
|
|
||||||
constexpr operator target_size_t() const { return this->_M_data; }
|
constexpr operator target_size_t() const { return this->_M_data; }
|
||||||
|
|
||||||
|
template <std::size_t _Hi, std::size_t _Lo = _Hi>
|
||||||
|
constexpr auto set(Bit <_Hi - _Lo + 1> val) {
|
||||||
|
static_cast <void> (this->slice<_Hi, _Lo>());
|
||||||
|
const auto mask = // Mask those bit in the middle
|
||||||
|
(target_size_t(1) << (_Hi + 1)) - (target_size_t(1) << _Lo);
|
||||||
|
const auto data = // Set those bit in the middle
|
||||||
|
static_cast <target_size_t> (val) << _Lo;
|
||||||
|
this->_M_data = (this->_M_data & ~mask) | data;
|
||||||
|
}
|
||||||
|
|
||||||
template <std::size_t _Hi, std::size_t _Lo = _Hi>
|
template <std::size_t _Hi, std::size_t _Lo = _Hi>
|
||||||
constexpr auto slice() const -> Bit <_Hi - _Lo + 1> {
|
constexpr auto slice() const -> Bit <_Hi - _Lo + 1> {
|
||||||
static_assert(_Lo <= _Hi, "Bit::slice: _Lo should be no greater than _Hi");
|
static_assert(_Lo <= _Hi, "Bit::slice: _Lo should be no greater than _Hi");
|
||||||
|
Reference in New Issue
Block a user