blob: fa2eebda7126ea59e97bad3085fa0caa7d36e65b [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
30volatile unsigned Values[] = { 0x0, 0x1, 0x7ffffffe, 0x7fffffff,
31 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff,
32 0x7e, 0x7f, 0x80, 0x81,
33 0xfe, 0xff, 0x100, 0x101,
34 0x7ffe, 0x7fff, 0x8000, 0x8001,
35 0xfffe, 0xffff, 0x10000, 0x10001, };
36const 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 ,
Matt Wala9a0168a2014-07-23 14:56:10 -070053 ICMP_U_TABLE
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070054#undef X
55#define X(cmp, op) \
56 { \
Matt Wala89cbfb02014-08-11 17:44:40 -070057 STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070058 (FuncTypeUnsigned)(FuncTypeSigned)Subzero_::icmp##cmp \
59 } \
60 ,
Matt Wala9a0168a2014-07-23 14:56:10 -070061 ICMP_S_TABLE
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070062#undef X
Matt Wala9a0168a2014-07-23 14:56:10 -070063 };
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 ,
140 ICMP_U_TABLE
141#undef X
142#define X(cmp, op) \
143 { \
Matt Wala89cbfb02014-08-11 17:44:40 -0700144 STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \
Matt Wala9a0168a2014-07-23 14:56:10 -0700145 (FuncTypeUnsigned)(FuncTypeSigned)Subzero_::icmp##cmp \
146 } \
147 ,
148 ICMP_S_TABLE
149#undef X
150 };
151 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 ,
209 ICMP_U_TABLE
210 ICMP_S_TABLE
211 };
212 const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs);
213 const static size_t NumElements = Vectors<T>::NumElements;
214 const static size_t MAX_NUMBER_OF_ELEMENTS_FOR_EXHAUSTIVE_TESTING = 8;
215
216 // Check if the type is small enough to try all possible input pairs.
217 if (NumElements <= MAX_NUMBER_OF_ELEMENTS_FOR_EXHAUSTIVE_TESTING) {
218 for (size_t f = 0; f < NumFuncs; ++f) {
219 Ty Value1, Value2;
220 memset(&Value1, 0, sizeof(Value1));
221 for (bool IsValue1Done = false; !IsValue1Done;
222 IsValue1Done = incrementI1Vector<T>(Value1)) {
223 memset(&Value2, 0, sizeof(Value2));
224 for (bool IsValue2Done = false; !IsValue2Done;
225 IsValue2Done = incrementI1Vector<T>(Value2)) {
226 Ty ResultSz = Funcs[f].FuncSz(Value1, Value2);
227 Ty ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
228 ++TotalTests;
229 if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) {
230 ++Passes;
231 } else {
232 ++Failures;
233 std::cout << "test" << Funcs[f].Name << Vectors<T>::TypeName << "("
234 << vectAsString<T>(Value1) << ","
235 << vectAsString<T>(Value2)
236 << "): sz=" << vectAsString<T>(ResultSz)
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700237 << " llc=" << vectAsString<T>(ResultLlc) << "\n";
Matt Wala9a0168a2014-07-23 14:56:10 -0700238 }
239 }
240 }
241 }
242 } else {
243 for (size_t f = 0; f < NumFuncs; ++f) {
244 PRNG Index;
245 for (size_t i = 0; i < MaxTestsPerFunc; ++i) {
246 Ty Value1, Value2;
247 // Initialize the test vectors.
248 for (size_t j = 0; j < NumElements; ++j) {
249 Value1[j] = Index() % 2;
250 Value2[j] = Index() % 2;
251 }
252 // Perform the test.
253 Ty ResultSz = Funcs[f].FuncSz(Value1, Value2);
254 Ty ResultLlc = Funcs[f].FuncLlc(Value1, Value2);
255 ++TotalTests;
256 if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) {
257 ++Passes;
258 } else {
259 ++Failures;
260 std::cout << "test" << Funcs[f].Name << Vectors<T>::TypeName << "("
261 << vectAsString<T>(Value1) << "," << vectAsString<T>(Value2)
262 << "): sz=" << vectAsString<T>(ResultSz)
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700263 << " llc=" << vectAsString<T>(ResultLlc) << "\n";
Matt Wala9a0168a2014-07-23 14:56:10 -0700264 }
265 }
266 }
267 }
268}
269
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700270int main(int argc, char **argv) {
271 size_t TotalTests = 0;
272 size_t Passes = 0;
273 size_t Failures = 0;
274
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700275 testsInt<uint8_t, myint8_t>(TotalTests, Passes, Failures);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700276 testsInt<uint16_t, int16_t>(TotalTests, Passes, Failures);
277 testsInt<uint32_t, int32_t>(TotalTests, Passes, Failures);
278 testsInt<uint64_t, int64_t>(TotalTests, Passes, Failures);
Matt Wala9a0168a2014-07-23 14:56:10 -0700279 testsVecInt<v4ui32, v4si32>(TotalTests, Passes, Failures);
280 testsVecInt<v8ui16, v8si16>(TotalTests, Passes, Failures);
281 testsVecInt<v16ui8, v16si8>(TotalTests, Passes, Failures);
282 testsVecI1<v4i1>(TotalTests, Passes, Failures);
283 testsVecI1<v8i1>(TotalTests, Passes, Failures);
284 testsVecI1<v16i1>(TotalTests, Passes, Failures);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700285
286 std::cout << "TotalTests=" << TotalTests << " Passes=" << Passes
287 << " Failures=" << Failures << "\n";
288 return Failures;
289}