From 1918c5e9da22b74305a0854c02e8955bb80c0f59 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Thu, 9 Oct 2025 12:27:13 +0000 Subject: [PATCH] refactor: add helper to create uint64 bitmask Signed-off-by: Mateusz Jablonski --- shared/source/aub/aub_helper.cpp | 3 ++- shared/source/aub_mem_dump/aub_mem_dump.h | 4 ---- ...eiver_simulated_common_hw_xehp_and_later.inl | 5 +++-- ...eam_receiver_simulated_common_hw_gen12lp.cpp | 5 +++-- shared/source/helpers/bit_helpers.h | 7 ++++++- shared/test/unit_test/aub/aub_helper_tests.cpp | 17 +++++++++-------- ...ub_command_stream_receiver_tests_gen12lp.inl | 3 ++- ...bx_command_stream_receiver_tests_gen12lp.inl | 7 ++++--- .../unit_test/helpers/bit_helpers_tests.cpp | 7 ++++++- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/shared/source/aub/aub_helper.cpp b/shared/source/aub/aub_helper.cpp index bf67c9bce7..f87ab8031e 100644 --- a/shared/source/aub/aub_helper.cpp +++ b/shared/source/aub/aub_helper.cpp @@ -11,6 +11,7 @@ #include "shared/source/aub_mem_dump/page_table_entry_bits.h" #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/basic_math.h" +#include "shared/source/helpers/bit_helpers.h" #include "shared/source/helpers/constants.h" #include "shared/source/helpers/gfx_core_helper.h" #include "shared/source/helpers/string.h" @@ -58,7 +59,7 @@ uint64_t AubHelper::getTotalMemBankSize(const ReleaseHelper *releaseHelper) { } uint64_t AubHelper::getPTEntryBits(uint64_t pdEntryBits) { - pdEntryBits &= ~BIT(PageTableEntry::localMemoryBit); + pdEntryBits &= ~makeBitMask(); return pdEntryBits; } diff --git a/shared/source/aub_mem_dump/aub_mem_dump.h b/shared/source/aub_mem_dump/aub_mem_dump.h index f65c49ab39..5c4ff9b27f 100644 --- a/shared/source/aub_mem_dump/aub_mem_dump.h +++ b/shared/source/aub_mem_dump/aub_mem_dump.h @@ -10,10 +10,6 @@ namespace AubMemDump { -#ifndef BIT -#define BIT(x) (((uint64_t)1) << (x)) -#endif - typedef CmdServicesMemTraceVersion::SteppingValues SteppingValues; typedef CmdServicesMemTraceMemoryWrite::DataTypeHintValues DataTypeHintValues; diff --git a/shared/source/command_stream/command_stream_receiver_simulated_common_hw_xehp_and_later.inl b/shared/source/command_stream/command_stream_receiver_simulated_common_hw_xehp_and_later.inl index 70d61571c3..10d8d5d228 100644 --- a/shared/source/command_stream/command_stream_receiver_simulated_common_hw_xehp_and_later.inl +++ b/shared/source/command_stream/command_stream_receiver_simulated_common_hw_xehp_and_later.inl @@ -8,6 +8,7 @@ #include "shared/source/aub_mem_dump/page_table_entry_bits.h" #include "shared/source/command_stream/command_stream_receiver_simulated_common_hw_base.inl" #include "shared/source/debug_settings/debug_settings_manager.h" +#include "shared/source/helpers/bit_helpers.h" #include "shared/source/helpers/gfx_core_helper.h" #include "shared/source/memory_manager/memory_banks.h" #include "shared/source/memory_manager/memory_pool.h" @@ -19,9 +20,9 @@ template uint64_t CommandStreamReceiverSimulatedCommonHw::getPPGTTAdditionalBits(GraphicsAllocation *gfxAllocation) { if (debugManager.flags.AUBDumpForceAllToLocalMemory.get() || (gfxAllocation && gfxAllocation->getMemoryPool() == MemoryPool::localMemory)) { - return BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit) | BIT(PageTableEntry::localMemoryBit); + return makeBitMask(); } - return BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit); + return makeBitMask(); } template diff --git a/shared/source/gen12lp/command_stream_receiver_simulated_common_hw_gen12lp.cpp b/shared/source/gen12lp/command_stream_receiver_simulated_common_hw_gen12lp.cpp index 0e3c2b1647..a613305006 100644 --- a/shared/source/gen12lp/command_stream_receiver_simulated_common_hw_gen12lp.cpp +++ b/shared/source/gen12lp/command_stream_receiver_simulated_common_hw_gen12lp.cpp @@ -7,6 +7,7 @@ #include "shared/source/command_stream/command_stream_receiver_simulated_common_hw_base.inl" #include "shared/source/gen12lp/hw_cmds_base.h" +#include "shared/source/helpers/bit_helpers.h" namespace NEO { typedef Gen12LpFamily Family; @@ -18,8 +19,8 @@ uint32_t CommandStreamReceiverSimulatedCommonHw::getMemoryBankForGtt( template <> uint64_t CommandStreamReceiverSimulatedCommonHw::getPPGTTAdditionalBits(GraphicsAllocation *gfxAllocation) { - return BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit) | - ((gfxAllocation && gfxAllocation->getMemoryPool() == MemoryPool::localMemory) ? BIT(PageTableEntry::localMemoryBit) : 0); + return makeBitMask() | + ((gfxAllocation && gfxAllocation->getMemoryPool() == MemoryPool::localMemory) ? makeBitMask() : 0); } template class CommandStreamReceiverSimulatedCommonHw; diff --git a/shared/source/helpers/bit_helpers.h b/shared/source/helpers/bit_helpers.h index 9f197389f1..a573e6830e 100644 --- a/shared/source/helpers/bit_helpers.h +++ b/shared/source/helpers/bit_helpers.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2024 Intel Corporation + * Copyright (C) 2019-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -49,4 +49,9 @@ constexpr uint32_t getMostSignificantSetBitIndex(uint64_t field) { return index; } +template +constexpr uint64_t makeBitMask() { + return ((1ull << bits) | ...); +} + } // namespace NEO diff --git a/shared/test/unit_test/aub/aub_helper_tests.cpp b/shared/test/unit_test/aub/aub_helper_tests.cpp index a1dc561f6a..9ba0aae018 100644 --- a/shared/test/unit_test/aub/aub_helper_tests.cpp +++ b/shared/test/unit_test/aub/aub_helper_tests.cpp @@ -11,6 +11,7 @@ #include "shared/source/command_stream/command_stream_receiver_simulated_common_hw.h" #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/aligned_memory.h" +#include "shared/source/helpers/bit_helpers.h" #include "shared/source/helpers/gfx_core_helper.h" #include "shared/source/release_helper/release_helper.h" #include "shared/source/tbx/tbx_proto.h" @@ -22,9 +23,9 @@ using namespace NEO; TEST(AubHelper, WhenGetPtEntryBitsIsCalledThenEntryBitsAreNotMasked) { - uint64_t entryBits = BIT(PageTableEntry::presentBit) | - BIT(PageTableEntry::writableBit) | - BIT(PageTableEntry::userSupervisorBit); + uint64_t entryBits = makeBitMask(); uint64_t maskedEntryBits = AubHelper::getPTEntryBits(entryBits); EXPECT_EQ(entryBits, maskedEntryBits); } @@ -48,12 +49,12 @@ TEST(AubHelper, GivenMultipleSubDevicesWhenGettingDeviceCountThenCorrectValueIsR } TEST(AubHelper, WhenMaskPTEntryBitsIsCalledThenLocalMemoryBitIsMasked) { - uint64_t entryBits = BIT(PageTableEntry::presentBit) | - BIT(PageTableEntry::writableBit) | - BIT(PageTableEntry::userSupervisorBit) | - BIT(PageTableEntry::localMemoryBit); + uint64_t entryBits = makeBitMask(); uint64_t maskedEntryBits = AubHelper::getPTEntryBits(entryBits); - EXPECT_EQ(entryBits & ~BIT(PageTableEntry::localMemoryBit), maskedEntryBits); + EXPECT_EQ(entryBits & ~makeBitMask(), maskedEntryBits); } TEST(AubHelper, WhenHBMSizePerTileInGigabytesIsSetThenGetMemBankSizeReturnsCorrectValue) { diff --git a/shared/test/unit_test/gen12lp/aub_command_stream_receiver_tests_gen12lp.inl b/shared/test/unit_test/gen12lp/aub_command_stream_receiver_tests_gen12lp.inl index f61d995e17..c2809877d1 100644 --- a/shared/test/unit_test/gen12lp/aub_command_stream_receiver_tests_gen12lp.inl +++ b/shared/test/unit_test/gen12lp/aub_command_stream_receiver_tests_gen12lp.inl @@ -8,6 +8,7 @@ #include "shared/source/aub_mem_dump/page_table_entry_bits.h" #include "shared/source/command_stream/aub_command_stream_receiver_hw.h" #include "shared/source/gen12lp/hw_info.h" +#include "shared/source/helpers/bit_helpers.h" #include "shared/source/os_interface/os_context.h" #include "shared/test/common/fixtures/device_fixture.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" @@ -43,7 +44,7 @@ GEN12LPTEST_F(Gen12LPAubCommandStreamReceiverTests, givenGraphicsAlloctionWithLo MockGraphicsAllocation allocation(nullptr, 0); allocation.overrideMemoryPool(MemoryPool::localMemory); auto bits = aubCsr->getPPGTTAdditionalBits(&allocation); - constexpr uint64_t expectedBits = BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit) | BIT(PageTableEntry::localMemoryBit); + constexpr uint64_t expectedBits = makeBitMask(); EXPECT_EQ(expectedBits, bits); } diff --git a/shared/test/unit_test/gen12lp/tbx_command_stream_receiver_tests_gen12lp.inl b/shared/test/unit_test/gen12lp/tbx_command_stream_receiver_tests_gen12lp.inl index da6dbe3936..36357462fd 100644 --- a/shared/test/unit_test/gen12lp/tbx_command_stream_receiver_tests_gen12lp.inl +++ b/shared/test/unit_test/gen12lp/tbx_command_stream_receiver_tests_gen12lp.inl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2023 Intel Corporation + * Copyright (C) 2019-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -7,6 +7,7 @@ #include "shared/source/aub_mem_dump/page_table_entry_bits.h" #include "shared/source/command_stream/tbx_command_stream_receiver_hw.h" +#include "shared/source/helpers/bit_helpers.h" #include "shared/test/common/fixtures/device_fixture.h" #include "shared/test/common/mocks/mock_graphics_allocation.h" #include "shared/test/common/test_macros/test.h" @@ -19,7 +20,7 @@ GEN12LPTEST_F(Gen12LPTbxCommandStreamReceiverTests, givenNullPtrGraphicsAlloctio auto tbxCsr = std::make_unique>(*pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()); GraphicsAllocation *allocation = nullptr; auto bits = tbxCsr->getPPGTTAdditionalBits(allocation); - constexpr uint64_t expectedBits = BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit); + constexpr uint64_t expectedBits = makeBitMask(); EXPECT_EQ(expectedBits, bits); } @@ -29,7 +30,7 @@ GEN12LPTEST_F(Gen12LPTbxCommandStreamReceiverTests, givenGraphicsAlloctionWithLo MockGraphicsAllocation allocation(nullptr, 0); allocation.overrideMemoryPool(MemoryPool::localMemory); auto bits = tbxCsr->getPPGTTAdditionalBits(&allocation); - constexpr uint64_t expectedBits = BIT(PageTableEntry::presentBit) | BIT(PageTableEntry::writableBit) | BIT(PageTableEntry::localMemoryBit); + constexpr uint64_t expectedBits = makeBitMask(); EXPECT_EQ(expectedBits, bits); } diff --git a/shared/test/unit_test/helpers/bit_helpers_tests.cpp b/shared/test/unit_test/helpers/bit_helpers_tests.cpp index f65706dab8..9c52e003d6 100644 --- a/shared/test/unit_test/helpers/bit_helpers_tests.cpp +++ b/shared/test/unit_test/helpers/bit_helpers_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2024 Intel Corporation + * Copyright (C) 2019-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -12,6 +12,11 @@ using namespace NEO; +static_assert(makeBitMask<1, 3, 5>() == static_cast(0b101010)); +static_assert(makeBitMask<0>() == static_cast(0b1)); +static_assert(makeBitMask<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>() == static_cast(0xFFFF)); +static_assert(makeBitMask<63, 0>() == static_cast(0x8000000000000001)); + TEST(IsBitSetTests, givenDifferentValuesWhenTestingIsBitSetThenCorrectValueIsReturned) { size_t field1 = 0; size_t field2 = 0b1;