16ce11cbeab56177962b22c03a8802b3b20ff7f9
[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 static inline void inc(int *v)
50 {
51         __asm__("incl %0" : "+g" (*v) : : "cc");
52 }
53
54 #if 1
55 typedef struct kernel_fd_set {
56         int bla;
57         int blup;
58 } kernel_fd_set;
59 #else
60 typedef int kernel_fd_set;
61 #endif
62
63 void fd_set(int fd, kernel_fd_set* set) {
64         __asm__("btsl %1,%0" : "=m" (*(set)) : "r" (fd));
65 }
66
67 int fd_isset(int fd, kernel_fd_set *set) {
68         unsigned char result;
69
70         __asm__ __volatile__("btl %1,%2\n"
71                          "\tsetb %0"
72                         : "=q" (result)
73                         : "r" (fd),  "m" (*set));
74         return result;
75 }
76
77 int justcompile(void)
78 {
79         outb(123, 42);
80         outb(12345, 42);
81         return inb(20) + inb(5);
82 }
83
84 int main()
85 {
86         kernel_fd_set s;
87         int k;
88
89         fd_set(20, &s);
90         assert(fd_isset(20, &s));
91
92         printf("Swap16(0xAABB): %X Swap32(0xAABBCCDD): %X\n",
93                swap16(0xAABB), swap32(0xAABBCCDD));
94         k = 41;
95         inc(&k);
96         printf("mov(inc(41)): %d\n", mov(k));
97
98         return mov(0);
99 }
100
101 #else
102
103 int main()
104 {
105         printf("Warning: asmtest only work on x86\n");
106         return 0;
107 }
108
109 #endif