feat(bit): complete a bit simulate library

This commit is contained in:
DarkSharpness
2024-07-09 22:54:00 +08:00
parent 68dd99a826
commit 39254c4557
3 changed files with 245 additions and 0 deletions

110
include/bitop.h Normal file
View File

@ -0,0 +1,110 @@
#pragma once
#include "bit.h"
namespace dark::bits {
template <typename _Tp>
concept NonBit = !BitType<_Tp>;
template <typename _Tp>
auto cast(_Tp &&val) -> target_size_t {
return static_cast<target_size_t>(val);
}
template <BitType _Lhs, BitType _Rhs>
constexpr auto operator + (_Lhs lhs, _Rhs rhs) {
static_assert(_Lhs::_Bit_Len == _Rhs::_Bit_Len,
"operator +: lhs and rhs should have the same length");
return _Lhs { cast(lhs) + cast(rhs) };
}
template <BitType _Lhs, BitType _Rhs>
constexpr auto operator - (_Lhs lhs, _Rhs rhs) {
static_assert(_Lhs::_Bit_Len == _Rhs::_Bit_Len,
"operator -: lhs and rhs should have the same length");
return _Lhs { cast(lhs) - cast(rhs) };
}
template <BitType _Lhs, BitType _Rhs>
constexpr auto operator & (_Lhs lhs, _Rhs rhs) {
static_assert(_Lhs::_Bit_Len == _Rhs::_Bit_Len,
"operator &: lhs and rhs should have the same length");
return _Lhs { cast(lhs) & cast(rhs) };
}
template <BitType _Lhs, BitType _Rhs>
constexpr auto operator | (_Lhs lhs, _Rhs rhs) {
static_assert(_Lhs::_Bit_Len == _Rhs::_Bit_Len,
"operator |: lhs and rhs should have the same length");
return _Lhs { cast(lhs) | cast(rhs) };
}
template <BitType _Lhs, BitType _Rhs>
constexpr auto operator ^ (_Lhs lhs, _Rhs rhs) {
static_assert(_Lhs::_Bit_Len == _Rhs::_Bit_Len,
"operator ^: lhs and rhs should have the same length");
return _Lhs { cast(lhs) ^ cast(rhs) };
}
template <BitType _Lhs, NonBit _Rhs>
constexpr auto operator + (_Lhs lhs, _Rhs rhs) {
return _Lhs { cast(lhs) + cast(rhs) };
}
template <BitType _Lhs, NonBit _Rhs>
constexpr auto operator - (_Lhs lhs, _Rhs rhs) {
return _Lhs { cast(lhs) - cast(rhs) };
}
template <BitType _Lhs, NonBit _Rhs>
constexpr auto operator & (_Lhs lhs, _Rhs rhs) {
return _Lhs { cast(lhs) & cast(rhs) };
}
template <BitType _Lhs, NonBit _Rhs>
constexpr auto operator | (_Lhs lhs, _Rhs rhs) {
return _Lhs { cast(lhs) | cast(rhs) };
}
template <BitType _Lhs, NonBit _Rhs>
constexpr auto operator ^ (_Lhs lhs, _Rhs rhs) {
return _Lhs { cast(lhs) ^ cast(rhs) };
}
template <NonBit _Lhs, BitType _Rhs>
constexpr auto operator + (_Lhs lhs, _Rhs rhs) {
return _Rhs { cast(lhs) + cast(rhs) };
}
template <NonBit _Lhs, BitType _Rhs>
constexpr auto operator - (_Lhs lhs, _Rhs rhs) {
return _Rhs { cast(lhs) - cast(rhs) };
}
template <NonBit _Lhs, BitType _Rhs>
constexpr auto operator & (_Lhs lhs, _Rhs rhs) {
return _Rhs { cast(lhs) & cast(rhs) };
}
template <NonBit _Lhs, BitType _Rhs>
constexpr auto operator | (_Lhs lhs, _Rhs rhs) {
return _Rhs { cast(lhs) | cast(rhs) };
}
template <NonBit _Lhs, BitType _Rhs>
constexpr auto operator ^ (_Lhs lhs, _Rhs rhs) {
return _Rhs { cast(lhs) ^ cast(rhs) };
}
template <BitType _Tp>
constexpr auto operator ~ (_Tp val) {
return _Tp { ~cast(val) };
}
template <BitType _Tp>
constexpr auto operator ! (_Tp val) {
return _Tp { !cast(val) };
}
} // namespace dark::bits