summaryrefslogtreecommitdiffstats
path: root/libitm/testsuite/libitm.c/memcpy-1.c
diff options
context:
space:
mode:
authoraldyh <aldyh@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-08 11:13:41 +0000
committeraldyh <aldyh@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-08 11:13:41 +0000
commit4c0315d05fa0f707875686abc4f91f7a979a7c7b (patch)
treee07de8d0b6265f8d72388d335bd471022e753d57 /libitm/testsuite/libitm.c/memcpy-1.c
parentbf09288ee7b5f264f28081a84fde4c6aa1ac5c82 (diff)
downloadppe42-gcc-4c0315d05fa0f707875686abc4f91f7a979a7c7b.tar.gz
ppe42-gcc-4c0315d05fa0f707875686abc4f91f7a979a7c7b.zip
Merge from transactional-memory branch.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@181154 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libitm/testsuite/libitm.c/memcpy-1.c')
-rw-r--r--libitm/testsuite/libitm.c/memcpy-1.c154
1 files changed, 154 insertions, 0 deletions
diff --git a/libitm/testsuite/libitm.c/memcpy-1.c b/libitm/testsuite/libitm.c/memcpy-1.c
new file mode 100644
index 00000000000..e4b56573b26
--- /dev/null
+++ b/libitm/testsuite/libitm.c/memcpy-1.c
@@ -0,0 +1,154 @@
+/* This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+/* Verify memcpy operation. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <libitm.h>
+
+#define BEG_TRANSACTION \
+ _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
+ | pr_hasNoIrrevocable)
+#define END_TRANSACTION \
+ _ITM_commitTransaction ()
+
+#define MEMCPY _ITM_memcpyRtWt
+
+static unsigned char *buf1, *buf2;
+static size_t bufsize, page_size;
+static int fail;
+
+static void
+do_test (size_t align1, size_t align2, size_t len)
+{
+ size_t i, j;
+ unsigned char *s1, *s2;
+ unsigned char c1, c2;
+
+ if (align1 + len >= bufsize)
+ return;
+ if (align2 + len >= bufsize)
+ return;
+
+ c1 = random () >> 8;
+ c2 = random () >> 8;
+ memset (buf1, c1, bufsize);
+ memset (buf2, c2, bufsize);
+
+ s1 = buf1 + align1;
+ s2 = buf2 + align2;
+
+ for (i = 0, j = 1; i < len; i++, j += 23)
+ s1[i] = (j == c1 ? j + 1 : j);
+
+ BEG_TRANSACTION;
+ MEMCPY (s2, s1, len);
+ END_TRANSACTION;
+
+ if (memcmp (s1, s2, len) != 0)
+ {
+ printf ("Wrong result: dalign %zd salign %zd len %zd\n",
+ align2, align1, len);
+ fail = 1;
+ return;
+ }
+
+ for (i = (align2 > 64 ? align2 - 64 : 0); i < align2; ++i)
+ if (buf2[i] != c2)
+ {
+ printf ("Garbage before: ofs %zd\n", i);
+ fail = 1;
+ break;
+ }
+ for (i = align2 + len, j = i+64 < bufsize ? i+64 : bufsize; i < j; ++i)
+ if (buf2[i] != c2)
+ {
+ printf ("Garbage after: ofs %zd\n", i);
+ fail = 1;
+ break;
+ }
+}
+
+int main()
+{
+ size_t i, j;
+
+ page_size = getpagesize ();
+ bufsize = 2 * page_size;
+
+ buf1 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (buf1 == MAP_FAILED)
+ return 1;
+ buf2 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (buf2 == MAP_FAILED)
+ return 1;
+
+ if (mprotect (buf1, page_size, PROT_NONE))
+ return 1;
+ buf1 += page_size;
+ if (mprotect (buf1 + bufsize, page_size, PROT_NONE))
+ return 1;
+
+ if (mprotect (buf2, page_size, PROT_NONE))
+ return 1;
+ buf2 += page_size;
+ if (mprotect (buf2 + bufsize, page_size, PROT_NONE))
+ return 1;
+
+ for (i = 0; i < 18; ++i)
+ {
+ size_t len = 1 << i;
+
+ do_test (0, 0, len);
+ do_test (i, 0, len);
+ do_test (0, i, len);
+ do_test (i, i, len);
+
+ do_test (0, bufsize - len, len);
+ do_test (bufsize - len, 0, len);
+ do_test (i, bufsize - len, len);
+ do_test (bufsize - len, i, len);
+ }
+
+ for (i = 0; i < 32; ++i)
+ {
+ do_test (i, 0, i);
+ do_test (0, i, i);
+ do_test (i, i, i);
+
+ for (j = 0; j < 32; ++j)
+ {
+ do_test (i, bufsize - i - j, i);
+ do_test (bufsize - i - j, i, i);
+ }
+ }
+
+ for (i = 3; i < 32; ++i)
+ {
+ if ((i & (i - 1)) == 0)
+ continue;
+ do_test (0, 0, 16 * i);
+ do_test (i, 0, 16 * i);
+ do_test (0, i, 16 * i);
+ do_test (i, i, 16 * i);
+ }
+
+ return fail;
+}
OpenPOWER on IntegriCloud