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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/***************************************************************************
* 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 <functional>
#include <vector>
#include "test_utils.hpp"
namespace xsimd
{
int popcount(int v)
{
// from https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
return c;
}
template <class T, std::size_t N>
struct get_bool_base
{
using vector_type = std::array<bool, N>;
std::vector<vector_type> almost_all_false()
{
std::vector<vector_type> vectors;
vectors.reserve(N);
for (size_t i = 0; i < N; ++i)
{
vector_type v;
v.fill(false);
v[i] = true;
vectors.push_back(std::move(v));
}
return vectors;
}
std::vector<vector_type> almost_all_true()
{
auto vectors = almost_all_false();
flip(vectors);
return vectors;
}
void flip(vector_type& vec)
{
std::transform(vec.begin(), vec.end(), vec.begin(), std::logical_not<bool> {});
}
void flip(std::vector<vector_type>& vectors)
{
for (auto& vec : vectors)
{
flip(vec);
}
}
};
template <class T, size_t N = T::size>
struct get_bool;
template <class T>
struct get_bool<batch_bool<T>, 1> : public get_bool_base<T, 1>
{
using type = batch_bool<T>;
type all_true = type(true);
type all_false = type(false);
type half = { 0 };
type ihalf = { 1 };
type interspersed = { 0 };
};
template <class T>
struct get_bool<batch_bool<T>, 2> : public get_bool_base<T, 2>
{
using type = batch_bool<T>;
type all_true = type(true);
type all_false = type(false);
type half = { 0, 1 };
type ihalf = { 1, 0 };
type interspersed = { 0, 1 };
};
template <class T>
struct get_bool<batch_bool<T>, 4> : public get_bool_base<T, 4>
{
using type = batch_bool<T>;
type all_true = true;
type all_false = false;
type half = { 0, 0, 1, 1 };
type ihalf = { 1, 1, 0, 0 };
type interspersed = { 0, 1, 0, 1 };
};
template <class T>
struct get_bool<batch_bool<T>, 8> : public get_bool_base<T, 8>
{
using type = batch_bool<T>;
type all_true = true;
type all_false = false;
type half = { 0, 0, 0, 0, 1, 1, 1, 1 };
type ihalf = { 1, 1, 1, 1, 0, 0, 0, 0 };
type interspersed = { 0, 1, 0, 1, 0, 1, 0, 1 };
};
template <class T>
struct get_bool<batch_bool<T>, 16> : public get_bool_base<T, 16>
{
using type = batch_bool<T>;
type all_true = true;
type all_false = false;
type half = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };
type ihalf = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
type interspersed = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
};
template <class T>
struct get_bool<batch_bool<T>, 32> : public get_bool_base<T, 32>
{
using type = batch_bool<T>;
type all_true = true;
type all_false = false;
type half = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
type ihalf = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
type interspersed = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
};
template <class T>
struct get_bool<batch_bool<T>, 64> : public get_bool_base<T, 64>
{
using type = batch_bool<T>;
type all_true = true;
type all_false = false;
type half = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
type ihalf = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
type interspersed = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
};
}
template <class B>
struct batch_bool_test
{
using batch_type = B;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
using batch_bool_type = typename B::batch_bool_type;
using array_type = std::array<value_type, size>;
using bool_array_type = std::array<bool, size>;
array_type lhs;
array_type rhs;
bool_array_type all_true;
bool_array_type ba;
batch_bool_test()
{
for (size_t i = 0; i < size; ++i)
{
lhs[i] = value_type(i);
rhs[i] = i == 0 % 2 ? lhs[i] : lhs[i] * value_type(2);
all_true[i] = true;
ba[i] = i == 0 % 2 ? true : false;
}
}
template <size_t... Is>
struct pack
{
};
template <size_t... Values>
void check_constructor_from_sequence(std::integral_constant<size_t, 0>, pack<Values...>) const
{
bool_array_type res = { bool(Values % 3)... };
bool_array_type tmp;
batch_bool_type b0(bool(Values % 3)...);
b0.store_unaligned(tmp.data());
INFO("batch_bool(values...)");
CHECK_EQ(tmp, res);
batch_bool_type b1 { bool(Values % 3)... };
b1.store_unaligned(tmp.data());
INFO("batch_bool{values...}");
CHECK_EQ(tmp, res);
}
template <size_t I, size_t... Values>
void check_constructor_from_sequence(std::integral_constant<size_t, I>, pack<Values...>) const
{
return check_constructor_from_sequence(std::integral_constant<size_t, I - 1>(), pack<Values..., I>());
}
void test_constructors() const
{
batch_bool_type a;
// value uninitialized, cannot test it.
(void)a;
bool_array_type res;
batch_bool_type b(true);
b.store_unaligned(res.data());
INFO("batch_bool{value}");
CHECK_EQ(res, all_true);
batch_bool_type c { true };
c.store_unaligned(res.data());
INFO("batch_bool{value}");
CHECK_EQ(res, all_true);
check_constructor_from_sequence(std::integral_constant<size_t, size>(), pack<>());
}
void test_load_store() const
{
bool_array_type res;
batch_bool_type b(batch_bool_type::load_unaligned(ba.data()));
b.store_unaligned(res.data());
CHECK_EQ(res, ba);
alignas(xsimd::default_arch::alignment()) bool_array_type arhs(this->ba);
alignas(xsimd::default_arch::alignment()) bool_array_type ares;
b = batch_bool_type::load_aligned(arhs.data());
b.store_aligned(ares.data());
CHECK_EQ(ares, arhs);
}
void test_any_all() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
// any
{
auto any_check_false = (batch_lhs() != batch_lhs());
bool any_res_false = xsimd::any(any_check_false);
CHECK_FALSE(any_res_false);
auto any_check_true = (batch_lhs() == batch_rhs());
bool any_res_true = xsimd::any(any_check_true);
CHECK_UNARY(any_res_true);
for (const auto& vec : bool_g.almost_all_false())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool any_res = xsimd::any(b);
CHECK_UNARY(any_res);
}
for (const auto& vec : bool_g.almost_all_true())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool any_res = xsimd::any(b);
CHECK_UNARY(any_res);
}
}
// all
{
auto all_check_false = (batch_lhs() == batch_rhs());
bool all_res_false = xsimd::all(all_check_false);
CHECK_FALSE(all_res_false);
auto all_check_true = (batch_lhs() == batch_lhs());
bool all_res_true = xsimd::all(all_check_true);
CHECK_UNARY(all_res_true);
for (const auto& vec : bool_g.almost_all_false())
{
// TODO: implement batch_bool(bool*)
// It currently compiles (need to understand why) but does not
// give expected result
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool all_res = xsimd::all(b);
CHECK_FALSE(all_res);
}
for (const auto& vec : bool_g.almost_all_true())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool all_res = xsimd::all(b);
CHECK_FALSE(all_res);
}
}
// none
{
auto none_check_false = (batch_lhs() == batch_rhs());
bool none_res_false = xsimd::none(none_check_false);
CHECK_FALSE(none_res_false);
auto none_check_true = (batch_lhs() != batch_lhs());
bool none_res_true = xsimd::none(none_check_true);
CHECK_UNARY(none_res_true);
for (const auto& vec : bool_g.almost_all_false())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool none_res = xsimd::none(b);
CHECK_FALSE(none_res);
}
for (const auto& vec : bool_g.almost_all_true())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
bool none_res = xsimd::none(b);
CHECK_FALSE(none_res);
}
}
}
void test_logical_operations() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
size_t s = size;
// operator!=
{
bool res = xsimd::all(bool_g.half != bool_g.ihalf);
CHECK_UNARY(res);
}
// operator==
{
CHECK_BATCH_EQ(bool_g.half, !bool_g.ihalf);
}
// operator &&
{
batch_bool_type res = bool_g.half && bool_g.ihalf;
bool_array_type ares;
res.store_unaligned(ares.data());
size_t nb_false = std::count(ares.cbegin(), ares.cend(), false);
CHECK_EQ(nb_false, s);
}
// operator ||
{
batch_bool_type res = bool_g.half || bool_g.ihalf;
bool_array_type ares;
res.store_unaligned(ares.data());
size_t nb_true = std::count(ares.cbegin(), ares.cend(), true);
CHECK_EQ(nb_true, s);
}
// operator ^
{
batch_bool_type res = bool_g.half ^ bool_g.ihalf;
bool_array_type ares;
res.store_unaligned(ares.data());
size_t nb_true = std::count(ares.cbegin(), ares.cend(), true);
CHECK_EQ(nb_true, s);
}
// bitwise_andnot
{
batch_bool_type res = xsimd::bitwise_andnot(bool_g.half, bool_g.half);
bool_array_type ares;
res.store_unaligned(ares.data());
size_t nb_false = std::count(ares.cbegin(), ares.cend(), false);
CHECK_EQ(nb_false, s);
}
}
void test_bitwise_operations() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
// operator version
{
INFO("operator~");
CHECK_BATCH_EQ(bool_g.half, ~bool_g.ihalf);
}
{
INFO("operator|");
CHECK_BATCH_EQ(bool_g.half | bool_g.ihalf, bool_g.all_true);
}
{
INFO("operator&");
CHECK_BATCH_EQ(bool_g.half & bool_g.ihalf, bool_g.all_false);
}
// free function version
{
INFO("bitwise_not");
CHECK_BATCH_EQ(bool_g.half, xsimd::bitwise_not(bool_g.ihalf));
}
{
INFO("bitwise_or");
CHECK_BATCH_EQ(xsimd::bitwise_or(bool_g.half, bool_g.ihalf), bool_g.all_true);
}
{
INFO("bitwise_and");
CHECK_BATCH_EQ(xsimd::bitwise_and(bool_g.half, bool_g.ihalf), bool_g.all_false);
}
}
void test_update_operations() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
{
auto tmp = bool_g.half;
tmp |= bool_g.ihalf;
bool res = xsimd::all(tmp);
INFO("operator|=");
CHECK_UNARY(res);
}
{
auto tmp = bool_g.half;
tmp &= bool_g.half;
INFO("operator&=");
CHECK_BATCH_EQ(tmp, bool_g.half);
}
{
auto tmp = bool_g.half;
tmp ^= bool_g.ihalf;
bool res = xsimd::all(tmp);
INFO("operator^=");
CHECK_UNARY(res);
}
}
void test_mask() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
const uint64_t full_mask = ((uint64_t)-1) >> (64 - batch_bool_type::size);
CHECK_EQ(bool_g.all_false.mask(), 0);
CHECK_EQ(batch_bool_type::from_mask(bool_g.all_false.mask()).mask(), bool_g.all_false.mask());
CHECK_EQ(bool_g.all_true.mask(), full_mask);
CHECK_EQ(batch_bool_type::from_mask(bool_g.all_true.mask()).mask(), bool_g.all_true.mask());
CHECK_EQ(bool_g.half.mask(), full_mask & ((uint64_t)-1) << (batch_bool_type::size / 2));
CHECK_EQ(batch_bool_type::from_mask(bool_g.half.mask()).mask(), bool_g.half.mask());
CHECK_EQ(bool_g.ihalf.mask(), full_mask & ~(((uint64_t)-1) << (batch_bool_type::size / 2)));
CHECK_EQ(batch_bool_type::from_mask(bool_g.ihalf.mask()).mask(), bool_g.ihalf.mask());
CHECK_EQ(bool_g.interspersed.mask(), full_mask & 0xAAAAAAAAAAAAAAAAul);
CHECK_EQ(batch_bool_type::from_mask(bool_g.interspersed.mask()).mask(), bool_g.interspersed.mask());
}
void test_count() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
CHECK_EQ(count(bool_g.all_false), 0);
CHECK_EQ(count(bool_g.all_true), batch_bool_type::size);
CHECK_EQ(count(bool_g.half), batch_bool_type::size / 2);
}
void test_comparison() const
{
auto bool_g = xsimd::get_bool<batch_bool_type> {};
// eq
{
CHECK_BATCH_EQ(bool_g.half, !bool_g.ihalf);
CHECK_BATCH_EQ(xsimd::eq(bool_g.half, !bool_g.ihalf), bool_g.all_true);
}
// neq
{
CHECK_BATCH_EQ(xsimd::neq(bool_g.half, bool_g.ihalf), bool_g.all_true);
}
}
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());
}
};
TEST_CASE_TEMPLATE("[xsimd batch bool]", B, BATCH_TYPES)
{
batch_bool_test<B> Test;
SUBCASE("constructors") { Test.test_constructors(); }
SUBCASE("load store") { Test.test_load_store(); }
SUBCASE("any all") { Test.test_any_all(); }
SUBCASE("logical operations") { Test.test_logical_operations(); }
SUBCASE("bitwise operations") { Test.test_bitwise_operations(); }
SUBCASE("update operations") { Test.test_update_operations(); }
SUBCASE("mask") { Test.test_mask(); }
SUBCASE("count") { Test.test_count(); }
SUBCASE("eq neq") { Test.test_comparison(); }
}
#endif
|