Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 1 | //===- subzero/crosstest/test_arith_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 crosstesting arithmetic operations |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 14 | /* crosstest.py --test=test_arith.cpp --test=test_arith_frem.ll \ |
Jan Voung | f37fbbe | 2014-07-09 16:13:13 -0700 | [diff] [blame] | 15 | --test=test_arith_sqrt.ll --driver=test_arith_main.cpp \ |
| 16 | --prefix=Subzero_ --output=test_arith */ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 17 | |
| 18 | #include <stdint.h> |
| 19 | |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 20 | #include <climits> // CHAR_BIT |
| 21 | #include <limits> |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 22 | #include <cfloat> |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 23 | #include <cmath> // fmodf |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 24 | #include <cstring> // memcmp |
| 25 | #include <iostream> |
| 26 | |
| 27 | // Include test_arith.h twice - once normally, and once within the |
| 28 | // Subzero_ namespace, corresponding to the llc and Subzero translated |
| 29 | // object files, respectively. |
| 30 | #include "test_arith.h" |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 31 | #include "xdefs.h" |
| 32 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 33 | namespace Subzero_ { |
| 34 | #include "test_arith.h" |
| 35 | } |
| 36 | |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 37 | template <class T> bool inputsMayTriggerException(T Value1, T Value2) { |
| 38 | // Avoid HW divide-by-zero exception. |
| 39 | if (Value2 == 0) |
| 40 | return true; |
| 41 | // Avoid HW overflow exception (on x86-32). TODO: adjust |
| 42 | // for other architecture. |
| 43 | if (Value1 == std::numeric_limits<T>::min() && Value2 == -1) |
| 44 | return true; |
| 45 | return false; |
| 46 | } |
| 47 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 48 | template <typename TypeUnsigned, typename TypeSigned> |
| 49 | void testsInt(size_t &TotalTests, size_t &Passes, size_t &Failures) { |
| 50 | typedef TypeUnsigned (*FuncTypeUnsigned)(TypeUnsigned, TypeUnsigned); |
| 51 | typedef TypeSigned (*FuncTypeSigned)(TypeSigned, TypeSigned); |
Matt Wala | 35ec373 | 2014-07-18 16:32:16 -0700 | [diff] [blame] | 52 | volatile unsigned Values[] = INT_VALUE_ARRAY; |
| 53 | const static size_t NumValues = sizeof(Values) / sizeof(*Values); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 54 | static struct { |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 55 | // For functions that operate on unsigned values, the |
| 56 | // FuncLlcSigned and FuncSzSigned fields are NULL. For functions |
| 57 | // that operate on signed values, the FuncLlcUnsigned and |
| 58 | // FuncSzUnsigned fields are NULL. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 59 | const char *Name; |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 60 | FuncTypeUnsigned FuncLlcUnsigned; |
| 61 | FuncTypeUnsigned FuncSzUnsigned; |
| 62 | FuncTypeSigned FuncLlcSigned; |
| 63 | FuncTypeSigned FuncSzSigned; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 64 | bool ExcludeDivExceptions; // for divide related tests |
| 65 | } Funcs[] = { |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 66 | #define X(inst, op, isdiv, isshift) \ |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 67 | { STR(inst), test##inst, Subzero_::test##inst, NULL, NULL, isdiv } \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 68 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 69 | UINTOP_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 70 | #undef X |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 71 | #define X(inst, op, isdiv, isshift) \ |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 72 | { STR(inst), NULL, NULL, test##inst, Subzero_::test##inst, isdiv } \ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 73 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 74 | SINTOP_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 75 | #undef X |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 76 | }; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 77 | const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs); |
| 78 | |
| 79 | if (sizeof(TypeUnsigned) <= sizeof(uint32_t)) { |
| 80 | // This is the "normal" version of the loop nest, for 32-bit or |
| 81 | // narrower types. |
| 82 | for (size_t f = 0; f < NumFuncs; ++f) { |
| 83 | for (size_t i = 0; i < NumValues; ++i) { |
| 84 | for (size_t j = 0; j < NumValues; ++j) { |
| 85 | TypeUnsigned Value1 = Values[i]; |
| 86 | TypeUnsigned Value2 = Values[j]; |
| 87 | // Avoid HW divide-by-zero exception. |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 88 | if (Funcs[f].ExcludeDivExceptions && |
| 89 | inputsMayTriggerException<TypeSigned>(Value1, Value2)) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 90 | continue; |
| 91 | ++TotalTests; |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 92 | TypeUnsigned ResultSz, ResultLlc; |
| 93 | if (Funcs[f].FuncSzUnsigned) { |
| 94 | ResultSz = Funcs[f].FuncSzUnsigned(Value1, Value2); |
| 95 | ResultLlc = Funcs[f].FuncLlcUnsigned(Value1, Value2); |
| 96 | } else { |
| 97 | ResultSz = Funcs[f].FuncSzSigned(Value1, Value2); |
| 98 | ResultLlc = Funcs[f].FuncLlcSigned(Value1, Value2); |
| 99 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 100 | if (ResultSz == ResultLlc) { |
| 101 | ++Passes; |
| 102 | } else { |
| 103 | ++Failures; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 104 | std::cout << "test" << Funcs[f].Name |
| 105 | << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1 |
| 106 | << ", " << Value2 << "): sz=" << (unsigned)ResultSz |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 107 | << " llc=" << (unsigned)ResultLlc << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } else { |
| 113 | // This is the 64-bit version. Test values are synthesized from |
| 114 | // the 32-bit values in Values[]. |
| 115 | for (size_t f = 0; f < NumFuncs; ++f) { |
| 116 | for (size_t iLo = 0; iLo < NumValues; ++iLo) { |
| 117 | for (size_t iHi = 0; iHi < NumValues; ++iHi) { |
| 118 | for (size_t jLo = 0; jLo < NumValues; ++jLo) { |
| 119 | for (size_t jHi = 0; jHi < NumValues; ++jHi) { |
| 120 | TypeUnsigned Value1 = |
| 121 | (((TypeUnsigned)Values[iHi]) << 32) + Values[iLo]; |
| 122 | TypeUnsigned Value2 = |
| 123 | (((TypeUnsigned)Values[jHi]) << 32) + Values[jLo]; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 124 | if (Funcs[f].ExcludeDivExceptions && |
| 125 | inputsMayTriggerException<TypeSigned>(Value1, Value2)) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 126 | continue; |
| 127 | ++TotalTests; |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 128 | TypeUnsigned ResultSz, ResultLlc; |
| 129 | if (Funcs[f].FuncSzUnsigned) { |
| 130 | ResultSz = Funcs[f].FuncSzUnsigned(Value1, Value2); |
| 131 | ResultLlc = Funcs[f].FuncLlcUnsigned(Value1, Value2); |
| 132 | } else { |
| 133 | ResultSz = Funcs[f].FuncSzSigned(Value1, Value2); |
| 134 | ResultLlc = Funcs[f].FuncLlcSigned(Value1, Value2); |
| 135 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 136 | if (ResultSz == ResultLlc) { |
| 137 | ++Passes; |
| 138 | } else { |
| 139 | ++Failures; |
| 140 | std::cout << "test" << Funcs[f].Name |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 141 | << (CHAR_BIT * sizeof(TypeUnsigned)) << "(" << Value1 |
| 142 | << ", " << Value2 << "): sz=" << (unsigned)ResultSz |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 143 | << " llc=" << (unsigned)ResultLlc << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 153 | const static size_t MaxTestsPerFunc = 100000; |
| 154 | |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 155 | template <typename TypeUnsignedLabel, typename TypeSignedLabel> |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 156 | void testsVecInt(size_t &TotalTests, size_t &Passes, size_t &Failures) { |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 157 | typedef typename Vectors<TypeUnsignedLabel>::Ty TypeUnsigned; |
| 158 | typedef typename Vectors<TypeSignedLabel>::Ty TypeSigned; |
| 159 | typedef typename Vectors<TypeUnsignedLabel>::ElementTy ElementTypeUnsigned; |
| 160 | typedef typename Vectors<TypeSignedLabel>::ElementTy ElementTypeSigned; |
| 161 | |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 162 | typedef TypeUnsigned (*FuncTypeUnsigned)(TypeUnsigned, TypeUnsigned); |
| 163 | typedef TypeSigned (*FuncTypeSigned)(TypeSigned, TypeSigned); |
Matt Wala | 35ec373 | 2014-07-18 16:32:16 -0700 | [diff] [blame] | 164 | volatile unsigned Values[] = INT_VALUE_ARRAY; |
| 165 | const static size_t NumValues = sizeof(Values) / sizeof(*Values); |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 166 | static struct { |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 167 | // For functions that operate on unsigned values, the |
| 168 | // FuncLlcSigned and FuncSzSigned fields are NULL. For functions |
| 169 | // that operate on signed values, the FuncLlcUnsigned and |
| 170 | // FuncSzUnsigned fields are NULL. |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 171 | const char *Name; |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 172 | FuncTypeUnsigned FuncLlcUnsigned; |
| 173 | FuncTypeUnsigned FuncSzUnsigned; |
| 174 | FuncTypeSigned FuncLlcSigned; |
| 175 | FuncTypeSigned FuncSzSigned; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 176 | bool ExcludeDivExceptions; // for divide related tests |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 177 | bool MaskShiftOperations; // for shift related tests |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 178 | } Funcs[] = { |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 179 | #define X(inst, op, isdiv, isshift) \ |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 180 | { STR(inst), test##inst, Subzero_::test##inst, NULL, NULL, isdiv, isshift } \ |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 181 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 182 | UINTOP_TABLE |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 183 | #undef X |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 184 | #define X(inst, op, isdiv, isshift) \ |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 185 | { STR(inst), NULL, NULL, test##inst, Subzero_::test##inst, isdiv, isshift } \ |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 186 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 187 | SINTOP_TABLE |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 188 | #undef X |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 189 | }; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 190 | const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs); |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 191 | const static size_t NumElementsInType = Vectors<TypeUnsigned>::NumElements; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 192 | for (size_t f = 0; f < NumFuncs; ++f) { |
| 193 | PRNG Index; |
| 194 | for (size_t i = 0; i < MaxTestsPerFunc; ++i) { |
| 195 | // Initialize the test vectors. |
| 196 | TypeUnsigned Value1, Value2; |
| 197 | for (size_t j = 0; j < NumElementsInType;) { |
Matt Wala | 35ec373 | 2014-07-18 16:32:16 -0700 | [diff] [blame] | 198 | ElementTypeUnsigned Element1 = Values[Index() % NumValues]; |
| 199 | ElementTypeUnsigned Element2 = Values[Index() % NumValues]; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 200 | if (Funcs[f].ExcludeDivExceptions && |
| 201 | inputsMayTriggerException<ElementTypeSigned>(Element1, Element2)) |
| 202 | continue; |
Matt Wala | afeaee4 | 2014-08-07 13:47:30 -0700 | [diff] [blame] | 203 | if (Funcs[f].MaskShiftOperations) |
| 204 | Element2 &= CHAR_BIT * sizeof(ElementTypeUnsigned) - 1; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 205 | Value1[j] = Element1; |
| 206 | Value2[j] = Element2; |
| 207 | ++j; |
| 208 | } |
| 209 | // Perform the test. |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 210 | TypeUnsigned ResultSz, ResultLlc; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 211 | ++TotalTests; |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 212 | if (Funcs[f].FuncSzUnsigned) { |
| 213 | ResultSz = Funcs[f].FuncSzUnsigned(Value1, Value2); |
| 214 | ResultLlc = Funcs[f].FuncLlcUnsigned(Value1, Value2); |
| 215 | } else { |
| 216 | ResultSz = Funcs[f].FuncSzSigned(Value1, Value2); |
| 217 | ResultLlc = Funcs[f].FuncLlcSigned(Value1, Value2); |
| 218 | } |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 219 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) { |
| 220 | ++Passes; |
| 221 | } else { |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 222 | ++Failures; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 223 | std::cout << "test" << Funcs[f].Name << "v" << NumElementsInType << "i" |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 224 | << (CHAR_BIT * sizeof(ElementTypeUnsigned)) << "(" |
| 225 | << vectAsString<TypeUnsignedLabel>(Value1) << "," |
| 226 | << vectAsString<TypeUnsignedLabel>(Value2) |
| 227 | << "): sz=" << vectAsString<TypeUnsignedLabel>(ResultSz) |
| 228 | << " llc=" << vectAsString<TypeUnsignedLabel>(ResultLlc) |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 229 | << "\n"; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 235 | template <typename Type> |
| 236 | void testsFp(size_t &TotalTests, size_t &Passes, size_t &Failures) { |
| 237 | static const Type NegInf = -1.0 / 0.0; |
| 238 | static const Type PosInf = 1.0 / 0.0; |
| 239 | static const Type Nan = 0.0 / 0.0; |
Jan Voung | f37fbbe | 2014-07-09 16:13:13 -0700 | [diff] [blame] | 240 | static const Type NegNan = -0.0 / 0.0; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 241 | volatile Type Values[] = FP_VALUE_ARRAY(NegInf, PosInf, NegNan, Nan); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 242 | const static size_t NumValues = sizeof(Values) / sizeof(*Values); |
| 243 | typedef Type (*FuncType)(Type, Type); |
| 244 | static struct { |
| 245 | const char *Name; |
| 246 | FuncType FuncLlc; |
| 247 | FuncType FuncSz; |
| 248 | } Funcs[] = { |
| 249 | #define X(inst, op, func) \ |
| 250 | { STR(inst), (FuncType)test##inst, (FuncType)Subzero_::test##inst } \ |
| 251 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 252 | FPOP_TABLE |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 253 | #undef X |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 254 | }; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 255 | const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs); |
| 256 | |
| 257 | for (size_t f = 0; f < NumFuncs; ++f) { |
| 258 | for (size_t i = 0; i < NumValues; ++i) { |
| 259 | for (size_t j = 0; j < NumValues; ++j) { |
| 260 | Type Value1 = Values[i]; |
| 261 | Type Value2 = Values[j]; |
| 262 | ++TotalTests; |
| 263 | Type ResultSz = Funcs[f].FuncSz(Value1, Value2); |
| 264 | Type ResultLlc = Funcs[f].FuncLlc(Value1, Value2); |
| 265 | // Compare results using memcmp() in case they are both NaN. |
| 266 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(Type))) { |
| 267 | ++Passes; |
| 268 | } else { |
| 269 | ++Failures; |
| 270 | std::cout << std::fixed << "test" << Funcs[f].Name |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 271 | << (CHAR_BIT * sizeof(Type)) << "(" << Value1 << ", " |
| 272 | << Value2 << "): sz=" << ResultSz << " llc=" << ResultLlc |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 273 | << "\n"; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
Jan Voung | f37fbbe | 2014-07-09 16:13:13 -0700 | [diff] [blame] | 278 | for (size_t i = 0; i < NumValues; ++i) { |
| 279 | Type Value = Values[i]; |
| 280 | ++TotalTests; |
| 281 | Type ResultSz = Subzero_::mySqrt(Value); |
| 282 | Type ResultLlc = mySqrt(Value); |
| 283 | // Compare results using memcmp() in case they are both NaN. |
| 284 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(Type))) { |
| 285 | ++Passes; |
| 286 | } else { |
| 287 | ++Failures; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 288 | std::cout << std::fixed << "test_sqrt" << (CHAR_BIT * sizeof(Type)) << "(" |
| 289 | << Value << "): sz=" << ResultSz << " llc=" << ResultLlc |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 290 | << "\n"; |
Jan Voung | f37fbbe | 2014-07-09 16:13:13 -0700 | [diff] [blame] | 291 | } |
Jim Stichnoth | 8c980d0 | 2015-03-19 13:01:50 -0700 | [diff] [blame] | 292 | ++TotalTests; |
| 293 | ResultSz = Subzero_::myFabs(Value); |
| 294 | ResultLlc = myFabs(Value); |
| 295 | // Compare results using memcmp() in case they are both NaN. |
| 296 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(Type))) { |
| 297 | ++Passes; |
| 298 | } else { |
| 299 | ++Failures; |
| 300 | std::cout << std::fixed << "test_fabs" << (CHAR_BIT * sizeof(Type)) << "(" |
| 301 | << Value << "): sz=" << ResultSz << " llc=" << ResultLlc |
| 302 | << "\n"; |
| 303 | } |
Jan Voung | f37fbbe | 2014-07-09 16:13:13 -0700 | [diff] [blame] | 304 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 307 | void testsVecFp(size_t &TotalTests, size_t &Passes, size_t &Failures) { |
| 308 | static const float NegInf = -1.0 / 0.0; |
| 309 | static const float PosInf = 1.0 / 0.0; |
| 310 | static const float Nan = 0.0 / 0.0; |
| 311 | static const float NegNan = -0.0 / 0.0; |
| 312 | volatile float Values[] = FP_VALUE_ARRAY(NegInf, PosInf, NegNan, Nan); |
| 313 | const static size_t NumValues = sizeof(Values) / sizeof(*Values); |
| 314 | typedef v4f32 (*FuncType)(v4f32, v4f32); |
| 315 | static struct { |
| 316 | const char *Name; |
| 317 | FuncType FuncLlc; |
| 318 | FuncType FuncSz; |
| 319 | } Funcs[] = { |
| 320 | #define X(inst, op, func) \ |
| 321 | { STR(inst), (FuncType)test##inst, (FuncType)Subzero_::test##inst } \ |
| 322 | , |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 323 | FPOP_TABLE |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 324 | #undef X |
Jim Stichnoth | d9dc82e | 2015-03-03 17:06:33 -0800 | [diff] [blame] | 325 | }; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 326 | const static size_t NumFuncs = sizeof(Funcs) / sizeof(*Funcs); |
| 327 | const static size_t NumElementsInType = 4; |
| 328 | for (size_t f = 0; f < NumFuncs; ++f) { |
| 329 | PRNG Index; |
| 330 | for (size_t i = 0; i < MaxTestsPerFunc; ++i) { |
| 331 | // Initialize the test vectors. |
| 332 | v4f32 Value1, Value2; |
| 333 | for (size_t j = 0; j < NumElementsInType; ++j) { |
Matt Wala | 35ec373 | 2014-07-18 16:32:16 -0700 | [diff] [blame] | 334 | Value1[j] = Values[Index() % NumValues]; |
| 335 | Value2[j] = Values[Index() % NumValues]; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 336 | } |
| 337 | // Perform the test. |
| 338 | v4f32 ResultSz = Funcs[f].FuncSz(Value1, Value2); |
| 339 | v4f32 ResultLlc = Funcs[f].FuncLlc(Value1, Value2); |
| 340 | ++TotalTests; |
| 341 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) { |
| 342 | ++Passes; |
| 343 | } else { |
| 344 | ++Failures; |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 345 | std::cout << "test" << Funcs[f].Name << "v4f32" |
| 346 | << "(" << vectAsString<v4f32>(Value1) << "," |
| 347 | << vectAsString<v4f32>(Value2) |
| 348 | << "): sz=" << vectAsString<v4f32>(ResultSz) << " llc" |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 349 | << vectAsString<v4f32>(ResultLlc) << "\n"; |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 350 | } |
Jim Stichnoth | 8c980d0 | 2015-03-19 13:01:50 -0700 | [diff] [blame] | 351 | // Special case for unary fabs operation. Use Value1, ignore Value2. |
| 352 | ResultSz = Subzero_::myFabs(Value1); |
| 353 | ResultLlc = myFabs(Value1); |
| 354 | ++TotalTests; |
| 355 | if (!memcmp(&ResultSz, &ResultLlc, sizeof(ResultSz))) { |
| 356 | ++Passes; |
| 357 | } else { |
| 358 | ++Failures; |
| 359 | std::cout << "test_fabs_v4f32" |
| 360 | << "(" << vectAsString<v4f32>(Value1) |
| 361 | << "): sz=" << vectAsString<v4f32>(ResultSz) << " llc" |
| 362 | << vectAsString<v4f32>(ResultLlc) << "\n"; |
| 363 | } |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 368 | #ifdef X8664_STACK_HACK |
| 369 | extern "C" int wrapped_main(int argc, char *argv[]) { |
| 370 | #else // !defined(X8664_STACK_HACK) |
| 371 | int main(int argc, char *argv[]) { |
| 372 | #endif // X8664_STACK_HACK |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 373 | size_t TotalTests = 0; |
| 374 | size_t Passes = 0; |
| 375 | size_t Failures = 0; |
| 376 | |
Jim Stichnoth | 3ef786f | 2014-09-08 11:19:21 -0700 | [diff] [blame] | 377 | testsInt<bool, bool>(TotalTests, Passes, Failures); |
Jim Stichnoth | 7da431b | 2014-08-05 11:22:37 -0700 | [diff] [blame] | 378 | testsInt<uint8_t, myint8_t>(TotalTests, Passes, Failures); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 379 | testsInt<uint16_t, int16_t>(TotalTests, Passes, Failures); |
| 380 | testsInt<uint32_t, int32_t>(TotalTests, Passes, Failures); |
John Porto | 1d23542 | 2015-08-12 12:37:53 -0700 | [diff] [blame] | 381 | testsInt<uint64, int64>(TotalTests, Passes, Failures); |
Matt Wala | 89a7c2b | 2014-07-22 10:55:30 -0700 | [diff] [blame] | 382 | testsVecInt<v4ui32, v4si32>(TotalTests, Passes, Failures); |
| 383 | testsVecInt<v8ui16, v8si16>(TotalTests, Passes, Failures); |
| 384 | testsVecInt<v16ui8, v16si8>(TotalTests, Passes, Failures); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 385 | testsFp<float>(TotalTests, Passes, Failures); |
| 386 | testsFp<double>(TotalTests, Passes, Failures); |
Matt Wala | 7fa22d8 | 2014-07-17 12:41:31 -0700 | [diff] [blame] | 387 | testsVecFp(TotalTests, Passes, Failures); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 388 | |
| 389 | std::cout << "TotalTests=" << TotalTests << " Passes=" << Passes |
| 390 | << " Failures=" << Failures << "\n"; |
| 391 | return Failures; |
| 392 | } |