10 #include <sys/types.h>
11 #include <sys/select.h>
27 /* sNaN is s|111 1111 1|1xx xxxx xxxx xxxx xxxx xxxx */
28 /* qNaN is s|111 1111 1|0xx xxxx xxxx xxxx xxxx xxxx (some x must be nonzero) */
29 int snan = 0x7fc11111;
30 int qnan = 0x7f811111;
31 unsigned short snan1[5];
32 /* sNaN80 is s|111 1111 1111 1111 |10xx xx...xx (some x must be nonzero) */
33 unsigned short snan80[5] = { 0x1111, 0x1111, 0x1111, 0x8111, 0x7fff };
37 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
45 " fcomi %%st(1), %%st" "\n"
49 " pop res_fcomi_1_pi""\n"
55 " fcomi %%st(1), %%st" "\n"
59 " pop res_fcomi_pi_1""\n"
65 " fcomi %%st(1), %%st" "\n"
69 " pop res_fcomi_1_1""\n"
73 if ((res_fcomi_1_pi & ARITH) != (0)) {
74 printf("[BAD]\tfcomi_1_pi with flags:%lx\n", flags);
77 if ((res_fcomi_pi_1 & ARITH) != (CF)) {
78 printf("[BAD]\tfcomi_pi_1 with flags:%lx->%lx\n", flags, res_fcomi_pi_1 & ARITH);
81 if ((res_fcomi_1_1 & ARITH) != (ZF)) {
82 printf("[BAD]\tfcomi_1_1 with flags:%lx\n", flags);
85 if (fetestexcept(FE_INVALID) != 0) {
86 printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
92 int test_qnan(long flags)
94 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
101 " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
102 " fcomi %%st(1), %%st" "\n"
103 " ffree %%st(0)" "\n"
104 " ffree %%st(1)" "\n"
106 " pop res_fcomi_nan_1""\n"
110 if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
111 printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
114 if (fetestexcept(FE_INVALID) != FE_INVALID) {
115 printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
121 int testu_qnan(long flags)
123 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
130 " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
131 " fucomi %%st(1), %%st" "\n"
132 " ffree %%st(0)" "\n"
133 " ffree %%st(1)" "\n"
135 " pop res_fcomi_nan_1""\n"
139 if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
140 printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
143 if (fetestexcept(FE_INVALID) != 0) {
144 printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
150 int testu_snan(long flags)
152 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
157 // " flds snan""\n" // WRONG, this will convert 32-bit fp snan to a *qnan* in 80-bit fp register!
158 // " fstpt snan1""\n" // if uncommented, it prints "snan1:7fff c111 1100 0000 0000" - c111, not 8111!
159 // " fnclex""\n" // flds of a snan raised FE_INVALID, clear it
160 " fldt snan80""\n" // fldt never raise FE_INVALID
162 " fucomi %%st(1), %%st" "\n"
163 " ffree %%st(0)" "\n"
164 " ffree %%st(1)" "\n"
166 " pop res_fcomi_nan_1""\n"
170 if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
171 printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
174 // printf("snan:%x snan1:%04x %04x %04x %04x %04x\n", snan, snan1[4], snan1[3], snan1[2], snan1[1], snan1[0]);
175 if (fetestexcept(FE_INVALID) != FE_INVALID) {
176 printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
182 int testp(long flags)
184 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
192 " fcomip %%st(1), %%st" "\n"
193 " ffree %%st(0)" "\n"
195 " pop res_fcomi_1_pi""\n"
201 " fcomip %%st(1), %%st" "\n"
202 " ffree %%st(0)" "\n"
204 " pop res_fcomi_pi_1""\n"
210 " fcomip %%st(1), %%st" "\n"
211 " ffree %%st(0)" "\n"
213 " pop res_fcomi_1_1""\n"
217 if ((res_fcomi_1_pi & ARITH) != (0)) {
218 printf("[BAD]\tfcomi_1_pi with flags:%lx\n", flags);
221 if ((res_fcomi_pi_1 & ARITH) != (CF)) {
222 printf("[BAD]\tfcomi_pi_1 with flags:%lx->%lx\n", flags, res_fcomi_pi_1 & ARITH);
225 if ((res_fcomi_1_1 & ARITH) != (ZF)) {
226 printf("[BAD]\tfcomi_1_1 with flags:%lx\n", flags);
229 if (fetestexcept(FE_INVALID) != 0) {
230 printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
236 int testp_qnan(long flags)
238 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
245 " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
246 " fcomip %%st(1), %%st" "\n"
247 " ffree %%st(0)" "\n"
249 " pop res_fcomi_nan_1""\n"
253 if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
254 printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
257 if (fetestexcept(FE_INVALID) != FE_INVALID) {
258 printf("[BAD]\tFE_INVALID is not set in %s\n", __func__);
264 int testup_qnan(long flags)
266 feclearexcept(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW);
273 " fnclex""\n" // fld of a qnan raised FE_INVALID, clear it
274 " fucomip %%st(1), %%st" "\n"
275 " ffree %%st(0)" "\n"
277 " pop res_fcomi_nan_1""\n"
281 if ((res_fcomi_nan_1 & ARITH) != (ZF|CF|PF)) {
282 printf("[BAD]\tfcomi_qnan_1 with flags:%lx\n", flags);
285 if (fetestexcept(FE_INVALID) != 0) {
286 printf("[BAD]\tFE_INVALID is set in %s\n", __func__);
292 void sighandler(int sig)
294 printf("[FAIL]\tGot signal %d, exiting\n", sig);
298 int main(int argc, char **argv, char **envp)
302 /* SIGILL triggers on 32-bit kernels w/o fcomi emulation
303 * when run with "no387 nofxsr". Other signals are caught
306 signal(SIGILL, sighandler);
307 signal(SIGFPE, sighandler);
308 signal(SIGSEGV, sighandler);
310 printf("[RUN]\tTesting f[u]comi[p] instructions\n");
313 err |= testu_qnan(0);
314 err |= testu_snan(0);
315 err |= test(CF|ZF|PF);
316 err |= test_qnan(CF|ZF|PF);
317 err |= testu_qnan(CF|ZF|PF);
318 err |= testu_snan(CF|ZF|PF);
320 err |= testp_qnan(0);
321 err |= testup_qnan(0);
322 err |= testp(CF|ZF|PF);
323 err |= testp_qnan(CF|ZF|PF);
324 err |= testup_qnan(CF|ZF|PF);
326 printf("[OK]\tf[u]comi[p]\n");
328 printf("[FAIL]\tf[u]comi[p] errors: %d\n", err);