1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_muloti4
3 // REQUIRES: int128
4 
5 #include "int_lib.h"
6 #include <stdio.h>
7 
8 #ifdef CRT_HAS_128BIT
9 
10 // Returns: a * b
11 
12 // Effects: sets overflow if a * b overflows
13 
14 COMPILER_RT_ABI ti_int __muloti4(ti_int a, ti_int b, int *overflow);
15 
test__muloti4(ti_int a,ti_int b,ti_int expected,int expected_overflow)16 int test__muloti4(ti_int a, ti_int b, ti_int expected, int expected_overflow)
17 {
18     int ov;
19     ti_int x = __muloti4(a, b, &ov);
20     if (ov != expected_overflow) {
21       twords at;
22       at.all = a;
23       twords bt;
24       bt.all = b;
25       twords xt;
26       xt.all = x;
27       twords expectedt;
28       expectedt.all = expected;
29 
30       printf("error in __muloti4: overflow=%d expected=%d\n",
31 	     ov, expected_overflow);
32       printf("error in __muloti4: 0x%.16llX%.16llX * 0x%.16llX%.16llX = "
33 	     "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
34 	     at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low,
35 	     expectedt.s.high, expectedt.s.low);
36       return 1;
37     }
38     else if (!expected_overflow && x != expected)
39     {
40         twords at;
41         at.all = a;
42         twords bt;
43         bt.all = b;
44         twords xt;
45         xt.all = x;
46         twords expectedt;
47         expectedt.all = expected;
48         printf("error in __muloti4: 0x%.16llX%.16llX * 0x%.16llX%.16llX = "
49                "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
50                at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low,
51                expectedt.s.high, expectedt.s.low);
52 	return 1;
53     }
54     return 0;
55 }
56 
57 #endif
58 
main()59 int main()
60 {
61 #ifdef CRT_HAS_128BIT
62     if (test__muloti4(0, 0, 0, 0))
63         return 1;
64     if (test__muloti4(0, 1, 0, 0))
65         return 1;
66     if (test__muloti4(1, 0, 0, 0))
67         return 1;
68     if (test__muloti4(0, 10, 0, 0))
69         return 1;
70     if (test__muloti4(10, 0, 0, 0))
71         return 1;
72     if (test__muloti4(0, 81985529216486895LL, 0, 0))
73         return 1;
74     if (test__muloti4(81985529216486895LL, 0, 0, 0))
75         return 1;
76 
77     if (test__muloti4(0, -1, 0, 0))
78         return 1;
79     if (test__muloti4(-1, 0, 0, 0))
80         return 1;
81     if (test__muloti4(0, -10, 0, 0))
82         return 1;
83     if (test__muloti4(-10, 0, 0, 0))
84         return 1;
85     if (test__muloti4(0, -81985529216486895LL, 0, 0))
86         return 1;
87     if (test__muloti4(-81985529216486895LL, 0, 0, 0))
88         return 1;
89 
90     if (test__muloti4(1, 1, 1, 0))
91         return 1;
92     if (test__muloti4(1, 10, 10, 0))
93         return 1;
94     if (test__muloti4(10, 1, 10, 0))
95         return 1;
96     if (test__muloti4(1, 81985529216486895LL, 81985529216486895LL, 0))
97         return 1;
98     if (test__muloti4(81985529216486895LL, 1, 81985529216486895LL, 0))
99         return 1;
100 
101     if (test__muloti4(1, -1, -1, 0))
102         return 1;
103     if (test__muloti4(1, -10, -10, 0))
104         return 1;
105     if (test__muloti4(-10, 1, -10, 0))
106         return 1;
107     if (test__muloti4(1, -81985529216486895LL, -81985529216486895LL, 0))
108         return 1;
109     if (test__muloti4(-81985529216486895LL, 1, -81985529216486895LL, 0))
110         return 1;
111 
112     if (test__muloti4(3037000499LL, 3037000499LL, 9223372030926249001LL, 0))
113         return 1;
114     if (test__muloti4(-3037000499LL, 3037000499LL, -9223372030926249001LL, 0))
115         return 1;
116     if (test__muloti4(3037000499LL, -3037000499LL, -9223372030926249001LL, 0))
117         return 1;
118     if (test__muloti4(-3037000499LL, -3037000499LL, 9223372030926249001LL, 0))
119         return 1;
120 
121     if (test__muloti4(4398046511103LL, 2097152LL, 9223372036852678656LL, 0))
122         return 1;
123     if (test__muloti4(-4398046511103LL, 2097152LL, -9223372036852678656LL, 0))
124         return 1;
125     if (test__muloti4(4398046511103LL, -2097152LL, -9223372036852678656LL, 0))
126         return 1;
127     if (test__muloti4(-4398046511103LL, -2097152LL, 9223372036852678656LL, 0))
128         return 1;
129 
130     if (test__muloti4(2097152LL, 4398046511103LL, 9223372036852678656LL, 0))
131         return 1;
132     if (test__muloti4(-2097152LL, 4398046511103LL, -9223372036852678656LL, 0))
133         return 1;
134     if (test__muloti4(2097152LL, -4398046511103LL, -9223372036852678656LL, 0))
135         return 1;
136     if (test__muloti4(-2097152LL, -4398046511103LL, 9223372036852678656LL, 0))
137         return 1;
138 
139     if (test__muloti4(make_ti(0x00000000000000B5LL, 0x04F333F9DE5BE000LL),
140                       make_ti(0x0000000000000000LL, 0x00B504F333F9DE5BLL),
141                       make_ti(0x7FFFFFFFFFFFF328LL, 0xDF915DA296E8A000LL), 0))
142         return 1;
143 
144      if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
145                        -2,
146                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
147        return 1;
148      if (test__muloti4(-2,
149                        make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
150                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
151          return 1;
152     if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
153                       -1,
154                       make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0))
155         return 1;
156     if (test__muloti4(-1,
157                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
158                       make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0))
159         return 1;
160     if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
161                       0,
162                       0, 0))
163         return 1;
164     if (test__muloti4(0,
165                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
166                       0, 0))
167         return 1;
168     if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
169                       1,
170                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0))
171         return 1;
172     if (test__muloti4(1,
173                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
174                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0))
175         return 1;
176      if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
177                        2,
178                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
179          return 1;
180      if (test__muloti4(2,
181                        make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL),
182                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
183          return 1;
184 
185      if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL),
186                        -2,
187                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
188          return 1;
189      if (test__muloti4(-2,
190                        make_ti(0x8000000000000000LL, 0x0000000000000000LL),
191                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
192          return 1;
193      if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL),
194                        -1,
195                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
196          return 1;
197      if (test__muloti4(-1,
198                        make_ti(0x8000000000000000LL, 0x0000000000000000LL),
199                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
200          return 1;
201     if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL),
202                       0,
203                       0, 0))
204         return 1;
205     if (test__muloti4(0,
206                       make_ti(0x8000000000000000LL, 0x0000000000000000LL),
207                       0, 0))
208         return 1;
209     if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL),
210                       1,
211                       make_ti(0x8000000000000000LL, 0x0000000000000000LL), 0))
212         return 1;
213     if (test__muloti4(1,
214                       make_ti(0x8000000000000000LL, 0x0000000000000000LL),
215                       make_ti(0x8000000000000000LL, 0x0000000000000000LL), 0))
216         return 1;
217      if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL),
218                        2,
219                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
220          return 1;
221      if (test__muloti4(2,
222                        make_ti(0x8000000000000000LL, 0x0000000000000000LL),
223                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
224          return 1;
225 
226      if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL),
227                        -2,
228                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
229          return 1;
230      if (test__muloti4(-2,
231                        make_ti(0x8000000000000000LL, 0x0000000000000001LL),
232                        make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1))
233          return 1;
234     if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL),
235                       -1,
236                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0))
237         return 1;
238     if (test__muloti4(-1,
239                       make_ti(0x8000000000000000LL, 0x0000000000000001LL),
240                       make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0))
241         return 1;
242     if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL),
243                       0,
244                       0, 0))
245         return 1;
246     if (test__muloti4(0,
247                       make_ti(0x8000000000000000LL, 0x0000000000000001LL),
248                       0, 0))
249         return 1;
250     if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL),
251                       1,
252                       make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0))
253         return 1;
254     if (test__muloti4(1,
255                       make_ti(0x8000000000000000LL, 0x0000000000000001LL),
256                       make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0))
257         return 1;
258      if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL),
259                        2,
260                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
261          return 1;
262      if (test__muloti4(2,
263                        make_ti(0x8000000000000000LL, 0x0000000000000001LL),
264                        make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1))
265          return 1;
266 
267 #else
268     printf("skipped\n");
269 #endif
270     return 0;
271 }
272