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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/***************************************************************************
* 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"
#include <climits>
namespace xsimd
{
template <class T, std::size_t N = T::size>
struct test_int_min_max
{
bool run()
{
return true;
}
};
template <class T>
struct test_int_min_max<batch<T>, 2>
{
void run()
{
using B = batch<T>;
using BB = batch_bool<T>;
using A = std::array<T, 2>;
T max = std::numeric_limits<T>::max();
T min = std::numeric_limits<T>::min();
std::array<T, 2> maxmin_cmp { { max, min } };
B maxmin = { max, min };
INFO("numeric max and min");
CHECK_BATCH_EQ(maxmin, maxmin_cmp);
B a = { 1, 3 };
B b(2);
B c = { 2, 3 };
auto r1 = xsimd::max(a, c);
auto r3 = xsimd::min(a, c);
INFO("max");
CHECK_BATCH_EQ(r1, (A { { 2, 3 } }));
INFO("min");
CHECK_BATCH_EQ(r3, (A { { 1, 3 } }));
auto r4 = a < b; // test lt
BB e4 = { 1, 0 };
CHECK_UNARY(xsimd::all(r4 == e4));
}
};
template <class T>
struct test_int_min_max<batch<T>, 4>
{
void run()
{
using B = batch<T>;
using BB = batch_bool<T>;
using A = std::array<T, 4>;
B a = { 1, 3, 1, 1 };
B b(2);
B c = { 2, 3, 2, 3 };
auto r1 = xsimd::max(a, c);
auto r3 = xsimd::min(a, c);
INFO("max");
CHECK_BATCH_EQ(r1, (A { { 2, 3, 2, 3 } }));
INFO("min");
CHECK_BATCH_EQ(r3, (A { { 1, 3, 1, 1 } }));
auto r4 = a < b; // test lt
BB e4 = { 1, 0, 1, 1 };
CHECK_UNARY(xsimd::all(r4 == e4));
}
};
template <class T>
struct test_int_min_max<batch<T>, 8>
{
void run()
{
using B = batch<T>;
using BB = batch_bool<T>;
using A = std::array<T, 8>;
T max = std::numeric_limits<T>::max();
T min = std::numeric_limits<T>::min();
std::array<T, 8> maxmin_cmp { { 0, 0, max, 0, min, 0, 0, 0 } };
B maxmin = { 0, 0, max, 0, min, 0, 0, 0 };
INFO("numeric max and min");
CHECK_BATCH_EQ(maxmin, maxmin_cmp);
B a { 1, 3, 1, 3, 1, 1, 3, 3 };
B b { 2 };
B c { 2, 3, 2, 3, 2, 3, 2, 3 };
auto r1 = xsimd::max(a, c);
auto r3 = xsimd::min(a, c);
auto r4 = a < b; // test lt
INFO("max");
CHECK_BATCH_EQ(r1, (A { { 2, 3, 2, 3, 2, 3, 3, 3 } }));
INFO("min");
CHECK_BATCH_EQ(r3, (A { { 1, 3, 1, 3, 1, 1, 2, 3 } }));
BB e4 = { 1, 0, 1, 0, 1, 1, 0, 0 };
CHECK_UNARY(xsimd::all(r4 == e4));
}
};
template <class T>
struct test_int_min_max<batch<T>, 16>
{
void run()
{
using B = batch<T>;
using BB = batch_bool<T>;
using A = std::array<T, 16>;
T max = std::numeric_limits<T>::max();
T min = std::numeric_limits<T>::min();
std::array<T, 16> maxmin_cmp { { 0, 0, max, 0, min, 0, 0, 0, 0, 0, max, 0, min, 0, 0, 0 } };
B maxmin = { 0, 0, max, 0, min, 0, 0, 0, 0, 0, max, 0, min, 0, 0, 0 };
INFO("numeric max and min");
CHECK_BATCH_EQ(maxmin, maxmin_cmp);
B a = { 1, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, min, max, max, min };
B b(2);
B c = { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3 };
auto r1 = xsimd::max(a, b);
auto r3 = xsimd::min(a, b);
auto r4 = a < b; // test lt
auto r5 = a == c;
auto r6 = a != c;
INFO("max");
CHECK_BATCH_EQ(r1, (A { { 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 2, max, max, 2 } }));
INFO("min");
CHECK_BATCH_EQ(r3, (A { { 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, min, 2, 2, min } }));
BB e4 = { 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1 };
CHECK_UNARY(xsimd::all(r4 == e4));
BB e5 = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 };
CHECK_UNARY(xsimd::all(r5 == e5));
CHECK_UNARY(xsimd::all(r6 == !e5));
}
};
template <class T>
struct test_int_min_max<batch<T>, 32>
{
void run()
{
using B = batch<T>;
using BB = batch_bool<T>;
using A = std::array<T, 32>;
T max = std::numeric_limits<T>::max();
T min = std::numeric_limits<T>::min();
B a = { 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, min, max, max, min };
B b = 2;
auto r1 = xsimd::max(a, b);
auto r3 = xsimd::min(a, b);
auto r4 = a < b; // test lt
INFO("max");
CHECK_BATCH_EQ(r1, (A { { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 2, max, max, 2 } }));
INFO("min");
CHECK_BATCH_EQ(r3, (A { { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, min, 2, 2, min } }));
BB e4 = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1 };
CHECK_UNARY(xsimd::all(r4 == e4));
}
};
}
template <class B>
struct batch_int_test
{
using batch_type = B;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
using array_type = std::array<value_type, size>;
using bool_array_type = std::array<bool, size>;
array_type lhs;
array_type rhs;
array_type shift;
batch_int_test()
{
using signed_value_type = typename std::make_signed<value_type>::type;
for (size_t i = 0; i < size; ++i)
{
bool negative_lhs = std::is_signed<value_type>::value && (i % 2 == 1);
lhs[i] = value_type(i) * (negative_lhs ? -10 : 10);
if (lhs[i] == value_type(0))
{
lhs[i] += value_type(1);
}
rhs[i] = value_type(i) + value_type(4);
shift[i] = signed_value_type(i) % (CHAR_BIT * sizeof(value_type));
}
}
void test_modulo() const
{
// batch % batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return l % r; });
batch_type res = batch_lhs() % batch_rhs();
INFO("batch % batch");
CHECK_BATCH_EQ(res, expected);
}
}
void test_shift() const
{
int32_t nb_sh = 3;
// batch << scalar
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(),
[nb_sh](const value_type& v)
{ return v << nb_sh; });
batch_type res = batch_lhs() << nb_sh;
INFO("batch << scalar");
CHECK_BATCH_EQ(res, expected);
}
// batch << batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), shift.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return l << r; });
batch_type res = batch_lhs() << batch_shift();
INFO("batch << batch");
CHECK_BATCH_EQ(res, expected);
}
// batch >> scalar
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(),
[nb_sh](const value_type& v)
{ return v >> nb_sh; });
batch_type res = batch_lhs() >> nb_sh;
INFO("batch >> scalar");
CHECK_BATCH_EQ(res, expected);
}
// batch >> batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), shift.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return l >> r; });
batch_type res = batch_lhs() >> batch_shift();
INFO("batch >> batch");
CHECK_BATCH_EQ(res, expected);
}
}
void test_more_shift() const
{
int32_t s = static_cast<int32_t>(sizeof(value_type) * 8);
batch_type lhs = batch_type(value_type(1));
batch_type res;
for (int32_t i = 0; i < s; ++i)
{
res = lhs << i;
value_type expected = value_type(1) << i;
for (std::size_t j = 0; j < size; ++j)
{
CHECK_EQ(res.get(j), expected);
}
}
lhs = batch_type(std::numeric_limits<value_type>::max());
for (int32_t i = 0; i < s; ++i)
{
res = lhs >> value_type(i);
value_type expected = std::numeric_limits<value_type>::max() >> i;
for (std::size_t j = 0; j < size; ++j)
{
CHECK_EQ(res.get(j), expected);
}
}
}
void test_min_max() const
{
xsimd::test_int_min_max<batch_type> t;
t.run();
}
void test_less_than_underflow() const
{
batch_type test_negative_compare = batch_type(5) - 6;
if (std::is_unsigned<value_type>::value)
{
CHECK_FALSE(xsimd::any(test_negative_compare < 1));
}
else
{
CHECK_UNARY(xsimd::all(test_negative_compare < 1));
}
}
private:
batch_type batch_lhs() const
{
return batch_type::load_unaligned(lhs.data());
}
batch_type batch_rhs() const
{
return batch_type::load_unaligned(rhs.data());
}
batch_type batch_shift() const
{
return batch_type::load_unaligned(shift.data());
}
};
TEST_CASE_TEMPLATE("[batch int tests]", B, BATCH_INT_TYPES)
{
batch_int_test<B> Test;
SUBCASE("modulo")
{
Test.test_modulo();
}
SUBCASE("shift")
{
Test.test_shift();
}
SUBCASE("more_shift")
{
Test.test_more_shift();
}
SUBCASE("min_max")
{
Test.test_min_max();
}
SUBCASE("less_than_underflow")
{
Test.test_less_than_underflow();
}
}
#endif
|