don't use llabs, edg has no prototype
[libfirm] / ir / be / test / ll.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef long long int ll_t;
5 typedef unsigned long long int ull_t;
6
7 #ifdef __GNUC__
8 ll_t mul_ll(ll_t a, ll_t b) __attribute__((noinline));
9 ll_t shl_ll(ll_t a, ll_t b) __attribute__((noinline));
10 ll_t shr_ll(ll_t a, ll_t b) __attribute__((noinline));
11 ll_t add_ll(ll_t a, ll_t b) __attribute__((noinline));
12 ll_t sub_ll(ll_t a, ll_t b) __attribute__((noinline));
13 ll_t div_ll(ll_t a, ll_t b) __attribute__((noinline));
14 ll_t mod_ll(ll_t a, ll_t b) __attribute__((noinline));
15 ll_t divmod_ll(ll_t a, ll_t b) __attribute__((noinline));
16 ll_t abs_ll(ll_t a) __attribute__((noinline));
17 ll_t neg_ll(ll_t a) __attribute__((noinline));
18
19 ull_t mul_ull(ull_t a, ull_t b) __attribute__((noinline));
20 ull_t shl_ull(ull_t a, ull_t b) __attribute__((noinline));
21 ull_t shr_ull(ull_t a, ull_t b) __attribute__((noinline));
22 ull_t add_ull(ull_t a, ull_t b) __attribute__((noinline));
23 ull_t sub_ull(ull_t a, ull_t b) __attribute__((noinline));
24 ull_t div_ull(ull_t a, ull_t b) __attribute__((noinline));
25 ull_t mod_ull(ull_t a, ull_t b) __attribute__((noinline));
26 ull_t divmod_ull(ull_t a, ull_t b) __attribute__((noinline));
27 ull_t neg_ull(ull_t a) __attribute__((noinline));
28 #endif
29
30 ll_t mul_ll(ll_t a, ll_t b) {
31         return a * b;
32 }
33
34 ll_t shl_ll(ll_t a, ll_t b) {
35         return a << b;
36 }
37
38 ll_t shr_ll(ll_t a, ll_t b) {
39         return a >> b;
40 }
41
42 ll_t add_ll(ll_t a, ll_t b) {
43         return a + b;
44 }
45
46 ll_t sub_ll(ll_t a, ll_t b) {
47         return a - b;
48 }
49
50 ll_t div_ll(ll_t a, ll_t b) {
51         return a / b;
52 }
53
54 ll_t mod_ll(ll_t a, ll_t b) {
55         return a % b;
56 }
57
58 ll_t divmod_ll(ll_t a, ll_t b) {
59         return (a / b) + (a % b);
60 }
61
62 ll_t neg_ll(ll_t a) {
63         return -a;
64 }
65
66 ll_t abs_ll(ll_t a) {
67         return a < 0 ? -a : a;
68 }
69
70 #if 0
71 double conv_ll_d(ll_t a) {
72         return (double)a;
73 }
74
75 ll_t conv_d_ll(double a) {
76         return (ll_t)a;
77 }
78 #endif
79
80 /* unsigned */
81
82 ull_t mul_ull(ull_t a, ull_t b) {
83         return a * b;
84 }
85
86 ull_t shl_ull(ull_t a, ull_t b) {
87         return a << b;
88 }
89
90 ull_t shr_ull(ull_t a, ull_t b) {
91         return a >> b;
92 }
93
94 ull_t add_ull(ull_t a, ull_t b) {
95         return a + b;
96 }
97
98 ull_t sub_ull(ull_t a, ull_t b) {
99         return a - b;
100 }
101
102 ull_t div_ull(ull_t a, ull_t b) {
103         return a / b;
104 }
105
106 ull_t mod_ull(ull_t a, ull_t b) {
107         return a % b;
108 }
109
110 ull_t divmod_ull(ull_t a, ull_t b) {
111         return (a / b) + (a % b);
112 }
113
114 ull_t neg_ull(ull_t a) {
115         return -a;
116 }
117
118 #if 0
119 double conv_ull_d(ull_t a) {
120         return (double)a;
121 }
122
123 ull_t conv_d_ull(double a) {
124         return (ull_t)a;
125 }
126 #endif
127
128 int main(void) {
129         ll_t a = 0xff;
130         ll_t b = 0x123456789;
131         ll_t c = 0x8001023000002460;
132         double d = (double)c;
133
134         ull_t ua = 0xff;
135         ull_t ub = 0x123456789;
136         ull_t uc = 0x8001023000002460;
137
138         printf("%lld * %lld  = %lld\n", a, b, mul_ll(a, b));
139         printf("%lld + %lld  = %lld\n", a, b, add_ll(a, b));
140         printf("%lld - %lld  = %lld\n", a, b, sub_ll(a, b));
141         printf("%lld / %lld  = %lld\n", b, a, div_ll(b, a));
142         printf("%lld %% %lld  = %lld\n", b, a, mod_ll(b, a));
143         printf("%lld / + %% %lld  = %lld\n", b, a, divmod_ll(b, a));
144         printf("%lld << %d = %lld\n", a, 2, shl_ll(a, 2));
145         printf("%lld >> %d = %lld\n", a, 2, shr_ll(a, 2));
146         printf("abs(%lld)    = %lld\n", c, abs_ll(c));
147         printf("neg(%lld)    = %lld\n", b, neg_ll(b));
148 #if 0
149         printf("conv(%lld)   = %lf\n",  c, conv_ll_d(c));
150         printf("conv(%lf)    = %lld\n", d, conv_d_ll(d));
151 #endif
152
153         printf("%llu * %llu  = %llu\n", ua, ub, mul_ull(ua, ub));
154         printf("%llu + %llu  = %llu\n", ua, ub, add_ull(ua, ub));
155         printf("%llu - %llu  = %llu\n", ua, ub, sub_ull(ua, ub));
156         printf("%llu / %llu  = %llu\n", ub, ua, div_ull(ub, ua));
157         printf("%llu %% %llu  = %llu\n", ub, ua, mod_ull(ub, ua));
158         printf("%llu / + %% %llu  = %llu\n", ub, ua, divmod_ull(ub, ua));
159         printf("%llu << %d = %llu\n", ua, 2, shl_ull(ua, 2));
160         printf("%llu >> %d = %llu\n", ua, 2, shr_ull(ua, 2));
161         printf("neg(%llu)    = %llu\n", ub, neg_ull(ub));
162 #if 0
163         printf("conv(%llu)   = %lf\n",  uc, conv_ull_d(uc));
164         printf("conv(%lf)    = %llu\n", d, conv_d_ull(d));
165 #endif
166         return 0;
167 }