1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xsimd/xsimd.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
#include "test_utils.hpp"
namespace xsimd
{
template <typename T, std::size_t N>
struct init_swizzle_base
{
using swizzle_vector_type = std::array<T, N>;
swizzle_vector_type lhs_in, exped_reverse, exped_fill, exped_dup, exped_ror, exped_rol;
template <int... Indices>
std::vector<swizzle_vector_type> create_swizzle_vectors()
{
std::vector<swizzle_vector_type> vects;
/* Generate input data */
for (std::size_t i = 0; i < N; ++i)
{
lhs_in[i] = 2 * i + 1;
}
vects.push_back(std::move(lhs_in));
/* Expected reversed data */
for (std::size_t i = 0; i < N; ++i)
{
exped_reverse[i] = lhs_in[N - 1 - i];
exped_fill[i] = lhs_in[N - 1];
exped_dup[i] = lhs_in[2 * (i / 2)];
exped_ror[i] = lhs_in[(i - 1) % N];
exped_rol[i] = lhs_in[(i + 1) % N];
}
vects.push_back(std::move(exped_reverse));
vects.push_back(std::move(exped_fill));
vects.push_back(std::move(exped_dup));
vects.push_back(std::move(exped_ror));
vects.push_back(std::move(exped_rol));
return vects;
}
};
}
template <class T>
struct Reversor
{
static constexpr T get(T i, T n)
{
return n - 1 - i;
}
};
template <class T>
struct Last
{
static constexpr T get(T, T n)
{
return n - 1;
}
};
template <class T>
struct Dup
{
static constexpr T get(T i, T)
{
return 2 * (i / 2);
}
};
template <class T>
struct as_index
{
using type = xsimd::as_unsigned_integer_t<T>;
};
template <class T>
struct as_index<std::complex<T>> : as_index<T>
{
};
template <class B>
struct insert_test
{
using batch_type = B;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
void insert_first()
{
value_type fill_value = 0;
value_type sentinel_value = 1;
batch_type v(fill_value);
batch_type w = insert(v, sentinel_value, ::xsimd::index<0>());
std::array<value_type, batch_type::size> data;
w.store_unaligned(data.data());
CHECK_SCALAR_EQ(data.front(), sentinel_value);
for (size_t i = 1; i < batch_type::size; ++i)
CHECK_SCALAR_EQ(data[i], fill_value);
}
void insert_last()
{
value_type fill_value = 0;
value_type sentinel_value = 1;
batch_type v(fill_value);
batch_type w = insert(v, sentinel_value, ::xsimd::index<batch_type::size - 1>());
std::array<value_type, batch_type::size> data;
w.store_unaligned(data.data());
for (size_t i = 0; i < batch_type::size - 1; ++i)
CHECK_SCALAR_EQ(data[i], fill_value);
CHECK_SCALAR_EQ(data.back(), sentinel_value);
}
};
TEST_CASE_TEMPLATE("[insert_test]", B, BATCH_TYPES)
{
insert_test<B> Test;
SUBCASE("insert_first")
{
Test.insert_first();
}
SUBCASE("insert_last")
{
Test.insert_last();
}
}
template <class B>
struct swizzle_test
{
using batch_type = B;
using value_type = typename B::value_type;
using arch_type = typename B::arch_type;
static constexpr size_t size = B::size;
void rotate_right()
{
xsimd::init_swizzle_base<value_type, size> swizzle_base;
auto swizzle_vecs = swizzle_base.create_swizzle_vectors();
auto v_lhs = swizzle_vecs[0];
auto v_exped = swizzle_vecs[4];
B b_lhs = B::load_unaligned(v_lhs.data());
B b_exped = B::load_unaligned(v_exped.data());
B b_res = xsimd::rotate_right<1>(b_lhs);
CHECK_BATCH_EQ(b_res, b_exped);
}
void rotate_left()
{
xsimd::init_swizzle_base<value_type, size> swizzle_base;
auto swizzle_vecs = swizzle_base.create_swizzle_vectors();
auto v_lhs = swizzle_vecs[0];
auto v_exped = swizzle_vecs[5];
B b_lhs = B::load_unaligned(v_lhs.data());
B b_exped = B::load_unaligned(v_exped.data());
B b_res = xsimd::rotate_left<1>(b_lhs);
CHECK_BATCH_EQ(b_res, b_exped);
}
void swizzle_reverse()
{
xsimd::init_swizzle_base<value_type, size> swizzle_base;
auto swizzle_vecs = swizzle_base.create_swizzle_vectors();
auto v_lhs = swizzle_vecs[0];
auto v_exped = swizzle_vecs[1];
B b_lhs = B::load_unaligned(v_lhs.data());
B b_exped = B::load_unaligned(v_exped.data());
using index_type = typename as_index<value_type>::type;
auto index_batch = xsimd::make_batch_constant<index_type, arch_type, Reversor<index_type>>();
B b_res = xsimd::swizzle(b_lhs, index_batch);
CHECK_BATCH_EQ(b_res, b_exped);
B b_dyres = xsimd::swizzle(b_lhs, (xsimd::batch<index_type, arch_type>)index_batch);
CHECK_BATCH_EQ(b_dyres, b_exped);
}
void swizzle_fill()
{
xsimd::init_swizzle_base<value_type, size> swizzle_base;
auto swizzle_vecs = swizzle_base.create_swizzle_vectors();
auto v_lhs = swizzle_vecs[0];
auto v_exped = swizzle_vecs[2];
B b_lhs = B::load_unaligned(v_lhs.data());
B b_exped = B::load_unaligned(v_exped.data());
using index_type = typename as_index<value_type>::type;
auto index_batch = xsimd::make_batch_constant<index_type, arch_type, Last<index_type>>();
B b_res = xsimd::swizzle(b_lhs, index_batch);
CHECK_BATCH_EQ(b_res, b_exped);
B b_dyres = xsimd::swizzle(b_lhs, (xsimd::batch<index_type, arch_type>)index_batch);
CHECK_BATCH_EQ(b_dyres, b_exped);
}
void swizzle_dup()
{
xsimd::init_swizzle_base<value_type, size> swizzle_base;
auto swizzle_vecs = swizzle_base.create_swizzle_vectors();
auto v_lhs = swizzle_vecs[0];
auto v_exped = swizzle_vecs[3];
B b_lhs = B::load_unaligned(v_lhs.data());
B b_exped = B::load_unaligned(v_exped.data());
using index_type = typename as_index<value_type>::type;
auto index_batch = xsimd::make_batch_constant<index_type, arch_type, Dup<index_type>>();
B b_res = xsimd::swizzle(b_lhs, index_batch);
CHECK_BATCH_EQ(b_res, b_exped);
B b_dyres = xsimd::swizzle(b_lhs, (xsimd::batch<index_type, arch_type>)index_batch);
CHECK_BATCH_EQ(b_dyres, b_exped);
}
};
TEST_CASE_TEMPLATE("[swizzle]", B, BATCH_SWIZZLE_TYPES)
{
swizzle_test<B> Test;
SUBCASE("reverse")
{
Test.swizzle_reverse();
}
SUBCASE("rotate")
{
Test.rotate_left();
Test.rotate_right();
}
SUBCASE("fill")
{
Test.swizzle_fill();
}
SUBCASE("dup")
{
Test.swizzle_dup();
}
}
#endif
|