1#include "test/IntegrationTest/test.h"
2
3#include "src/__support/GPU/utils.h"
4#include "src/stdlib/aligned_alloc.h" // Adjust path if needed
5#include "src/stdlib/free.h"
6
7using namespace LIBC_NAMESPACE;
8
9TEST_MAIN(int, char **, char **) {
10 // aligned_alloc with valid alignment and size
11 void *ptr = LIBC_NAMESPACE::aligned_alloc(32, 16);
12 EXPECT_NE(ptr, nullptr);
13 EXPECT_TRUE(__builtin_is_aligned(ptr, 32));
14
15 LIBC_NAMESPACE::free(ptr);
16
17 // aligned_alloc fails if alignment is not power of two
18 void *bad_align = LIBC_NAMESPACE::aligned_alloc(30, 99);
19 EXPECT_EQ(bad_align, nullptr);
20
21 // aligned_alloc with a divergent size.
22 size_t alignment = 1 << (__gpu_lane_id() % 8 + 1);
23 void *div =
24 LIBC_NAMESPACE::aligned_alloc(alignment, (gpu::get_thread_id() + 1) * 4);
25 EXPECT_NE(div, nullptr);
26 EXPECT_TRUE(__builtin_is_aligned(div, alignment));
27
28 return 0;
29}
30

source code of libc/test/integration/src/stdlib/gpu/aligned_alloc.cpp