//===----------------------------------------------------------------------===// // // Part of the libcu++ Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===// #include #include #include #include #include #include #include "cuda/std/__type_traits/is_constant_evaluated.h" #include "test_macros.h" template __host__ __device__ constexpr void test_log2() { int i = 0; for (T value = 1; value <= cuda::std::numeric_limits::max() / 2; value *= 2) { assert(cuda::ilog2(value) == i); if (i >= 1) { assert(cuda::ilog2(static_cast(value - 1)) == i - 1); assert(cuda::ilog2(static_cast(value + 1)) == i); // not true if value == 1 } i++; } assert(cuda::ilog2(cuda::std::numeric_limits::max()) == cuda::std::numeric_limits::digits - 1); } template __host__ __device__ constexpr void test_log10() { int i = 0; for (T value = 1; value <= cuda::std::numeric_limits::max() / 10; value *= 10) { assert(cuda::ilog10(value) == i); assert(cuda::ilog10(value + value / 2) == i); if (i >= 1) { assert(cuda::ilog10(static_cast(value - 1)) == i - 1); assert(cuda::ilog10(static_cast(value + 1)) == i); assert(cuda::ilog10(value - value / 2) == i - 1); assert(cuda::ilog10(value - value / 2 - 1) == i - 1); } i++; } if (!cuda::std::__cccl_default_is_constant_evaluated()) { constexpr auto max_v = cuda::std::numeric_limits::max(); assert(cuda::ilog10(max_v) == static_cast(cuda::std::floor(cuda::std::log10(max_v)))); } } template __host__ __device__ constexpr void test() { test_log2(); test_log10(); } __host__ __device__ constexpr bool test() { // Builtin integer types: test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); #if !defined(TEST_COMPILER_NVRTC) // cstdint types: test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); #endif // !TEST_COMPILER_NVRTC #if _CCCL_HAS_INT128() test<__int128_t>(); test<__uint128_t>(); #endif // _CCCL_HAS_INT128() return true; } int main(int, char**) { test(); static_assert(test()); return 0; }