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