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
14COMPILER_RT_ABI ti_int __muloti4(ti_int a, ti_int b, int *overflow);
15
16int 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, overflow: &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(format: "error in __muloti4: overflow=%d expected=%d\n",
31 ov, expected_overflow);
32 printf(format: "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(format: "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
59int main()
60{
61#ifdef CRT_HAS_128BIT
62 if (test__muloti4(a: 0, b: 0, expected: 0, expected_overflow: 0))
63 return 1;
64 if (test__muloti4(a: 0, b: 1, expected: 0, expected_overflow: 0))
65 return 1;
66 if (test__muloti4(a: 1, b: 0, expected: 0, expected_overflow: 0))
67 return 1;
68 if (test__muloti4(a: 0, b: 10, expected: 0, expected_overflow: 0))
69 return 1;
70 if (test__muloti4(a: 10, b: 0, expected: 0, expected_overflow: 0))
71 return 1;
72 if (test__muloti4(a: 0, b: 81985529216486895LL, expected: 0, expected_overflow: 0))
73 return 1;
74 if (test__muloti4(a: 81985529216486895LL, b: 0, expected: 0, expected_overflow: 0))
75 return 1;
76
77 if (test__muloti4(a: 0, b: -1, expected: 0, expected_overflow: 0))
78 return 1;
79 if (test__muloti4(a: -1, b: 0, expected: 0, expected_overflow: 0))
80 return 1;
81 if (test__muloti4(a: 0, b: -10, expected: 0, expected_overflow: 0))
82 return 1;
83 if (test__muloti4(a: -10, b: 0, expected: 0, expected_overflow: 0))
84 return 1;
85 if (test__muloti4(a: 0, b: -81985529216486895LL, expected: 0, expected_overflow: 0))
86 return 1;
87 if (test__muloti4(a: -81985529216486895LL, b: 0, expected: 0, expected_overflow: 0))
88 return 1;
89
90 if (test__muloti4(a: 1, b: 1, expected: 1, expected_overflow: 0))
91 return 1;
92 if (test__muloti4(a: 1, b: 10, expected: 10, expected_overflow: 0))
93 return 1;
94 if (test__muloti4(a: 10, b: 1, expected: 10, expected_overflow: 0))
95 return 1;
96 if (test__muloti4(a: 1, b: 81985529216486895LL, expected: 81985529216486895LL, expected_overflow: 0))
97 return 1;
98 if (test__muloti4(a: 81985529216486895LL, b: 1, expected: 81985529216486895LL, expected_overflow: 0))
99 return 1;
100
101 if (test__muloti4(a: 1, b: -1, expected: -1, expected_overflow: 0))
102 return 1;
103 if (test__muloti4(a: 1, b: -10, expected: -10, expected_overflow: 0))
104 return 1;
105 if (test__muloti4(a: -10, b: 1, expected: -10, expected_overflow: 0))
106 return 1;
107 if (test__muloti4(a: 1, b: -81985529216486895LL, expected: -81985529216486895LL, expected_overflow: 0))
108 return 1;
109 if (test__muloti4(a: -81985529216486895LL, b: 1, expected: -81985529216486895LL, expected_overflow: 0))
110 return 1;
111
112 if (test__muloti4(a: 3037000499LL, b: 3037000499LL, expected: 9223372030926249001LL, expected_overflow: 0))
113 return 1;
114 if (test__muloti4(a: -3037000499LL, b: 3037000499LL, expected: -9223372030926249001LL, expected_overflow: 0))
115 return 1;
116 if (test__muloti4(a: 3037000499LL, b: -3037000499LL, expected: -9223372030926249001LL, expected_overflow: 0))
117 return 1;
118 if (test__muloti4(a: -3037000499LL, b: -3037000499LL, expected: 9223372030926249001LL, expected_overflow: 0))
119 return 1;
120
121 if (test__muloti4(a: 4398046511103LL, b: 2097152LL, expected: 9223372036852678656LL, expected_overflow: 0))
122 return 1;
123 if (test__muloti4(a: -4398046511103LL, b: 2097152LL, expected: -9223372036852678656LL, expected_overflow: 0))
124 return 1;
125 if (test__muloti4(a: 4398046511103LL, b: -2097152LL, expected: -9223372036852678656LL, expected_overflow: 0))
126 return 1;
127 if (test__muloti4(a: -4398046511103LL, b: -2097152LL, expected: 9223372036852678656LL, expected_overflow: 0))
128 return 1;
129
130 if (test__muloti4(a: 2097152LL, b: 4398046511103LL, expected: 9223372036852678656LL, expected_overflow: 0))
131 return 1;
132 if (test__muloti4(a: -2097152LL, b: 4398046511103LL, expected: -9223372036852678656LL, expected_overflow: 0))
133 return 1;
134 if (test__muloti4(a: 2097152LL, b: -4398046511103LL, expected: -9223372036852678656LL, expected_overflow: 0))
135 return 1;
136 if (test__muloti4(a: -2097152LL, b: -4398046511103LL, expected: 9223372036852678656LL, expected_overflow: 0))
137 return 1;
138
139 if (test__muloti4(a: make_ti(h: 0x00000000000000B5LL, l: 0x04F333F9DE5BE000LL),
140 b: make_ti(h: 0x0000000000000000LL, l: 0x00B504F333F9DE5BLL),
141 expected: make_ti(h: 0x7FFFFFFFFFFFF328LL, l: 0xDF915DA296E8A000LL), expected_overflow: 0))
142 return 1;
143
144 if (test__muloti4(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
145 b: -2,
146 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
147 return 1;
148 if (test__muloti4(a: -2,
149 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
150 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
151 return 1;
152 if (test__muloti4(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
153 b: -1,
154 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 0))
155 return 1;
156 if (test__muloti4(a: -1,
157 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
158 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 0))
159 return 1;
160 if (test__muloti4(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
161 b: 0,
162 expected: 0, expected_overflow: 0))
163 return 1;
164 if (test__muloti4(a: 0,
165 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
166 expected: 0, expected_overflow: 0))
167 return 1;
168 if (test__muloti4(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
169 b: 1,
170 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL), expected_overflow: 0))
171 return 1;
172 if (test__muloti4(a: 1,
173 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
174 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL), expected_overflow: 0))
175 return 1;
176 if (test__muloti4(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
177 b: 2,
178 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
179 return 1;
180 if (test__muloti4(a: 2,
181 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
182 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
183 return 1;
184
185 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
186 b: -2,
187 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
188 return 1;
189 if (test__muloti4(a: -2,
190 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
191 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
192 return 1;
193 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
194 b: -1,
195 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
196 return 1;
197 if (test__muloti4(a: -1,
198 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
199 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
200 return 1;
201 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
202 b: 0,
203 expected: 0, expected_overflow: 0))
204 return 1;
205 if (test__muloti4(a: 0,
206 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
207 expected: 0, expected_overflow: 0))
208 return 1;
209 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
210 b: 1,
211 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 0))
212 return 1;
213 if (test__muloti4(a: 1,
214 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
215 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 0))
216 return 1;
217 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
218 b: 2,
219 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
220 return 1;
221 if (test__muloti4(a: 2,
222 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
223 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
224 return 1;
225
226 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
227 b: -2,
228 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
229 return 1;
230 if (test__muloti4(a: -2,
231 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
232 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 1))
233 return 1;
234 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
235 b: -1,
236 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL), expected_overflow: 0))
237 return 1;
238 if (test__muloti4(a: -1,
239 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
240 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL), expected_overflow: 0))
241 return 1;
242 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
243 b: 0,
244 expected: 0, expected_overflow: 0))
245 return 1;
246 if (test__muloti4(a: 0,
247 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
248 expected: 0, expected_overflow: 0))
249 return 1;
250 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
251 b: 1,
252 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 0))
253 return 1;
254 if (test__muloti4(a: 1,
255 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
256 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL), expected_overflow: 0))
257 return 1;
258 if (test__muloti4(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
259 b: 2,
260 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
261 return 1;
262 if (test__muloti4(a: 2,
263 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
264 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL), expected_overflow: 1))
265 return 1;
266
267#else
268 printf("skipped\n");
269#endif
270 return 0;
271}
272

source code of compiler-rt/test/builtins/Unit/muloti4_test.c