|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * |
| 3 | +* Martin Renou * |
| 4 | +* Copyright (c) QuantStack * |
| 5 | +* * |
| 6 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 7 | +* * |
| 8 | +* The full license is in the file LICENSE, distributed with this software. * |
| 9 | +****************************************************************************/ |
| 10 | + |
| 11 | +#include <functional> |
| 12 | + |
| 13 | +#include "test_utils.hpp" |
| 14 | + |
| 15 | +template <class B> |
| 16 | +class batch : public testing::Test |
| 17 | +{ |
| 18 | +protected: |
| 19 | + |
| 20 | + using batch_type = B; |
| 21 | + using value_type = typename B::value_type; |
| 22 | + static constexpr size_t size = B::size; |
| 23 | + using array_type = std::array<value_type, size>; |
| 24 | + |
| 25 | + array_type lhs; |
| 26 | + array_type rhs; |
| 27 | + |
| 28 | + batch() |
| 29 | + { |
| 30 | + init_operands(); |
| 31 | + } |
| 32 | + |
| 33 | + void test_load_store() const |
| 34 | + { |
| 35 | + array_type res; |
| 36 | + batch_type b; |
| 37 | + b.load_unaligned(lhs.data()); |
| 38 | + b.store_unaligned(res.data()); |
| 39 | + EXPECT_EQ(res, lhs) << print_function_name("load_unaligned / store_unaligned"); |
| 40 | + |
| 41 | + alignas(XSIMD_DEFAULT_ALIGNMENT) array_type arhs(this->rhs); |
| 42 | + alignas(XSIMD_DEFAULT_ALIGNMENT) array_type ares; |
| 43 | + b.load_aligned(arhs.data()); |
| 44 | + b.store_aligned(ares.data()); |
| 45 | + EXPECT_EQ(ares, rhs) << print_function_name("load_aligned / store_aligned"); |
| 46 | + } |
| 47 | + |
| 48 | + void test_constructors() const |
| 49 | + { |
| 50 | + array_type tmp; |
| 51 | + std::fill(tmp.begin(), tmp.end(), value_type(2)); |
| 52 | + batch_type b0(2); |
| 53 | + EXPECT_EQ(b0, tmp) << print_function_name("batch(value_type)"); |
| 54 | + |
| 55 | + batch_type b1(lhs.data()); |
| 56 | + EXPECT_EQ(b1, lhs) << print_function_name("batch(value_type*)"); |
| 57 | + } |
| 58 | + |
| 59 | + void test_arithmetic() const |
| 60 | + { |
| 61 | + // batch + batch |
| 62 | + { |
| 63 | + array_type expected; |
| 64 | + std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(), std::plus<value_type>()); |
| 65 | + batch_type res = batch_lhs() + batch_rhs(); |
| 66 | + EXPECT_BATCH_EQ(res, expected) << print_function_name("batch + batch"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +private: |
| 71 | + |
| 72 | + batch_type batch_lhs() const |
| 73 | + { |
| 74 | + return batch_type(lhs.data()); |
| 75 | + } |
| 76 | + |
| 77 | + batch_type batch_rhs() const |
| 78 | + { |
| 79 | + return batch_type(rhs.data()); |
| 80 | + } |
| 81 | + |
| 82 | + template <class T = value_type> |
| 83 | + xsimd::enable_integral_t<T, void> init_operands() |
| 84 | + { |
| 85 | + for (size_t i = 0; i < size; ++i) |
| 86 | + { |
| 87 | + bool negative_lhs = std::is_signed<T>::value && (i % 2 == 1); |
| 88 | + lhs[i] = value_type(i) * (negative_lhs ? -10 : 10); |
| 89 | + rhs[i] = value_type(i) + value_type(4); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + template <class T = value_type> |
| 94 | + xsimd::enable_floating_point_t<T, void> init_operands() |
| 95 | + { |
| 96 | + for (size_t i = 0; i < size; ++i) |
| 97 | + { |
| 98 | + lhs[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25)); |
| 99 | + rhs[i] = value_type(10.2) / (i + 2) + value_type(0.25); |
| 100 | + } |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +TYPED_TEST_SUITE_P(batch); |
| 105 | + |
| 106 | +TYPED_TEST_P(batch, load_store) |
| 107 | +{ |
| 108 | + this->test_load_store(); |
| 109 | +} |
| 110 | + |
| 111 | +TYPED_TEST_P(batch, constructors) |
| 112 | +{ |
| 113 | + this->test_constructors(); |
| 114 | +} |
| 115 | + |
| 116 | +TYPED_TEST_P(batch, arithmetic) |
| 117 | +{ |
| 118 | + this->test_arithmetic(); |
| 119 | +} |
| 120 | + |
| 121 | +REGISTER_TYPED_TEST_SUITE_P( |
| 122 | + batch, |
| 123 | + load_store, |
| 124 | + constructors, |
| 125 | + arithmetic |
| 126 | +); |
| 127 | + |
| 128 | + |
| 129 | +#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION |
| 130 | +INSTANTIATE_TYPED_TEST_SUITE_P(sse, |
| 131 | + batch, |
| 132 | + sse_types, |
| 133 | + simd_test_names); |
| 134 | +#endif |
0 commit comments