blob: 87c8c44a449361f9fb277ed74c852ea5e180261b [file] [log] [blame]
Fabiano Rosas936fda42021-02-22 16:40:35 -03001#include <assert.h>
2#include <unistd.h>
3#include <signal.h>
Matheus Ferst8189cb82022-03-05 07:16:46 +01004#include <stdint.h>
Fabiano Rosas936fda42021-02-22 16:40:35 -03005
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
Matheus Ferst63c2b742022-03-05 07:16:46 +010012#ifdef __has_builtin
13#if !__has_builtin(__builtin_bcdsub)
14#define NO_BUILTIN_BCDSUB
15#endif
16#endif
Fabiano Rosas936fda42021-02-22 16:40:35 -030017
Matheus Ferst63c2b742022-03-05 07:16:46 +010018#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); \
Fabiano Rosas936fda42021-02-22 16:40:35 -030054 } while (0)
55
Fabiano Rosas936fda42021-02-22 16:40:35 -030056/*
57 * Unbounded result is equal to zero:
58 * sign = (PS) ? 0b1111 : 0b1100
59 * CR6 = 0b0010
60 */
61void test_bcdsub_eq(void)
62{
Fabiano Rosas936fda42021-02-22 16:40:35 -030063 /* maximum positive BCD value */
Matheus Ferst8189cb82022-03-05 07:16:46 +010064 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);
Fabiano Rosas936fda42021-02-22 16:40:35 -030070}
71
72/*
73 * Unbounded result is greater than zero:
74 * sign = (PS) ? 0b1111 : 0b1100
75 * CR6 = (overflow) ? 0b0101 : 0b0100
76 */
77void test_bcdsub_gt(void)
78{
Matheus Ferst8189cb82022-03-05 07:16:46 +010079 /* 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));
Fabiano Rosas936fda42021-02-22 16:40:35 -030084
Matheus Ferst8189cb82022-03-05 07:16:46 +010085 TEST(0x9999999999999999, 0x999999999999998c, 0x0, 0x1d, 0,
86 0x9999999999999999, 0x999999999999999c, CRF_GT);
87 TEST(0x9999999999999999, 0x999999999999998c, 0x0, 0x1d, 1,
88 0x9999999999999999, 0x999999999999999f, CRF_GT);
Fabiano Rosas936fda42021-02-22 16:40:35 -030089}
90
91/*
92 * Unbounded result is less than zero:
93 * sign = 0b1101
94 * CR6 = (overflow) ? 0b1001 : 0b1000
95 */
96void test_bcdsub_lt(void)
97{
Matheus Ferst8189cb82022-03-05 07:16:46 +010098 /* 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);
Fabiano Rosas936fda42021-02-22 16:40:35 -0300101
Matheus Ferst8189cb82022-03-05 07:16:46 +0100102 /* 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));
Fabiano Rosas936fda42021-02-22 16:40:35 -0300107}
108
109void test_bcdsub_invalid(void)
110{
Matheus Ferst8189cb82022-03-05 07:16:46 +0100111 TEST(0x0, 0x1c, 0x0, 0xf00, 0, UNDEF, UNDEF, CRF_SO);
112 TEST(0x0, 0x1c, 0x0, 0xf00, 1, UNDEF, UNDEF, CRF_SO);
Fabiano Rosas936fda42021-02-22 16:40:35 -0300113
Matheus Ferst8189cb82022-03-05 07:16:46 +0100114 TEST(0x0, 0xf00, 0x0, 0x1c, 0, UNDEF, UNDEF, CRF_SO);
115 TEST(0x0, 0xf00, 0x0, 0x1c, 1, UNDEF, UNDEF, CRF_SO);
Fabiano Rosas936fda42021-02-22 16:40:35 -0300116
Matheus Ferst8189cb82022-03-05 07:16:46 +0100117 TEST(0x0, 0xbad, 0x0, 0xf00, 0, UNDEF, UNDEF, CRF_SO);
118 TEST(0x0, 0xbad, 0x0, 0xf00, 1, UNDEF, UNDEF, CRF_SO);
Fabiano Rosas936fda42021-02-22 16:40:35 -0300119}
120
121int 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}