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