From 5574542bfe6c2db1518f6dd1ce8051628db7970d Mon Sep 17 00:00:00 2001 From: DarkSharpness <2040703891@qq.com> Date: Thu, 11 Jul 2024 20:56:06 +0800 Subject: [PATCH] feat(bit): complete operators for operation on bit type --- include/bit_op.h | 1 - include/operator.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) delete mode 100644 include/bit_op.h create mode 100644 include/operator.h diff --git a/include/bit_op.h b/include/bit_op.h deleted file mode 100644 index 6f70f09..0000000 --- a/include/bit_op.h +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/include/operator.h b/include/operator.h new file mode 100644 index 0000000..3014e80 --- /dev/null +++ b/include/operator.h @@ -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 +constexpr auto cast(const _Tp& value) { + return static_cast (value); +} + +template +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 +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 +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 +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 +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 +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 +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 +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