gcc.target/i386/pr55583.c: Use long long for 64-bit integer
[official-gcc.git] / gcc / testsuite / gcc.target / s390 / htm-builtins-1.c
blob4f95bf3accaac2c7660e7129201eb268a2b24c4b
1 /* Functional tests of the htm __builtin_... macros. */
3 /* { dg-do run } */
4 /* { dg-require-effective-target htm } */
5 /* { dg-options "-O3 -march=zEC12 -mzarch" } */
7 /* ---------------------------- included header files ---------------------- */
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <htmintrin.h>
14 /* ---------------------------- local definitions -------------------------- */
16 #define DEFAULT_MAX_REPETITIONS 7
17 #define DEFAULT_REQUIRED_QUORUM 4
18 #define NUM_WARMUP_RUNS 2
20 /* ---------------------------- local macros ------------------------------- */
22 #define TEST_DF_REP(name) \
23 { #name, name, DEFAULT_MAX_REPETITIONS, DEFAULT_REQUIRED_QUORUM }
24 #define TEST_NO_REP(name) { #name, name, 1, 1 }
26 /* ---------------------------- local types -------------------------------- */
28 typedef int (*test_func_t)(void);
30 typedef struct
32 const char *name;
33 test_func_t test_func;
34 int max_repetitions;
35 int required_quorum;
36 } test_table_entry_t;
38 /* ---------------------------- local variables ---------------------------- */
40 __attribute__ ((aligned(256))) static struct __htm_tdb local_tdb256;
41 static struct __htm_tdb local_tdb;
42 static int do_dump_tdb = 0;
44 /* ---------------------------- exported variables (globals) --------------- */
46 __attribute__ ((aligned(256))) struct
48 float float_1;
49 float float_2;
50 float float_3;
51 } global = { 1.0, 2.5, 0.0 };
53 __attribute__ ((aligned(256))) struct
55 volatile uint64_t c1;
56 char pad1[256 - sizeof(uint64_t)];
57 volatile uint64_t c2;
58 char pad2[256 - sizeof(uint64_t)];
59 volatile uint64_t c3;
60 } counters = { 0 };
62 /* ---------------------------- local helper functions --------------------- */
64 static void dump_tdb (struct __htm_tdb *tdb)
66 unsigned char *p;
67 int i;
68 int j;
70 if (do_dump_tdb == 0)
72 return;
74 p = (unsigned char *)tdb;
75 for (i = 0; i < 16; i++)
77 fprintf (stderr, "0x%02x ", i * 16);
78 for (j = 0; j < 16; j++)
80 fprintf (stderr, "%02x", (int)p[i * 16 + j]);
81 if (j < 15)
83 fprintf (stderr, " ");
85 if (j == 7)
87 fprintf (stderr, " ");
90 fprintf (stderr, "\n");
93 return;
96 /* ---------------------------- local test functions ----------------------- */
98 /* Check values of the constants defined in htmintrin.h. */
99 static int test_constants (void)
101 if (_HTM_TBEGIN_STARTED != 0)
103 return 100 * _HTM_TBEGIN_STARTED + 1;
105 if (_HTM_TBEGIN_INDETERMINATE != 1)
107 return 100 * _HTM_TBEGIN_INDETERMINATE + 2;
109 if (_HTM_TBEGIN_TRANSIENT != 2)
111 return 100 * _HTM_TBEGIN_TRANSIENT + 3;
113 if (_HTM_TBEGIN_PERSISTENT != 3)
115 return 100 * _HTM_TBEGIN_PERSISTENT + 4;
118 return 0;
121 static int test_tbegin_ntstg_tend (void)
123 int rc;
125 counters.c1 = 0;
126 counters.c2 = 0;
127 if ((rc = __builtin_tbegin ((void *)0)) == 0)
129 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
130 counters.c2 = 2;
131 rc = __builtin_tend ();
132 if (rc != 0)
134 return 100 * rc + 5;
136 if (counters.c1 != 1)
138 return 100 * counters.c1 + 2;
140 if (counters.c2 != 2)
142 return 100 * counters.c2 + 3;
145 else
147 return 100 * rc + 4;
150 return 0;
153 static int test_tbegin_ntstg_tabort (void)
155 float f;
157 counters.c1 = 0;
158 counters.c2 = 0;
159 f = 0;
160 if (__builtin_tbegin ((void *)0) == 0)
162 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
163 counters.c2 = 2;
164 f = 1;
165 __builtin_tabort (256);
166 return 1;
168 if (counters.c1 != 1)
170 return 100 * counters.c1 + 2;
172 if (counters.c2 != 0)
174 return 100 * counters.c2 + 3;
176 if (f != 0)
178 return 100 * f + 4;
181 return 0;
184 static int test_tbegin_nofloat (void)
186 int rc;
188 counters.c1 = 0;
189 counters.c2 = 0;
190 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
192 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
193 counters.c2 = 2;
194 rc = __builtin_tend ();
195 if (rc != 0)
197 return 100 * rc + 5;
199 if (counters.c1 != 1)
201 return 100 * counters.c1 + 2;
203 if (counters.c2 != 2)
205 return 100 * counters.c2 + 3;
208 else
210 return 100 * rc + 4;
213 return 0;
216 static int test_tbegin_retry (void)
218 int rc;
220 counters.c1 = 0;
221 counters.c2 = 0;
222 counters.c3 = 0;
223 if ((rc = __builtin_tbegin_retry ((void *)0, 5)) == 0)
225 int do_abort;
227 do_abort = (counters.c1 == 0) ? 1 : 0;
228 __builtin_non_tx_store (
229 (uint64_t *)&counters.c1, counters.c1 + 1);
230 if (do_abort == 1)
232 __builtin_tabort (256);
234 counters.c2 = counters.c2 + 10;
235 __builtin_non_tx_store ((uint64_t *)&counters.c3, 3);
236 rc = __builtin_tend ();
237 if (rc != 0)
239 return 100 * rc + 5;
241 if (counters.c1 != 2)
243 return 100 * counters.c1 + 2;
245 if (counters.c2 != 10)
247 return 100 * counters.c2 + 3;
249 if (counters.c3 != 3)
251 return 100 * counters.c3 + 6;
254 else
256 return 100 * rc + 4;
259 return 0;
262 static int test_tbegin_retry_nofloat (void)
264 int rc;
266 counters.c1 = 0;
267 counters.c2 = 0;
268 counters.c3 = 0;
269 if ((rc = __builtin_tbegin_retry_nofloat ((void *)0, 5)) == 0)
271 int do_abort;
273 do_abort = (counters.c1 == 0) ? 1 : 0;
274 __builtin_non_tx_store (
275 (uint64_t *)&counters.c1, counters.c1 + 1);
276 if (do_abort == 1)
278 __builtin_tabort (256);
280 counters.c2 = counters.c2 + 10;
281 __builtin_non_tx_store ((uint64_t *)&counters.c3, 3);
282 rc = __builtin_tend ();
283 if (rc != 0)
285 return 100 * rc + 5;
287 if (counters.c1 != 2)
289 return 100 * counters.c1 + 2;
291 if (counters.c2 != 10)
293 return 100 * counters.c2 + 3;
295 if (counters.c3 != 3)
297 return 100 * counters.c3 + 6;
300 else
302 return 100 * rc + 4;
305 return 0;
308 static int test_tbegin_aborts (void)
310 float f;
311 int rc;
313 f = 77;
314 if ((rc = __builtin_tbegin ((void *)0)) == 0)
316 f = 88;
317 __builtin_tabort (256);
318 return 2;
320 else if (rc != 2)
322 return 3;
324 if (f != 77)
326 return 4;
328 f = 66;
329 if ((rc = __builtin_tbegin ((void *)0)) == 0)
331 f = 99;
332 __builtin_tabort (257);
333 return 5;
335 else if (rc != 3)
337 return 100 * rc + 6;
339 if (f != 66)
341 return 100 * f + 7;
343 if ((rc = __builtin_tbegin ((void *)0)) == 0)
345 global.float_3 = global.float_1 + global.float_2;
346 rc = __builtin_tend ();
347 if (rc != 0)
349 return 100 * rc + 8;
352 else
354 return 100 * rc + 9;
356 if (global.float_3 != global.float_1 + global.float_2)
358 return 100 * rc + 10;
361 return 0;
364 static __attribute__((noinline)) void indirect_abort(int abort_code)
366 __builtin_tabort (abort_code);
368 return;
371 static int test_tbegin_indirect_aborts (void)
373 float f;
374 int rc;
376 f = 77;
377 if ((rc = __builtin_tbegin ((void *)0)) == 0)
379 f = 88;
380 indirect_abort(256);
381 return 2;
383 else if (rc != 2)
385 return 100 * rc + 3;
387 if (f != 77)
389 return 100 * rc + 4;
391 f = 66;
392 if ((rc = __builtin_tbegin ((void *)0)) == 0)
394 f = 99;
395 indirect_abort(257);
396 return 5;
398 else if (rc != 3)
400 return 100 * rc + 6;
402 if (f != 66)
404 return 100 * f + 7;
407 return 0;
410 static int test_tbegin_nofloat_aborts (void)
412 int rc;
414 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
416 __builtin_tabort (256);
417 return 2;
419 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
421 __builtin_tabort (257);
422 return 1005;
424 else if (rc != 3)
426 return 1000 * rc + 6;
429 return 0;
432 static int test_tbegin_nofloat_indirect_aborts (void)
434 int rc;
436 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
438 indirect_abort (256);
439 return 2;
441 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
443 indirect_abort (257);
444 return 1005;
446 else if (rc != 3)
448 return 1000 * rc + 6;
451 return 0;
454 static
455 int _test_tbegin_retry_aborts (int retries, uint64_t abort_code)
457 int rc;
459 counters.c1 = 0;
460 if ((rc = __builtin_tbegin_retry ((void *)0, retries)) == 0)
462 __builtin_non_tx_store ((uint64_t *)&counters.c1, counters.c1 + 1);
463 __builtin_tabort (abort_code);
464 return 2;
466 else
468 if ((abort_code & 1) == 0)
470 if (rc != 2)
472 return 100 * rc + 2003;
474 else if (counters.c1 != (uint64_t)retries + 1)
476 return 1000 * counters.c1 + 100 * retries + 4;
479 else
481 if (rc != 3)
483 return 100 * rc + 3005;
485 else if (counters.c1 != 1)
487 return 1000 * counters.c1 + 100 * retries + 6;
492 return 0;
495 static int test_tbegin_retry_aborts (void)
497 int rc;
498 int retries;
500 for (retries = 1; retries <= 3; retries++)
502 rc = _test_tbegin_retry_aborts (retries, 256);
503 if (rc != 0)
505 return 10000 + rc;
508 for (retries = 1; retries <= 3; retries++)
510 rc = _test_tbegin_retry_aborts (retries, 257);
511 if (rc != 0)
513 return 20000 + rc;
516 if ((rc = __builtin_tbegin_retry ((void *)0, 5)) == 0)
518 global.float_3 = global.float_1 + global.float_2;
519 rc = __builtin_tend ();
520 if (rc != 0)
522 return 30000 + 100 * rc + 6;
525 else
527 return 30000 + 100 * rc + 7;
530 return 0;
533 static int _test_tbegin_retry_nofloat_aborts (int retries, uint64_t abort_code)
535 int rc;
537 counters.c1 = 0;
538 if ((rc = __builtin_tbegin_retry_nofloat ((void *)0, retries)) == 0)
540 __builtin_non_tx_store ((uint64_t *)&counters.c1, counters.c1 + 1);
541 __builtin_tabort (abort_code);
542 return 2;
544 else
546 if ((abort_code & 1) == 0)
548 if (rc != 2)
550 return 100 * rc + 2003;
552 else if (counters.c1 != (uint64_t)retries + 1)
554 return 1000 * counters.c1 + 100 * retries + 4;
557 else
559 if (rc != 3)
561 return 100 * rc + 3005;
563 else if (counters.c1 != 1)
565 return 1000 * counters.c1 + 100 * retries + 6;
570 return 0;
573 static int test_tbegin_retry_nofloat_aborts (void)
575 int rc;
576 int retries;
578 for (retries = 1; retries <= 3; retries++)
580 rc = _test_tbegin_retry_nofloat_aborts (retries, 256);
581 if (rc != 0)
583 return 10 * retries + rc;
586 for (retries = 1; retries <= 3; retries++)
588 rc = _test_tbegin_retry_nofloat_aborts (retries, 257);
589 if (rc != 0)
591 return 10000 + 10 * retries + rc;
595 return 0;
598 static int test_tbegin_tdb (void)
600 int rc;
602 local_tdb.format = 0;
603 if ((rc = __builtin_tbegin (&local_tdb)) == 0)
605 rc = __builtin_tend ();
606 if (rc != 0)
608 return 100 * rc + 1;
610 if (local_tdb.format != 0)
612 dump_tdb (&local_tdb);
613 return 100 * local_tdb.format + 2;
616 else
618 return 100 * rc + 3;
620 local_tdb.format = 0;
621 if ((rc = __builtin_tbegin (&local_tdb)) == 0)
623 __builtin_tabort (257);
624 return 4;
626 else
628 if (rc != 3)
630 return 100 * rc + 5;
632 if (local_tdb.format != 1)
634 dump_tdb (&local_tdb);
635 return 100 * local_tdb.format + 6;
638 local_tdb256.format = 0;
639 if ((rc = __builtin_tbegin (&local_tdb256)) == 0)
641 rc = __builtin_tend ();
642 if (rc != 0)
644 return 1100 * rc + 1;
646 if (local_tdb256.format != 0)
648 dump_tdb (&local_tdb256);
649 return 1100 * local_tdb256.format + 2;
652 else
654 return 1100 * rc + 3;
656 local_tdb256.format = 0;
657 if ((rc = __builtin_tbegin (&local_tdb256)) == 0)
659 __builtin_tabort (257);
660 return 2004;
662 else
664 if (rc != 3)
666 return 2100 * rc + 5;
668 if (local_tdb256.format != 1)
670 dump_tdb (&local_tdb256);
671 return 2100 * local_tdb256.format + 6;
675 return 0;
678 static int test_tbegin_nofloat_tdb (void)
680 int rc;
682 local_tdb.format = 0;
683 if ((rc = __builtin_tbegin_nofloat (&local_tdb)) == 0)
685 rc = __builtin_tend ();
686 if (rc != 0)
688 return 100 * rc + 1;
690 if (local_tdb.format != 0)
692 dump_tdb (&local_tdb);
693 return 100 * local_tdb.format + 2;
696 else
698 return 3;
700 local_tdb.format = 0;
701 if ((rc = __builtin_tbegin_nofloat (&local_tdb)) == 0)
703 __builtin_tabort (257);
704 return 4;
706 else
708 if (rc != 3)
710 return 100 * rc + 5;
712 if (local_tdb.format != 1)
714 dump_tdb (&local_tdb);
715 return 100 * local_tdb.format + 6;
718 local_tdb256.format = 0;
719 if ((rc = __builtin_tbegin_nofloat (&local_tdb256)) == 0)
721 rc = __builtin_tend ();
722 if (rc != 0)
724 return 1100 * rc + 1;
726 if (local_tdb256.format != 0)
728 dump_tdb (&local_tdb256);
729 return 1100 * local_tdb256.format + 2;
732 else
734 return 1003;
736 local_tdb256.format = 0;
737 if ((rc = __builtin_tbegin_nofloat (&local_tdb256)) == 0)
739 __builtin_tabort (257);
740 return 2004;
742 else
744 if (rc != 3)
746 return 2100 * rc + 5;
748 if (local_tdb256.format != 1)
750 dump_tdb (&local_tdb256);
751 return 2100 * local_tdb256.format + 6;
755 return 0;
758 static int test_tbegin_retry_tdb (void)
760 int rc;
762 local_tdb256.format = 0;
763 if ((rc = __builtin_tbegin_retry (&local_tdb256, 2)) == 0)
765 rc = __builtin_tend ();
766 if (rc != 0)
768 return 1100 * rc + 1;
770 if (local_tdb256.format != 0)
772 dump_tdb (&local_tdb256);
773 return 1100 * local_tdb256.format + 2;
776 else
778 return 1003;
780 local_tdb256.format = 0;
781 if ((rc = __builtin_tbegin_retry (&local_tdb256, 2)) == 0)
783 __builtin_tabort (257);
784 return 2004;
786 else
788 if (rc != 3)
790 return 2100 * rc + 5;
792 if (local_tdb256.format != 1)
794 dump_tdb (&local_tdb256);
795 return 2100 * local_tdb256.format + 6;
799 return 0;
802 static int test_tbegin_retry_nofloat_tdb (void)
804 int rc;
806 local_tdb.format = 0;
807 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb, 2)) == 0)
809 rc = __builtin_tend ();
810 if (rc != 0)
812 return 100 * rc + 1;
814 if (local_tdb.format != 0)
816 dump_tdb (&local_tdb);
817 return 100 * local_tdb.format + 2;
820 else
822 return 100 * rc + 3;
824 local_tdb.format = 0;
825 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb, 2)) == 0)
827 __builtin_tabort (257);
828 return 4;
830 else
832 if (rc != 3)
834 return 100 * rc + 5;
836 if (local_tdb.format != 1)
838 dump_tdb (&local_tdb);
839 return 100 * local_tdb.format + 6;
842 local_tdb256.format = 0;
843 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb256, 2)) == 0)
845 rc = __builtin_tend ();
846 if (rc != 0)
848 return 1100 * rc + 1;
850 if (local_tdb256.format != 0)
852 dump_tdb (&local_tdb256);
853 return 1100 * local_tdb256.format + 2;
856 else
858 return 1100 * rc + 3;
860 local_tdb256.format = 0;
861 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb256, 2)) == 0)
863 __builtin_tabort (257);
864 return 2004;
866 else
868 if (rc != 3)
870 return 2100 * rc + 5;
872 if (local_tdb256.format != 1)
874 dump_tdb (&local_tdb256);
875 return 2100 * local_tdb256.format + 6;
879 return 0;
882 static int test_etnd (void)
884 int rc;
886 counters.c1 = 0;
887 counters.c2 = 0;
888 counters.c3 = 0;
889 if ((rc = __builtin_tbegin ((void *)0)) == 0)
891 counters.c1 = __builtin_tx_nesting_depth ();
892 if (__builtin_tbegin ((void *)0) == 0)
894 counters.c2 = __builtin_tx_nesting_depth ();
895 if (__builtin_tbegin ((void *)0) == 0)
897 counters.c3 = __builtin_tx_nesting_depth ();
898 __builtin_tend ();
900 __builtin_tend ();
902 __builtin_tend ();
904 else
906 return 100 * rc + 1;
908 if (counters.c1 != 1)
910 return 100 * counters.c1 + 2;
912 if (counters.c2 != 2)
914 return 100 * counters.c2 + 3;
916 if (counters.c3 != 3)
918 return 100 * counters.c3 + 4;
921 return 0;
924 static int test_tbeginc (void)
926 int rc;
928 counters.c1 = 0;
929 __builtin_tbeginc ();
930 counters.c1 = 1;
931 rc = __builtin_tend ();
932 if (rc != 0)
934 return 10000 * rc + 1;
936 if (counters.c1 != 1)
938 return 100000 * counters.c1 + 3;
941 return 0;
944 /* ---------------------------- local testing framework functions ---------- */
946 static int run_one_test (const test_table_entry_t *test_entry)
948 int do_print_passes;
949 int succeeded;
950 int rc;
951 int i;
953 /* Warmup run to get all necessary data and instruction pages into the page
954 * tables. */
956 int run;
958 do_dump_tdb = 0;
959 for (run = 0; run < NUM_WARMUP_RUNS; run++)
961 test_entry->test_func ();
963 do_dump_tdb = 1;
965 do_print_passes = (
966 test_entry->required_quorum != 1 ||
967 test_entry->max_repetitions != 1);
968 printf ("RRR RUN %s\n", test_entry->name);
969 if (do_print_passes == 1)
971 printf (
972 " (requires %d successful out of %d runs)\n",
973 test_entry->required_quorum,
974 test_entry->max_repetitions);
976 succeeded = 0;
977 rc = 0;
978 for (rc = 0, i = 0; i < test_entry->max_repetitions; i++)
980 if (do_print_passes == 1)
982 if (i == 0)
984 printf (" ");
986 else
988 printf (",");
991 rc = test_entry->test_func ();
992 if (rc == 0)
994 if (do_print_passes == 1)
996 printf (" success");
998 succeeded++;
999 if (succeeded >= test_entry->required_quorum)
1001 break;
1004 else
1006 printf (" failed (rc = %d)", rc);
1009 if (do_print_passes == 1 || rc != 0)
1011 printf ("\n");
1013 if (succeeded >= test_entry->required_quorum)
1015 printf ("+++ OK %s\n", test_entry->name);
1017 return 0;
1019 else
1021 printf ("--- FAIL %s\n", test_entry->name);
1023 return (rc != 0) ? rc : -1;
1027 static int run_all_tests (const test_table_entry_t *test_table)
1029 const test_table_entry_t *test;
1030 int rc;
1032 for (
1033 rc = 0, test = &test_table[0];
1034 test->test_func != NULL && rc == 0; test++)
1036 rc = run_one_test (test);
1039 return rc;
1042 /* ---------------------------- interface functions ------------------------ */
1044 int main (void)
1046 const test_table_entry_t test_table[] = {
1047 TEST_NO_REP (test_constants),
1048 TEST_DF_REP (test_tbegin_ntstg_tend),
1049 TEST_DF_REP (test_tbegin_ntstg_tabort),
1050 TEST_DF_REP (test_tbegin_nofloat),
1051 TEST_NO_REP (test_tbegin_retry),
1052 TEST_NO_REP (test_tbegin_retry_nofloat),
1053 TEST_DF_REP (test_tbegin_aborts),
1054 TEST_DF_REP (test_tbegin_indirect_aborts),
1055 TEST_DF_REP (test_tbegin_nofloat_aborts),
1056 TEST_DF_REP (test_tbegin_nofloat_indirect_aborts),
1057 TEST_NO_REP (test_tbegin_retry_aborts),
1058 TEST_NO_REP (test_tbegin_retry_nofloat_aborts),
1059 TEST_DF_REP (test_tbegin_tdb),
1060 TEST_DF_REP (test_tbegin_nofloat_tdb),
1061 TEST_NO_REP (test_tbegin_retry_tdb),
1062 TEST_NO_REP (test_tbegin_retry_nofloat_tdb),
1063 TEST_DF_REP (test_etnd),
1064 TEST_DF_REP (test_tbeginc),
1065 { (void *)0, 0, 0 }
1069 int rc;
1071 rc = run_all_tests (test_table);
1073 return rc;