xref: /qemu/tests/tcg/ppc64/bcdsub.c (revision 75ac231c)
1 #include <assert.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <stdint.h>
5 
6 #define CRF_LT  (1 << 3)
7 #define CRF_GT  (1 << 2)
8 #define CRF_EQ  (1 << 1)
9 #define CRF_SO  (1 << 0)
10 #define UNDEF   0
11 
12 #ifdef __has_builtin
13 #if !__has_builtin(__builtin_bcdsub)
14 #define NO_BUILTIN_BCDSUB
15 #endif
16 #endif
17 
18 #ifdef NO_BUILTIN_BCDSUB
19 #define BCDSUB(T, A, B, PS) \
20     ".long 4 << 26 | (" #T ") << 21 | (" #A ") << 16 | (" #B ") << 11"  \
21     " | 1 << 10 | (" #PS ") << 9 | 65\n\t"
22 #else
23 #define BCDSUB(T, A, B, PS) "bcdsub. " #T ", " #A ", " #B ", " #PS "\n\t"
24 #endif
25 
26 #define TEST(AH, AL, BH, BL, PS, TH, TL, CR6)                                  \
27     do {                                                                       \
28         int cr = 0;                                                            \
29         uint64_t th, tl;                                                       \
30         /*                                                                     \
31          * Use GPR pairs to load the VSR values and place the resulting VSR and\
32          * CR6 in th, tl, and cr. Note that we avoid newer instructions (e.g., \
33          * mtvsrdd/mfvsrld) so we can run this test on POWER8 machines.        \
34          */                                                                    \
35         asm ("mtvsrd 32, %3\n\t"                                               \
36              "mtvsrd 33, %4\n\t"                                               \
37              "xxmrghd 32, 32, 33\n\t"                                          \
38              "mtvsrd 33, %5\n\t"                                               \
39              "mtvsrd 34, %6\n\t"                                               \
40              "xxmrghd 33, 33, 34\n\t"                                          \
41              BCDSUB(0, 0, 1, PS)                                               \
42              "mfocrf %0, 0b10\n\t"                                             \
43              "mfvsrd %1, 32\n\t"                                               \
44              "xxswapd 32, 32\n\t"                                              \
45              "mfvsrd %2, 32\n\t"                                               \
46              : "=r" (cr), "=r" (th), "=r" (tl)                                 \
47              : "r" (AH), "r" (AL), "r" (BH), "r" (BL)                          \
48              : "v0", "v1", "v2");                                              \
49         if (TH != UNDEF || TL != UNDEF) {                                      \
50             assert(tl == TL);                                                  \
51             assert(th == TH);                                                  \
52         }                                                                      \
53         assert((cr >> 4) == CR6);                                              \
54     } while (0)
55 
56 /*
57  * Unbounded result is equal to zero:
58  *   sign = (PS) ? 0b1111 : 0b1100
59  *   CR6 = 0b0010
60  */
61 void test_bcdsub_eq(void)
62 {
63     /* maximum positive BCD value */
64     TEST(0x9999999999999999, 0x999999999999999c,
65          0x9999999999999999, 0x999999999999999c,
66          0, 0x0, 0xc, CRF_EQ);
67     TEST(0x9999999999999999, 0x999999999999999c,
68          0x9999999999999999, 0x999999999999999c,
69          1, 0x0, 0xf, CRF_EQ);
70 }
71 
72 /*
73  * Unbounded result is greater than zero:
74  *   sign = (PS) ? 0b1111 : 0b1100
75  *   CR6 = (overflow) ? 0b0101 : 0b0100
76  */
77 void test_bcdsub_gt(void)
78 {
79     /* maximum positive and negative one BCD values */
80     TEST(0x9999999999999999, 0x999999999999999c, 0x0, 0x1d, 0,
81          0x0, 0xc, (CRF_GT | CRF_SO));
82     TEST(0x9999999999999999, 0x999999999999999c, 0x0, 0x1d, 1,
83          0x0, 0xf, (CRF_GT | CRF_SO));
84 
85     TEST(0x9999999999999999, 0x999999999999998c, 0x0, 0x1d, 0,
86          0x9999999999999999, 0x999999999999999c, CRF_GT);
87     TEST(0x9999999999999999, 0x999999999999998c, 0x0, 0x1d, 1,
88          0x9999999999999999, 0x999999999999999f, CRF_GT);
89 }
90 
91 /*
92  * Unbounded result is less than zero:
93  *   sign = 0b1101
94  *   CR6 = (overflow) ? 0b1001 : 0b1000
95  */
96 void test_bcdsub_lt(void)
97 {
98     /* positive zero and positive one BCD values */
99     TEST(0x0, 0xc, 0x0, 0x1c, 0, 0x0, 0x1d, CRF_LT);
100     TEST(0x0, 0xc, 0x0, 0x1c, 1, 0x0, 0x1d, CRF_LT);
101 
102     /* maximum negative and positive one BCD values */
103     TEST(0x9999999999999999, 0x999999999999999d, 0x0, 0x1c, 0,
104          0x0, 0xd, (CRF_LT | CRF_SO));
105     TEST(0x9999999999999999, 0x999999999999999d, 0x0, 0x1c, 1,
106          0x0, 0xd, (CRF_LT | CRF_SO));
107 }
108 
109 void test_bcdsub_invalid(void)
110 {
111     TEST(0x0, 0x1c, 0x0, 0xf00, 0, UNDEF, UNDEF, CRF_SO);
112     TEST(0x0, 0x1c, 0x0, 0xf00, 1, UNDEF, UNDEF, CRF_SO);
113 
114     TEST(0x0, 0xf00, 0x0, 0x1c, 0, UNDEF, UNDEF, CRF_SO);
115     TEST(0x0, 0xf00, 0x0, 0x1c, 1, UNDEF, UNDEF, CRF_SO);
116 
117     TEST(0x0, 0xbad, 0x0, 0xf00, 0, UNDEF, UNDEF, CRF_SO);
118     TEST(0x0, 0xbad, 0x0, 0xf00, 1, UNDEF, UNDEF, CRF_SO);
119 }
120 
121 int main(void)
122 {
123     struct sigaction action;
124 
125     action.sa_handler = _exit;
126     sigaction(SIGABRT, &action, NULL);
127 
128     test_bcdsub_eq();
129     test_bcdsub_gt();
130     test_bcdsub_lt();
131     test_bcdsub_invalid();
132 
133     return 0;
134 }
135