1#undef _GNU_SOURCE
2#define _GNU_SOURCE 1
3#undef __USE_GNU
4#define __USE_GNU 1
5#include <unistd.h>
6#include <stdlib.h>
7#include <string.h>
8#include <stdio.h>
9#include <signal.h>
10#include <sys/types.h>
11#include <sys/select.h>
12#include <sys/time.h>
13#include <sys/wait.h>
14
15#define TEST(insn) \
16long double __attribute__((noinline)) insn(long flags) \
17{						\
18	long double out;			\
19	asm ("\n"				\
20	"	push	%1""\n"			\
21	"	popf""\n"			\
22	"	fldpi""\n"			\
23	"	fld1""\n"			\
24	"	" #insn " %%st(1), %%st" "\n"	\
25	"	ffree	%%st(1)" "\n"		\
26	: "=t" (out)				\
27	: "r" (flags)				\
28	);					\
29	return out;				\
30}
31
32TEST(fcmovb)
33TEST(fcmove)
34TEST(fcmovbe)
35TEST(fcmovu)
36TEST(fcmovnb)
37TEST(fcmovne)
38TEST(fcmovnbe)
39TEST(fcmovnu)
40
41enum {
42	CF = 1 << 0,
43	PF = 1 << 2,
44	ZF = 1 << 6,
45};
46
47void sighandler(int sig)
48{
49	printf("[FAIL]\tGot signal %d, exiting\n", sig);
50	exit(1);
51}
52
53int main(int argc, char **argv, char **envp)
54{
55	int err = 0;
56
57	/* SIGILL triggers on 32-bit kernels w/o fcomi emulation
58	 * when run with "no387 nofxsr". Other signals are caught
59	 * just in case.
60	 */
61	signal(SIGILL, sighandler);
62	signal(SIGFPE, sighandler);
63	signal(SIGSEGV, sighandler);
64
65	printf("[RUN]\tTesting fcmovCC instructions\n");
66	/* If fcmovCC() returns 1.0, the move wasn't done */
67	err |= !(fcmovb(0)   == 1.0); err |= !(fcmovnb(0)  != 1.0);
68	err |= !(fcmove(0)   == 1.0); err |= !(fcmovne(0)  != 1.0);
69	err |= !(fcmovbe(0)  == 1.0); err |= !(fcmovnbe(0) != 1.0);
70	err |= !(fcmovu(0)   == 1.0); err |= !(fcmovnu(0)  != 1.0);
71
72	err |= !(fcmovb(CF)  != 1.0); err |= !(fcmovnb(CF)  == 1.0);
73	err |= !(fcmove(CF)  == 1.0); err |= !(fcmovne(CF)  != 1.0);
74	err |= !(fcmovbe(CF) != 1.0); err |= !(fcmovnbe(CF) == 1.0);
75	err |= !(fcmovu(CF)  == 1.0); err |= !(fcmovnu(CF)  != 1.0);
76
77	err |= !(fcmovb(ZF)  == 1.0); err |= !(fcmovnb(ZF)  != 1.0);
78	err |= !(fcmove(ZF)  != 1.0); err |= !(fcmovne(ZF)  == 1.0);
79	err |= !(fcmovbe(ZF) != 1.0); err |= !(fcmovnbe(ZF) == 1.0);
80	err |= !(fcmovu(ZF)  == 1.0); err |= !(fcmovnu(ZF)  != 1.0);
81
82	err |= !(fcmovb(PF)  == 1.0); err |= !(fcmovnb(PF)  != 1.0);
83	err |= !(fcmove(PF)  == 1.0); err |= !(fcmovne(PF)  != 1.0);
84	err |= !(fcmovbe(PF) == 1.0); err |= !(fcmovnbe(PF) != 1.0);
85	err |= !(fcmovu(PF)  != 1.0); err |= !(fcmovnu(PF)  == 1.0);
86
87        if (!err)
88                printf("[OK]\tfcmovCC\n");
89	else
90		printf("[FAIL]\tfcmovCC errors: %d\n", err);
91
92	return err;
93}
94