1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_mulosi4
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 si_int __mulosi4(si_int a, si_int b, int *overflow);
12
13int test__mulosi4(si_int a, si_int b, si_int expected, int expected_overflow)
14{
15 int ov;
16 si_int x = __mulosi4(a, b, overflow: &ov);
17 if (ov != expected_overflow)
18 printf(format: "error in __mulosi4: overflow=%d expected=%d\n",
19 ov, expected_overflow);
20 else if (!expected_overflow && x != expected) {
21 printf(format: "error in __mulosi4: 0x%X * 0x%X = 0x%X (overflow=%d), "
22 "expected 0x%X (overflow=%d)\n",
23 a, b, x, ov, expected, expected_overflow);
24 return 1;
25 }
26 return 0;
27}
28
29
30int main()
31{
32 if (test__mulosi4(a: 0, b: 0, expected: 0, expected_overflow: 0))
33 return 1;
34 if (test__mulosi4(a: 0, b: 1, expected: 0, expected_overflow: 0))
35 return 1;
36 if (test__mulosi4(a: 1, b: 0, expected: 0, expected_overflow: 0))
37 return 1;
38 if (test__mulosi4(a: 0, b: 10, expected: 0, expected_overflow: 0))
39 return 1;
40 if (test__mulosi4(a: 10, b: 0, expected: 0, expected_overflow: 0))
41 return 1;
42 if (test__mulosi4(a: 0, b: 0x1234567, expected: 0, expected_overflow: 0))
43 return 1;
44 if (test__mulosi4(a: 0x1234567, b: 0, expected: 0, expected_overflow: 0))
45 return 1;
46
47 if (test__mulosi4(a: 0, b: -1, expected: 0, expected_overflow: 0))
48 return 1;
49 if (test__mulosi4(a: -1, b: 0, expected: 0, expected_overflow: 0))
50 return 1;
51 if (test__mulosi4(a: 0, b: -10, expected: 0, expected_overflow: 0))
52 return 1;
53 if (test__mulosi4(a: -10, b: 0, expected: 0, expected_overflow: 0))
54 return 1;
55 if (test__mulosi4(a: 0, b: -0x1234567, expected: 0, expected_overflow: 0))
56 return 1;
57 if (test__mulosi4(a: -0x1234567, b: 0, expected: 0, expected_overflow: 0))
58 return 1;
59
60 if (test__mulosi4(a: 1, b: 1, expected: 1, expected_overflow: 0))
61 return 1;
62 if (test__mulosi4(a: 1, b: 10, expected: 10, expected_overflow: 0))
63 return 1;
64 if (test__mulosi4(a: 10, b: 1, expected: 10, expected_overflow: 0))
65 return 1;
66 if (test__mulosi4(a: 1, b: 0x1234567, expected: 0x1234567, expected_overflow: 0))
67 return 1;
68 if (test__mulosi4(a: 0x1234567, b: 1, expected: 0x1234567, expected_overflow: 0))
69 return 1;
70
71 if (test__mulosi4(a: 1, b: -1, expected: -1, expected_overflow: 0))
72 return 1;
73 if (test__mulosi4(a: 1, b: -10, expected: -10, expected_overflow: 0))
74 return 1;
75 if (test__mulosi4(a: -10, b: 1, expected: -10, expected_overflow: 0))
76 return 1;
77 if (test__mulosi4(a: 1, b: -0x1234567, expected: -0x1234567, expected_overflow: 0))
78 return 1;
79 if (test__mulosi4(a: -0x1234567, b: 1, expected: -0x1234567, expected_overflow: 0))
80 return 1;
81
82 if (test__mulosi4(a: 0x7FFFFFFF, b: -2, expected: 0x80000001, expected_overflow: 1))
83 return 1;
84 if (test__mulosi4(a: -2, b: 0x7FFFFFFF, expected: 0x80000001, expected_overflow: 1))
85 return 1;
86 if (test__mulosi4(a: 0x7FFFFFFF, b: -1, expected: 0x80000001, expected_overflow: 0))
87 return 1;
88 if (test__mulosi4(a: -1, b: 0x7FFFFFFF, expected: 0x80000001, expected_overflow: 0))
89 return 1;
90 if (test__mulosi4(a: 0x7FFFFFFF, b: 0, expected: 0, expected_overflow: 0))
91 return 1;
92 if (test__mulosi4(a: 0, b: 0x7FFFFFFF, expected: 0, expected_overflow: 0))
93 return 1;
94 if (test__mulosi4(a: 0x7FFFFFFF, b: 1, expected: 0x7FFFFFFF, expected_overflow: 0))
95 return 1;
96 if (test__mulosi4(a: 1, b: 0x7FFFFFFF, expected: 0x7FFFFFFF, expected_overflow: 0))
97 return 1;
98 if (test__mulosi4(a: 0x7FFFFFFF, b: 2, expected: 0x80000001, expected_overflow: 1))
99 return 1;
100 if (test__mulosi4(a: 2, b: 0x7FFFFFFF, expected: 0x80000001, expected_overflow: 1))
101 return 1;
102
103 if (test__mulosi4(a: 0x80000000, b: -2, expected: 0x80000000, expected_overflow: 1))
104 return 1;
105 if (test__mulosi4(a: -2, b: 0x80000000, expected: 0x80000000, expected_overflow: 1))
106 return 1;
107 if (test__mulosi4(a: 0x80000000, b: -1, expected: 0x80000000, expected_overflow: 1))
108 return 1;
109 if (test__mulosi4(a: -1, b: 0x80000000, expected: 0x80000000, expected_overflow: 1))
110 return 1;
111 if (test__mulosi4(a: 0x80000000, b: 0, expected: 0, expected_overflow: 0))
112 return 1;
113 if (test__mulosi4(a: 0, b: 0x80000000, expected: 0, expected_overflow: 0))
114 return 1;
115 if (test__mulosi4(a: 0x80000000, b: 1, expected: 0x80000000, expected_overflow: 0))
116 return 1;
117 if (test__mulosi4(a: 1, b: 0x80000000, expected: 0x80000000, expected_overflow: 0))
118 return 1;
119 if (test__mulosi4(a: 0x80000000, b: 2, expected: 0x80000000, expected_overflow: 1))
120 return 1;
121 if (test__mulosi4(a: 2, b: 0x80000000, expected: 0x80000000, expected_overflow: 1))
122 return 1;
123
124 if (test__mulosi4(a: 0x80000001, b: -2, expected: 0x80000001, expected_overflow: 1))
125 return 1;
126 if (test__mulosi4(a: -2, b: 0x80000001, expected: 0x80000001, expected_overflow: 1))
127 return 1;
128 if (test__mulosi4(a: 0x80000001, b: -1, expected: 0x7FFFFFFF, expected_overflow: 0))
129 return 1;
130 if (test__mulosi4(a: -1, b: 0x80000001, expected: 0x7FFFFFFF, expected_overflow: 0))
131 return 1;
132 if (test__mulosi4(a: 0x80000001, b: 0, expected: 0, expected_overflow: 0))
133 return 1;
134 if (test__mulosi4(a: 0, b: 0x80000001, expected: 0, expected_overflow: 0))
135 return 1;
136 if (test__mulosi4(a: 0x80000001, b: 1, expected: 0x80000001, expected_overflow: 0))
137 return 1;
138 if (test__mulosi4(a: 1, b: 0x80000001, expected: 0x80000001, expected_overflow: 0))
139 return 1;
140 if (test__mulosi4(a: 0x80000001, b: 2, expected: 0x80000000, expected_overflow: 1))
141 return 1;
142 if (test__mulosi4(a: 2, b: 0x80000001, expected: 0x80000000, expected_overflow: 1))
143 return 1;
144
145 return 0;
146}
147

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