|
| 1 | +#include <algorithm> |
| 2 | +#include <functional> |
| 3 | +#include <iostream> |
| 4 | +#include <limits> |
| 5 | +#include <memory> |
| 6 | +#include <stdint.h> |
| 7 | + |
| 8 | +#include "common.h" |
| 9 | + |
| 10 | +template <typename RetTy, typename Ty> |
| 11 | +using Fn2Ty = std::function<RetTy(Ty *, Ty *, RetTy)>; |
| 12 | +template <typename RetTy, typename Ty> |
| 13 | +static void checkVectorFunction(Fn2Ty<RetTy, Ty> ScalarFn, |
| 14 | + Fn2Ty<RetTy, Ty> VectorFn, const char *Name) { |
| 15 | + std::cout << "Checking " << Name << "\n"; |
| 16 | + |
| 17 | + unsigned N = 1000; |
| 18 | + std::unique_ptr<Ty[]> Src1(new Ty[N]); |
| 19 | + std::unique_ptr<Ty[]> Src2(new Ty[N]); |
| 20 | + init_data(Src1, N); |
| 21 | + init_data(Src2, N); |
| 22 | + |
| 23 | + // Test VectorFn with different input data. |
| 24 | + { |
| 25 | + // Check with random inputs. |
| 26 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 27 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 28 | + if (Reference != ToCheck) { |
| 29 | + std::cerr << "Miscompare\n"; |
| 30 | + exit(1); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + { |
| 35 | + // Check with Src1 > Src2 for all elements. |
| 36 | + for (unsigned I = 0; I != N; ++I) { |
| 37 | + Src1[I] = std::numeric_limits<Ty>::max(); |
| 38 | + Src2[I] = std::numeric_limits<Ty>::min(); |
| 39 | + } |
| 40 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 41 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 42 | + if (Reference != ToCheck) { |
| 43 | + std::cerr << "Miscompare\n"; |
| 44 | + exit(1); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + { |
| 49 | + // Check with Src1 < Src2 for all elements. |
| 50 | + for (unsigned I = 0; I != N; ++I) { |
| 51 | + Src1[I] = std::numeric_limits<Ty>::min(); |
| 52 | + Src2[I] = std::numeric_limits<Ty>::max(); |
| 53 | + } |
| 54 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 55 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 56 | + if (Reference != ToCheck) { |
| 57 | + std::cerr << "Miscompare\n"; |
| 58 | + exit(1); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + { |
| 63 | + // Check with only Src1[998] > Src2[998]. |
| 64 | + for (unsigned I = 0; I != N; ++I) |
| 65 | + Src1[I] = Src2[I] = std::numeric_limits<Ty>::min(); |
| 66 | + Src1[998] = std::numeric_limits<Ty>::max(); |
| 67 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 68 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 69 | + if (Reference != ToCheck) { |
| 70 | + std::cerr << "Miscompare\n"; |
| 71 | + exit(1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + // Check with only Src1[0] > Src2[0]. |
| 77 | + for (unsigned I = 0; I != N; ++I) |
| 78 | + Src1[I] = Src2[I] = std::numeric_limits<Ty>::min(); |
| 79 | + Src1[0] = std::numeric_limits<Ty>::max(); |
| 80 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 81 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 82 | + if (Reference != ToCheck) { |
| 83 | + std::cerr << "Miscompare\n"; |
| 84 | + exit(1); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + { |
| 89 | + // Check with only Src1[N - 1] > Src2[N - 1]. |
| 90 | + for (unsigned I = 0; I != N; ++I) |
| 91 | + Src1[I] = Src2[I] = std::numeric_limits<Ty>::min(); |
| 92 | + Src1[N - 1] = std::numeric_limits<Ty>::max(); |
| 93 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 94 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 95 | + if (Reference != ToCheck) { |
| 96 | + std::cerr << "Miscompare\n"; |
| 97 | + exit(1); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + { |
| 102 | + // Check with only Src1[0] > Src2[0] and Src1[N - 1] > Src2[N - 1]. |
| 103 | + for (unsigned I = 0; I != N; ++I) |
| 104 | + Src1[I] = Src2[I] = std::numeric_limits<Ty>::min(); |
| 105 | + Src1[0] = Src1[N - 1] = std::numeric_limits<Ty>::max(); |
| 106 | + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); |
| 107 | + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); |
| 108 | + if (Reference != ToCheck) { |
| 109 | + std::cerr << "Miscompare\n"; |
| 110 | + exit(1); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +int main(void) { |
| 116 | + rng = std::mt19937(15); |
| 117 | + |
| 118 | + { |
| 119 | + // Find the last index where A[I] > B[I] and update Rdx when the condition |
| 120 | + // is true. |
| 121 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 122 | + int32_t Rdx = -1;, |
| 123 | + for (int32_t I = 0; I < TC; I++) { |
| 124 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 125 | + } |
| 126 | + return Rdx;, |
| 127 | + int32_t |
| 128 | + ); |
| 129 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 130 | + "findlast_true_update"); |
| 131 | + } |
| 132 | + |
| 133 | + { |
| 134 | + // Update Rdx when the condition A[I] > B[I] is false. |
| 135 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 136 | + int32_t Rdx = -1;, |
| 137 | + for (int32_t I = 0; I < TC; I++) { |
| 138 | + Rdx = A[I] > B[I] ? Rdx : I; |
| 139 | + } |
| 140 | + return Rdx;, |
| 141 | + int32_t |
| 142 | + ); |
| 143 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 144 | + "findlast_false_update"); |
| 145 | + } |
| 146 | + |
| 147 | + { |
| 148 | + // Find the last index with the start value TC. |
| 149 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 150 | + int32_t Rdx = TC;, |
| 151 | + for (int32_t I = 0; I < TC; I++) { |
| 152 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 153 | + } |
| 154 | + return Rdx;, |
| 155 | + int32_t |
| 156 | + ); |
| 157 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 158 | + "findlast_start_TC"); |
| 159 | + } |
| 160 | + |
| 161 | + { |
| 162 | + // Increment the induction variable by 2. |
| 163 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 164 | + int32_t Rdx = -1;, |
| 165 | + for (int32_t I = 0; I < TC; I += 2) { |
| 166 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 167 | + } |
| 168 | + return Rdx;, |
| 169 | + int32_t |
| 170 | + ); |
| 171 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 172 | + "findlast_inc_2"); |
| 173 | + } |
| 174 | + |
| 175 | + { |
| 176 | + // Check with decreasing induction variable. |
| 177 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 178 | + int32_t Rdx = -1;, |
| 179 | + for (int32_t I = TC; I > 0; I--) { |
| 180 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 181 | + } |
| 182 | + return Rdx;, |
| 183 | + int32_t |
| 184 | + ); |
| 185 | + checkVectorFunction<int32_t, int32_t>( |
| 186 | + ScalarFn, VectorFn, "findlast_start_decreasing_induction"); |
| 187 | + } |
| 188 | + |
| 189 | + { |
| 190 | + // Check with the induction variable starts from 3. |
| 191 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 192 | + int32_t Rdx = -1;, |
| 193 | + for (int32_t I = 3; I < TC; I++) { |
| 194 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 195 | + } |
| 196 | + return Rdx;, |
| 197 | + int32_t |
| 198 | + ); |
| 199 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 200 | + "findlast_iv_start_3"); |
| 201 | + } |
| 202 | + |
| 203 | + { |
| 204 | + // Check with start value of 3 and induction variable starts at 3. |
| 205 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 206 | + int32_t Rdx = 3;, |
| 207 | + for (int32_t I = 3; I < TC; I++) { |
| 208 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 209 | + } |
| 210 | + return Rdx;, |
| 211 | + int32_t |
| 212 | + ); |
| 213 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 214 | + "findlast_start_3_iv_start_3"); |
| 215 | + } |
| 216 | + |
| 217 | + { |
| 218 | + // Check with start value of 2 and induction variable starts at 3. |
| 219 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 220 | + int32_t Rdx = 2;, |
| 221 | + for (int32_t I = 3; I < TC; I++) { |
| 222 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 223 | + } |
| 224 | + return Rdx;, |
| 225 | + int32_t |
| 226 | + ); |
| 227 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 228 | + "findlast_start_2_iv_start_3"); |
| 229 | + } |
| 230 | + |
| 231 | + { |
| 232 | + // Check with start value of 4 and induction variable starts at 3. |
| 233 | + DEFINE_SCALAR_AND_VECTOR_FN2_TYPE( |
| 234 | + int32_t Rdx = 4;, |
| 235 | + for (int32_t I = 3; I < TC; I++) { |
| 236 | + Rdx = A[I] > B[I] ? I : Rdx; |
| 237 | + } |
| 238 | + return Rdx;, |
| 239 | + int32_t |
| 240 | + ); |
| 241 | + checkVectorFunction<int32_t, int32_t>(ScalarFn, VectorFn, |
| 242 | + "findlast_start_4_iv_start_3"); |
| 243 | + } |
| 244 | + |
| 245 | + return 0; |
| 246 | +} |
0 commit comments