mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:17:45 +00:00
AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount}, this commit removes all calls to these functions and replaces them with the equivalent functions in AK/BuiltinWrappers.h.
This commit is contained in:
parent
26bb3e1acf
commit
08e4a1a4dc
20 changed files with 108 additions and 115 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/String.h>
|
||||
|
@ -771,13 +772,13 @@ u32 Processor::smp_wake_n_idle_processors(u32 wake_count)
|
|||
while (did_wake_count < wake_count) {
|
||||
// Try to get a set of idle CPUs and flip them to busy
|
||||
u32 idle_mask = s_idle_cpu_mask.load(AK::MemoryOrder::memory_order_relaxed) & ~(1u << current_id);
|
||||
u32 idle_count = __builtin_popcountl(idle_mask);
|
||||
u32 idle_count = popcount(idle_mask);
|
||||
if (idle_count == 0)
|
||||
break; // No (more) idle processor available
|
||||
|
||||
u32 found_mask = 0;
|
||||
for (u32 i = 0; i < idle_count; i++) {
|
||||
u32 cpu = __builtin_ffsl(idle_mask) - 1;
|
||||
u32 cpu = bit_scan_forward(idle_mask) - 1;
|
||||
idle_mask &= ~(1u << cpu);
|
||||
found_mask |= 1u << cpu;
|
||||
}
|
||||
|
@ -785,9 +786,9 @@ u32 Processor::smp_wake_n_idle_processors(u32 wake_count)
|
|||
idle_mask = s_idle_cpu_mask.fetch_and(~found_mask, AK::MemoryOrder::memory_order_acq_rel) & found_mask;
|
||||
if (idle_mask == 0)
|
||||
continue; // All of them were flipped to busy, try again
|
||||
idle_count = __builtin_popcountl(idle_mask);
|
||||
idle_count = popcount(idle_mask);
|
||||
for (u32 i = 0; i < idle_count; i++) {
|
||||
u32 cpu = __builtin_ffsl(idle_mask) - 1;
|
||||
u32 cpu = bit_scan_forward(idle_mask) - 1;
|
||||
idle_mask &= ~(1u << cpu);
|
||||
|
||||
// Send an IPI to that CPU to wake it up. There is a possibility
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <Kernel/Assertions.h>
|
||||
|
@ -80,7 +81,7 @@ OwnPtr<PhysicalRegion> PhysicalRegion::try_take_pages_from_beginning(unsigned pa
|
|||
NonnullRefPtrVector<PhysicalPage> PhysicalRegion::take_contiguous_free_pages(size_t count)
|
||||
{
|
||||
auto rounded_page_count = next_power_of_two(count);
|
||||
auto order = __builtin_ctz(rounded_page_count);
|
||||
auto order = count_trailing_zeroes(rounded_page_count);
|
||||
|
||||
Optional<PhysicalAddress> page_base;
|
||||
for (auto& zone : m_usable_zones) {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/Format.h>
|
||||
#include <Kernel/Memory/MemoryManager.h>
|
||||
#include <Kernel/Memory/PhysicalPage.h>
|
||||
|
@ -31,7 +32,7 @@ PhysicalZone::PhysicalZone(PhysicalAddress base_address, size_t page_count)
|
|||
bucket.bitmap.grow(bitmap_size_for_order, false);
|
||||
}
|
||||
|
||||
auto first_order = __builtin_ctz(page_count);
|
||||
auto first_order = count_trailing_zeroes(page_count);
|
||||
size_t block_size = 2u << first_order;
|
||||
auto& bucket = m_buckets[first_order];
|
||||
size_t remaining_chunk_count = chunk_count;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/Singleton.h>
|
||||
#include <AK/Time.h>
|
||||
|
@ -77,7 +78,7 @@ Thread& Scheduler::pull_next_runnable_thread()
|
|||
return g_ready_queues->with([&](auto& ready_queues) -> Thread& {
|
||||
auto priority_mask = ready_queues.mask;
|
||||
while (priority_mask != 0) {
|
||||
auto priority = __builtin_ffsl(priority_mask);
|
||||
auto priority = bit_scan_forward(priority_mask);
|
||||
VERIFY(priority > 0);
|
||||
auto& ready_queue = ready_queues.queues[--priority];
|
||||
for (auto& thread : ready_queue.thread_list) {
|
||||
|
@ -116,7 +117,7 @@ Thread* Scheduler::peek_next_runnable_thread()
|
|||
return g_ready_queues->with([&](auto& ready_queues) -> Thread* {
|
||||
auto priority_mask = ready_queues.mask;
|
||||
while (priority_mask != 0) {
|
||||
auto priority = __builtin_ffsl(priority_mask);
|
||||
auto priority = bit_scan_forward(priority_mask);
|
||||
VERIFY(priority > 0);
|
||||
auto& ready_queue = ready_queues.queues[--priority];
|
||||
for (auto& thread : ready_queue.thread_list) {
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Types.h>
|
||||
|
@ -185,12 +186,12 @@ RefPtr<StorageDevice> AHCIController::device(u32 index) const
|
|||
{
|
||||
NonnullRefPtrVector<StorageDevice> connected_devices;
|
||||
u32 pi = hba().control_regs.pi;
|
||||
u32 bit = __builtin_ffsl(pi);
|
||||
u32 bit = bit_scan_forward(pi);
|
||||
while (bit) {
|
||||
dbgln_if(AHCI_DEBUG, "Checking implemented port {}, pi {:b}", bit - 1, pi);
|
||||
pi &= ~(1u << (bit - 1));
|
||||
auto checked_device = device_by_port(bit - 1);
|
||||
bit = __builtin_ffsl(pi);
|
||||
bit = bit_scan_forward(pi);
|
||||
if (checked_device.is_null())
|
||||
continue;
|
||||
connected_devices.append(checked_device.release_nonnull());
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <Kernel/Debug.h>
|
||||
#include <Kernel/FileSystem/OpenFileDescription.h>
|
||||
#include <Kernel/Net/Socket.h>
|
||||
|
@ -473,7 +474,7 @@ bool Thread::SignalBlocker::check_pending_signals(bool from_add_blocker)
|
|||
if (m_did_unblock)
|
||||
return false;
|
||||
|
||||
auto matching_pending_signal = __builtin_ffsl(thread().pending_signals() & m_pending_set);
|
||||
auto matching_pending_signal = bit_scan_forward(thread().pending_signals() & m_pending_set);
|
||||
if (matching_pending_signal == 0)
|
||||
return false;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue