Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / openmp / runtime / test / api / kmp_aligned_malloc.c
blob5302fec9ac286cbd1e05a70b12c4398d302c3d0b
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <omp.h>
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) {
12 alignment <<= 1;
14 return (alignment >> 1);
17 int test_kmp_aligned_malloc()
19 int err = 0;
20 #pragma omp parallel shared(err)
22 int i;
23 int* ptr;
24 uint64_t addr;
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);
32 addr = (uint64_t)ptr;
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);
36 err = 1;
38 kmp_free(ptr);
41 ptr = kmp_aligned_malloc(128, 127);
42 if (ptr != NULL) {
43 printf("thread %d: kmp_aligned_malloc() didn't return NULL when "
44 "alignment was not power of 2\n", tid);
45 err = 1;
47 } /* end of parallel */
48 return !err;
51 int main()
53 int i;
54 int num_failed=0;
56 for(i = 0; i < REPETITIONS; i++) {
57 if(!test_kmp_aligned_malloc()) {
58 num_failed++;
61 return num_failed;