diff --git a/python/src/bit.cpp b/python/src/bit.cpp index 6909900f5..d51233bec 100644 --- a/python/src/bit.cpp +++ b/python/src/bit.cpp @@ -47,11 +47,14 @@ void init_bit(py::module &m) { .def("value", &RegisterValue::value) .def(py::self == py::self) .def(py::self != py::self) - .def("__or__", [](const RegisterValue& v, uint32_t rhs) { - return v | rhs; + .def("__or__", [](const RegisterValue &lhs, const RegisterValue &rhs) { + return lhs | rhs; }) - .def("__ior__", [](RegisterValue &v, uint32_t rhs) -> RegisterValue& { - v |= rhs; - return v; + .def("__or__", [](const RegisterValue& lhs, uint32_t rhs) { + return lhs | rhs; + }) + .def("__ior__", [](RegisterValue &lhs, uint32_t rhs) -> RegisterValue& { + lhs |= rhs; + return lhs; }, py::return_value_policy::reference_internal); } diff --git a/slsSupportLib/include/sls/bit_utils.h b/slsSupportLib/include/sls/bit_utils.h index 649ca8bb5..414c39d86 100644 --- a/slsSupportLib/include/sls/bit_utils.h +++ b/slsSupportLib/include/sls/bit_utils.h @@ -29,7 +29,7 @@ class RegisterAddress { constexpr explicit RegisterAddress(uint32_t value) : value_(value) {} explicit RegisterAddress(const std::string &value); std::string str() const; - uint32_t value() const noexcept { return value_; } + constexpr uint32_t value() const noexcept { return value_; } constexpr bool operator==(const RegisterAddress &other) const { return (value_ == other.value_); @@ -49,8 +49,8 @@ class BitAddress { BitAddress(RegisterAddress address, uint32_t bitPosition); BitAddress(const std::string &address, const std::string &bitPosition); std::string str() const; - RegisterAddress address() const noexcept { return addr_; } - uint32_t bitPosition() const noexcept { return bitPos_; } + constexpr RegisterAddress address() const noexcept { return addr_; } + constexpr uint32_t bitPosition() const noexcept { return bitPos_; } constexpr bool operator==(const BitAddress &other) const { return (addr_ == other.addr_ && bitPos_ == other.bitPos_); @@ -70,14 +70,25 @@ class RegisterValue { explicit RegisterValue(const std::string &value); std::string str() const; - uint32_t value() const noexcept { return value_; } + constexpr uint32_t value() const noexcept { return value_; } - RegisterValue &operator|=(uint32_t rhs) noexcept { + constexpr RegisterValue &operator|=(const RegisterValue &rhs) noexcept { + value_ |= rhs.value(); + return *this; + } + + constexpr RegisterValue operator|(const RegisterValue &rhs) const noexcept { + RegisterValue tmp(*this); + tmp |= rhs; + return tmp; + } + + constexpr RegisterValue &operator|=(uint32_t rhs) noexcept { value_ |= rhs; return *this; } - RegisterValue operator|(uint32_t rhs) const noexcept { + constexpr RegisterValue operator|(uint32_t rhs) const noexcept { RegisterValue tmp(*this); tmp |= rhs; return tmp; diff --git a/slsSupportLib/tests/test-bit_utils.cpp b/slsSupportLib/tests/test-bit_utils.cpp index 7fe022d67..d69a969b3 100644 --- a/slsSupportLib/tests/test-bit_utils.cpp +++ b/slsSupportLib/tests/test-bit_utils.cpp @@ -84,6 +84,7 @@ TEST_CASE("Convert RegisterValue using classes ", "[support][.bit_utils]") { CHECK(reg1.str() == vec_ans[i]); CHECK((reg0 | 0xffffffffu) == RegisterValue(0xffffffffu)); CHECK((reg0 | 0x0) == reg0); + CHECK((reg0 | reg1) == reg0); } }