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

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Sun Mar 23 10:37:06 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);
----------------
jhuber6 wrote:

Looked into it again, for this version you'll need to manually construct  the `1` constant as `size_t `because otherwise it will create a 32-bit result. (I highly doubt anyone will ever need more than 2^32 alignment, but still). So, either use the previous version, or do it like in https://godbolt.org/z/KMjezrjox, I don't feel too strongly either way.

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


More information about the libc-commits mailing list