Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 1 | //===- 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 Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 13 | |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 14 | /* 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 Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 19 | #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" |
| 26 | namespace Subzero_ { |
| 27 | #include "test_icmp.h" |
| 28 | } |
| 29 | |
| 30 | volatile 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, }; |
| 36 | const static size_t NumValues = sizeof(Values) / sizeof(*Values); |
| 37 | |
| 38 | template <typename TypeUnsigned, typename TypeSigned> |
| 39 | void 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 Wala | 89cbfb0 | 2014-08-11 17:44:40 -0700 | [diff] [blame] | 49 | STR(cmp), (FuncTypeUnsigned)icmp##cmp, \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 50 | (FuncTypeUnsigned)Subzero_::icmp##cmp \ |
| 51 | } \ |
| 52 | , |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 53 | ICMP_U_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 54 | #undef X |
| 55 | #define X(cmp, op) \ |
| 56 | { \ |
Matt Wala | 89cbfb0 | 2014-08-11 17:44:40 -0700 | [diff] [blame] | 57 | STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 58 | (FuncTypeUnsigned)(FuncTypeSigned)Subzero_::icmp##cmp \ |
| 59 | } \ |
| 60 | , |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 61 | ICMP_S_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 62 | #undef X |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 63 | }; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 64 | 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 Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 81 | std::cout << "icmp" << Funcs[f].Name |
| 82 | << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1 |
| 83 | << ", " << Value2 << "): sz=" << ResultSz |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 84 | << " llc=" << ResultLlc << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 85 | } |
| 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 Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 109 | << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1 |
| 110 | << ", " << Value2 << "): sz=" << ResultSz |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 111 | << " llc=" << ResultLlc << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 121 | const static size_t MaxTestsPerFunc = 100000; |
| 122 | |
| 123 | template <typename TypeUnsignedLabel, typename TypeSignedLabel> |
| 124 | void 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 Wala | 89cbfb0 | 2014-08-11 17:44:40 -0700 | [diff] [blame] | 136 | STR(cmp), (FuncTypeUnsigned)icmp##cmp, \ |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 137 | (FuncTypeUnsigned)Subzero_::icmp##cmp \ |
| 138 | } \ |
| 139 | , |
| 140 | ICMP_U_TABLE |
| 141 | #undef X |
| 142 | #define X(cmp, op) \ |
| 143 | { \ |
Matt Wala | 89cbfb0 | 2014-08-11 17:44:40 -0700 | [diff] [blame] | 144 | STR(cmp), (FuncTypeUnsigned)(FuncTypeSigned)icmp##cmp, \ |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 145 | (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 Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 177 | << "\n"; |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Return true on wraparound |
| 184 | template <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 | |
| 197 | template <typename T> |
| 198 | void 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 Wala | 89cbfb0 | 2014-08-11 17:44:40 -0700 | [diff] [blame] | 207 | { STR(cmp), (FuncType)icmpi1##cmp, (FuncType)Subzero_::icmpi1##cmp } \ |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 208 | , |
| 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 Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 237 | << " llc=" << vectAsString<T>(ResultLlc) << "\n"; |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 238 | } |
| 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 Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 263 | << " llc=" << vectAsString<T>(ResultLlc) << "\n"; |
Matt Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 270 | int main(int argc, char **argv) { |
| 271 | size_t TotalTests = 0; |
| 272 | size_t Passes = 0; |
| 273 | size_t Failures = 0; |
| 274 | |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 275 | testsInt<uint8_t, myint8_t>(TotalTests, Passes, Failures); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 276 | 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 Wala | 9a0168a | 2014-07-23 14:56:10 -0700 | [diff] [blame] | 279 | 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 Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 285 | |
| 286 | std::cout << "TotalTests=" << TotalTests << " Passes=" << Passes |
| 287 | << " Failures=" << Failures << "\n"; |
| 288 | return Failures; |
| 289 | } |