xref: /openbmc/qemu/tests/tcg/s390x/cvb.c (revision 03582094)
1 /*
2  * Test the CONVERT TO BINARY instruction.
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include <assert.h>
7 #include <signal.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 static int signum;
13 
14 static void signal_handler(int n)
15 {
16     signum = n;
17 }
18 
19 #define FAIL 0x1234567887654321
20 #define OK32(x) (0x1234567800000000 | (uint32_t)(x))
21 
22 static int64_t cvb(uint64_t x)
23 {
24     int64_t ret = FAIL;
25 
26     signum = -1;
27     asm("cvb %[ret],%[x]" : [ret] "+r" (ret) : [x] "R" (x));
28 
29     return ret;
30 }
31 
32 static int64_t cvby(uint64_t x)
33 {
34     int64_t ret = FAIL;
35 
36     signum = -1;
37     asm("cvby %[ret],%[x]" : [ret] "+r" (ret) : [x] "T" (x));
38 
39     return ret;
40 }
41 
42 static int64_t cvbg(__uint128_t x)
43 {
44     int64_t ret = FAIL;
45 
46     signum = -1;
47     asm("cvbg %[ret],%[x]" : [ret] "+r" (ret) : [x] "T" (x));
48 
49     return ret;
50 }
51 
52 int main(void)
53 {
54     __uint128_t m = (((__uint128_t)0x9223372036854775) << 16) | 0x8070;
55     struct sigaction act;
56     int err;
57 
58     memset(&act, 0, sizeof(act));
59     act.sa_handler = signal_handler;
60     err = sigaction(SIGFPE, &act, NULL);
61     assert(err == 0);
62     err = sigaction(SIGILL, &act, NULL);
63     assert(err == 0);
64 
65     assert(cvb(0xc) == OK32(0) && signum == -1);
66     assert(cvb(0x1c) == OK32(1) && signum == -1);
67     assert(cvb(0x25594c) == OK32(25594) && signum == -1);
68     assert(cvb(0x1d) == OK32(-1) && signum == -1);
69     assert(cvb(0x2147483647c) == OK32(0x7fffffff) && signum == -1);
70     assert(cvb(0x2147483648d) == OK32(-0x80000000) && signum == -1);
71     assert(cvb(0x7) == FAIL && signum == SIGILL);
72     assert(cvb(0x2147483648c) == OK32(0x80000000) && signum == SIGFPE);
73     assert(cvb(0x3000000000c) == OK32(0xb2d05e00) && signum == SIGFPE);
74     assert(cvb(0x2147483649d) == OK32(0x7fffffff) && signum == SIGFPE);
75     assert(cvb(0x3000000000d) == OK32(0x4d2fa200) && signum == SIGFPE);
76 
77     assert(cvby(0xc) == OK32(0));
78     assert(cvby(0x1c) == OK32(1));
79     assert(cvby(0x25594c) == OK32(25594));
80     assert(cvby(0x1d) == OK32(-1));
81     assert(cvby(0x2147483647c) == OK32(0x7fffffff));
82     assert(cvby(0x2147483648d) == OK32(-0x80000000));
83     assert(cvby(0x7) == FAIL && signum == SIGILL);
84     assert(cvby(0x2147483648c) == OK32(0x80000000) && signum == SIGFPE);
85     assert(cvby(0x3000000000c) == OK32(0xb2d05e00) && signum == SIGFPE);
86     assert(cvby(0x2147483649d) == OK32(0x7fffffff) && signum == SIGFPE);
87     assert(cvby(0x3000000000d) == OK32(0x4d2fa200) && signum == SIGFPE);
88 
89     assert(cvbg(0xc) == 0);
90     assert(cvbg(0x1c) == 1);
91     assert(cvbg(0x25594c) == 25594);
92     assert(cvbg(0x1d) == -1);
93     assert(cvbg(m + 0xc) == 0x7fffffffffffffff);
94     assert(cvbg(m + 0x1d) == -0x8000000000000000);
95     assert(cvbg(0x7) == FAIL && signum == SIGILL);
96     assert(cvbg(m + 0x1c) == FAIL && signum == SIGFPE);
97     assert(cvbg(m + 0x2d) == FAIL && signum == SIGFPE);
98     assert(cvbg(((__uint128_t)1 << 80) + 0xc) == FAIL && signum == SIGFPE);
99     assert(cvbg(((__uint128_t)1 << 80) + 0xd) == FAIL && signum == SIGFPE);
100 
101     return EXIT_SUCCESS;
102 }
103