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