1 // RUN: %libomp-compile-and-run
5 #include "omp_testsuite.h"
7 int alignments
[] = {64, 128, 256, 512, 1024, 2048, 4096};
9 unsigned aligned_by(uint64_t addr
) {
10 uint64_t alignment
= 1;
11 while((addr
& (alignment
-1)) == 0) {
14 return (alignment
>> 1);
17 int test_kmp_aligned_malloc()
20 #pragma omp parallel shared(err)
25 int tid
= omp_get_thread_num();
27 for(i
= 0; i
< sizeof(alignments
)/sizeof(int); i
++) {
28 int alignment
= alignments
[i
];
29 // allocate 64 bytes with 64-byte alignment
30 // allocate 128 bytes with 128-byte alignment, etc.
31 ptr
= (int*)kmp_aligned_malloc(alignment
, alignment
);
33 if(addr
& (alignment
-1)) {
34 printf("thread %d: addr = %p (aligned to %u bytes) but expected "
35 " alignment = %d\n", tid
, ptr
, aligned_by(addr
), alignment
);
41 ptr
= kmp_aligned_malloc(128, 127);
43 printf("thread %d: kmp_aligned_malloc() didn't return NULL when "
44 "alignment was not power of 2\n", tid
);
47 } /* end of parallel */
56 for(i
= 0; i
< REPETITIONS
; i
++) {
57 if(!test_kmp_aligned_malloc()) {