diff options
| author | Rodrigo Locatti <reinuseslisp@airmail.cc> | 2021-01-15 04:48:58 -0300 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-15 04:48:58 -0300 | 
| commit | 5b9aedfc215e2f7227a8604d2d28fb462949d537 (patch) | |
| tree | ed1f105b1fef3fc9a5209c5106accaaee283ebf2 | |
| parent | c8bf0caca0a3b923edcf199824903c9576c6df5c (diff) | |
| parent | 8620de6b2030bef35360d029354f672cde8978f1 (diff) | |
Merge pull request #5356 from lioncash/clz
common/bit_util: Replace CLZ/CTZ operations with standardized ones
| -rw-r--r-- | src/common/bit_util.h | 76 | ||||
| -rw-r--r-- | src/core/hle/kernel/k_priority_queue.h | 4 | ||||
| -rw-r--r-- | src/core/hle/kernel/k_scheduler.cpp | 8 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/page_heap.h | 4 | ||||
| -rw-r--r-- | src/core/hle/kernel/process_capability.cpp | 4 | ||||
| -rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/tests/common/bit_utils.cpp | 23 | ||||
| -rw-r--r-- | src/video_core/cdma_pusher.cpp | 4 | ||||
| -rw-r--r-- | src/video_core/cdma_pusher.h | 2 | ||||
| -rw-r--r-- | src/video_core/command_classes/codecs/h264.cpp | 4 | 
10 files changed, 17 insertions, 113 deletions
| diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 29f59a9a3..685e7fc9b 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -22,82 +22,6 @@ template <typename T>  }  #ifdef _MSC_VER -[[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { -    unsigned long leading_zero = 0; - -    if (_BitScanReverse(&leading_zero, value) != 0) { -        return 31 - leading_zero; -    } - -    return 32; -} - -[[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { -    unsigned long leading_zero = 0; - -    if (_BitScanReverse64(&leading_zero, value) != 0) { -        return 63 - leading_zero; -    } - -    return 64; -} -#else -[[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { -    if (value == 0) { -        return 32; -    } - -    return static_cast<u32>(__builtin_clz(value)); -} - -[[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { -    if (value == 0) { -        return 64; -    } - -    return static_cast<u32>(__builtin_clzll(value)); -} -#endif - -#ifdef _MSC_VER -[[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { -    unsigned long trailing_zero = 0; - -    if (_BitScanForward(&trailing_zero, value) != 0) { -        return trailing_zero; -    } - -    return 32; -} - -[[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { -    unsigned long trailing_zero = 0; - -    if (_BitScanForward64(&trailing_zero, value) != 0) { -        return trailing_zero; -    } - -    return 64; -} -#else -[[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { -    if (value == 0) { -        return 32; -    } - -    return static_cast<u32>(__builtin_ctz(value)); -} - -[[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { -    if (value == 0) { -        return 64; -    } - -    return static_cast<u32>(__builtin_ctzll(value)); -} -#endif - -#ifdef _MSC_VER  [[nodiscard]] inline u32 MostSignificantBit32(const u32 value) {      unsigned long result; diff --git a/src/core/hle/kernel/k_priority_queue.h b/src/core/hle/kernel/k_priority_queue.h index 99fb8fe93..0dc929040 100644 --- a/src/core/hle/kernel/k_priority_queue.h +++ b/src/core/hle/kernel/k_priority_queue.h @@ -8,11 +8,11 @@  #pragma once  #include <array> +#include <bit>  #include <concepts>  #include "common/assert.h"  #include "common/bit_set.h" -#include "common/bit_util.h"  #include "common/common_types.h"  #include "common/concepts.h" @@ -268,7 +268,7 @@ private:      }      constexpr s32 GetNextCore(u64& affinity) { -        const s32 core = Common::CountTrailingZeroes64(affinity); +        const s32 core = std::countr_zero(affinity);          ClearAffinityBit(affinity, core);          return core;      } diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 42f0ea483..12b5619fb 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -5,6 +5,8 @@  // This file references various implementation details from Atmosphere, an open-source firmware for  // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. +#include <bit> +  #include "common/assert.h"  #include "common/bit_util.h"  #include "common/fiber.h" @@ -31,12 +33,12 @@ static void IncrementScheduledCount(Kernel::Thread* thread) {  void KScheduler::RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedule,                                   Core::EmuThreadHandle global_thread) { -    u32 current_core = global_thread.host_handle; +    const u32 current_core = global_thread.host_handle;      bool must_context_switch = global_thread.guest_handle != InvalidHandle &&                                 (current_core < Core::Hardware::NUM_CPU_CORES);      while (cores_pending_reschedule != 0) { -        u32 core = Common::CountTrailingZeroes64(cores_pending_reschedule); +        const auto core = static_cast<u32>(std::countr_zero(cores_pending_reschedule));          ASSERT(core < Core::Hardware::NUM_CPU_CORES);          if (!must_context_switch || core != current_core) {              auto& phys_core = kernel.PhysicalCore(core); @@ -109,7 +111,7 @@ u64 KScheduler::UpdateHighestPriorityThreadsImpl(KernelCore& kernel) {      // Idle cores are bad. We're going to try to migrate threads to each idle core in turn.      while (idle_cores != 0) { -        u32 core_id = Common::CountTrailingZeroes64(idle_cores); +        const auto core_id = static_cast<u32>(std::countr_zero(idle_cores));          if (Thread* suggested = priority_queue.GetSuggestedFront(core_id); suggested != nullptr) {              s32 migration_candidates[Core::Hardware::NUM_CPU_CORES];              size_t num_candidates = 0; diff --git a/src/core/hle/kernel/memory/page_heap.h b/src/core/hle/kernel/memory/page_heap.h index 22b0de860..131093284 100644 --- a/src/core/hle/kernel/memory/page_heap.h +++ b/src/core/hle/kernel/memory/page_heap.h @@ -8,11 +8,11 @@  #pragma once  #include <array> +#include <bit>  #include <vector>  #include "common/alignment.h"  #include "common/assert.h" -#include "common/bit_util.h"  #include "common/common_funcs.h"  #include "common/common_types.h"  #include "core/hle/kernel/memory/memory_types.h" @@ -105,7 +105,7 @@ private:                          ASSERT(depth == 0);                          return -1;                      } -                    offset = offset * 64 + Common::CountTrailingZeroes64(v); +                    offset = offset * 64 + static_cast<u32>(std::countr_zero(v));                      ++depth;                  } while (depth < static_cast<s32>(used_depths)); diff --git a/src/core/hle/kernel/process_capability.cpp b/src/core/hle/kernel/process_capability.cpp index 0f128c586..0566311b6 100644 --- a/src/core/hle/kernel/process_capability.cpp +++ b/src/core/hle/kernel/process_capability.cpp @@ -2,6 +2,8 @@  // Licensed under GPLv2 or any later version  // Refer to the license.txt file included. +#include <bit> +  #include "common/bit_util.h"  #include "common/logging/log.h"  #include "core/hle/kernel/errors.h" @@ -60,7 +62,7 @@ constexpr CapabilityType GetCapabilityType(u32 value) {  u32 GetFlagBitOffset(CapabilityType type) {      const auto value = static_cast<u32>(type); -    return static_cast<u32>(Common::BitSize<u32>() - Common::CountLeadingZeroes32(value)); +    return static_cast<u32>(Common::BitSize<u32>() - static_cast<u32>(std::countl_zero(value)));  }  } // Anonymous namespace diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 8a606b448..33fa89583 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,6 +1,5 @@  add_executable(tests      common/bit_field.cpp -    common/bit_utils.cpp      common/fibers.cpp      common/param_package.cpp      common/ring_buffer.cpp diff --git a/src/tests/common/bit_utils.cpp b/src/tests/common/bit_utils.cpp deleted file mode 100644 index 479b5995a..000000000 --- a/src/tests/common/bit_utils.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <catch2/catch.hpp> -#include <math.h> -#include "common/bit_util.h" - -namespace Common { - -TEST_CASE("BitUtils::CountTrailingZeroes", "[common]") { -    REQUIRE(Common::CountTrailingZeroes32(0) == 32); -    REQUIRE(Common::CountTrailingZeroes64(0) == 64); -    REQUIRE(Common::CountTrailingZeroes32(9) == 0); -    REQUIRE(Common::CountTrailingZeroes32(8) == 3); -    REQUIRE(Common::CountTrailingZeroes32(0x801000) == 12); -    REQUIRE(Common::CountTrailingZeroes64(9) == 0); -    REQUIRE(Common::CountTrailingZeroes64(8) == 3); -    REQUIRE(Common::CountTrailingZeroes64(0x801000) == 12); -    REQUIRE(Common::CountTrailingZeroes64(0x801000000000UL) == 36); -} - -} // namespace Common diff --git a/src/video_core/cdma_pusher.cpp b/src/video_core/cdma_pusher.cpp index 94679d5d1..33b3c060b 100644 --- a/src/video_core/cdma_pusher.cpp +++ b/src/video_core/cdma_pusher.cpp @@ -18,10 +18,10 @@  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  // +#include <bit>  #include "command_classes/host1x.h"  #include "command_classes/nvdec.h"  #include "command_classes/vic.h" -#include "common/bit_util.h"  #include "video_core/cdma_pusher.h"  #include "video_core/command_classes/nvdec_common.h"  #include "video_core/engines/maxwell_3d.h" @@ -56,7 +56,7 @@ void CDmaPusher::Step() {      for (const u32 value : values) {          if (mask != 0) { -            const u32 lbs = Common::CountTrailingZeroes32(mask); +            const auto lbs = static_cast<u32>(std::countr_zero(mask));              mask &= ~(1U << lbs);              ExecuteCommand(static_cast<u32>(offset + lbs), value);              continue; diff --git a/src/video_core/cdma_pusher.h b/src/video_core/cdma_pusher.h index 8ca70b6dd..e5f212c1a 100644 --- a/src/video_core/cdma_pusher.h +++ b/src/video_core/cdma_pusher.h @@ -126,7 +126,7 @@ private:      s32 count{};      s32 offset{}; -    s32 mask{}; +    u32 mask{};      bool incrementing{};      // Queue of command lists to be processed diff --git a/src/video_core/command_classes/codecs/h264.cpp b/src/video_core/command_classes/codecs/h264.cpp index 65bbeac78..fea6aed98 100644 --- a/src/video_core/command_classes/codecs/h264.cpp +++ b/src/video_core/command_classes/codecs/h264.cpp @@ -19,7 +19,7 @@  //  #include <array> -#include "common/bit_util.h" +#include <bit>  #include "video_core/command_classes/codecs/h264.h"  #include "video_core/gpu.h"  #include "video_core/memory_manager.h" @@ -266,7 +266,7 @@ void H264BitWriter::WriteExpGolombCodedInt(s32 value) {  }  void H264BitWriter::WriteExpGolombCodedUInt(u32 value) { -    const s32 size = 32 - Common::CountLeadingZeroes32(static_cast<s32>(value + 1)); +    const s32 size = 32 - std::countl_zero(value + 1);      WriteBits(1, size);      value -= (1U << (size - 1)) - 1; | 
