s390.c: (s390_expand_builtin): Allow -mhtm to be enabled without -march=zEC12.
[gcc.git] / libitm / testsuite / libitm.c / memcpy-1.c
1 /* This program is free software; you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation; either version 2 of the License, or
4 (at your option) any later version.
5
6 This program is distributed in the hope that it will be useful, but
7 WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 General Public License for more details.
10
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14 02110-1301, USA. */
15
16 /* Verify memcpy operation. */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <libitm.h>
23
24 #define BEG_TRANSACTION \
25 _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
26 | pr_hasNoIrrevocable)
27 #define END_TRANSACTION \
28 _ITM_commitTransaction ()
29
30 #define MEMCPY _ITM_memcpyRtWt
31
32 static unsigned char *buf1, *buf2;
33 static size_t bufsize, page_size;
34 static int fail;
35
36 static void
37 do_test (size_t align1, size_t align2, size_t len)
38 {
39 size_t i, j;
40 unsigned char *s1, *s2;
41 unsigned char c1, c2;
42
43 if (align1 + len >= bufsize)
44 return;
45 if (align2 + len >= bufsize)
46 return;
47
48 c1 = random () >> 8;
49 c2 = random () >> 8;
50 memset (buf1, c1, bufsize);
51 memset (buf2, c2, bufsize);
52
53 s1 = buf1 + align1;
54 s2 = buf2 + align2;
55
56 for (i = 0, j = 1; i < len; i++, j += 23)
57 s1[i] = (j == c1 ? j + 1 : j);
58
59 BEG_TRANSACTION;
60 MEMCPY (s2, s1, len);
61 END_TRANSACTION;
62
63 if (memcmp (s1, s2, len) != 0)
64 {
65 printf ("Wrong result: dalign %zd salign %zd len %zd\n",
66 align2, align1, len);
67 fail = 1;
68 return;
69 }
70
71 for (i = (align2 > 64 ? align2 - 64 : 0); i < align2; ++i)
72 if (buf2[i] != c2)
73 {
74 printf ("Garbage before: ofs %zd\n", i);
75 fail = 1;
76 break;
77 }
78 for (i = align2 + len, j = i+64 < bufsize ? i+64 : bufsize; i < j; ++i)
79 if (buf2[i] != c2)
80 {
81 printf ("Garbage after: ofs %zd\n", i);
82 fail = 1;
83 break;
84 }
85 }
86
87 #ifndef MAP_ANONYMOUS
88 # ifdef MAP_ANON
89 # define MAP_ANONYMOUS MAP_ANON
90 # endif
91 #endif
92
93 int main()
94 {
95 size_t i, j;
96
97 page_size = getpagesize ();
98 bufsize = 2 * page_size;
99
100 buf1 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
101 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
102 if (buf1 == MAP_FAILED)
103 return 1;
104 buf2 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
105 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
106 if (buf2 == MAP_FAILED)
107 return 1;
108
109 if (mprotect (buf1, page_size, PROT_NONE))
110 return 1;
111 buf1 += page_size;
112 if (mprotect (buf1 + bufsize, page_size, PROT_NONE))
113 return 1;
114
115 if (mprotect (buf2, page_size, PROT_NONE))
116 return 1;
117 buf2 += page_size;
118 if (mprotect (buf2 + bufsize, page_size, PROT_NONE))
119 return 1;
120
121 for (i = 0; i < 18; ++i)
122 {
123 size_t len = 1 << i;
124
125 do_test (0, 0, len);
126 do_test (i, 0, len);
127 do_test (0, i, len);
128 do_test (i, i, len);
129
130 do_test (0, bufsize - len, len);
131 do_test (bufsize - len, 0, len);
132 do_test (i, bufsize - len, len);
133 do_test (bufsize - len, i, len);
134 }
135
136 for (i = 0; i < 32; ++i)
137 {
138 do_test (i, 0, i);
139 do_test (0, i, i);
140 do_test (i, i, i);
141
142 for (j = 0; j < 32; ++j)
143 {
144 do_test (i, bufsize - i - j, i);
145 do_test (bufsize - i - j, i, i);
146 }
147 }
148
149 for (i = 3; i < 32; ++i)
150 {
151 if ((i & (i - 1)) == 0)
152 continue;
153 do_test (0, 0, 16 * i);
154 do_test (i, 0, 16 * i);
155 do_test (0, i, 16 * i);
156 do_test (i, i, 16 * i);
157 }
158
159 return fail;
160 }