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 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
// clang-format off
template <class>
struct type_helper;
#define DEFINE_TYPE_NAME(T) \
template <> struct type_helper<T> { static char const * name() { return #T; } };
DEFINE_TYPE_NAME(unsigned char)
DEFINE_TYPE_NAME(unsigned short)
DEFINE_TYPE_NAME(unsigned int)
DEFINE_TYPE_NAME(unsigned long)
DEFINE_TYPE_NAME(unsigned long long)
DEFINE_TYPE_NAME(char)
DEFINE_TYPE_NAME(short)
DEFINE_TYPE_NAME(int)
DEFINE_TYPE_NAME(long)
DEFINE_TYPE_NAME(long long)
#undef DEFINE_TYPE_NAME
// clang-format on
#define DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(FUNC) \
struct BitManipFunction_##FUNC { \
template <class T> \
static KOKKOS_FUNCTION auto eval_constexpr(T x) { \
return Kokkos::FUNC(x); \
} \
template <class T> \
static KOKKOS_FUNCTION auto eval_builtin(T x) { \
return Kokkos::Experimental::FUNC##_builtin(x); \
} \
static char const* name() { return #FUNC; } \
}
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(countl_zero);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(countl_one);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(countr_zero);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(countr_one);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(popcount);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(has_single_bit);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(bit_ceil);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(bit_floor);
DEFINE_BIT_MANIPULATION_FUNCTION_EVAL(bit_width);
#undef DEFINE_BIT_MANIPULATION_FUNCTION_EVAL
template <class Space, class Func, class Arg, std::size_t N>
struct TestBitManipFunction {
Arg val_[N];
TestBitManipFunction(const Arg (&val)[N]) {
std::copy(val, val + N, val_);
run();
}
void run() const {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, N), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for " << Func::name() << "("
<< type_helper<Arg>::name() << ")";
}
KOKKOS_FUNCTION void operator()(int i, int& e) const {
if (Func::eval_builtin(val_[i]) != Func::eval_constexpr(val_[i])) {
++e;
Kokkos::printf("value at %x which is %d was expected to be %d\n",
(unsigned)val_[i], (int)Func::eval_builtin(val_[i]),
(int)Func::eval_constexpr(val_[i]));
}
}
};
template <class Space, class... Func, class Arg, std::size_t N>
void do_test_bit_manip_function(const Arg (&x)[N]) {
(void)std::initializer_list<int>{
(TestBitManipFunction<Space, Func, Arg, N>(x), 0)...};
}
#define TEST_BIT_MANIP_FUNCTION(FUNC) \
do_test_bit_manip_function<TEST_EXECSPACE, BitManipFunction_##FUNC>
template <class UInt>
void test_bit_manip_countl_zero() {
using Kokkos::Experimental::countl_zero_builtin;
static_assert(noexcept(countl_zero_builtin(UInt())));
static_assert(std::is_same_v<decltype(countl_zero_builtin(UInt())), int>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(countl_zero)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(127),
UInt(128),
UInt(max),
});
}
TEST(TEST_CATEGORY, bit_manip_countl_zero) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_countl_zero<unsigned char>();
test_bit_manip_countl_zero<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_countl_zero<unsigned int>();
test_bit_manip_countl_zero<unsigned long>();
test_bit_manip_countl_zero<unsigned long long>();
}
template <class UInt>
void test_bit_manip_countl_one() {
using Kokkos::Experimental::countl_one_builtin;
static_assert(noexcept(countl_one_builtin(UInt())));
static_assert(std::is_same_v<decltype(countl_one_builtin(UInt())), int>);
constexpr auto dig = Kokkos::Experimental::digits_v<UInt>;
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(countl_one)
({
// clang-format off
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(100),
UInt(127),
UInt(128),
UInt(max),
UInt(max - 1),
UInt(max - 2),
UInt(max - 3),
UInt(max - 4),
UInt(max - 5),
UInt(max - 6),
UInt(max - 7),
UInt(max - 8),
UInt(max - 9),
UInt(max - 126),
UInt(max - 127),
UInt(max - 128),
UInt(UInt(1) << (dig - 1)),
UInt(UInt(3) << (dig - 2)),
UInt(UInt(7) << (dig - 3)),
UInt(UInt(255) << (dig - 8)),
// clang-format on
});
}
TEST(TEST_CATEGORY, bit_manip_countl_one) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_countl_one<unsigned char>();
test_bit_manip_countl_one<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_countl_one<unsigned int>();
test_bit_manip_countl_one<unsigned long>();
test_bit_manip_countl_one<unsigned long long>();
}
template <class UInt>
void test_bit_manip_countr_zero() {
using Kokkos::Experimental::countr_zero_builtin;
static_assert(noexcept(countr_zero_builtin(UInt())));
static_assert(std::is_same_v<decltype(countr_zero_builtin(UInt())), int>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(countr_zero)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(126),
UInt(127),
UInt(128),
UInt(129),
UInt(130),
UInt(max),
});
}
TEST(TEST_CATEGORY, bit_manip_countr_zero) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
#if defined(KOKKOS_ENABLE_SYCL) && \
!defined(KOKKOS_ARCH_INTEL_GPU) // FIXME_SYCL returns wrong result
if (!std::is_same_v<TEST_EXECSPACE, Kokkos::SYCL>)
#endif
test_bit_manip_countr_zero<unsigned char>();
test_bit_manip_countr_zero<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_countr_zero<unsigned int>();
test_bit_manip_countr_zero<unsigned long>();
test_bit_manip_countr_zero<unsigned long long>();
}
template <class UInt>
void test_bit_manip_countr_one() {
using Kokkos::Experimental::countr_one_builtin;
static_assert(noexcept(countr_one_builtin(UInt())));
static_assert(std::is_same_v<decltype(countr_one_builtin(UInt())), int>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(countr_one)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(126),
UInt(127),
UInt(128),
UInt(max - 1),
UInt(max),
});
}
TEST(TEST_CATEGORY, bit_manip_countr_one) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
#if defined(KOKKOS_ENABLE_SYCL) && \
!defined(KOKKOS_ARCH_INTEL_GPU) // FIXME_SYCL returns wrong result
if (!std::is_same_v<TEST_EXECSPACE, Kokkos::SYCL>)
#endif
test_bit_manip_countr_one<unsigned char>();
test_bit_manip_countr_one<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_countr_one<unsigned int>();
test_bit_manip_countr_one<unsigned long>();
test_bit_manip_countr_one<unsigned long long>();
}
template <class UInt>
void test_bit_manip_popcount() {
using Kokkos::Experimental::popcount_builtin;
static_assert(noexcept(popcount_builtin(UInt())));
static_assert(std::is_same_v<decltype(popcount_builtin(UInt())), int>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(popcount)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(127),
UInt(max),
UInt(max - 1),
});
}
TEST(TEST_CATEGORY, bit_manip_popcount) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_popcount<unsigned char>();
test_bit_manip_popcount<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_popcount<unsigned int>();
test_bit_manip_popcount<unsigned long>();
test_bit_manip_popcount<unsigned long long>();
}
template <class UInt>
void test_bit_manip_has_single_bit() {
using Kokkos::Experimental::has_single_bit_builtin;
static_assert(noexcept(has_single_bit_builtin(UInt())));
static_assert(std::is_same_v<decltype(has_single_bit_builtin(UInt())), bool>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
constexpr UInt one = 1;
TEST_BIT_MANIP_FUNCTION(has_single_bit)
({
// clang-format off
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(max),
UInt(one << 0),
UInt(one << 1),
UInt(one << 2),
UInt(one << 3),
UInt(one << 4),
UInt(one << 5),
UInt(one << 6),
UInt(one << 7),
// clang-format on
});
}
TEST(TEST_CATEGORY, bit_manip_has_single_bit) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_has_single_bit<unsigned char>();
test_bit_manip_has_single_bit<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_has_single_bit<unsigned int>();
test_bit_manip_has_single_bit<unsigned long>();
test_bit_manip_has_single_bit<unsigned long long>();
}
template <class UInt>
void test_bit_manip_bit_floor() {
using Kokkos::Experimental::bit_floor_builtin;
static_assert(noexcept(bit_floor_builtin(UInt())));
static_assert(std::is_same_v<decltype(bit_floor_builtin(UInt())), UInt>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(bit_floor)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(125),
UInt(126),
UInt(127),
UInt(128),
UInt(129),
UInt(max),
});
}
TEST(TEST_CATEGORY, bit_manip_bit_floor) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_bit_floor<unsigned char>();
test_bit_manip_bit_floor<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_bit_floor<unsigned int>();
test_bit_manip_bit_floor<unsigned long>();
test_bit_manip_bit_floor<unsigned long long>();
}
template <class UInt>
void test_bit_manip_bit_ceil() {
using Kokkos::Experimental::bit_ceil_builtin;
static_assert(noexcept(bit_ceil_builtin(UInt())));
static_assert(std::is_same_v<decltype(bit_ceil_builtin(UInt())), UInt>);
TEST_BIT_MANIP_FUNCTION(bit_ceil)
({
// clang-format off
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(60),
UInt(61),
UInt(62),
UInt(63),
UInt(64),
UInt(65),
UInt(66),
UInt(67),
UInt(68),
UInt(69),
// clang-format on
});
}
TEST(TEST_CATEGORY, bit_manip_bit_ceil) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_bit_ceil<unsigned char>();
test_bit_manip_bit_ceil<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_bit_ceil<unsigned int>();
test_bit_manip_bit_ceil<unsigned long>();
test_bit_manip_bit_ceil<unsigned long long>();
}
template <class UInt>
void test_bit_manip_bit_width() {
using Kokkos::Experimental::bit_width_builtin;
static_assert(noexcept(bit_width_builtin(UInt())));
static_assert(std::is_same_v<decltype(bit_width_builtin(UInt())), UInt>);
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_MANIP_FUNCTION(bit_width)
({
UInt(0),
UInt(1),
UInt(2),
UInt(3),
UInt(4),
UInt(5),
UInt(6),
UInt(7),
UInt(8),
UInt(9),
UInt(max - 1),
UInt(max),
});
}
TEST(TEST_CATEGORY, bit_manip_bit_width) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_bit_width<unsigned char>();
test_bit_manip_bit_width<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_bit_width<unsigned int>();
test_bit_manip_bit_width<unsigned long>();
test_bit_manip_bit_width<unsigned long long>();
}
#undef TEST_BIT_MANIP_FUNCTION
#define DEFINE_BIT_ROTATE_FUNCTION_EVAL(FUNC) \
struct BitRotateFunction_##FUNC { \
template <class T> \
static KOKKOS_FUNCTION auto eval_constexpr(T x, int s) { \
return Kokkos::FUNC(x, s); \
} \
template <class T> \
static KOKKOS_FUNCTION auto eval_builtin(T x, int s) { \
return Kokkos::Experimental::FUNC##_builtin(x, s); \
} \
static char const* name() { return #FUNC; } \
}
DEFINE_BIT_ROTATE_FUNCTION_EVAL(rotl);
DEFINE_BIT_ROTATE_FUNCTION_EVAL(rotr);
#undef DEFINE_BIT_ROTATE_FUNCTION_EVAL
template <class T>
struct P {
using type = T;
T x;
int s;
};
template <class Space, class Func, class Arg, std::size_t N>
struct TestBitRotateFunction {
Arg val_[N];
TestBitRotateFunction(const Arg (&val)[N]) {
std::copy(val, val + N, val_);
run();
}
void run() const {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, N), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for " << Func::name() << "("
<< type_helper<typename Arg::type>::name() << ", int)";
}
KOKKOS_FUNCTION void operator()(int i, int& e) const {
if (Func::eval_builtin(val_[i].x, val_[i].s) !=
Func::eval_constexpr(val_[i].x, val_[i].s)) {
++e;
Kokkos::printf(
"value at %x rotated by %d which is %x was expected to be %x\n",
(unsigned)val_[i].x, val_[i].s,
(unsigned)Func::eval_builtin(val_[i].x, val_[i].s),
(unsigned)Func::eval_constexpr(val_[i].x, val_[i].s));
}
}
};
template <class Space, class... Func, class Arg, std::size_t N>
void do_test_bit_rotate_function(const Arg (&x)[N]) {
(void)std::initializer_list<int>{
(TestBitRotateFunction<Space, Func, Arg, N>(x), 0)...};
}
#define TEST_BIT_ROTATE_FUNCTION(FUNC) \
do_test_bit_rotate_function<TEST_EXECSPACE, BitRotateFunction_##FUNC>
template <class UInt>
void test_bit_manip_rotl() {
using Kokkos::Experimental::rotl_builtin;
static_assert(noexcept(rotl_builtin(UInt(), 0)));
static_assert(std::is_same_v<decltype(rotl_builtin(UInt(), 0)), UInt>);
constexpr auto dig = Kokkos::Experimental::digits_v<UInt>;
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_ROTATE_FUNCTION(rotl)
({
// clang-format off
P<UInt>{UInt(0), 0},
P<UInt>{UInt(0), 1},
P<UInt>{UInt(0), 4},
P<UInt>{UInt(0), 8},
P<UInt>{max, 0},
P<UInt>{max, 1},
P<UInt>{max, 4},
P<UInt>{max, 8},
P<UInt>{UInt(1), 0},
P<UInt>{UInt(1), 1},
P<UInt>{UInt(1), 4},
P<UInt>{UInt(1), dig},
P<UInt>{UInt(7), dig},
P<UInt>{UInt(6), dig - 1},
P<UInt>{UInt(3), 6},
P<UInt>{UInt(max - 1), 0},
P<UInt>{UInt(max - 1), 1},
P<UInt>{UInt(max - 1), 2},
P<UInt>{UInt(max - 1), 3},
P<UInt>{UInt(max - 1), 4},
P<UInt>{UInt(max - 1), 5},
P<UInt>{UInt(max - 1), 6},
P<UInt>{UInt(max - 1), 7},
P<UInt>{UInt(1), 0},
P<UInt>{UInt(1), 1},
P<UInt>{UInt(1), 2},
P<UInt>{UInt(1), 3},
P<UInt>{UInt(1), 4},
P<UInt>{UInt(1), 5},
P<UInt>{UInt(1), 6},
P<UInt>{UInt(1), 7},
// clang-format on
});
}
TEST(TEST_CATEGORY, bit_manip_rotl) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_rotl<unsigned char>();
test_bit_manip_rotl<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_rotl<unsigned int>();
test_bit_manip_rotl<unsigned long>();
test_bit_manip_rotl<unsigned long long>();
}
template <class UInt>
void test_bit_manip_rotr() {
using Kokkos::rotr;
using Kokkos::Experimental::rotr_builtin;
static_assert(noexcept(rotr_builtin(UInt(), 0)));
static_assert(std::is_same_v<decltype(rotr_builtin(UInt(), 0)), UInt>);
constexpr auto dig = Kokkos::Experimental::digits_v<UInt>;
constexpr auto max = Kokkos::Experimental::finite_max_v<UInt>;
TEST_BIT_ROTATE_FUNCTION(rotr)
({
// clang-format off
P<UInt>{UInt(0), 0},
P<UInt>{UInt(0), 1},
P<UInt>{UInt(0), 4},
P<UInt>{UInt(0), 8},
P<UInt>{max, 0},
P<UInt>{max, 1},
P<UInt>{max, 4},
P<UInt>{max, 8},
P<UInt>{UInt(128), 0},
P<UInt>{UInt(128), 1},
P<UInt>{UInt(128), 4},
P<UInt>{UInt(1), dig},
P<UInt>{UInt(7), dig},
P<UInt>{UInt(6), dig - 1},
P<UInt>{UInt(36), dig - 2},
P<UInt>{UInt(max - 1), 0},
P<UInt>{UInt(max - 1), 1},
P<UInt>{UInt(max - 1), 2},
P<UInt>{UInt(max - 1), 3},
P<UInt>{UInt(max - 1), 4},
P<UInt>{UInt(max - 1), 5},
P<UInt>{UInt(max - 1), 6},
P<UInt>{UInt(max - 1), 7},
P<UInt>{UInt(128), 0},
P<UInt>{UInt(128), 1},
P<UInt>{UInt(128), 2},
P<UInt>{UInt(128), 3},
P<UInt>{UInt(128), 4},
P<UInt>{UInt(128), 5},
P<UInt>{UInt(128), 6},
P<UInt>{UInt(128), 0},
// clang-format on
});
}
TEST(TEST_CATEGORY, bit_manip_rotr) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_rotr<unsigned char>();
test_bit_manip_rotr<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_rotr<unsigned int>();
test_bit_manip_rotr<unsigned long>();
test_bit_manip_rotr<unsigned long long>();
}
#undef TEST_BIT_ROTATE_FUNCTION
template <class Space, class T>
struct TestByteswapFunction {
TestByteswapFunction() { run(); }
void run() const {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, 1), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for byteswap("
<< type_helper<T>::name() << ")";
}
KOKKOS_FUNCTION void operator()(int, int& e) const {
T value;
T expected;
switch (sizeof(T)) {
case 1:
value = static_cast<T>(0x12);
expected = static_cast<T>(0x12);
break;
case 2:
value = static_cast<T>(0x1234);
expected = static_cast<T>(0x3412);
break;
case 4:
value = static_cast<T>(0x60AF8503);
expected = static_cast<T>(0x0385AF60);
break;
case 8:
value = static_cast<T>(0xABCDFE9477936406);
expected = static_cast<T>(0x0664937794FECDAB);
break;
default: Kokkos::abort("logic error");
}
using Kokkos::Experimental::byteswap_builtin;
if (byteswap_builtin(value) != expected) {
++e;
Kokkos::printf("value at %llx which is %llx was expected to be %llx\n",
(unsigned long long)value,
(unsigned long long)byteswap_builtin(value),
(unsigned long long)expected);
}
}
};
template <class Integral>
void test_bit_manip_byteswap() {
using Kokkos::rotr;
using Kokkos::Experimental::byteswap_builtin;
static_assert(noexcept(byteswap_builtin(Integral())));
static_assert(
std::is_same_v<decltype(byteswap_builtin(Integral())), Integral>);
TestByteswapFunction<TEST_EXECSPACE, Integral>();
}
TEST(TEST_CATEGORY, bit_manip_byeswap) {
// FIXME_NVHPC: NVC++-W-0155-Compiler failed to translate accelerator region
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
if constexpr (!std::is_same_v<TEST_EXECSPACE,
Kokkos::Experimental::OpenACC>) {
#endif
test_bit_manip_byteswap<char>();
test_bit_manip_byteswap<unsigned char>();
test_bit_manip_byteswap<short>();
test_bit_manip_byteswap<unsigned short>();
#if defined(KOKKOS_ENABLE_OPENACC) && defined(KOKKOS_COMPILER_NVHPC)
}
#endif
test_bit_manip_byteswap<int>();
test_bit_manip_byteswap<unsigned int>();
test_bit_manip_byteswap<long>();
test_bit_manip_byteswap<unsigned long>();
test_bit_manip_byteswap<long long>();
test_bit_manip_byteswap<unsigned long long>();
}
// CUDA doesn't provide memcmp
KOKKOS_FUNCTION int my_memcmp(void const* lhs, void const* rhs, size_t count) {
auto u1 = static_cast<unsigned char const*>(lhs);
auto u2 = static_cast<unsigned char const*>(rhs);
while (count-- != 0) {
if (*u1 != *u2) {
return (*u1 < *u2) ? -1 : +1;
}
++u1;
++u2;
}
return 0;
}
template <class Space>
struct TestBitCastFunction {
TestBitCastFunction() { run(); }
void run() const {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, 1), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for bit_cast()";
}
template <typename To, typename From>
#if defined(KOKKOS_COMPILER_GNU) && (900 <= KOKKOS_COMPILER_GNU) && \
(KOKKOS_COMPILER_GNU < 930)
// workaround compiler bug seen in GCC 9.0.1 and GCC 9.2.0
KOKKOS_FUNCTION bool check(const From& from) const
#else
static KOKKOS_FUNCTION bool check(const From& from)
#endif
{
using Kokkos::Experimental::bit_cast_builtin;
return bit_cast_builtin<From>(bit_cast_builtin<To>(from)) == from;
}
KOKKOS_FUNCTION void operator()(int, int& e) const {
using Kokkos::bit_cast;
if (bit_cast<int>(123) != 123) {
++e;
Kokkos::printf("failed check #1\n");
}
if (bit_cast<int>(123u) != 123) {
++e;
Kokkos::printf("failed check #2\n");
}
if (bit_cast<int>(~0u) != ~0) {
++e;
Kokkos::printf("failed check #3\n");
}
if constexpr (sizeof(int) == sizeof(float)) {
if (!check<int>(12.34f)) {
++e;
Kokkos::printf("failed check #4\n");
}
}
if constexpr (sizeof(unsigned long long) == sizeof(double)) {
if (!check<unsigned long long>(123.456)) {
++e;
Kokkos::printf("failed check #5\n");
}
}
struct S {
int i;
KOKKOS_FUNCTION bool operator==(const char* s) const {
return my_memcmp(&i, s, sizeof(i)) == 0;
}
};
char arr[sizeof(int)];
char arr2[sizeof(int)];
for (size_t i = 0; i < sizeof(int); ++i) {
arr[i] = i + 1;
arr2[i] = (i + 1) * -(i % 2);
}
if (!(bit_cast<S>(arr) == arr)) {
++e;
Kokkos::printf("failed check #6\n");
}
if (!(bit_cast<S>(arr2) == arr2)) {
++e;
Kokkos::printf("failed check #7\n");
}
}
};
TEST(TEST_CATEGORY, bit_manip_bit_cast) {
TestBitCastFunction<TEST_EXECSPACE>();
}
|