* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
[official-gcc.git] / gcc / testsuite / gcc.target / s390 / htm-builtins-1.c
blobff43be9fe736e27024e406ab852ee960411c4e33
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 volatile uint64_t c2;
57 volatile uint64_t c3;
58 } counters = { 0, 0, 0 };
60 /* ---------------------------- local helper functions --------------------- */
62 static void dump_tdb (struct __htm_tdb *tdb)
64 unsigned char *p;
65 int i;
66 int j;
68 if (do_dump_tdb == 0)
70 return;
72 p = (unsigned char *)tdb;
73 for (i = 0; i < 16; i++)
75 fprintf (stderr, "0x%02x ", i * 16);
76 for (j = 0; j < 16; j++)
78 fprintf (stderr, "%02x", (int)p[i * 16 + j]);
79 if (j < 15)
81 fprintf (stderr, " ");
83 if (j == 7)
85 fprintf (stderr, " ");
88 fprintf (stderr, "\n");
91 return;
94 /* ---------------------------- local test functions ----------------------- */
96 /* Check values of the constants defined in htmintrin.h. */
97 static int test_constants (void)
99 if (_HTM_TBEGIN_STARTED != 0)
101 return 100 * _HTM_TBEGIN_STARTED + 1;
103 if (_HTM_TBEGIN_INDETERMINATE != 1)
105 return 100 * _HTM_TBEGIN_INDETERMINATE + 2;
107 if (_HTM_TBEGIN_TRANSIENT != 2)
109 return 100 * _HTM_TBEGIN_TRANSIENT + 3;
111 if (_HTM_TBEGIN_PERSISTENT != 3)
113 return 100 * _HTM_TBEGIN_PERSISTENT + 4;
116 return 0;
119 static int test_tbegin_ntstg_tend (void)
121 int rc;
123 counters.c1 = 0;
124 counters.c2 = 0;
125 if ((rc = __builtin_tbegin ((void *)0)) == 0)
127 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
128 counters.c2 = 2;
129 rc = __builtin_tend ();
130 if (rc != 0)
132 return 100 * rc + 5;
134 if (counters.c1 != 1)
136 return 100 * counters.c1 + 2;
138 if (counters.c2 != 2)
140 return 100 * counters.c2 + 3;
143 else
145 return 100 * rc + 4;
148 return 0;
151 static int test_tbegin_ntstg_tabort (void)
153 float f;
155 counters.c1 = 0;
156 counters.c2 = 0;
157 f = 0;
158 if (__builtin_tbegin ((void *)0) == 0)
160 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
161 counters.c2 = 2;
162 f = 1;
163 __builtin_tabort (256);
164 return 1;
166 if (counters.c1 != 1)
168 return 100 * counters.c1 + 2;
170 if (counters.c2 != 0)
172 return 100 * counters.c2 + 3;
174 if (f != 0)
176 return 100 * f + 4;
179 return 0;
182 static int test_tbegin_nofloat (void)
184 int rc;
186 counters.c1 = 0;
187 counters.c2 = 0;
188 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
190 __builtin_non_tx_store ((uint64_t *)&counters.c1, 1);
191 counters.c2 = 2;
192 rc = __builtin_tend ();
193 if (rc != 0)
195 return 100 * rc + 5;
197 if (counters.c1 != 1)
199 return 100 * counters.c1 + 2;
201 if (counters.c2 != 2)
203 return 100 * counters.c2 + 3;
206 else
208 return 100 * rc + 4;
211 return 0;
214 static int test_tbegin_retry (void)
216 int rc;
218 counters.c1 = 0;
219 counters.c2 = 0;
220 counters.c3 = 0;
221 if ((rc = __builtin_tbegin_retry ((void *)0, 5)) == 0)
223 int do_abort;
225 do_abort = (counters.c1 == 0) ? 1 : 0;
226 __builtin_non_tx_store (
227 (uint64_t *)&counters.c1, counters.c1 + 1);
228 if (do_abort == 1)
230 __builtin_tabort (256);
232 counters.c2 = counters.c2 + 10;
233 __builtin_non_tx_store ((uint64_t *)&counters.c3, 3);
234 rc = __builtin_tend ();
235 if (rc != 0)
237 return 100 * rc + 5;
239 if (counters.c1 != 2)
241 return 100 * counters.c1 + 2;
243 if (counters.c2 != 10)
245 return 100 * counters.c2 + 3;
247 if (counters.c3 != 3)
249 return 100 * counters.c3 + 6;
252 else
254 return 100 * rc + 4;
257 return 0;
260 static int test_tbegin_retry_nofloat (void)
262 int rc;
264 counters.c1 = 0;
265 counters.c2 = 0;
266 counters.c3 = 0;
267 if ((rc = __builtin_tbegin_retry_nofloat ((void *)0, 5)) == 0)
269 int do_abort;
271 do_abort = (counters.c1 == 0) ? 1 : 0;
272 __builtin_non_tx_store (
273 (uint64_t *)&counters.c1, counters.c1 + 1);
274 if (do_abort == 1)
276 __builtin_tabort (256);
278 counters.c2 = counters.c2 + 10;
279 __builtin_non_tx_store ((uint64_t *)&counters.c3, 3);
280 rc = __builtin_tend ();
281 if (rc != 0)
283 return 100 * rc + 5;
285 if (counters.c1 != 2)
287 return 100 * counters.c1 + 2;
289 if (counters.c2 != 10)
291 return 100 * counters.c2 + 3;
293 if (counters.c3 != 3)
295 return 100 * counters.c3 + 6;
298 else
300 return 100 * rc + 4;
303 return 0;
306 static int test_tbegin_aborts (void)
308 float f;
309 int rc;
311 f = 77;
312 if ((rc = __builtin_tbegin ((void *)0)) == 0)
314 f = 88;
315 __builtin_tabort (256);
316 return 2;
318 else if (rc != 2)
320 return 3;
322 if (f != 77)
324 return 4;
326 f = 66;
327 if ((rc = __builtin_tbegin ((void *)0)) == 0)
329 f = 99;
330 __builtin_tabort (257);
331 return 5;
333 else if (rc != 3)
335 return 100 * rc + 6;
337 if (f != 66)
339 return 100 * f + 7;
341 if ((rc = __builtin_tbegin ((void *)0)) == 0)
343 global.float_3 = global.float_1 + global.float_2;
344 rc = __builtin_tend ();
345 if (rc != 0)
347 return 100 * rc + 8;
350 else
352 return 100 * rc + 9;
354 if (global.float_3 != global.float_1 + global.float_2)
356 return 100 * rc + 10;
359 return 0;
362 static __attribute__((noinline)) void indirect_abort(int abort_code)
364 __builtin_tabort (abort_code);
366 return;
369 static int test_tbegin_indirect_aborts (void)
371 float f;
372 int rc;
374 f = 77;
375 if ((rc = __builtin_tbegin ((void *)0)) == 0)
377 f = 88;
378 indirect_abort(256);
379 return 2;
381 else if (rc != 2)
383 return 100 * rc + 3;
385 if (f != 77)
387 return 100 * rc + 4;
389 f = 66;
390 if ((rc = __builtin_tbegin ((void *)0)) == 0)
392 f = 99;
393 indirect_abort(257);
394 return 5;
396 else if (rc != 3)
398 return 100 * rc + 6;
400 if (f != 66)
402 return 100 * f + 7;
405 return 0;
408 static int test_tbegin_nofloat_aborts (void)
410 int rc;
412 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
414 __builtin_tabort (256);
415 return 2;
417 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
419 __builtin_tabort (257);
420 return 1005;
422 else if (rc != 3)
424 return 1000 * rc + 6;
427 return 0;
430 static int test_tbegin_nofloat_indirect_aborts (void)
432 int rc;
434 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
436 indirect_abort (256);
437 return 2;
439 if ((rc = __builtin_tbegin_nofloat ((void *)0)) == 0)
441 indirect_abort (257);
442 return 1005;
444 else if (rc != 3)
446 return 1000 * rc + 6;
449 return 0;
452 static
453 int _test_tbegin_retry_aborts (int retries, uint64_t abort_code)
455 int rc;
457 counters.c1 = 0;
458 if ((rc = __builtin_tbegin_retry ((void *)0, retries)) == 0)
460 __builtin_non_tx_store ((uint64_t *)&counters.c1, counters.c1 + 1);
461 __builtin_tabort (abort_code);
462 return 2;
464 else
466 if ((abort_code & 1) == 0)
468 if (rc != 2)
470 return 100 * rc + 2003;
472 else if (counters.c1 != (uint64_t)retries + 1)
474 return 1000 * counters.c1 + 100 * retries + 4;
477 else
479 if (rc != 3)
481 return 100 * rc + 3005;
483 else if (counters.c1 != 1)
485 return 1000 * counters.c1 + 100 * retries + 6;
490 return 0;
493 static int test_tbegin_retry_aborts (void)
495 int rc;
496 int retries;
498 for (retries = 1; retries <= 3; retries++)
500 rc = _test_tbegin_retry_aborts (retries, 256);
501 if (rc != 0)
503 return 10000 + rc;
506 for (retries = 1; retries <= 3; retries++)
508 rc = _test_tbegin_retry_aborts (retries, 257);
509 if (rc != 0)
511 return 20000 + rc;
514 if ((rc = __builtin_tbegin_retry ((void *)0, 5)) == 0)
516 global.float_3 = global.float_1 + global.float_2;
517 rc = __builtin_tend ();
518 if (rc != 0)
520 return 30000 + 100 * rc + 6;
523 else
525 return 30000 + 100 * rc + 7;
528 return 0;
531 static int _test_tbegin_retry_nofloat_aborts (int retries, uint64_t abort_code)
533 int rc;
535 counters.c1 = 0;
536 if ((rc = __builtin_tbegin_retry_nofloat ((void *)0, retries)) == 0)
538 __builtin_non_tx_store ((uint64_t *)&counters.c1, counters.c1 + 1);
539 __builtin_tabort (abort_code);
540 return 2;
542 else
544 if ((abort_code & 1) == 0)
546 if (rc != 2)
548 return 100 * rc + 2003;
550 else if (counters.c1 != (uint64_t)retries + 1)
552 return 1000 * counters.c1 + 100 * retries + 4;
555 else
557 if (rc != 3)
559 return 100 * rc + 3005;
561 else if (counters.c1 != 1)
563 return 1000 * counters.c1 + 100 * retries + 6;
568 return 0;
571 static int test_tbegin_retry_nofloat_aborts (void)
573 int rc;
574 int retries;
576 for (retries = 1; retries <= 3; retries++)
578 rc = _test_tbegin_retry_nofloat_aborts (retries, 256);
579 if (rc != 0)
581 return 10 * retries + rc;
584 for (retries = 1; retries <= 3; retries++)
586 rc = _test_tbegin_retry_nofloat_aborts (retries, 257);
587 if (rc != 0)
589 return 10000 + 10 * retries + rc;
593 return 0;
596 static int test_tbegin_tdb (void)
598 int rc;
600 local_tdb.format = 0;
601 if ((rc = __builtin_tbegin (&local_tdb)) == 0)
603 rc = __builtin_tend ();
604 if (rc != 0)
606 return 100 * rc + 1;
608 if (local_tdb.format != 0)
610 dump_tdb (&local_tdb);
611 return 100 * local_tdb.format + 2;
614 else
616 return 100 * rc + 3;
618 local_tdb.format = 0;
619 if ((rc = __builtin_tbegin (&local_tdb)) == 0)
621 __builtin_tabort (257);
622 return 4;
624 else
626 if (rc != 3)
628 return 100 * rc + 5;
630 if (local_tdb.format != 1)
632 dump_tdb (&local_tdb);
633 return 100 * local_tdb.format + 6;
636 local_tdb256.format = 0;
637 if ((rc = __builtin_tbegin (&local_tdb256)) == 0)
639 rc = __builtin_tend ();
640 if (rc != 0)
642 return 1100 * rc + 1;
644 if (local_tdb256.format != 0)
646 dump_tdb (&local_tdb256);
647 return 1100 * local_tdb256.format + 2;
650 else
652 return 1100 * rc + 3;
654 local_tdb256.format = 0;
655 if ((rc = __builtin_tbegin (&local_tdb256)) == 0)
657 __builtin_tabort (257);
658 return 2004;
660 else
662 if (rc != 3)
664 return 2100 * rc + 5;
666 if (local_tdb256.format != 1)
668 dump_tdb (&local_tdb256);
669 return 2100 * local_tdb256.format + 6;
673 return 0;
676 static int test_tbegin_nofloat_tdb (void)
678 int rc;
680 local_tdb.format = 0;
681 if ((rc = __builtin_tbegin_nofloat (&local_tdb)) == 0)
683 rc = __builtin_tend ();
684 if (rc != 0)
686 return 100 * rc + 1;
688 if (local_tdb.format != 0)
690 dump_tdb (&local_tdb);
691 return 100 * local_tdb.format + 2;
694 else
696 return 3;
698 local_tdb.format = 0;
699 if ((rc = __builtin_tbegin_nofloat (&local_tdb)) == 0)
701 __builtin_tabort (257);
702 return 4;
704 else
706 if (rc != 3)
708 return 100 * rc + 5;
710 if (local_tdb.format != 1)
712 dump_tdb (&local_tdb);
713 return 100 * local_tdb.format + 6;
716 local_tdb256.format = 0;
717 if ((rc = __builtin_tbegin_nofloat (&local_tdb256)) == 0)
719 rc = __builtin_tend ();
720 if (rc != 0)
722 return 1100 * rc + 1;
724 if (local_tdb256.format != 0)
726 dump_tdb (&local_tdb256);
727 return 1100 * local_tdb256.format + 2;
730 else
732 return 1003;
734 local_tdb256.format = 0;
735 if ((rc = __builtin_tbegin_nofloat (&local_tdb256)) == 0)
737 __builtin_tabort (257);
738 return 2004;
740 else
742 if (rc != 3)
744 return 2100 * rc + 5;
746 if (local_tdb256.format != 1)
748 dump_tdb (&local_tdb256);
749 return 2100 * local_tdb256.format + 6;
753 return 0;
756 static int test_tbegin_retry_tdb (void)
758 int rc;
760 local_tdb256.format = 0;
761 if ((rc = __builtin_tbegin_retry (&local_tdb256, 2)) == 0)
763 rc = __builtin_tend ();
764 if (rc != 0)
766 return 1100 * rc + 1;
768 if (local_tdb256.format != 0)
770 dump_tdb (&local_tdb256);
771 return 1100 * local_tdb256.format + 2;
774 else
776 return 1003;
778 local_tdb256.format = 0;
779 if ((rc = __builtin_tbegin_retry (&local_tdb256, 2)) == 0)
781 __builtin_tabort (257);
782 return 2004;
784 else
786 if (rc != 3)
788 return 2100 * rc + 5;
790 if (local_tdb256.format != 1)
792 dump_tdb (&local_tdb256);
793 return 2100 * local_tdb256.format + 6;
797 return 0;
800 static int test_tbegin_retry_nofloat_tdb (void)
802 int rc;
804 local_tdb.format = 0;
805 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb, 2)) == 0)
807 rc = __builtin_tend ();
808 if (rc != 0)
810 return 100 * rc + 1;
812 if (local_tdb.format != 0)
814 dump_tdb (&local_tdb);
815 return 100 * local_tdb.format + 2;
818 else
820 return 100 * rc + 3;
822 local_tdb.format = 0;
823 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb, 2)) == 0)
825 __builtin_tabort (257);
826 return 4;
828 else
830 if (rc != 3)
832 return 100 * rc + 5;
834 if (local_tdb.format != 1)
836 dump_tdb (&local_tdb);
837 return 100 * local_tdb.format + 6;
840 local_tdb256.format = 0;
841 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb256, 2)) == 0)
843 rc = __builtin_tend ();
844 if (rc != 0)
846 return 1100 * rc + 1;
848 if (local_tdb256.format != 0)
850 dump_tdb (&local_tdb256);
851 return 1100 * local_tdb256.format + 2;
854 else
856 return 1100 * rc + 3;
858 local_tdb256.format = 0;
859 if ((rc = __builtin_tbegin_retry_nofloat (&local_tdb256, 2)) == 0)
861 __builtin_tabort (257);
862 return 2004;
864 else
866 if (rc != 3)
868 return 2100 * rc + 5;
870 if (local_tdb256.format != 1)
872 dump_tdb (&local_tdb256);
873 return 2100 * local_tdb256.format + 6;
877 return 0;
880 static int test_etnd (void)
882 int rc;
884 counters.c1 = 0;
885 counters.c2 = 0;
886 counters.c3 = 0;
887 if ((rc = __builtin_tbegin ((void *)0)) == 0)
889 counters.c1 = __builtin_tx_nesting_depth ();
890 if (__builtin_tbegin ((void *)0) == 0)
892 counters.c2 = __builtin_tx_nesting_depth ();
893 if (__builtin_tbegin ((void *)0) == 0)
895 counters.c3 = __builtin_tx_nesting_depth ();
896 __builtin_tend ();
898 __builtin_tend ();
900 __builtin_tend ();
902 else
904 return 100 * rc + 1;
906 if (counters.c1 != 1)
908 return 100 * counters.c1 + 2;
910 if (counters.c2 != 2)
912 return 100 * counters.c2 + 3;
914 if (counters.c3 != 3)
916 return 100 * counters.c3 + 4;
919 return 0;
922 static int test_tbeginc (void)
924 int rc;
926 counters.c1 = 0;
927 __builtin_tbeginc ();
928 counters.c1 = 1;
929 rc = __builtin_tend ();
930 if (rc != 0)
932 return 10000 * rc + 1;
934 if (counters.c1 != 1)
936 return 100000 * counters.c1 + 3;
939 return 0;
942 /* ---------------------------- local testing framework functions ---------- */
944 static int run_one_test (const test_table_entry_t *test_entry)
946 int do_print_passes;
947 int succeeded;
948 int rc;
949 int i;
951 /* Warmup run to get all necessary data and instruction pages into the page
952 * tables. */
954 int run;
956 do_dump_tdb = 0;
957 for (run = 0; run < NUM_WARMUP_RUNS; run++)
959 test_entry->test_func ();
961 do_dump_tdb = 1;
963 do_print_passes = (
964 test_entry->required_quorum != 1 ||
965 test_entry->max_repetitions != 1);
966 printf ("RRR RUN %s\n", test_entry->name);
967 if (do_print_passes == 1)
969 printf (
970 " (requires %d successful out of %d runs)\n",
971 test_entry->required_quorum,
972 test_entry->max_repetitions);
974 succeeded = 0;
975 rc = 0;
976 for (rc = 0, i = 0; i < test_entry->max_repetitions; i++)
978 if (do_print_passes == 1)
980 if (i == 0)
982 printf (" ");
984 else
986 printf (",");
989 rc = test_entry->test_func ();
990 if (rc == 0)
992 if (do_print_passes == 1)
994 printf (" success");
996 succeeded++;
997 if (succeeded >= test_entry->required_quorum)
999 break;
1002 else
1004 printf (" failed (rc = %d)", rc);
1007 if (do_print_passes == 1 || rc != 0)
1009 printf ("\n");
1011 if (succeeded >= test_entry->required_quorum)
1013 printf ("+++ OK %s\n", test_entry->name);
1015 return 0;
1017 else
1019 printf ("--- FAIL %s\n", test_entry->name);
1021 return (rc != 0) ? rc : -1;
1025 static int run_all_tests (const test_table_entry_t *test_table)
1027 const test_table_entry_t *test;
1028 int rc;
1030 for (
1031 rc = 0, test = &test_table[0];
1032 test->test_func != NULL && rc == 0; test++)
1034 rc = run_one_test (test);
1037 return rc;
1040 /* ---------------------------- interface functions ------------------------ */
1042 int main (void)
1044 const test_table_entry_t test_table[] = {
1045 TEST_NO_REP (test_constants),
1046 TEST_DF_REP (test_tbegin_ntstg_tend),
1047 TEST_DF_REP (test_tbegin_ntstg_tabort),
1048 TEST_DF_REP (test_tbegin_nofloat),
1049 TEST_NO_REP (test_tbegin_retry),
1050 TEST_NO_REP (test_tbegin_retry_nofloat),
1051 TEST_DF_REP (test_tbegin_aborts),
1052 TEST_DF_REP (test_tbegin_indirect_aborts),
1053 TEST_DF_REP (test_tbegin_nofloat_aborts),
1054 TEST_DF_REP (test_tbegin_nofloat_indirect_aborts),
1055 TEST_NO_REP (test_tbegin_retry_aborts),
1056 TEST_NO_REP (test_tbegin_retry_nofloat_aborts),
1057 TEST_DF_REP (test_tbegin_tdb),
1058 TEST_DF_REP (test_tbegin_nofloat_tdb),
1059 TEST_NO_REP (test_tbegin_retry_tdb),
1060 TEST_NO_REP (test_tbegin_retry_nofloat_tdb),
1061 TEST_DF_REP (test_etnd),
1062 TEST_DF_REP (test_tbeginc),
1063 { (void *)0, 0, 0 }
1067 int rc;
1069 rc = run_all_tests (test_table);
1071 return rc;