1 #include <assert.h>
2 
3 #define C 55
4 
5 int i, j, k;
6 
7 static void
test_bzero(unsigned size)8 test_bzero (unsigned size)
9 {
10   unsigned bsize = size * sizeof (int);
11   int *x = __builtin_malloc (bsize);
12   __builtin_memset (x, C, bsize);
13 
14 #pragma omp target map(tofrom: x[:size]) map(from: bsize)
15   {
16     __builtin_bzero (x, bsize);
17   }
18 
19   char *buffer = (char *) x;
20   for (unsigned i = 0; i < bsize; ++i)
21     assert (buffer[i] == 0);
22 }
23 
24 static void
test_memcpy(unsigned size)25 test_memcpy (unsigned size)
26 {
27   unsigned bsize = size * sizeof (int);
28   int *x = __builtin_malloc (bsize);
29   __builtin_memset (x, C, bsize);
30   int *y = __builtin_malloc (bsize);
31 
32 #pragma omp target map(tofrom: x[:size], y[:size]) map(from: bsize)
33   {
34     __builtin_memcpy (y, x, bsize);
35   }
36 
37   char *buffer = (char *) y;
38   for (unsigned i = 0; i < bsize; ++i)
39     assert (buffer[i] == C);
40 }
41 
42 static void
test_mempcpy(unsigned size)43 test_mempcpy (unsigned size)
44 {
45   unsigned bsize = size * sizeof (int);
46   int *x = __builtin_malloc (bsize);
47   __builtin_memset (x, C, bsize);
48   int *y = __builtin_malloc (bsize);
49   int *ptr = 0;
50 
51 #pragma omp target map(tofrom :x[:size], y[:size], ptr) map(from: bsize)
52   {
53     ptr = __builtin_mempcpy (y, x, bsize);
54   }
55 
56   char *buffer = (char *) y;
57   for (unsigned i = 0; i < bsize; ++i)
58     assert (buffer[i] == C);
59 
60   assert (ptr == y + size);
61 }
62 
63 static void
test_memset(unsigned size)64 test_memset (unsigned size)
65 {
66   unsigned bsize = size * sizeof (int);
67   int *x = __builtin_malloc (bsize);
68   __builtin_bzero (x, bsize);
69 
70 #pragma omp target map(tofrom : x[:size]) map(from: bsize)
71   {
72     __builtin_memset (x, C, bsize);
73   }
74 
75   char *buffer = (char *) x;
76   for (unsigned i = 0; i < bsize; ++i)
77     assert (buffer[i] == C);
78 }
79 
80 int
main(void)81 main (void)
82 {
83   unsigned tests[] = {1, 2, 3, 4, 5, 8, 15, 17, 23, 33, 0};
84 
85   for (unsigned i = 0; tests[i]; i++)
86     {
87       test_bzero (tests[i]);
88       test_memset (tests[i]);
89       test_memcpy (tests[i]);
90       test_mempcpy (tests[i]);
91     }
92 }
93