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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/***************************************************************************
* 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 <array>
#include <functional>
#include <type_traits>
#include <vector>
#include "test_utils.hpp"
namespace xsimd
{
namespace test_detail
{
template <class T, std::size_t N>
struct ct_mask_arch
{
static constexpr bool supported() noexcept { return true; }
static constexpr bool available() noexcept { return true; }
static constexpr std::size_t alignment() noexcept { return 0; }
static constexpr bool requires_alignment() noexcept { return false; }
static constexpr char const* name() noexcept { return "ct_mask_arch"; }
};
template <class T, std::size_t N>
struct ct_mask_register
{
std::array<T, N> data {};
};
struct mask_all_false
{
static constexpr bool get(std::size_t, std::size_t) { return false; }
};
struct mask_all_true
{
static constexpr bool get(std::size_t, std::size_t) { return true; }
};
struct mask_prefix1
{
static constexpr bool get(std::size_t i, std::size_t) { return i < 1; }
};
struct mask_suffix1
{
static constexpr bool get(std::size_t i, std::size_t n) { return i >= (n - 1); }
};
struct mask_ends
{
static constexpr bool get(std::size_t i, std::size_t n)
{
return (i < 1) || (i >= (n - 1));
}
};
struct mask_interleaved
{
static constexpr bool get(std::size_t i, std::size_t) { return (i % 2) == 0; }
};
template <class T>
struct alternating_numeric
{
static constexpr T get(std::size_t i, std::size_t)
{
return (i % 2) ? T(2) : T(1);
}
};
}
namespace types
{
template <class T, std::size_t N>
struct simd_register<T, test_detail::ct_mask_arch<T, N>>
{
using register_type = test_detail::ct_mask_register<T, N>;
register_type data;
constexpr operator register_type() const noexcept { return data; }
};
template <class T, std::size_t N>
struct has_simd_register<T, test_detail::ct_mask_arch<T, N>> : std::true_type
{
};
}
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 T>
struct batch_bool_test
{
using batch_type = T;
using value_type = typename T::value_type;
static constexpr size_t size = T::size;
using batch_bool_type = typename T::batch_bool_type;
using array_type = std::array<value_type, size>;
using bool_array_type = std::array<bool, size>;
// Compile-time check helpers for batch_bool_constant masks
template <class B, class Enable = void>
struct xsimd_ct_mask_checker;
// Small masks: safe to compare numeric masks at compile time
template <class B>
struct xsimd_ct_mask_checker<B, typename std::enable_if<(B::size <= 31)>::type>
{
static constexpr std::size_t sum_indices(uint64_t bits, std::size_t index, std::size_t remaining)
{
return remaining == 0
? 0u
: ((bits & 1u ? index : 0u) + sum_indices(bits >> 1, index + 1, remaining - 1));
}
static constexpr uint32_t low_mask_bits(std::size_t width)
{
return width == 0 ? 0u : (static_cast<uint32_t>(1u << width) - 1u);
}
template <class Mask, class ValueType, bool Enable>
struct splice_checker
{
static void run()
{
}
};
template <class Mask, class ValueType>
struct splice_checker<Mask, ValueType, true>
{
static void run()
{
constexpr std::size_t begin = 1;
constexpr std::size_t end = (Mask::size > 3 ? 3 : Mask::size);
constexpr std::size_t length = (end > begin) ? (end - begin) : 0;
using slice_arch = xsimd::test_detail::ct_mask_arch<ValueType, length>;
constexpr auto slice = xsimd::detail::splice<slice_arch, begin, end>(Mask {});
constexpr uint32_t src_mask = static_cast<uint32_t>(Mask::mask());
constexpr uint32_t expected = (src_mask >> begin) & low_mask_bits(length);
static_assert(static_cast<uint32_t>(slice.mask()) == expected, "splice mask expected");
constexpr uint32_t slice_bits = static_cast<uint32_t>(slice.mask());
constexpr uint32_t shifted_source = src_mask >> begin;
static_assert((length == 0) || ((slice_bits & 1u) == (shifted_source & 1u)), "slice first bit matches");
static_assert((length <= 1) || (((slice_bits >> (length - 1)) & 1u) == ((shifted_source >> (length - 1)) & 1u)),
"slice last bit matches");
}
};
template <class Mask, class ValueType, bool Enable>
struct half_checker
{
static void run()
{
}
};
template <class Mask, class ValueType>
struct half_checker<Mask, ValueType, true>
{
static void run()
{
constexpr std::size_t total = Mask::size;
constexpr std::size_t mid = total / 2;
using lower_arch = xsimd::test_detail::ct_mask_arch<ValueType, mid>;
using upper_arch = xsimd::test_detail::ct_mask_arch<ValueType, total - mid>;
constexpr auto lower = xsimd::detail::lower_half<lower_arch>(Mask {});
constexpr auto upper = xsimd::detail::upper_half<upper_arch>(Mask {});
constexpr uint32_t source_mask = static_cast<uint32_t>(Mask::mask());
static_assert(static_cast<uint32_t>(lower.mask()) == (source_mask & low_mask_bits(mid)),
"lower_half mask matches");
static_assert(static_cast<uint32_t>(upper.mask()) == ((source_mask >> mid) & low_mask_bits(total - mid)),
"upper_half mask matches");
constexpr auto lower_splice = xsimd::detail::splice<lower_arch, 0, mid>(Mask {});
constexpr auto upper_splice = xsimd::detail::splice<upper_arch, mid, total>(Mask {});
static_assert(lower.mask() == lower_splice.mask(), "lower_half equals splice");
static_assert(upper.mask() == upper_splice.mask(), "upper_half equals splice");
constexpr uint32_t lower_bits = static_cast<uint32_t>(lower.mask());
constexpr uint32_t upper_bits = static_cast<uint32_t>(upper.mask());
constexpr std::size_t upper_size = decltype(upper)::size;
static_assert((mid == 0) || ((lower_bits & 1u) == (source_mask & 1u)), "lower first element");
static_assert((mid <= 1) || (((lower_bits >> (mid - 1)) & 1u) == ((source_mask >> (mid - 1)) & 1u)),
"lower last element");
static_assert((upper_size == 0) || ((upper_bits & 1u) == ((source_mask >> mid) & 1u)),
"upper first element");
static_assert((upper_size <= 1) || (((upper_bits >> (upper_size - 1)) & 1u) == ((source_mask >> (total - 1)) & 1u)),
"upper last element");
}
};
static void run()
{
using value_type = typename B::value_type;
using arch_type = typename B::arch_type;
constexpr auto m_zero = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_all_false, arch_type>();
constexpr auto m_one = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_all_true, arch_type>();
constexpr auto m_prefix = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_prefix1, arch_type>();
constexpr auto m_suffix = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_suffix1, arch_type>();
constexpr auto m_ends = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_ends, arch_type>();
constexpr auto m_interleaved = xsimd::make_batch_bool_constant<value_type, xsimd::test_detail::mask_interleaved, arch_type>();
static_assert((m_zero | m_one).mask() == m_one.mask(), "0|1 == 1");
static_assert((m_zero & m_one).mask() == m_zero.mask(), "0&1 == 0");
static_assert((m_zero ^ m_zero).mask() == m_zero.mask(), "0^0 == 0");
static_assert((m_one ^ m_one).mask() == m_zero.mask(), "1^1 == 0");
static_assert((!m_zero).mask() == m_one.mask(), "!0 == 1");
static_assert((~m_zero).mask() == m_one.mask(), "~0 == 1");
static_assert((!m_one).mask() == m_zero.mask(), "!1 == 0");
static_assert((~m_one).mask() == m_zero.mask(), "~1 == 0");
static_assert(((m_prefix && m_suffix).mask()) == (m_prefix & m_suffix).mask(), "&& consistent");
static_assert(((m_prefix || m_suffix).mask()) == (m_prefix | m_suffix).mask(), "|| consistent");
static_assert((m_prefix | m_suffix).mask() == m_ends.mask(), "prefix|suffix == ends");
static_assert(B::size == 1 || (m_prefix & m_suffix).mask() == m_zero.mask(), "prefix&suffix == 0 when size>1");
static_assert(m_zero.none(), "zero mask none");
static_assert(!m_zero.any(), "zero mask any");
static_assert(!m_zero.all(), "zero mask all");
static_assert(m_zero.countr_zero() == B::size, "zero mask trailing zeros");
static_assert(m_zero.countl_zero() == B::size, "zero mask leading zeros");
static_assert(m_one.all(), "all mask all");
static_assert(m_one.any(), "all mask any");
static_assert(!m_one.none(), "all mask none");
static_assert(m_one.countr_zero() == 0, "all mask trailing zeros");
static_assert(m_one.countl_zero() == 0, "all mask leading zeros");
constexpr auto prefix_bits = static_cast<uint32_t>(m_prefix.mask());
constexpr auto suffix_bits = static_cast<uint32_t>(m_suffix.mask());
constexpr auto ends_bits_mask = static_cast<uint32_t>(m_ends.mask());
static_assert((B::size == 0) || ((prefix_bits & 1u) != 0u), "prefix first element set");
static_assert((B::size <= 1) || ((prefix_bits & (1u << 1)) == 0u), "prefix second element cleared");
static_assert((B::size == 0) || (((suffix_bits >> (B::size - 1)) & 1u) != 0u), "suffix last element set");
static_assert((B::size <= 1) || ((suffix_bits & 1u) == 0u), "suffix first element cleared");
static_assert((B::size == 0) || ((ends_bits_mask & 1u) != 0u), "ends first element set");
static_assert((B::size == 0) || (((ends_bits_mask >> (B::size - 1)) & 1u) != 0u), "ends last element set");
static_assert((B::size <= 2) || (((ends_bits_mask >> 1) & 1u) == 0u), "ends interior element cleared");
static_assert(std::is_same<decltype(m_prefix.as_batch_bool()), typename B::batch_bool_type>::value,
"as_batch_bool type");
static_assert(std::is_same<decltype(static_cast<typename B::batch_bool_type>(m_prefix)), typename B::batch_bool_type>::value,
"conversion operator type");
// splice API is validated indirectly via arch-specific masked implementations.
constexpr std::size_t prefix_zero = m_prefix.countr_zero();
constexpr std::size_t prefix_one = m_prefix.countr_one();
static_assert(prefix_zero == 0, "prefix mask zero leading zeros from LSB");
static_assert((B::size == 0 ? prefix_one == 0 : prefix_one == 1), "prefix mask trailing ones count");
constexpr std::size_t suffix_zero = m_suffix.countl_zero();
constexpr std::size_t suffix_one = m_suffix.countl_one();
static_assert(suffix_zero == 0, "suffix mask leading zeros count");
static_assert((B::size == 0 ? suffix_one == 0 : suffix_one == 1), "suffix mask trailing ones count");
splice_checker<decltype(m_interleaved), value_type, (B::size > 1)>::run();
half_checker<decltype(m_ends), value_type, (B::size > 0 && (B::size % 2 == 0))>::run();
}
};
// Large masks: avoid calling mask() in constant expressions
template <class B>
struct xsimd_ct_mask_checker<B, typename std::enable_if<(B::size > 31)>::type>
{
static void run() { }
};
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 <typename F, size_t... Values>
static batch_bool_type make_batch_impl(F&& f, std::integral_constant<size_t, 0>, pack<Values...>)
{
return batch_bool_type(bool(f(Values))...);
}
template <typename F, size_t I, size_t... Values>
static batch_bool_type make_batch_impl(F&& f, std::integral_constant<size_t, I>, pack<Values...>)
{
return make_batch_impl(std::forward<F>(f), std::integral_constant<size_t, I - 1>(), pack<I - 1, Values...>());
}
template <typename F>
static batch_bool_type make_batch(F&& f)
{
return make_batch_impl(std::forward<F>(f), std::integral_constant<size_t, size>(), pack<> {});
}
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);
}
{
auto f_bool = [](size_t i)
{ return bool(i % 3); };
bool_array_type res;
for (size_t i = 0; i < res.size(); i++)
{
res[i] = f_bool(i);
}
bool_array_type tmp;
batch_bool_type b0 = make_batch(f_bool);
b0.store_unaligned(tmp.data());
INFO("batch_bool(values...)");
CHECK_EQ(tmp, res);
batch_bool_type b1 = make_batch(f_bool);
b1.store_unaligned(tmp.data());
INFO("batch_bool{values...}");
CHECK_EQ(tmp, res);
}
}
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);
auto bool_g = xsimd::get_bool<batch_bool_type> {};
// load/store, almost all false
{
size_t i = 0;
for (const auto& vec : bool_g.almost_all_false())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
batch_bool_type expected = make_batch([i](size_t x)
{ return x == i; });
i++;
CHECK_UNARY(xsimd::all(b == expected));
b.store_unaligned(res.data());
// Check that the representation is bitwise exact.
CHECK_UNARY(memcmp(res.data(), vec.data(), sizeof(res)) == 0);
}
}
// load/store, almost all true
{
size_t i = 0;
for (const auto& vec : bool_g.almost_all_true())
{
batch_bool_type b = batch_bool_type::load_unaligned(vec.data());
batch_bool_type expected = make_batch([i](size_t x)
{ return x != i; });
i++;
CHECK_UNARY(xsimd::all(b == expected));
b.store_unaligned(res.data());
CHECK_UNARY(memcmp(res.data(), vec.data(), sizeof(res)) == 0);
}
}
}
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);
}
{
INFO("operator^");
CHECK_BATCH_EQ(bool_g.half ^ bool_g.all_true, bool_g.ihalf);
}
// 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);
}
{
INFO("bitwise_xor");
CHECK_BATCH_EQ(xsimd::bitwise_xor(bool_g.half, bool_g.all_true), bool_g.ihalf);
}
}
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);
CHECK_BATCH_EQ(xsimd::neq(bool_g.all_true, bool_g.all_true), bool_g.all_false);
}
}
void test_mask_compile_time() const
{
xsimd_ct_mask_checker<T>::run();
}
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(); }
SUBCASE("mask utils (compile-time)") { Test.test_mask_compile_time(); }
}
#endif
|