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