From f0c5ac3c8222d4d89b8e2d2a726427a7ec64e538 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 10 Dec 2025 17:20:14 +0100 Subject: kstd: extract bitwise enum operations --- .clang-format | 2 + .../include/x86_64/cpu/impl/control_registers.hpp | 77 +++++----------------- .../x86_64/cpu/impl/model_specific_register.hpp | 44 ++++--------- arch/x86_64/include/x86_64/memory/page_table.hpp | 31 ++++----- libs/kstd/CMakeLists.txt | 2 + libs/kstd/include/kstd/ext/bitfield_enum | 64 ++++++++++++++++++ 6 files changed, 110 insertions(+), 110 deletions(-) create mode 100644 libs/kstd/include/kstd/ext/bitfield_enum diff --git a/.clang-format b/.clang-format index a47e396..e54cb03 100644 --- a/.clang-format +++ b/.clang-format @@ -61,6 +61,8 @@ IncludeCategories: Priority: 110 - Regex: '"[[:alnum:]._\/]+\.hpp"' Priority: 300 + - Regex: '' + Priority: 400 - Regex: '<[[:alnum:]._\/]+\.hpp>' Priority: 600 - Regex: '<[[:alnum:]._]+(?!\.(h|hpp))>' diff --git a/arch/x86_64/include/x86_64/cpu/impl/control_registers.hpp b/arch/x86_64/include/x86_64/cpu/impl/control_registers.hpp index d892360..0c2254a 100644 --- a/arch/x86_64/include/x86_64/cpu/impl/control_registers.hpp +++ b/arch/x86_64/include/x86_64/cpu/impl/control_registers.hpp @@ -1,8 +1,12 @@ #ifndef TEACHOS_X86_64_CPU_IMPL_CONTROL_REGISTERS_HPP #define TEACHOS_X86_64_CPU_IMPL_CONTROL_REGISTERS_HPP +// IWYU pragma: private + #include "kapi/memory/address.hpp" +#include + #include #include #include @@ -83,72 +87,12 @@ namespace teachos::cpu::x86_64 paging = 1uz << 31 }; - constexpr auto operator|(cr0_flags lhs, cr0_flags rhs) -> cr0_flags - { - return static_cast(static_cast>(lhs) | - static_cast>(rhs)); - } - - constexpr auto operator|=(cr0_flags & lhs, cr0_flags rhs) -> cr0_flags & - { - lhs = lhs | rhs; - return lhs; - } - - constexpr auto operator&(cr0_flags lhs, cr0_flags rhs) -> cr0_flags - { - return static_cast(static_cast>(lhs) & - static_cast>(rhs)); - } - - constexpr auto operator&=(cr0_flags & lhs, cr0_flags rhs) -> cr0_flags & - { - lhs = lhs & rhs; - return lhs; - } - - constexpr auto operator~(cr0_flags lhs) -> cr0_flags - { - // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - return static_cast(~static_cast>(lhs)); - } - enum struct cr3_flags : std::uint64_t { page_level_write_through = 1uz << 0, page_level_cache_disable = 1uz << 1, }; - constexpr auto operator|(cr3_flags lhs, cr3_flags rhs) -> cr3_flags - { - return static_cast(static_cast>(lhs) | - static_cast>(rhs)); - } - - constexpr auto operator|=(cr3_flags & lhs, cr3_flags rhs) -> cr3_flags & - { - lhs = lhs | rhs; - return lhs; - } - - constexpr auto operator&(cr3_flags lhs, cr3_flags rhs) -> cr3_flags - { - return static_cast(static_cast>(lhs) & - static_cast>(rhs)); - } - - constexpr auto operator&=(cr3_flags & lhs, cr3_flags rhs) -> cr3_flags & - { - lhs = lhs & rhs; - return lhs; - } - - constexpr auto operator~(cr3_flags lhs) -> cr3_flags - { - // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - return static_cast(~static_cast>(lhs)); - } - struct cr3_value { constexpr cr3_value() = default; @@ -193,4 +137,17 @@ namespace teachos::cpu::x86_64 } // namespace teachos::cpu::x86_64 +namespace kstd::ext +{ + template<> + struct is_bitfield_enum : std::true_type + { + }; + + template<> + struct is_bitfield_enum : std::true_type + { + }; +} // namespace kstd::ext + #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/cpu/impl/model_specific_register.hpp b/arch/x86_64/include/x86_64/cpu/impl/model_specific_register.hpp index 8a41a8a..ff2c8ad 100644 --- a/arch/x86_64/include/x86_64/cpu/impl/model_specific_register.hpp +++ b/arch/x86_64/include/x86_64/cpu/impl/model_specific_register.hpp @@ -1,6 +1,10 @@ #ifndef TEACHOS_X86_64_CPU_IMPL_MODEL_SPECIFIC_REGISTER_HPP #define TEACHOS_X86_64_CPU_IMPL_MODEL_SPECIFIC_REGISTER_HPP +// IWYU pragma: private + +#include + #include #include #include @@ -73,40 +77,20 @@ namespace teachos::cpu::x86_64 execute_disable_bit_enable = 1uz << 11, }; - constexpr auto operator|(ia32_efer_flags lhs, ia32_efer_flags rhs) -> ia32_efer_flags - { - return static_cast(static_cast>(lhs) | - static_cast>(rhs)); - } - - constexpr auto operator|=(ia32_efer_flags & lhs, ia32_efer_flags rhs) -> ia32_efer_flags & - { - lhs = lhs | rhs; - return lhs; - } - - constexpr auto operator&(ia32_efer_flags lhs, ia32_efer_flags rhs) -> ia32_efer_flags - { - return static_cast(static_cast>(lhs) & - static_cast>(rhs)); - } - - constexpr auto operator&=(ia32_efer_flags & lhs, ia32_efer_flags rhs) -> ia32_efer_flags & - { - lhs = lhs & rhs; - return lhs; - } - - constexpr auto operator~(ia32_efer_flags lhs) -> ia32_efer_flags - { - // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - return static_cast(~static_cast>(lhs)); - } - } // namespace impl using i32_efer = impl::model_specific_register; } // namespace teachos::cpu::x86_64 +namespace kstd::ext +{ + + template<> + struct is_bitfield_enum : std::true_type + { + }; + +} // namespace kstd::ext + #endif \ No newline at end of file diff --git a/arch/x86_64/include/x86_64/memory/page_table.hpp b/arch/x86_64/include/x86_64/memory/page_table.hpp index ce34e23..22616e8 100644 --- a/arch/x86_64/include/x86_64/memory/page_table.hpp +++ b/arch/x86_64/include/x86_64/memory/page_table.hpp @@ -3,11 +3,14 @@ #include "kapi/memory.hpp" +#include + #include #include #include #include #include +#include #include namespace teachos::memory::x86_64 @@ -87,26 +90,6 @@ namespace teachos::memory::x86_64 std::array m_entries{}; }; - constexpr auto operator|(page_table::entry::flags lhs, page_table::entry::flags rhs) -> page_table::entry::flags - { - return std::bit_cast(std::to_underlying(lhs) | std::to_underlying(rhs)); - } - - constexpr auto operator|=(page_table::entry::flags & lhs, page_table::entry::flags rhs) -> page_table::entry::flags & - { - return lhs = lhs | rhs; - } - - constexpr auto operator&(page_table::entry::flags lhs, page_table::entry::flags rhs) -> page_table::entry::flags - { - return std::bit_cast(std::to_underlying(lhs) & std::to_underlying(rhs)); - } - - constexpr auto operator~(page_table::entry::flags flags) - { - return std::bit_cast(~std::to_underlying(flags)); - } - //! A recursively mapped page table. template requires(Level > 0uz && Level < 5uz) @@ -155,4 +138,12 @@ namespace teachos::memory::x86_64 } // namespace teachos::memory::x86_64 +namespace kstd::ext +{ + template<> + struct is_bitfield_enum : std::true_type + { + }; +} // namespace kstd::ext + #endif \ No newline at end of file diff --git a/libs/kstd/CMakeLists.txt b/libs/kstd/CMakeLists.txt index e0c551c..d83e704 100644 --- a/libs/kstd/CMakeLists.txt +++ b/libs/kstd/CMakeLists.txt @@ -22,6 +22,8 @@ target_sources("kstd" PUBLIC "include/kstd/bits/shared_ptr.hpp" "include/kstd/bits/unique_ptr.hpp" + "include/kstd/ext/bitfield_enum" + "include/kstd/asm_ptr" "include/kstd/memory" "include/kstd/mutex" diff --git a/libs/kstd/include/kstd/ext/bitfield_enum b/libs/kstd/include/kstd/ext/bitfield_enum new file mode 100644 index 0000000..327af45 --- /dev/null +++ b/libs/kstd/include/kstd/ext/bitfield_enum @@ -0,0 +1,64 @@ +#ifndef KSTD_EXT_BITFIELD_ENUM_HPP +#define KSTD_EXT_BITFIELD_ENUM_HPP + +#include +#include +#include + +namespace kstd::ext +{ + + template + requires std::is_enum_v + struct is_bitfield_enum : std::false_type + { + }; + + template + concept bitfield_enum = is_bitfield_enum::value; + +}; // namespace kstd::ext + +template +constexpr auto operator|(EnumType lhs, EnumType rhs) -> EnumType +{ + return std::bit_cast(std::to_underlying(lhs) | std::to_underlying(rhs)); +} + +template +constexpr auto operator|=(EnumType & lhs, EnumType rhs) -> EnumType & +{ + return lhs = lhs | rhs; +} + +template +constexpr auto operator&(EnumType lhs, EnumType rhs) -> EnumType +{ + return std::bit_cast(std::to_underlying(lhs) & std::to_underlying(rhs)); +} + +template +constexpr auto operator&=(EnumType & lhs, EnumType rhs) -> EnumType & +{ + return lhs = lhs & rhs; +} + +template +constexpr auto operator^(EnumType lhs, EnumType rhs) -> EnumType +{ + return std::bit_cast(std::to_underlying(lhs) ^ std::to_underlying(rhs)); +} + +template +constexpr auto operator^=(EnumType & lhs, EnumType rhs) -> EnumType & +{ + return lhs = lhs ^ rhs; +} + +template +constexpr auto operator~(EnumType lhs) -> EnumType +{ + return std::bit_cast(~std::to_underlying(lhs)); +} + +#endif \ No newline at end of file -- cgit v1.2.3