feat(bit): complete operators for operation on bit type

This commit is contained in:
DarkSharpness
2024-07-11 20:56:06 +08:00
parent 1693df2820
commit 5574542bfe
2 changed files with 75 additions and 1 deletions

View File

@ -1 +0,0 @@
#pragma once

75
include/operator.h Normal file
View File

@ -0,0 +1,75 @@
#pragma once
#include "bit.h"
namespace dark {
using dark::concepts::bit_type;
using dark::concepts::int_type;
using dark::concepts::bit_match;
template <typename _Tp>
constexpr auto cast(const _Tp& value) {
return static_cast <max_size_t> (value);
}
template <typename _Tp, typename _Up>
consteval auto get_common_length() -> std::size_t {
static_assert(bit_match <_Tp, _Up>);
if constexpr (bit_type <_Tp>) {
return _Tp::_Bit_Len;
} else {
static_assert(bit_type <_Up>, "Invalid common length");
return _Up::_Bit_Len;
}
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator + (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator - (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator * (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator / (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator & (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator | (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
template <typename _Tp, typename _Up>
requires bit_match <_Tp, _Up>
constexpr auto operator ^ (const _Tp &lhs, const _Up &rhs) {
constexpr auto _Len = get_common_length <_Tp, _Up>();
return Bit <_Len> (cast(lhs) + cast(rhs));
}
} // namespace dark