|
| 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 Ty> |
| 11 | +using FnTy = std::function<void(Ty *, unsigned)>; |
| 12 | +template <typename Ty> |
| 13 | +static void checkVectorFunction(FnTy<Ty> ScalarFn, FnTy<Ty> VectorFn, |
| 14 | + const char *Name) { |
| 15 | + std::cout << "Checking " << Name << "\n"; |
| 16 | + |
| 17 | + unsigned N = 1000; |
| 18 | + std::unique_ptr<Ty[]> Reference(new Ty[N]); |
| 19 | + std::unique_ptr<Ty[]> ToCheck(new Ty[N]); |
| 20 | + |
| 21 | + init_data(Reference, N); |
| 22 | + std::copy(&Reference[0], &Reference[0] + N, &ToCheck[0]); |
| 23 | + |
| 24 | + // Run scalar and vector versions |
| 25 | + ScalarFn(&Reference[0], N); |
| 26 | + VectorFn(&ToCheck[0], N); |
| 27 | + |
| 28 | + // Check results match |
| 29 | + check(Reference, ToCheck, N); |
| 30 | +} |
| 31 | + |
| 32 | +#define FOR_COND for (unsigned I = 0; I < TC; ++I) |
| 33 | + |
| 34 | +#define DEFINE_CONDITIONAL_INCREMENT_BODY(Stride) \ |
| 35 | + FOR_COND { \ |
| 36 | + if (I % Stride == 0) { \ |
| 37 | + A[I] = A[I] + 1; \ |
| 38 | + } \ |
| 39 | + } \ |
| 40 | + return 0; |
| 41 | + |
| 42 | +#define DEFINE_CONDITIONAL_INCREMENT_BY_VALUE_BODY(Marker) \ |
| 43 | + FOR_COND { \ |
| 44 | + if (A[I] == Marker) { \ |
| 45 | + A[I] = A[I] + 1; \ |
| 46 | + } \ |
| 47 | + } \ |
| 48 | + return 0; |
| 49 | + |
| 50 | +int main(void) { |
| 51 | + rng = std::mt19937(15); |
| 52 | + |
| 53 | + { |
| 54 | + // Conditional increment with stride 2 (50% active lanes) |
| 55 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 56 | + , DEFINE_CONDITIONAL_INCREMENT_BODY(4), unsigned); |
| 57 | + checkVectorFunction<int64_t>(ScalarFn, VectorFn, |
| 58 | + "conditional_increment_i64_stride_4"); |
| 59 | + } |
| 60 | + |
| 61 | + { |
| 62 | + // Test with int32_t type - stride 4 |
| 63 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 64 | + , DEFINE_CONDITIONAL_INCREMENT_BODY(4), unsigned); |
| 65 | + checkVectorFunction<int32_t>(ScalarFn, VectorFn, |
| 66 | + "conditional_increment_i32_stride_4"); |
| 67 | + } |
| 68 | + |
| 69 | + { |
| 70 | + // Test with int16_t type - stride 4 |
| 71 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 72 | + , DEFINE_CONDITIONAL_INCREMENT_BODY(4), unsigned); |
| 73 | + checkVectorFunction<int16_t>(ScalarFn, VectorFn, |
| 74 | + "conditional_increment_i16_stride_4"); |
| 75 | + } |
| 76 | + |
| 77 | + { |
| 78 | + // Conditional increment by value (sparse condition) |
| 79 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 80 | + , DEFINE_CONDITIONAL_INCREMENT_BY_VALUE_BODY(0), unsigned); |
| 81 | + checkVectorFunction<int64_t>(ScalarFn, VectorFn, |
| 82 | + "conditional_increment_i64_by_value"); |
| 83 | + } |
| 84 | + |
| 85 | + { |
| 86 | + // Conditional increment by value (sparse condition) |
| 87 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 88 | + , DEFINE_CONDITIONAL_INCREMENT_BY_VALUE_BODY(0), unsigned); |
| 89 | + checkVectorFunction<int32_t>(ScalarFn, VectorFn, |
| 90 | + "conditional_increment_i32_by_value"); |
| 91 | + } |
| 92 | + |
| 93 | + { |
| 94 | + // Conditional increment by value (sparse condition) |
| 95 | + DEFINE_SCALAR_AND_VECTOR_FN1_TYPE( |
| 96 | + , DEFINE_CONDITIONAL_INCREMENT_BY_VALUE_BODY(0), unsigned); |
| 97 | + checkVectorFunction<int16_t>(ScalarFn, VectorFn, |
| 98 | + "conditional_increment_i16_by_value"); |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
0 commit comments