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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/***************************************************************************
* 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"
template <class B>
struct constant_batch_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;
using array_type = std::array<value_type, size>;
using bool_array_type = std::array<bool, size>;
using batch_bool_type = typename batch_type::batch_bool_type;
struct generator
{
static constexpr value_type get(size_t index, size_t /*size*/)
{
return index % 2 ? 0 : 1;
}
};
void test_init_from_generator() const
{
array_type expected;
size_t i = 0;
std::generate(expected.begin(), expected.end(),
[&i]()
{ return generator::get(i++, size); });
constexpr auto b = xsimd::make_batch_constant<value_type, arch_type, generator>();
INFO("batch(value_type)");
CHECK_BATCH_EQ((batch_type)b, expected);
}
void test_cast() const
{
constexpr auto cst_b = xsimd::make_batch_constant<value_type, arch_type, generator>();
auto b0 = cst_b.as_batch();
auto b1 = (batch_type)cst_b;
CHECK_BATCH_EQ(b0, b1);
// The actual values are already tested in test_init_from_generator
}
struct arange
{
static constexpr value_type get(size_t index, size_t /*size*/)
{
return index;
}
};
void test_init_from_generator_arange() const
{
array_type expected;
size_t i = 0;
std::generate(expected.begin(), expected.end(),
[&i]()
{ return arange::get(i++, size); });
constexpr auto b = xsimd::make_batch_constant<value_type, arch_type, arange>();
INFO("batch(value_type)");
CHECK_BATCH_EQ((batch_type)b, expected);
}
template <value_type V>
struct constant
{
static constexpr value_type get(size_t /*index*/, size_t /*size*/)
{
return V;
}
};
void test_init_from_constant() const
{
array_type expected;
std::fill(expected.begin(), expected.end(), constant<3>::get(0, 0));
constexpr auto b = xsimd::make_batch_constant<value_type, arch_type, constant<3>>();
INFO("batch(value_type)");
CHECK_BATCH_EQ((batch_type)b, expected);
}
void test_ops() const
{
constexpr auto n12 = xsimd::make_batch_constant<value_type, arch_type, constant<12>>();
constexpr auto n3 = xsimd::make_batch_constant<value_type, arch_type, constant<3>>();
constexpr auto n12_add_n3 = n12 + n3;
constexpr auto n15 = xsimd::make_batch_constant<value_type, arch_type, constant<15>>();
static_assert(std::is_same<decltype(n12_add_n3), decltype(n15)>::value, "n12 + n3 == n15");
constexpr auto n12_sub_n3 = n12 - n3;
constexpr auto n9 = xsimd::make_batch_constant<value_type, arch_type, constant<9>>();
static_assert(std::is_same<decltype(n12_sub_n3), decltype(n9)>::value, "n12 - n3 == n9");
constexpr auto n12_mul_n3 = n12 * n3;
constexpr auto n36 = xsimd::make_batch_constant<value_type, arch_type, constant<36>>();
static_assert(std::is_same<decltype(n12_mul_n3), decltype(n36)>::value, "n12 * n3 == n36");
constexpr auto n12_div_n3 = n12 / n3;
constexpr auto n4 = xsimd::make_batch_constant<value_type, arch_type, constant<4>>();
static_assert(std::is_same<decltype(n12_div_n3), decltype(n4)>::value, "n12 / n3 == n4");
constexpr auto n12_mod_n3 = n12 % n3;
constexpr auto n0 = xsimd::make_batch_constant<value_type, arch_type, constant<0>>();
static_assert(std::is_same<decltype(n12_mod_n3), decltype(n0)>::value, "n12 % n3 == n0");
constexpr auto n12_land_n3 = n12 & n3;
static_assert(std::is_same<decltype(n12_land_n3), decltype(n0)>::value, "n12 & n3 == n0");
constexpr auto n12_lor_n3 = n12 | n3;
static_assert(std::is_same<decltype(n12_lor_n3), decltype(n15)>::value, "n12 | n3 == n15");
constexpr auto n12_lxor_n3 = n12 ^ n3;
static_assert(std::is_same<decltype(n12_lxor_n3), decltype(n15)>::value, "n12 ^ n3 == n15");
constexpr auto n12_uadd = +n12;
static_assert(std::is_same<decltype(n12_uadd), decltype(n12)>::value, "+n12 == n12");
constexpr auto n12_inv = ~n12;
constexpr auto n12_inv_ = xsimd::make_batch_constant<value_type, arch_type, constant<(value_type)~12>>();
static_assert(std::is_same<decltype(n12_inv), decltype(n12_inv_)>::value, "~n12 == n12_inv");
constexpr auto n12_usub = -n12;
constexpr auto n12_usub_ = xsimd::make_batch_constant<value_type, arch_type, constant<(value_type)-12>>();
static_assert(std::is_same<decltype(n12_usub), decltype(n12_usub_)>::value, "-n12 == n12_usub");
}
};
TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
{
constant_batch_test<B> Test;
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
SUBCASE("as_batch") { Test.test_cast(); }
SUBCASE("init_from_generator_arange")
{
Test.test_init_from_generator_arange();
}
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
SUBCASE("operators")
{
Test.test_ops();
}
}
template <class B>
struct constant_bool_batch_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;
using array_type = std::array<value_type, size>;
using bool_array_type = std::array<bool, size>;
using batch_bool_type = typename batch_type::batch_bool_type;
struct generator
{
static constexpr bool get(size_t index, size_t /*size*/)
{
return index % 2;
}
};
void test_init_from_generator() const
{
bool_array_type expected;
size_t i = 0;
std::generate(expected.begin(), expected.end(),
[&i]()
{ return generator::get(i++, size); });
constexpr auto b = xsimd::make_batch_bool_constant<value_type, arch_type, generator>();
INFO("batch_bool_constant(value_type)");
CHECK_BATCH_EQ((batch_bool_type)b, expected);
}
struct split
{
static constexpr bool get(size_t index, size_t size)
{
return index < size / 2;
}
};
void test_init_from_generator_split() const
{
bool_array_type expected;
size_t i = 0;
std::generate(expected.begin(), expected.end(),
[&i]()
{ return split::get(i++, size); });
constexpr auto b = xsimd::make_batch_bool_constant<value_type, arch_type, split>();
INFO("batch_bool_constant(value_type)");
CHECK_BATCH_EQ((batch_bool_type)b, expected);
}
struct inv_split
{
static constexpr bool get(size_t index, size_t size)
{
return !split().get(index, size);
}
};
template <bool Val>
struct constant
{
static constexpr bool get(size_t /*index*/, size_t /*size*/)
{
return Val;
}
};
void test_cast() const
{
constexpr auto all_true = xsimd::make_batch_bool_constant<value_type, arch_type, constant<true>>();
auto b0 = all_true.as_batch_bool();
auto b1 = (batch_bool_type)all_true;
CHECK_BATCH_EQ(b0, batch_bool_type(true));
CHECK_BATCH_EQ(b1, batch_bool_type(true));
}
void test_ops() const
{
constexpr auto all_true = xsimd::make_batch_bool_constant<value_type, arch_type, constant<true>>();
constexpr auto all_false = xsimd::make_batch_bool_constant<value_type, arch_type, constant<false>>();
constexpr auto x = xsimd::make_batch_bool_constant<value_type, arch_type, split>();
constexpr auto y = xsimd::make_batch_bool_constant<value_type, arch_type, inv_split>();
constexpr auto x_or_y = x | y;
static_assert(std::is_same<decltype(x_or_y), decltype(all_true)>::value, "x | y == true");
constexpr auto x_lor_y = x || y;
static_assert(std::is_same<decltype(x_lor_y), decltype(all_true)>::value, "x || y == true");
constexpr auto x_and_y = x & y;
static_assert(std::is_same<decltype(x_and_y), decltype(all_false)>::value, "x & y == false");
constexpr auto x_land_y = x && y;
static_assert(std::is_same<decltype(x_land_y), decltype(all_false)>::value, "x && y == false");
constexpr auto x_xor_y = x ^ y;
static_assert(std::is_same<decltype(x_xor_y), decltype(all_true)>::value, "x ^ y == true");
constexpr auto not_x = !x;
static_assert(std::is_same<decltype(not_x), decltype(y)>::value, "!x == y");
constexpr auto inv_x = ~x;
static_assert(std::is_same<decltype(inv_x), decltype(y)>::value, "~x == y");
}
};
TEST_CASE_TEMPLATE("[constant bool batch]", B, BATCH_INT_TYPES)
{
constant_bool_batch_test<B> Test;
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
SUBCASE("as_batch") { Test.test_cast(); }
SUBCASE("init_from_generator_split")
{
Test.test_init_from_generator_split();
}
SUBCASE("operators")
{
Test.test_ops();
}
}
#endif
|