1 #include <stdio.h>
2 #include <stdint.h>
3 #include "opcodes.h"
4 
5 #define BRASLCLOBBER "0","1","2","3","4","5","14",      \
6       "f0","f1","f2","f3","f4","f5","f6","f7","memory","cc"
7 
8 void inner_iter() { putchar('.'); }
9 void outer_iter() { putchar('+'); }
10 
11 static void
12 check_brcth(int m, int n)
13 {
14    unsigned long cnt = ((unsigned long) m << 32) | n;
15 
16    printf("brcth %d x %d (cnt=0x%lx): ", m, n, cnt);
17    fflush(stdout);
18    asm volatile("1: lr %[cnt],%[n]\n\t"
19                 "2: brasl 14,inner_iter\n\t"
20                 "brct %[cnt],2b\n\t"
21                 "brasl 14,outer_iter\n\t"
22                 ".insn ril,0xcc0600000000,%[cnt],1b\n\t" // BRCTH
23                 : [cnt]"+r" (cnt)
24                 : [n]"r" (n)
25                 : BRASLCLOBBER);
26    printf(" (cnt=0x%lx)\n", cnt);
27 }
28 
29 #define DO_RISBXG(insn, _r1, _r2, i3, i4, i5)                           \
30    ({                                                                   \
31       register unsigned long r1 asm ("1") = _r1;                        \
32       register unsigned long r2 asm ("2") = _r2;                        \
33       asm volatile(insn(1,2, i3, i4, i5)                                \
34                    : "+d" (r1)                                          \
35                    : "d" (r1), "d" (r2));                               \
36       printf(#insn " r1(==%16.16lx),r2(==%16.16lx),0x" #i3 ",0x" #i4    \
37              ",0x" #i5 " = %16.16lx\n", _r1, _r2, r1);                  \
38    })
39 
40 /* Some "random" 64-bit numbers. */
41 
42 #define VAL_A 0x3a41e0a2afde1559
43 #define VAL_B 0x765487c11cd04ac4
44 #define VAL_C 0xb9cd1bdd399bef32
45 #define VAL_D 0xf8efadb884334ddd
46 
47 static void
48 check_risbxg(void)
49 {
50    /* Full copy low -> low */
51    DO_RISBXG(RISBLG, VAL_A, VAL_B, 00, 1f, 00);
52    DO_RISBXG(RISBLG, VAL_B, VAL_A, 00, 9f, 00);
53    /* Full copy high -> low */
54    DO_RISBXG(RISBLG, VAL_B, VAL_A, 00, 1f, 20);
55    DO_RISBXG(RISBLG, VAL_A, VAL_B, 00, 9f, 20);
56    /* Full copy high -> high */
57    DO_RISBXG(RISBHG, VAL_C, VAL_D, 00, 1f, 00);
58    DO_RISBXG(RISBHG, VAL_D, VAL_C, 00, 9f, 00);
59    /* Full copy low -> high */
60    DO_RISBXG(RISBHG, VAL_D, VAL_C, 00, 1f, 20);
61    DO_RISBXG(RISBHG, VAL_C, VAL_D, 00, 9f, 20);
62    /* Middle copy */
63    DO_RISBXG(RISBLG, VAL_C, VAL_A, 0b, 13, 05);
64    DO_RISBXG(RISBLG, VAL_C, VAL_A, 0b, 93, 05);
65    DO_RISBXG(RISBHG, VAL_A, VAL_C, 03, 0c, 25);
66    DO_RISBXG(RISBHG, VAL_A, VAL_C, 03, 8c, 25);
67    /* "Outer" copy (start > end) */
68    DO_RISBXG(RISBLG, VAL_B, VAL_D, 1d, 07, 0c);
69    DO_RISBXG(RISBLG, VAL_B, VAL_D, 1d, 87, 0c);
70    DO_RISBXG(RISBHG, VAL_D, VAL_B, 0c, 03, 25);
71    DO_RISBXG(RISBHG, VAL_D, VAL_B, 0c, 83, 25);
72 }
73 
74 /* Test high-word facility instructions. */
75 
76 int main()
77 {
78    check_brcth(5, 3);
79    check_brcth(1, 30);
80    check_brcth(16, 1);
81 
82    check_risbxg();
83    return 0;
84 }
85