[libc-commits] [libc] [libc] implement `memalignment` (PR #132493)

Mohamed Emad via libc-commits libc-commits at lists.llvm.org
Tue Mar 25 12:12:22 PDT 2025


================
@@ -0,0 +1,15 @@
+#include "src/stdlib/memalignment.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/CPP/bit.h"
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, memalignment, (const void *p)) {
+  if (p == nullptr)
+    return 0;
+
+  uintptr_t addr = reinterpret_cast<uintptr_t>(p);
+
+  return 1 << cpp::countr_zero(addr);
----------------
hulxv wrote:

doesn't `cpp::countr_zero` works as `__builtin_ctzg` ? Actually, it's a wrapper for it
https://github.com/llvm/llvm-project/blob/613a077b05b8352a48695be295037306f5fca151/libc/src/__support/CPP/bit.h#L78-L105

https://github.com/llvm/llvm-project/pull/132493


More information about the libc-commits mailing list