|
| 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 <random> |
| 12 | + |
| 13 | +#include "test_utils.hpp" |
| 14 | + |
| 15 | +template <class B> |
| 16 | +class xsimd_api_test : 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 | + using int8_vector_type = std::vector<int8_t, XSIMD_DEFAULT_ALLOCATOR(int8_t)>; |
| 25 | + using uint8_vector_type = std::vector<uint8_t, XSIMD_DEFAULT_ALLOCATOR(uint8_t)>; |
| 26 | + using int16_vector_type = std::vector<int16_t, XSIMD_DEFAULT_ALLOCATOR(int16_t)>; |
| 27 | + using uint16_vector_type = std::vector<uint16_t, XSIMD_DEFAULT_ALLOCATOR(uint16_t)>; |
| 28 | + using int32_vector_type = std::vector<int32_t, XSIMD_DEFAULT_ALLOCATOR(int32_t)>; |
| 29 | + using uint32_vector_type = std::vector<uint32_t, XSIMD_DEFAULT_ALLOCATOR(uint32_t)>; |
| 30 | + using int64_vector_type = std::vector<int64_t, XSIMD_DEFAULT_ALLOCATOR(int64_t)>; |
| 31 | + using uint64_vector_type = std::vector<uint64_t, XSIMD_DEFAULT_ALLOCATOR(uint64_t)>; |
| 32 | + using float_vector_type = std::vector<float, XSIMD_DEFAULT_ALLOCATOR(float)>; |
| 33 | + using double_vector_type = std::vector<double, XSIMD_DEFAULT_ALLOCATOR(double)>; |
| 34 | + |
| 35 | + int8_vector_type i8_vec; |
| 36 | + uint8_vector_type ui8_vec; |
| 37 | + int16_vector_type i16_vec; |
| 38 | + uint16_vector_type ui16_vec; |
| 39 | + int32_vector_type i32_vec; |
| 40 | + uint32_vector_type ui32_vec; |
| 41 | + int64_vector_type i64_vec; |
| 42 | + uint64_vector_type ui64_vec; |
| 43 | + float_vector_type f_vec; |
| 44 | + double_vector_type d_vec; |
| 45 | + |
| 46 | + array_type expected; |
| 47 | + |
| 48 | + xsimd_api_test() |
| 49 | + { |
| 50 | + init_test_vector(i8_vec); |
| 51 | + init_test_vector(ui8_vec); |
| 52 | + init_test_vector(i16_vec); |
| 53 | + init_test_vector(ui16_vec); |
| 54 | + init_test_vector(i32_vec); |
| 55 | + init_test_vector(ui32_vec); |
| 56 | + init_test_vector(i64_vec); |
| 57 | + init_test_vector(ui64_vec); |
| 58 | + init_test_vector(f_vec); |
| 59 | + init_test_vector(d_vec); |
| 60 | + } |
| 61 | + |
| 62 | + void test_load() |
| 63 | + { |
| 64 | + test_load_impl(i8_vec, "load int8_t"); |
| 65 | + test_load_impl(ui8_vec, "load uint8_t"); |
| 66 | + test_load_impl(i16_vec, "load int16_t"); |
| 67 | + test_load_impl(ui16_vec, "load uint16_t"); |
| 68 | + test_load_impl(i32_vec, "load int32_t"); |
| 69 | + test_load_impl(ui32_vec, "load uint32_t"); |
| 70 | + test_load_impl(i64_vec, "load int64_t"); |
| 71 | + test_load_impl(ui64_vec, "load uint64_t"); |
| 72 | + test_load_impl(f_vec, "load float"); |
| 73 | + test_load_impl(d_vec, "load double"); |
| 74 | + } |
| 75 | + |
| 76 | + void test_store() |
| 77 | + { |
| 78 | + test_store_impl(i8_vec, "load int8_t"); |
| 79 | + test_store_impl(ui8_vec, "load uint8_t"); |
| 80 | + test_store_impl(i16_vec, "load int16_t"); |
| 81 | + test_store_impl(ui16_vec, "load uint16_t"); |
| 82 | + test_store_impl(i32_vec, "load int32_t"); |
| 83 | + test_store_impl(ui32_vec, "load uint32_t"); |
| 84 | + test_store_impl(i64_vec, "load int64_t"); |
| 85 | + test_store_impl(ui64_vec, "load uint64_t"); |
| 86 | + test_store_impl(f_vec, "load float"); |
| 87 | + test_store_impl(d_vec, "load double"); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + void test_set() |
| 92 | + { |
| 93 | + test_set_impl<int8_t>("set int8_t"); |
| 94 | + test_set_impl<uint8_t>("set uint8_t"); |
| 95 | + test_set_impl<int16_t>("set int16_t"); |
| 96 | + test_set_impl<uint16_t>("set uint16_t"); |
| 97 | + test_set_impl<int32_t>("set int32_t"); |
| 98 | + test_set_impl<uint32_t>("set uint32_t"); |
| 99 | + test_set_impl<int64_t>("set int64_t"); |
| 100 | + test_set_impl<uint64_t>("set uint64_t"); |
| 101 | + test_set_impl<float>("set float"); |
| 102 | + test_set_impl<double>("set double"); |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + |
| 107 | + template <class V> |
| 108 | + void test_load_impl(const V& v, const std::string& name) |
| 109 | + { |
| 110 | + using src_value_type = typename V::value_type; |
| 111 | + batch_type b; |
| 112 | + std::copy(v.cbegin(), v.cend(), expected.begin()); |
| 113 | + |
| 114 | + b = xsimd::load_simd<src_value_type, value_type>(v.data(), xsimd::unaligned_mode()); |
| 115 | + EXPECT_BATCH_EQ(b, expected) << print_function_name(name + " unaligned"); |
| 116 | + |
| 117 | + b = xsimd::load_simd<src_value_type, value_type>(v.data(), xsimd::aligned_mode()); |
| 118 | + EXPECT_BATCH_EQ(b, expected) << print_function_name(name + " aligned"); |
| 119 | + } |
| 120 | + |
| 121 | + template <class V> |
| 122 | + void test_store_impl(const V& v, const std::string& name) |
| 123 | + { |
| 124 | + using src_value_type = typename V::value_type; |
| 125 | + batch_type b = xsimd::load_simd<src_value_type, value_type>(v.data(), xsimd::aligned_mode()); |
| 126 | + V res(size); |
| 127 | + |
| 128 | + xsimd::store_simd<src_value_type, value_type>(res.data(), b, xsimd::unaligned_mode()); |
| 129 | + EXPECT_VECTOR_EQ(res, v) << print_function_name(name + " unaligned"); |
| 130 | + |
| 131 | + xsimd::store_simd<src_value_type, value_type>(res.data(), b, xsimd::aligned_mode()); |
| 132 | + EXPECT_VECTOR_EQ(res, v) << print_function_name(name + " aligned"); |
| 133 | + } |
| 134 | + |
| 135 | + template <class T> |
| 136 | + void test_set_impl(const std::string& name) |
| 137 | + { |
| 138 | + T v = T(1); |
| 139 | + batch_type expected(v); |
| 140 | + batch_type res = xsimd::set_simd<T, value_type>(v); |
| 141 | + EXPECT_BATCH_EQ(res, expected) << print_function_name(name); |
| 142 | + } |
| 143 | + |
| 144 | + template <class V> |
| 145 | + void init_test_vector(V& vec) |
| 146 | + { |
| 147 | + vec.resize(size); |
| 148 | + |
| 149 | + value_type min = value_type(0); |
| 150 | + value_type max = value_type(100); |
| 151 | + |
| 152 | + std::default_random_engine generator; |
| 153 | + std::uniform_int_distribution<int> distribution(min, max); |
| 154 | + |
| 155 | + auto gen = [&distribution, &generator](){ |
| 156 | + return static_cast<value_type>(distribution(generator)); |
| 157 | + }; |
| 158 | + |
| 159 | + std::generate(vec.begin(), vec.end(), gen); |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +using xsimd_api_types = testing::Types< |
| 164 | + |
| 165 | +#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX512_VERSION |
| 166 | + xsimd::batch<uint8_t, 64>, |
| 167 | + xsimd::batch<int8_t, 64>, |
| 168 | + xsimd::batch<uint16_t, 32>, |
| 169 | + xsimd::batch<int16_t, 32>, |
| 170 | + xsimd::batch<uint32_t, 16>, |
| 171 | + xsimd::batch<int32_t, 16>, |
| 172 | + xsimd::batch<uint64_t, 8>, |
| 173 | + xsimd::batch<int64_t, 8>, |
| 174 | + xsimd::batch<float, 16>, |
| 175 | + xsimd::batch<double, 8> |
| 176 | +#elif XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX_VERSION |
| 177 | + xsimd::batch<uint8_t, 32>, |
| 178 | + xsimd::batch<int8_t, 32>, |
| 179 | + xsimd::batch<uint16_t, 16>, |
| 180 | + xsimd::batch<int16_t, 16>, |
| 181 | + xsimd::batch<uint32_t, 8>, |
| 182 | + xsimd::batch<int32_t, 8>, |
| 183 | + xsimd::batch<uint64_t, 4>, |
| 184 | + xsimd::batch<int64_t, 4>, |
| 185 | + xsimd::batch<float, 8>, |
| 186 | + xsimd::batch<double, 4> |
| 187 | +#elif XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION |
| 188 | + xsimd::batch<uint8_t, 16>, |
| 189 | + xsimd::batch<int8_t, 16>, |
| 190 | + xsimd::batch<uint16_t, 8>, |
| 191 | + xsimd::batch<int16_t, 8>, |
| 192 | + xsimd::batch<uint32_t, 4>, |
| 193 | + xsimd::batch<int32_t, 4>, |
| 194 | + xsimd::batch<uint64_t, 2>, |
| 195 | + xsimd::batch<int64_t, 2>, |
| 196 | + xsimd::batch<float, 4>, |
| 197 | + xsimd::batch<double, 2> |
| 198 | +#elif XSIMD_ARM_INSTR_SET >= XSIMD_ARM7_NEON_VERSION |
| 199 | + xsimd::batch<uint8_t, 16>, |
| 200 | + xsimd::batch<int8_t, 16>, |
| 201 | + xsimd::batch<uint16_t, 8>, |
| 202 | + xsimd::batch<int16_t, 8>, |
| 203 | + xsimd::batch<uint32_t, 4>, |
| 204 | + xsimd::batch<int32_t, 4>, |
| 205 | + xsimd::batch<uint64_t, 2>, |
| 206 | + xsimd::batch<int64_t, 2>, |
| 207 | + xsimd::batch<float, 4> |
| 208 | +#if XSIMD_ARM_INSTR_SET >= XSIMD_ARM8_64_NEON_VERSION |
| 209 | + , |
| 210 | + xsimd::batch<double, 2> |
| 211 | +#endif |
| 212 | +#endif |
| 213 | + >; |
| 214 | + |
| 215 | +TYPED_TEST_SUITE(xsimd_api_test, xsimd_api_types, simd_test_names); |
| 216 | + |
| 217 | +TYPED_TEST(xsimd_api_test, load) |
| 218 | +{ |
| 219 | + this->test_load(); |
| 220 | +} |
| 221 | + |
| 222 | +TYPED_TEST(xsimd_api_test, store) |
| 223 | +{ |
| 224 | + this->test_store(); |
| 225 | +} |
| 226 | + |
| 227 | +#ifdef XSIMD_BATCH_DOUBLE_SIZE |
| 228 | +TYPED_TEST(xsimd_api_test, set) |
| 229 | +{ |
| 230 | + this->test_set(); |
| 231 | +} |
| 232 | +#endif |
0 commit comments