Files
TheRock/math-libs/libhipcxx/.upstream-tests/test/std/numerics/bit/bitops.count/popcount.pass.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

138 lines
3.7 KiB
C++

//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// template <class T>
// constexpr int popcount(T x) noexcept;
// Returns: The number of bits set to one in the value of x.
//
// Remarks: This function shall not participate in overload resolution unless
// T is an unsigned integer type
#include <cuda/std/bit>
#include <cuda/std/cassert>
#include <cuda/std/cstdint>
#include <cuda/std/type_traits>
#include "test_macros.h"
TEST_DIAG_SUPPRESS_MSVC(4307) // integer overflow
class A
{};
enum E1 : unsigned char
{
rEd
};
enum class E2 : unsigned char
{
red
};
template <typename T>
__host__ __device__ constexpr bool constexpr_test()
{
static_assert(cuda::std::popcount(T(0)) == 0, "");
static_assert(cuda::std::popcount(T(1)) == 1, "");
static_assert(cuda::std::popcount(T(2)) == 1, "");
static_assert(cuda::std::popcount(T(3)) == 2, "");
static_assert(cuda::std::popcount(T(4)) == 1, "");
static_assert(cuda::std::popcount(T(5)) == 2, "");
static_assert(cuda::std::popcount(T(6)) == 2, "");
static_assert(cuda::std::popcount(T(7)) == 3, "");
static_assert(cuda::std::popcount(T(8)) == 1, "");
static_assert(cuda::std::popcount(T(9)) == 2, "");
static_assert(cuda::std::popcount(cuda::std::numeric_limits<T>::max()) == cuda::std::numeric_limits<T>::digits, "");
return true;
}
template <typename T>
__host__ __device__ inline void assert_popcount(T val, int expected)
{
volatile auto v = val;
assert(cuda::std::popcount(v) == expected);
}
template <typename T>
__host__ __device__ void runtime_test()
{
static_assert(cuda::std::is_same_v<int, decltype(cuda::std::popcount(T(0)))>);
static_assert(noexcept(cuda::std::popcount(T(0))));
assert_popcount(T(121), 5);
assert_popcount(T(122), 5);
assert_popcount(T(123), 6);
assert_popcount(T(124), 5);
assert_popcount(T(125), 6);
assert_popcount(T(126), 6);
assert_popcount(T(127), 7);
assert_popcount(T(128), 1);
assert_popcount(T(129), 2);
assert_popcount(T(130), 2);
}
int main(int, char**)
{
constexpr_test<unsigned char>();
constexpr_test<unsigned short>();
constexpr_test<unsigned>();
constexpr_test<unsigned long>();
constexpr_test<unsigned long long>();
constexpr_test<uint8_t>();
constexpr_test<uint16_t>();
constexpr_test<uint32_t>();
constexpr_test<uint64_t>();
constexpr_test<size_t>();
constexpr_test<uintmax_t>();
constexpr_test<uintptr_t>();
#if _CCCL_HAS_INT128()
constexpr_test<__uint128_t>();
#endif // _CCCL_HAS_INT128()
runtime_test<unsigned char>();
runtime_test<unsigned>();
runtime_test<unsigned short>();
runtime_test<unsigned long>();
runtime_test<unsigned long long>();
runtime_test<uint8_t>();
runtime_test<uint16_t>();
runtime_test<uint32_t>();
runtime_test<uint64_t>();
runtime_test<size_t>();
runtime_test<uintmax_t>();
runtime_test<uintptr_t>();
#if _CCCL_HAS_INT128()
runtime_test<__uint128_t>();
{
__uint128_t val = 128;
val <<= 32;
assert(cuda::std::popcount(val - 1) == 39);
assert(cuda::std::popcount(val) == 1);
assert(cuda::std::popcount(val + 1) == 2);
val <<= 2;
assert(cuda::std::popcount(val - 1) == 41);
assert(cuda::std::popcount(val) == 1);
assert(cuda::std::popcount(val + 1) == 2);
val <<= 3;
assert(cuda::std::popcount(val - 1) == 44);
assert(cuda::std::popcount(val) == 1);
assert(cuda::std::popcount(val + 1) == 2);
}
#endif // _CCCL_HAS_INT128()
return 0;
}