Skip to content
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
152894e
[libc] implement `memalignment`
hulxv Mar 21, 2025
000e4e2
chore: fix formatting of `memalignment.h`
hulxv Mar 21, 2025
845184e
test: adding tests for `memalignment`
hulxv Mar 21, 2025
224d792
chore: fix formatting of `memalignment_test.cpp`
hulxv Mar 21, 2025
76b752d
[libc] add `memalignment` to `x86_64` entrypoints
hulxv Mar 22, 2025
40ed080
[libc] fix header of `memalignment`
hulxv Mar 22, 2025
ce731cc
[libc] add `memalignment` to other architectures entrypoints
hulxv Mar 22, 2025
4593534
[libc] add `memalignment` to `stdlib-mallic.yaml`
hulxv Mar 22, 2025
8b50acb
[libc] improve `memalignment`
hulxv Mar 22, 2025
a2ae994
chore: correct file extension of `memalignment`
hulxv Mar 22, 2025
931115f
refactor: use `cpp::countr_zero` instead of `__builtin_ctzg`
hulxv Mar 22, 2025
ff7a9bc
build: add `memalignment` to `CMakeLists.txt`
hulxv Mar 22, 2025
edc13a9
chore: add new line to the EOF of `memalignment_test.cpp`
hulxv Mar 22, 2025
5ab7da5
fix: `memalignment` build entrypoints
hulxv Mar 22, 2025
23320f4
fix: correct the standard of `memalignment`
hulxv Mar 23, 2025
eb201b3
build: correct build entry for `memalignment`
hulxv Mar 23, 2025
c1f9e6f
test: add `AlignasSpecifiedAlignment` for `memalignment` unit tests
hulxv Mar 23, 2025
0a8b610
build: remove wrong entrypoint
hulxv Mar 23, 2025
373c02d
[libc] missing newline
hulxv Mar 25, 2025
5dab7c5
[libc] move `memalignment` to `stdlib.yaml`
hulxv Mar 25, 2025
af0644a
[libc] fix header of `memalignment_test.cpp`
hulxv Mar 25, 2025
4c0d249
[libc] move `libc.src.stdlib.memalignment`to correct entrypoints
hulxv Mar 25, 2025
ac80029
[libc] remove wrong external entrypoint in `stdlib`
hulxv Mar 25, 2025
203ce17
Merge branch 'main' into libc/feat/memalignment
hulxv Mar 26, 2025
e8c38bd
Update libc/src/stdlib/memalignment.cpp
jhuber6 Mar 26, 2025
46d9f09
[libc] add header to `memalignment.cpp`
hulxv Mar 27, 2025
cee9d85
fix formatting
hulxv Mar 27, 2025
3b56885
[libc] remove `MallocedPointers`
hulxv Mar 28, 2025
9d04fa4
[libc] fix wrong header
hulxv Mar 28, 2025
778f87d
fix formatting
hulxv Mar 28, 2025
9e5616d
[libc][test] fix wrong expectation in `LlvmLibcMemAlignmentTest.Speci…
hulxv Mar 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.malloc
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.rand
libc.src.stdlib.realloc
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.rand
libc.src.stdlib.srand
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.strtod
libc.src.stdlib.strtof
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/amdgpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/nvptx/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.strtod
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.qsort_r
libc.src.stdlib.rand
Expand Down
1 change: 1 addition & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
libc.src.stdlib.lldiv
libc.src.stdlib.memalignment
libc.src.stdlib.qsort
libc.src.stdlib.strtod
libc.src.stdlib.strtof
Expand Down
6 changes: 6 additions & 0 deletions libc/include/stdlib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ functions:
arguments:
- type: long long
- type: long long
- name: memalignment
standards:
- stdc
return_type: size_t
arguments:
- type: const void *
- name: posix_memalign
standards:
- posix
Expand Down
11 changes: 11 additions & 0 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ add_entrypoint_object(
.rand_util
)

add_entrypoint_object(
memalignment
SRCS
memalignment.cpp
HDRS
memalignment.h
DEPENDS
libc.src.__support.common
libc.hdr.types.size_t
)

if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")
Expand Down
25 changes: 25 additions & 0 deletions libc/src/stdlib/memalignment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Implementation for memalignment -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdlib/memalignment.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.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 size_t(1) << cpp::countr_zero(addr);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/stdlib/memalignment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for memalignment ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H
#define LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H

#include "hdr/types/size_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

size_t memalignment(const void *p);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDLIB_LDIV_H
11 changes: 11 additions & 0 deletions libc/test/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ add_libc_test(
libc.src.stdlib.srand
)

add_libc_test(
memalignment_test
SUITE
libc-stdlib-tests
SRCS
memalignment_test.cpp
DEPENDS
libc.include.stdlib
libc.src.stdlib.memalignment
)

if(LLVM_LIBC_FULL_BUILD)

add_libc_test(
Expand Down
59 changes: 59 additions & 0 deletions libc/test/src/stdlib/memalignment_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===-- Unittests for memalignment ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdlib/memalignment.h"
#include "test/UnitTest/Test.h"

#include <stdint.h>

TEST(LlvmLibcMemAlignmentTest, NullPointer) {
void *ptr = nullptr;
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr), static_cast<size_t>(0));
}

TEST(LlvmLibcMemAlignmentTest, SpecificAlignment) {

// These addresses have known alignment patterns - if we can construct them
uintptr_t addr_align2 = 0x2; // 2-byte aligned
uintptr_t addr_align4 = 0x4; // 4-byte aligned
uintptr_t addr_align8 = 0x8; // 8-byte aligned
uintptr_t addr_align16 = 0x10; // 16-byte aligned
uintptr_t addr_align32 = 0x20; // 32-byte aligned

void *ptr_align2 = reinterpret_cast<void *>(addr_align2);
void *ptr_align4 = reinterpret_cast<void *>(addr_align4);
void *ptr_align8 = reinterpret_cast<void *>(addr_align8);
void *ptr_align16 = reinterpret_cast<void *>(addr_align16);
void *ptr_align32 = reinterpret_cast<void *>(addr_align32);

EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align2), static_cast<size_t>(2));
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align4), static_cast<size_t>(4));
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align8), static_cast<size_t>(8));
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align16), static_cast<size_t>(16));
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align32), static_cast<size_t>(32));

uintptr_t addr_complex = 0x1234560; // 16-byte aligned (ends in 0)
void *ptr_complex = reinterpret_cast<void *>(addr_complex);
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_complex), static_cast<size_t>(16));
}

TEST(LlvmLibcMemAlignmentTest, AlignasSpecifiedAlignment) {
alignas(16) static int aligned_16;
alignas(32) static int aligned_32;
alignas(64) static int aligned_64;
alignas(128) static int aligned_128;
alignas(256) static int aligned_256;

EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_16), static_cast<size_t>(16));
EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_32), static_cast<size_t>(32));
EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_64), static_cast<size_t>(64));
EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_128),
static_cast<size_t>(128));
EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_256),
static_cast<size_t>(256));
}
Loading