Skip to content

Commit

Permalink
Merge pull request #44 from elbeno/use-stdx-bit
Browse files Browse the repository at this point in the history
🔥 Replace `compute_mask` with `stdx::bit_mask`
  • Loading branch information
elbeno authored Mar 13, 2024
2 parents ed7869f + 3256dcc commit 45ac88d
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(cmake/get_cpm.cmake)
cpmaddpackage("gh:intel/cicd-repo-infrastructure#main")

add_versioned_package("gh:boostorg/mp11#boost-1.83.0")
add_versioned_package("gh:intel/cpp-std-extensions#dcbb8ba")
add_versioned_package("gh:intel/cpp-std-extensions#f67b432")
add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#eb4239f")
add_versioned_package("gh:intel/safe-arithmetic#9ea549a")

Expand Down
3 changes: 2 additions & 1 deletion include/groov/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <groov/make_spec.hpp>
#include <groov/resolve.hpp>

#include <stdx/bit.hpp>
#include <stdx/ct_string.hpp>
#include <stdx/type_traits.hpp>

Expand Down Expand Up @@ -103,7 +104,7 @@ struct field : named_container<Name, SubFields...> {
detail::compute_identity<id_spec_t, RegType, Msb, Lsb>();

template <std::unsigned_integral RegType>
constexpr static auto mask = detail::compute_mask<RegType, Msb, Lsb>();
constexpr static auto mask = stdx::bit_mask<RegType, Msb, Lsb>();

template <std::unsigned_integral RegType>
constexpr static auto identity_mask =
Expand Down
22 changes: 3 additions & 19 deletions include/groov/identity.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <stdx/bit.hpp>
#include <stdx/utility.hpp>

#include <boost/mp11/algorithm.hpp>
Expand All @@ -11,23 +12,6 @@
#include <type_traits>

namespace groov {
namespace detail {
template <std::unsigned_integral T, std::size_t Bit>
requires(Bit <= std::numeric_limits<T>::digits)
constexpr auto mask_bits() -> T {
if constexpr (Bit == std::numeric_limits<T>::digits) {
return std::numeric_limits<T>::max();
} else {
return (T{1} << Bit) - T{1};
}
}

template <std::unsigned_integral T, std::size_t Msb, std::size_t Lsb>
constexpr auto compute_mask() -> T {
return mask_bits<T, Msb + 1>() - mask_bits<T, Lsb>();
}
} // namespace detail

template <typename T>
concept identity_spec = requires {
{
Expand All @@ -40,7 +24,7 @@ template <typename I, std::unsigned_integral T, std::size_t Msb,
std::size_t Lsb>
constexpr auto compute_identity_mask() {
if constexpr (identity_spec<I>) {
return compute_mask<T, Msb, Lsb>();
return stdx::bit_mask<T, Msb, Lsb>();
} else {
return T{};
}
Expand Down Expand Up @@ -68,7 +52,7 @@ struct zero {
struct one {
template <std::unsigned_integral T, std::size_t Msb, std::size_t Lsb>
constexpr static auto identity() -> T {
return detail::compute_mask<T, Msb, Lsb>();
return stdx::bit_mask<T, Msb, Lsb>();
}
};
struct any {
Expand Down
20 changes: 0 additions & 20 deletions test/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,3 @@ TEST_CASE("all fields inside a register with fields and subfields",
std::is_same_v<groov::detail::all_fields_t<boost::mp11::mp_list<R>>,
boost::mp11::mp_list<SubF00, SubF01, F1>>);
}

TEST_CASE("compute mask (whole register)", "[config]") {
constexpr auto m = groov::detail::compute_mask<std::uint32_t, 31, 0>();
static_assert(m == std::numeric_limits<std::uint32_t>::max());
}

TEST_CASE("mask bits (field at top of register)", "[config]") {
constexpr auto m = groov::detail::compute_mask<std::uint32_t, 31, 31>();
static_assert(m == 0x8000'0000u);
}

TEST_CASE("mask bits (field in middle of register)", "[config]") {
constexpr auto m = groov::detail::compute_mask<std::uint32_t, 16, 15>();
static_assert(m == 0x0001'8000u);
}

TEST_CASE("mask bits (field at bottom of register)", "[config]") {
constexpr auto m = groov::detail::compute_mask<std::uint32_t, 0, 0>();
static_assert(m == 0x0000'0001u);
}
24 changes: 0 additions & 24 deletions test/identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@ TEST_CASE("any id spec returns a zeroed identity", "[identity]") {
static_assert(i == 0);
}

TEST_CASE("compute a mask (whole range)", "[identity]") {
constexpr auto m = groov::detail::compute_mask<std::uint64_t, 63, 0>();
static_assert(m == std::numeric_limits<std::uint64_t>::max());
static_assert(std::is_same_v<decltype(m), std::uint64_t const>);
}

TEST_CASE("compute a mask (low bits)", "[identity]") {
constexpr auto m = groov::detail::compute_mask<std::uint8_t, 1, 0>();
static_assert(m == 0b0000'0011);
static_assert(std::is_same_v<decltype(m), std::uint8_t const>);
}

TEST_CASE("compute a mask (mid bits)", "[identity]") {
constexpr auto m = groov::detail::compute_mask<std::uint8_t, 4, 3>();
static_assert(m == 0b0001'1000);
static_assert(std::is_same_v<decltype(m), std::uint8_t const>);
}

TEST_CASE("compute a mask (high bits)", "[identity]") {
constexpr auto m = groov::detail::compute_mask<std::uint8_t, 7, 6>();
static_assert(m == 0b01100'0000);
static_assert(std::is_same_v<decltype(m), std::uint8_t const>);
}

TEST_CASE("replace write function has no identity", "[identity]") {
static_assert(groov::write_function<groov::w::replace>);
static_assert(std::is_same_v<groov::w::replace::id_spec, groov::id::none>);
Expand Down

0 comments on commit 45ac88d

Please sign in to comment.