updated be_AddSP semantics
[libfirm] / ir / be / test / asm_test.c
1 #include <stdio.h>
2 #include <assert.h>
3
4 #ifdef __i386__
5 static inline unsigned char inb(const unsigned short port)
6 {
7     unsigned char val;
8
9     __asm__ __volatile__ ("inb  %w1, %0" : "=a"(val) : "dN"(port));
10
11     return val;
12 }
13
14 static inline void outb(const unsigned short port, const unsigned char val)
15 {
16         int k = val; /* just here to test the b modifier in %b0 */
17     __asm__ __volatile__ ("outb %b0, %1" : : "a"(k), "dN"(port));
18 }
19
20 static void sincostest(double arg)
21 {
22         double cos, sin;
23
24         __asm__ ("fsincos" : "=t"(cos), "=u"(sin) : "0" (arg));
25         printf("Arg: %f Sin: %f Cos: %f\n", arg, sin, cos);
26 }
27
28 static inline int mov(int val)
29 {
30         int res;
31
32         __asm__ ("movl %1, %0" : "=r"(res) : "ri" (val));
33
34         return res;
35 }
36
37 static inline unsigned short swap16(unsigned short x)
38 {
39         __asm__("xchgb %b0, %h0 /* in: %1 out: %0 */" : "=q" (x) : "0" (x));
40         return x;
41 }
42
43 static inline unsigned int swap32(unsigned int x)
44 {
45         __asm__("bswap %0 /* %1 */" : "=r" (x) : "0" (x));
46         return x;
47 }
48
49 #if 1
50 typedef struct kernel_fd_set {
51         int bla;
52         int blup;
53 } kernel_fd_set;
54 #else
55 typedef int kernel_fd_set;
56 #endif
57
58 void fd_set(int fd, kernel_fd_set* set) {
59         __asm__("btsl %1,%0" : "=m" (*(set)) : "r" (fd));
60 }
61
62 int fd_isset(int fd, kernel_fd_set *set) {
63         unsigned char result;
64
65         __asm__ __volatile__("btl %1,%2\n"
66                          "\tsetb %0"
67                         : "=q" (result)
68                         : "r" (fd),  "m" (*set));
69         return result;
70 }
71
72 int justcompile(void)
73 {
74         outb(123, 42);
75         outb(12345, 42);
76         return inb(20) + inb(5);
77 }
78
79 int main()
80 {
81         kernel_fd_set s;
82
83         fd_set(20, &s);
84         assert(fd_isset(20, &s));
85
86         printf("Swap16: %d Swap32: %d\n", swap16(12), swap32(123551235));
87
88         return mov(0);
89 }
90
91 #else
92
93 int main()
94 {
95         printf("Warning: asmtest only work on x86\n");
96         return 0;
97 }
98
99 #endif