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