xref: /openbmc/qemu/tests/tcg/i386/test-i386-fbstp.c (revision f0984d40)
1 /* Test fbstp instruction.  */
2 
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <string.h>
6 
7 union u {
8     struct { uint64_t sig; uint16_t sign_exp; } s;
9     long double ld;
10 };
11 
12 volatile union u ld_invalid_1 = { .s = { 1, 1234 } };
13 volatile union u ld_invalid_2 = { .s = { 0, 1234 } };
14 volatile union u ld_invalid_3 = { .s = { 0, 0x7fff } };
15 volatile union u ld_invalid_4 = { .s = { (UINT64_C(1) << 63) - 1, 0x7fff } };
16 
17 int main(void)
18 {
19     int ret = 0;
20     unsigned char out[10];
21     memset(out, 0xfe, sizeof out);
22     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-0.0L) : "st");
23     out[9] &= 0x80;
24     if (memcmp(out, "\0\0\0\0\0\0\0\0\0\x80", sizeof out) != 0) {
25         printf("FAIL: fbstp -0\n");
26         ret = 1;
27     }
28     memset(out, 0x12, sizeof out);
29     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-0.1L) : "st");
30     out[9] &= 0x80;
31     if (memcmp(out, "\0\0\0\0\0\0\0\0\0\x80", sizeof out) != 0) {
32         printf("FAIL: fbstp -0.1\n");
33         ret = 1;
34     }
35     memset(out, 0x1f, sizeof out);
36     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-987654321987654321.0L) :
37                       "st");
38     out[9] &= 0x80;
39     if (memcmp(out, "\x21\x43\x65\x87\x19\x32\x54\x76\x98\x80",
40                sizeof out) != 0) {
41         printf("FAIL: fbstp -987654321987654321\n");
42         ret = 1;
43     }
44     memset(out, 0x12, sizeof out);
45     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (999999999999999999.5L) :
46                       "st");
47     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
48         printf("FAIL: fbstp 999999999999999999.5\n");
49         ret = 1;
50     }
51     memset(out, 0x12, sizeof out);
52     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (1000000000000000000.0L) :
53                       "st");
54     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
55         printf("FAIL: fbstp 1000000000000000000\n");
56         ret = 1;
57     }
58     memset(out, 0x12, sizeof out);
59     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (1e30L) : "st");
60     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
61         printf("FAIL: fbstp 1e30\n");
62         ret = 1;
63     }
64     memset(out, 0x12, sizeof out);
65     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-999999999999999999.5L) :
66                       "st");
67     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
68         printf("FAIL: fbstp -999999999999999999.5\n");
69         ret = 1;
70     }
71     memset(out, 0x12, sizeof out);
72     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-1000000000000000000.0L) :
73                       "st");
74     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
75         printf("FAIL: fbstp -1000000000000000000\n");
76         ret = 1;
77     }
78     memset(out, 0x12, sizeof out);
79     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-1e30L) : "st");
80     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
81         printf("FAIL: fbstp -1e30\n");
82         ret = 1;
83     }
84     memset(out, 0x12, sizeof out);
85     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (__builtin_infl()) : "st");
86     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
87         printf("FAIL: fbstp inf\n");
88         ret = 1;
89     }
90     memset(out, 0x12, sizeof out);
91     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-__builtin_infl()) :
92                       "st");
93     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
94         printf("FAIL: fbstp -inf\n");
95         ret = 1;
96     }
97     memset(out, 0x12, sizeof out);
98     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (__builtin_nanl("")) :
99                       "st");
100     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
101         printf("FAIL: fbstp nan\n");
102         ret = 1;
103     }
104     memset(out, 0x12, sizeof out);
105     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (-__builtin_nanl("")) :
106                       "st");
107     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
108         printf("FAIL: fbstp -nan\n");
109         ret = 1;
110     }
111     memset(out, 0x12, sizeof out);
112     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (ld_invalid_1.ld) :
113                       "st");
114     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
115         printf("FAIL: fbstp invalid 1\n");
116         ret = 1;
117     }
118     memset(out, 0x12, sizeof out);
119     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (ld_invalid_2.ld) :
120                       "st");
121     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
122         printf("FAIL: fbstp invalid 2\n");
123         ret = 1;
124     }
125     memset(out, 0x12, sizeof out);
126     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (ld_invalid_3.ld) :
127                       "st");
128     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
129         printf("FAIL: fbstp invalid 3\n");
130         ret = 1;
131     }
132     memset(out, 0x12, sizeof out);
133     __asm__ volatile ("fbstp %0" : "=m" (out) : "t" (ld_invalid_4.ld) :
134                       "st");
135     if (memcmp(out, "\0\0\0\0\0\0\0\xc0\xff\xff", sizeof out) != 0) {
136         printf("FAIL: fbstp invalid 4\n");
137         ret = 1;
138     }
139     return ret;
140 }
141