blob: f27d53bba169e8814d76885adbc8f2be9548e853 [file] [log] [blame]
Matt Wala9a0168a2014-07-23 14:56:10 -07001//===- subzero/crosstest/test_icmp_main.cpp - Driver for tests. -----------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Driver for cross testing the icmp bitcode instruction
11//
12//===----------------------------------------------------------------------===//
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070013
Matt Wala9a0168a2014-07-23 14:56:10 -070014/* crosstest.py --test=test_icmp.cpp --test=test_icmp_i1vec.ll \
15 --driver=test_icmp_main.cpp --prefix=Subzero_ --output=test_icmp */
16
17#include <climits> // CHAR_BIT
18#include <cstring> // memcmp, memset
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070019#include <stdint.h>
20#include <iostream>
21
22// Include test_icmp.h twice - once normally, and once within the
23// Subzero_ namespace, corresponding to the llc and Subzero translated
24// object files, respectively.
25#include "test_icmp.h"
26namespace Subzero_ {
27#include "test_icmp.h"
28}
29
Jim Stichnothdd842db2015-01-27 12:53:53 -080030volatile unsigned Values[] = {
31 0x0, 0x1, 0x7ffffffe, 0x7fffffff, 0x80000000, 0x80000001,
32 0xfffffffe, 0xffffffff, 0x7e, 0x7f, 0x80, 0x81,
33 0xfe, 0xff, 0x100, 0x101, 0x7ffe, 0x7fff,
34 0x8000, 0x8001, 0xfffe, 0xffff, 0x10000, 0x10001,
35};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070036const static size_t NumValues = sizeof(Values) / sizeof(*Values);
37
38template <typename TypeUnsigned, typename TypeSigned>
39void testsInt(size_t &TotalTests, size_t &Passes, size_t &Failures) {
40 typedef bool (*FuncTypeUnsigned)(TypeUnsigned, TypeUnsigned);
41 typedef bool (*FuncTypeSigned)(TypeSigned, TypeSigned);
42 static struct {
43 const char *Name;
44 FuncTypeUnsigned FuncLlc;
45 FuncTypeUnsigned FuncSz;
46 } Funcs[] = {
47#define X(cmp, op) \
48 { \
Matt Wala89cbfb02014-08-11 17:44:40 -070049 STR(cmp), (FuncTypeUnsigned)icmp##cmp, \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070050 (FuncTypeUnsigned)Subzero_::icmp##cmp \
51 } \
52 ,
Jim Stichnothd9dc82e2015-03-03 17:06:33 -080053 ICMP_U_TABLE
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070054#undef X
55#define X(cmp, op) \
56 { \
Jim Stichnothd9dc82e2015-03-03 17:06:33 -080057 STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \
58 (FuncTypeUnsigned)(FuncTypeSigned)Subzero_::icmp##cmp \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059 } \
60 ,
Jim Stichnothd9dc82e2015-03-03 17:06:33 -080061 ICMP_S_TABLE
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070062#undef X
Jim Stichnothd9dc82e2015-03-03 17:06:33 -080063 };
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070064 const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs);
65
66 if (sizeof(TypeUnsigned) <= sizeof(uint32_t)) {
67 // This is the "normal" version of the loop nest, for 32-bit or
68 // narrower types.
69 for (size_t f = 0; f < NumFuncs; ++f) {
70 for (size_t i = 0; i < NumValues; ++i) {
71 for (size_t j = 0; j < NumValues; ++j) {
72 TypeUnsigned Value1 = Values[i];
73 TypeUnsigned Value2 = Values[j];
74 ++TotalTests;
75 bool ResultSz = Funcs[f].FuncSz(Value1, Value2);
76 bool ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
77 if (ResultSz == ResultLlc) {
78 ++Passes;
79 } else {
80 ++Failures;
Matt Wala9a0168a2014-07-23 14:56:10 -070081 std::cout << "icmp" << Funcs[f].Name
82 << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1
83 << ", " << Value2 << "): sz=" << ResultSz
Jim Stichnoth7da431b2014-08-05 11:22:37 -070084 << " llc=" << ResultLlc << "\n";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070085 }
86 }
87 }
88 }
89 } else {
90 // This is the 64-bit version. Test values are synthesized from
91 // the 32-bit values in Values[].
92 for (size_t f = 0; f < NumFuncs; ++f) {
93 for (size_t iLo = 0; iLo < NumValues; ++iLo) {
94 for (size_t iHi = 0; iHi < NumValues; ++iHi) {
95 for (size_t jLo = 0; jLo < NumValues; ++jLo) {
96 for (size_t jHi = 0; jHi < NumValues; ++jHi) {
97 TypeUnsigned Value1 =
98 (((TypeUnsigned)Values[iHi]) << 32) + Values[iLo];
99 TypeUnsigned Value2 =
100 (((TypeUnsigned)Values[jHi]) << 32) + Values[jLo];
101 ++TotalTests;
102 bool ResultSz = Funcs[f].FuncSz(Value1, Value2);
103 bool ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
104 if (ResultSz == ResultLlc) {
105 ++Passes;
106 } else {
107 ++Failures;
108 std::cout << "icmp" << Funcs[f].Name
Matt Wala9a0168a2014-07-23 14:56:10 -0700109 << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1
110 << ", " << Value2 << "): sz=" << ResultSz
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700111 << " llc=" << ResultLlc << "\n";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700112 }
113 }
114 }
115 }
116 }
117 }
118 }
119}
120
Matt Wala9a0168a2014-07-23 14:56:10 -0700121const static size_t MaxTestsPerFunc = 100000;
122
123template <typename TypeUnsignedLabel, typename TypeSignedLabel>
124void testsVecInt(size_t &TotalTests, size_t &Passes, size_t &Failures) {
125 typedef typename Vectors<TypeUnsignedLabel>::Ty TypeUnsigned;
126 typedef typename Vectors<TypeSignedLabel>::Ty TypeSigned;
127 typedef TypeUnsigned (*FuncTypeUnsigned)(TypeUnsigned, TypeUnsigned);
128 typedef TypeSigned (*FuncTypeSigned)(TypeSigned, TypeSigned);
129 static struct {
130 const char *Name;
131 FuncTypeUnsigned FuncLlc;
132 FuncTypeUnsigned FuncSz;
133 } Funcs[] = {
134#define X(cmp, op) \
135 { \
Matt Wala89cbfb02014-08-11 17:44:40 -0700136 STR(cmp), (FuncTypeUnsigned)icmp##cmp, \
Matt Wala9a0168a2014-07-23 14:56:10 -0700137 (FuncTypeUnsigned)Subzero_::icmp##cmp \
138 } \
139 ,
Jim Stichnothd9dc82e2015-03-03 17:06:33 -0800140 ICMP_U_TABLE
Matt Wala9a0168a2014-07-23 14:56:10 -0700141#undef X
142#define X(cmp, op) \
143 { \
Jim Stichnothd9dc82e2015-03-03 17:06:33 -0800144 STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \
145 (FuncTypeUnsigned)(FuncTypeSigned)Subzero_::icmp##cmp \
Matt Wala9a0168a2014-07-23 14:56:10 -0700146 } \
147 ,
Jim Stichnothd9dc82e2015-03-03 17:06:33 -0800148 ICMP_S_TABLE
Matt Wala9a0168a2014-07-23 14:56:10 -0700149#undef X
Jim Stichnothd9dc82e2015-03-03 17:06:33 -0800150 };
Matt Wala9a0168a2014-07-23 14:56:10 -0700151 const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs);
152 const static size_t NumElementsInType = Vectors<TypeUnsigned>::NumElements;
153 for (size_t f = 0; f < NumFuncs; ++f) {
154 PRNG Index;
155 for (size_t i = 0; i < MaxTestsPerFunc; ++i) {
156 // Initialize the test vectors.
157 TypeUnsigned Value1, Value2;
158 for (size_t j = 0; j < NumElementsInType;) {
159 Value1[j] = Values[Index() % NumValues];
160 Value2[j] = Values[Index() % NumValues];
161 ++j;
162 }
163 // Perform the test.
164 TypeUnsigned ResultSz = Funcs[f].FuncSz(Value1, Value2);
165 TypeUnsigned ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
166 ++TotalTests;
167 if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) {
168 ++Passes;
169 } else {
170 ++Failures;
171 std::cout << "test" << Funcs[f].Name
172 << Vectors<TypeUnsignedLabel>::TypeName << "("
173 << vectAsString<TypeUnsignedLabel>(Value1) << ","
174 << vectAsString<TypeUnsignedLabel>(Value2)
175 << "): sz=" << vectAsString<TypeUnsignedLabel>(ResultSz)
176 << " llc=" << vectAsString<TypeUnsignedLabel>(ResultLlc)
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700177 << "\n";
Matt Wala9a0168a2014-07-23 14:56:10 -0700178 }
179 }
180 }
181}
182
183// Return true on wraparound
184template <typename T> bool incrementI1Vector(typename Vectors<T>::Ty &Vect) {
185 size_t Pos = 0;
186 const static size_t NumElements = Vectors<T>::NumElements;
187 for (Pos = 0; Pos < NumElements; ++Pos) {
188 if (Vect[Pos] == 0) {
189 Vect[Pos] = 1;
190 break;
191 }
192 Vect[Pos] = 0;
193 }
194 return (Pos == NumElements);
195}
196
197template <typename T>
198void testsVecI1(size_t &TotalTests, size_t &Passes, size_t &Failures) {
199 typedef typename Vectors<T>::Ty Ty;
200 typedef Ty (*FuncType)(Ty, Ty);
201 static struct {
202 const char *Name;
203 FuncType FuncLlc;
204 FuncType FuncSz;
205 } Funcs[] = {
206#define X(cmp, op) \
Matt Wala89cbfb02014-08-11 17:44:40 -0700207 { STR(cmp), (FuncType)icmpi1##cmp, (FuncType)Subzero_::icmpi1##cmp } \
Matt Wala9a0168a2014-07-23 14:56:10 -0700208 ,
Jim Stichnothd9dc82e2015-03-03 17:06:33 -0800209 ICMP_U_TABLE ICMP_S_TABLE};
Matt Wala9a0168a2014-07-23 14:56:10 -0700210 const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs);
211 const static size_t NumElements = Vectors<T>::NumElements;
212 const static size_t MAX_NUMBER_OF_ELEMENTS_FOR_EXHAUSTIVE_TESTING = 8;
213
214 // Check if the type is small enough to try all possible input pairs.
215 if (NumElements <= MAX_NUMBER_OF_ELEMENTS_FOR_EXHAUSTIVE_TESTING) {
216 for (size_t f = 0; f < NumFuncs; ++f) {
217 Ty Value1, Value2;
218 memset(&Value1, 0, sizeof(Value1));
219 for (bool IsValue1Done = false; !IsValue1Done;
220 IsValue1Done = incrementI1Vector<T>(Value1)) {
221 memset(&Value2, 0, sizeof(Value2));
222 for (bool IsValue2Done = false; !IsValue2Done;
223 IsValue2Done = incrementI1Vector<T>(Value2)) {
224 Ty ResultSz = Funcs[f].FuncSz(Value1, Value2);
225 Ty ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
226 ++TotalTests;
227 if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) {
228 ++Passes;
229 } else {
230 ++Failures;
231 std::cout << "test" << Funcs[f].Name << Vectors<T>::TypeName << "("
232 << vectAsString<T>(Value1) << ","
233 << vectAsString<T>(Value2)
234 << "): sz=" << vectAsString<T>(ResultSz)
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700235 << " llc=" << vectAsString<T>(ResultLlc) << "\n";
Matt Wala9a0168a2014-07-23 14:56:10 -0700236 }
237 }
238 }
239 }
240 } else {
241 for (size_t f = 0; f < NumFuncs; ++f) {
242 PRNG Index;
243 for (size_t i = 0; i < MaxTestsPerFunc; ++i) {
244 Ty Value1, Value2;
245 // Initialize the test vectors.
246 for (size_t j = 0; j < NumElements; ++j) {
247 Value1[j] = Index() % 2;
248 Value2[j] = Index() % 2;
249 }
250 // Perform the test.
251 Ty ResultSz = Funcs[f].FuncSz(Value1, Value2);
252 Ty ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
253 ++TotalTests;
254 if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) {
255 ++Passes;
256 } else {
257 ++Failures;
258 std::cout << "test" << Funcs[f].Name << Vectors<T>::TypeName << "("
259 << vectAsString<T>(Value1) << "," << vectAsString<T>(Value2)
260 << "): sz=" << vectAsString<T>(ResultSz)
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700261 << " llc=" << vectAsString<T>(ResultLlc) << "\n";
Matt Wala9a0168a2014-07-23 14:56:10 -0700262 }
263 }
264 }
265 }
266}
267
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700268int main(int argc, char **argv) {
269 size_t TotalTests = 0;
270 size_t Passes = 0;
271 size_t Failures = 0;
272
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700273 testsInt<uint8_t, myint8_t>(TotalTests, Passes, Failures);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700274 testsInt<uint16_t, int16_t>(TotalTests, Passes, Failures);
275 testsInt<uint32_t, int32_t>(TotalTests, Passes, Failures);
276 testsInt<uint64_t, int64_t>(TotalTests, Passes, Failures);
Matt Wala9a0168a2014-07-23 14:56:10 -0700277 testsVecInt<v4ui32, v4si32>(TotalTests, Passes, Failures);
278 testsVecInt<v8ui16, v8si16>(TotalTests, Passes, Failures);
279 testsVecInt<v16ui8, v16si8>(TotalTests, Passes, Failures);
280 testsVecI1<v4i1>(TotalTests, Passes, Failures);
281 testsVecI1<v8i1>(TotalTests, Passes, Failures);
282 testsVecI1<v16i1>(TotalTests, Passes, Failures);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700283
284 std::cout << "TotalTests=" << TotalTests << " Passes=" << Passes
285 << " Failures=" << Failures << "\n";
286 return Failures;
287}