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
14COMPILER_RT_ABI ti_int __mulvti3(ti_int a, ti_int b);
15
16int 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(format: "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
39int main()
40{
41#ifdef CRT_HAS_128BIT
42 if (test__mulvti3(a: 0, b: 0, expected: 0))
43 return 1;
44 if (test__mulvti3(a: 0, b: 1, expected: 0))
45 return 1;
46 if (test__mulvti3(a: 1, b: 0, expected: 0))
47 return 1;
48 if (test__mulvti3(a: 0, b: 10, expected: 0))
49 return 1;
50 if (test__mulvti3(a: 10, b: 0, expected: 0))
51 return 1;
52 if (test__mulvti3(a: 0, b: 81985529216486895LL, expected: 0))
53 return 1;
54 if (test__mulvti3(a: 81985529216486895LL, b: 0, expected: 0))
55 return 1;
56
57 if (test__mulvti3(a: 0, b: -1, expected: 0))
58 return 1;
59 if (test__mulvti3(a: -1, b: 0, expected: 0))
60 return 1;
61 if (test__mulvti3(a: 0, b: -10, expected: 0))
62 return 1;
63 if (test__mulvti3(a: -10, b: 0, expected: 0))
64 return 1;
65 if (test__mulvti3(a: 0, b: -81985529216486895LL, expected: 0))
66 return 1;
67 if (test__mulvti3(a: -81985529216486895LL, b: 0, expected: 0))
68 return 1;
69
70 if (test__mulvti3(a: 1, b: 1, expected: 1))
71 return 1;
72 if (test__mulvti3(a: 1, b: 10, expected: 10))
73 return 1;
74 if (test__mulvti3(a: 10, b: 1, expected: 10))
75 return 1;
76 if (test__mulvti3(a: 1, b: 81985529216486895LL, expected: 81985529216486895LL))
77 return 1;
78 if (test__mulvti3(a: 81985529216486895LL, b: 1, expected: 81985529216486895LL))
79 return 1;
80
81 if (test__mulvti3(a: 1, b: -1, expected: -1))
82 return 1;
83 if (test__mulvti3(a: 1, b: -10, expected: -10))
84 return 1;
85 if (test__mulvti3(a: -10, b: 1, expected: -10))
86 return 1;
87 if (test__mulvti3(a: 1, b: -81985529216486895LL, expected: -81985529216486895LL))
88 return 1;
89 if (test__mulvti3(a: -81985529216486895LL, b: 1, expected: -81985529216486895LL))
90 return 1;
91
92 if (test__mulvti3(a: 3037000499LL, b: 3037000499LL, expected: 9223372030926249001ULL))
93 return 1;
94 if (test__mulvti3(a: -3037000499LL, b: 3037000499LL, expected: -9223372030926249001LL))
95 return 1;
96 if (test__mulvti3(a: 3037000499LL, b: -3037000499LL, expected: -9223372030926249001LL))
97 return 1;
98 if (test__mulvti3(a: -3037000499LL, b: -3037000499LL, expected: 9223372030926249001ULL))
99 return 1;
100
101 if (test__mulvti3(a: 4398046511103LL, b: 2097152LL, expected: 9223372036852678656LL))
102 return 1;
103 if (test__mulvti3(a: -4398046511103LL, b: 2097152LL, expected: -9223372036852678656LL))
104 return 1;
105 if (test__mulvti3(a: 4398046511103LL, b: -2097152LL, expected: -9223372036852678656LL))
106 return 1;
107 if (test__mulvti3(a: -4398046511103LL, b: -2097152LL, expected: 9223372036852678656LL))
108 return 1;
109
110 if (test__mulvti3(a: 2097152LL, b: 4398046511103LL, expected: 9223372036852678656ULL))
111 return 1;
112 if (test__mulvti3(a: -2097152LL, b: 4398046511103LL, expected: -9223372036852678656LL))
113 return 1;
114 if (test__mulvti3(a: 2097152LL, b: -4398046511103LL, expected: -9223372036852678656LL))
115 return 1;
116 if (test__mulvti3(a: -2097152LL, b: -4398046511103LL, expected: 9223372036852678656LL))
117 return 1;
118
119 if (test__mulvti3(a: make_ti(h: 0x00000000000000B5LL, l: 0x04F333F9DE5BE000LL),
120 b: make_ti(h: 0x0000000000000000LL, l: 0x00B504F333F9DE5BLL),
121 expected: make_ti(h: 0x7FFFFFFFFFFFF328LL, l: 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(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
133 b: -1,
134 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL)))
135 return 1;
136 if (test__mulvti3(a: -1,
137 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
138 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL)))
139 return 1;
140 if (test__mulvti3(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
141 b: 0,
142 expected: 0))
143 return 1;
144 if (test__mulvti3(a: 0,
145 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
146 expected: 0))
147 return 1;
148 if (test__mulvti3(a: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
149 b: 1,
150 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL)))
151 return 1;
152 if (test__mulvti3(a: 1,
153 b: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL),
154 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 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(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
182 b: 0,
183 expected: 0))
184 return 1;
185 if (test__mulvti3(a: 0,
186 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
187 expected: 0))
188 return 1;
189 if (test__mulvti3(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
190 b: 1,
191 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL)))
192 return 1;
193 if (test__mulvti3(a: 1,
194 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000000LL),
195 expected: make_ti(h: 0x8000000000000000LL, l: 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(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
215 b: -1,
216 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL)))
217 return 1;
218 if (test__mulvti3(a: -1,
219 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
220 expected: make_ti(h: 0x7FFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL)))
221 return 1;
222 if (test__mulvti3(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
223 b: 0,
224 expected: 0))
225 return 1;
226 if (test__mulvti3(a: 0,
227 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
228 expected: 0))
229 return 1;
230 if (test__mulvti3(a: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
231 b: 1,
232 expected: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL)))
233 return 1;
234 if (test__mulvti3(a: 1,
235 b: make_ti(h: 0x8000000000000000LL, l: 0x0000000000000001LL),
236 expected: make_ti(h: 0x8000000000000000LL, l: 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

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