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