Kernel: Use BAN::Math::{ctz,popcount} instead of builtins

This commit is contained in:
2026-08-22 14:39:42 +03:00
parent fe64639842
commit ae24fb1dd0
7 changed files with 8 additions and 11 deletions
+1 -1
View File
@@ -347,7 +347,7 @@ namespace Kernel::Input
result.command_port = data->as.fixed_io_port.range_base; result.command_port = data->as.fixed_io_port.range_base;
break; break;
case ACPI::ResourceData::Type::IRQ: case ACPI::ResourceData::Type::IRQ:
if (__builtin_popcount(data->as.irq.irq_mask) != 1) if (BAN::Math::popcount(data->as.irq.irq_mask) != 1)
break; break;
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
if (data->as.irq.irq_mask & (1 << i)) if (data->as.irq.irq_mask & (1 << i))
+1 -2
View File
@@ -43,11 +43,10 @@ namespace Kernel
PageTable::with_per_cpu_fast_page(current_paddr, [&page_matched_bit](void* addr) { PageTable::with_per_cpu_fast_page(current_paddr, [&page_matched_bit](void* addr) {
for (size_t j = 0; j < PAGE_SIZE / sizeof(size_t); j++) for (size_t j = 0; j < PAGE_SIZE / sizeof(size_t); j++)
{ {
static_assert(sizeof(size_t) == sizeof(long));
auto& current = static_cast<size_t*>(addr)[j]; auto& current = static_cast<size_t*>(addr)[j];
if (current == BAN::numeric_limits<size_t>::max()) if (current == BAN::numeric_limits<size_t>::max())
continue; continue;
const int ctz = __builtin_ctzl(~current); const int ctz = BAN::Math::ctz(~current);
current |= static_cast<size_t>(1) << ctz; current |= static_cast<size_t>(1) << ctz;
page_matched_bit = j * sizeof(size_t) * 8 + ctz; page_matched_bit = j * sizeof(size_t) * 8 + ctz;
return; return;
+2 -4
View File
@@ -125,8 +125,6 @@ struct BitmapAllocator
// NOTE: We could optimize other bitmap functions than this // NOTE: We could optimize other bitmap functions than this
// but this one is the bottle neck so it doesn't matter // but this one is the bottle neck so it doesn't matter
static_assert(sizeof(unsigned long long) == sizeof(uint64_t));
if (index >= total_chunks) if (index >= total_chunks)
return index; return index;
@@ -134,7 +132,7 @@ struct BitmapAllocator
{ {
const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + (index - rem) / 8) >> rem; const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + (index - rem) / 8) >> rem;
if (qword != (1ull << (64 - rem)) - 1) if (qword != (1ull << (64 - rem)) - 1)
return index + __builtin_ctzll(~qword); return index + BAN::Math::ctz(~qword);
index += 64 - rem; index += 64 - rem;
} }
@@ -142,7 +140,7 @@ struct BitmapAllocator
{ {
const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + index / 8); const uint64_t qword = *reinterpret_cast<const uint64_t*>(base + index / 8);
if (qword != UINT64_MAX) if (qword != UINT64_MAX)
return index + __builtin_ctzll(~qword); return index + BAN::Math::ctz(~qword);
index += 64; index += 64;
} }
+1 -1
View File
@@ -75,7 +75,7 @@ namespace Kernel
if (flags & ~(O_ACCMODE | O_NOFOLLOW | O_APPEND | O_TRUNC | O_CLOEXEC | O_TTY_INIT | O_NOCTTY | O_DIRECTORY | O_CREAT | O_EXCL | O_NONBLOCK)) if (flags & ~(O_ACCMODE | O_NOFOLLOW | O_APPEND | O_TRUNC | O_CLOEXEC | O_TTY_INIT | O_NOCTTY | O_DIRECTORY | O_CREAT | O_EXCL | O_NONBLOCK))
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
if ((flags & O_ACCMODE) != O_RDWR && __builtin_popcount(flags & O_ACCMODE) != 1) if ((flags & O_ACCMODE) != O_RDWR && BAN::Math::popcount<unsigned>(flags & O_ACCMODE) != 1)
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
if ((flags & O_DIRECTORY) && !file.inode->mode().ifdir()) if ((flags & O_DIRECTORY) && !file.inode->mode().ifdir())
@@ -94,7 +94,7 @@ namespace Kernel
while (is != 0) while (is != 0)
{ {
const size_t idx = __builtin_ctz(is); const size_t idx = BAN::Math::ctz(is);
if (auto& device = m_devices[idx]) if (auto& device = m_devices[idx])
device->handle_irq(); device->handle_irq();
else else
+1 -1
View File
@@ -425,7 +425,7 @@ namespace Kernel
{ {
if (const uint32_t usable_slots = ~(m_port->sact | m_port->ci) & m_free_slots) if (const uint32_t usable_slots = ~(m_port->sact | m_port->ci) & m_free_slots)
{ {
const uint32_t slot = __builtin_ctz(usable_slots); const uint32_t slot = BAN::Math::ctz(usable_slots);
m_free_slots &= ~(1u << slot); m_free_slots &= ~(1u << slot);
return slot; return slot;
} }
+1 -1
View File
@@ -121,7 +121,7 @@ namespace Kernel
for (size_t i = 0; i < 16; i++) for (size_t i = 0; i < 16; i++)
if (page_size_bits & (1 << i)) if (page_size_bits & (1 << i))
dwarnln(" {} bytes", 1 << (12 + i)); dwarnln(" {} bytes", 1 << (12 + i));
if (__builtin_popcount(page_size_bits) != 1) if (BAN::Math::popcount(page_size_bits) != 1)
dwarnln(" ... XHCI spec only allows a single supported page size???"); dwarnln(" ... XHCI spec only allows a single supported page size???");
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
} }