1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_mulodi4
3
4#include "int_lib.h"
5#include <stdio.h>
6
7extern COMPILER_RT_ABI di_int __mulodi4(di_int a, di_int b, int* overflow);
8
9int test__mulodi4(di_int a, di_int b, di_int expected, int expected_overflow)
10{
11 int ov;
12 di_int x = __mulodi4(a, b, overflow: &ov);
13 if (ov != expected_overflow)
14 printf(format: "error in __mulodi4: overflow=%d expected=%d\n",
15 ov, expected_overflow);
16 else if (!expected_overflow && x != expected) {
17 printf(format: "error in __mulodi4: 0x%llX * 0x%llX = 0x%llX (overflow=%d), "
18 "expected 0x%llX (overflow=%d)\n",
19 a, b, x, ov, expected, expected_overflow);
20 return 1;
21 }
22 return 0;
23}
24
25int main()
26{
27 if (test__mulodi4(a: 0, b: 0, expected: 0, expected_overflow: 0))
28 return 1;
29 if (test__mulodi4(a: 0, b: 1, expected: 0, expected_overflow: 0))
30 return 1;
31 if (test__mulodi4(a: 1, b: 0, expected: 0, expected_overflow: 0))
32 return 1;
33 if (test__mulodi4(a: 0, b: 10, expected: 0, expected_overflow: 0))
34 return 1;
35 if (test__mulodi4(a: 10, b: 0, expected: 0, expected_overflow: 0))
36 return 1;
37 if (test__mulodi4(a: 0, b: 81985529216486895LL, expected: 0, expected_overflow: 0))
38 return 1;
39 if (test__mulodi4(a: 81985529216486895LL, b: 0, expected: 0, expected_overflow: 0))
40 return 1;
41
42 if (test__mulodi4(a: 0, b: -1, expected: 0, expected_overflow: 0))
43 return 1;
44 if (test__mulodi4(a: -1, b: 0, expected: 0, expected_overflow: 0))
45 return 1;
46 if (test__mulodi4(a: 0, b: -10, expected: 0, expected_overflow: 0))
47 return 1;
48 if (test__mulodi4(a: -10, b: 0, expected: 0, expected_overflow: 0))
49 return 1;
50 if (test__mulodi4(a: 0, b: -81985529216486895LL, expected: 0, expected_overflow: 0))
51 return 1;
52 if (test__mulodi4(a: -81985529216486895LL, b: 0, expected: 0, expected_overflow: 0))
53 return 1;
54
55 if (test__mulodi4(a: 1, b: 1, expected: 1, expected_overflow: 0))
56 return 1;
57 if (test__mulodi4(a: 1, b: 10, expected: 10, expected_overflow: 0))
58 return 1;
59 if (test__mulodi4(a: 10, b: 1, expected: 10, expected_overflow: 0))
60 return 1;
61 if (test__mulodi4(a: 1, b: 81985529216486895LL, expected: 81985529216486895LL, expected_overflow: 0))
62 return 1;
63 if (test__mulodi4(a: 81985529216486895LL, b: 1, expected: 81985529216486895LL, expected_overflow: 0))
64 return 1;
65
66 if (test__mulodi4(a: 1, b: -1, expected: -1, expected_overflow: 0))
67 return 1;
68 if (test__mulodi4(a: 1, b: -10, expected: -10, expected_overflow: 0))
69 return 1;
70 if (test__mulodi4(a: -10, b: 1, expected: -10, expected_overflow: 0))
71 return 1;
72 if (test__mulodi4(a: 1, b: -81985529216486895LL, expected: -81985529216486895LL, expected_overflow: 0))
73 return 1;
74 if (test__mulodi4(a: -81985529216486895LL, b: 1, expected: -81985529216486895LL, expected_overflow: 0))
75 return 1;
76
77 if (test__mulodi4(a: 3037000499LL, b: 3037000499LL, expected: 9223372030926249001LL, expected_overflow: 0))
78 return 1;
79 if (test__mulodi4(a: -3037000499LL, b: 3037000499LL, expected: -9223372030926249001LL, expected_overflow: 0))
80 return 1;
81 if (test__mulodi4(a: 3037000499LL, b: -3037000499LL, expected: -9223372030926249001LL, expected_overflow: 0))
82 return 1;
83 if (test__mulodi4(a: -3037000499LL, b: -3037000499LL, expected: 9223372030926249001LL, expected_overflow: 0))
84 return 1;
85
86 if (test__mulodi4(a: 4398046511103LL, b: 2097152LL, expected: 9223372036852678656LL, expected_overflow: 0))
87 return 1;
88 if (test__mulodi4(a: -4398046511103LL, b: 2097152LL, expected: -9223372036852678656LL, expected_overflow: 0))
89 return 1;
90 if (test__mulodi4(a: 4398046511103LL, b: -2097152LL, expected: -9223372036852678656LL, expected_overflow: 0))
91 return 1;
92 if (test__mulodi4(a: -4398046511103LL, b: -2097152LL, expected: 9223372036852678656LL, expected_overflow: 0))
93 return 1;
94
95 if (test__mulodi4(a: 2097152LL, b: 4398046511103LL, expected: 9223372036852678656LL, expected_overflow: 0))
96 return 1;
97 if (test__mulodi4(a: -2097152LL, b: 4398046511103LL, expected: -9223372036852678656LL, expected_overflow: 0))
98 return 1;
99 if (test__mulodi4(a: 2097152LL, b: -4398046511103LL, expected: -9223372036852678656LL, expected_overflow: 0))
100 return 1;
101 if (test__mulodi4(a: -2097152LL, b: -4398046511103LL, expected: 9223372036852678656LL, expected_overflow: 0))
102 return 1;
103
104 if (test__mulodi4(a: 0x7FFFFFFFFFFFFFFFLL, b: -2, expected: 2, expected_overflow: 1))
105 return 1;
106 if (test__mulodi4(a: -2, b: 0x7FFFFFFFFFFFFFFFLL, expected: 2, expected_overflow: 1))
107 return 1;
108 if (test__mulodi4(a: 0x7FFFFFFFFFFFFFFFLL, b: -1, expected: 0x8000000000000001LL, expected_overflow: 0))
109 return 1;
110 if (test__mulodi4(a: -1, b: 0x7FFFFFFFFFFFFFFFLL, expected: 0x8000000000000001LL, expected_overflow: 0))
111 return 1;
112 if (test__mulodi4(a: 0x7FFFFFFFFFFFFFFFLL, b: 0, expected: 0, expected_overflow: 0))
113 return 1;
114 if (test__mulodi4(a: 0, b: 0x7FFFFFFFFFFFFFFFLL, expected: 0, expected_overflow: 0))
115 return 1;
116 if (test__mulodi4(a: 0x7FFFFFFFFFFFFFFFLL, b: 1, expected: 0x7FFFFFFFFFFFFFFFLL, expected_overflow: 0))
117 return 1;
118 if (test__mulodi4(a: 1, b: 0x7FFFFFFFFFFFFFFFLL, expected: 0x7FFFFFFFFFFFFFFFLL, expected_overflow: 0))
119 return 1;
120 if (test__mulodi4(a: 0x7FFFFFFFFFFFFFFFLL, b: 2, expected: 0x8000000000000001LL, expected_overflow: 1))
121 return 1;
122 if (test__mulodi4(a: 2, b: 0x7FFFFFFFFFFFFFFFLL, expected: 0x8000000000000001LL, expected_overflow: 1))
123 return 1;
124
125 if (test__mulodi4(a: 0x8000000000000000LL, b: -2, expected: 0x8000000000000000LL, expected_overflow: 1))
126 return 1;
127 if (test__mulodi4(a: -2, b: 0x8000000000000000LL, expected: 0x8000000000000000LL, expected_overflow: 1))
128 return 1;
129 if (test__mulodi4(a: 0x8000000000000000LL, b: -1, expected: 0x8000000000000000LL, expected_overflow: 1))
130 return 1;
131 if (test__mulodi4(a: -1, b: 0x8000000000000000LL, expected: 0x8000000000000000LL, expected_overflow: 1))
132 return 1;
133 if (test__mulodi4(a: 0x8000000000000000LL, b: 0, expected: 0, expected_overflow: 0))
134 return 1;
135 if (test__mulodi4(a: 0, b: 0x8000000000000000LL, expected: 0, expected_overflow: 0))
136 return 1;
137 if (test__mulodi4(a: 0x8000000000000000LL, b: 1, expected: 0x8000000000000000LL, expected_overflow: 0))
138 return 1;
139 if (test__mulodi4(a: 1, b: 0x8000000000000000LL, expected: 0x8000000000000000LL, expected_overflow: 0))
140 return 1;
141 if (test__mulodi4(a: 0x8000000000000000LL, b: 2, expected: 0x8000000000000000LL, expected_overflow: 1))
142 return 1;
143 if (test__mulodi4(a: 2, b: 0x8000000000000000LL, expected: 0x8000000000000000LL, expected_overflow: 1))
144 return 1;
145
146 if (test__mulodi4(a: 0x8000000000000001LL, b: -2, expected: 0x8000000000000001LL, expected_overflow: 1))
147 return 1;
148 if (test__mulodi4(a: -2, b: 0x8000000000000001LL, expected: 0x8000000000000001LL, expected_overflow: 1))
149 return 1;
150 if (test__mulodi4(a: 0x8000000000000001LL, b: -1, expected: 0x7FFFFFFFFFFFFFFFLL, expected_overflow: 0))
151 return 1;
152 if (test__mulodi4(a: -1, b: 0x8000000000000001LL, expected: 0x7FFFFFFFFFFFFFFFLL, expected_overflow: 0))
153 return 1;
154 if (test__mulodi4(a: 0x8000000000000001LL, b: 0, expected: 0, expected_overflow: 0))
155 return 1;
156 if (test__mulodi4(a: 0, b: 0x8000000000000001LL, expected: 0, expected_overflow: 0))
157 return 1;
158 if (test__mulodi4(a: 0x8000000000000001LL, b: 1, expected: 0x8000000000000001LL, expected_overflow: 0))
159 return 1;
160 if (test__mulodi4(a: 1, b: 0x8000000000000001LL, expected: 0x8000000000000001LL, expected_overflow: 0))
161 return 1;
162 if (test__mulodi4(a: 0x8000000000000001LL, b: 2, expected: 0x8000000000000000LL, expected_overflow: 1))
163 return 1;
164 if (test__mulodi4(a: 2, b: 0x8000000000000001LL, expected: 0x8000000000000000LL, expected_overflow: 1))
165 return 1;
166
167 return 0;
168}
169

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