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

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