//===----------------------------------------------------------------------===// // // 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. // //===----------------------------------------------------------------------===// // MIT License // // Modifications Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include #include #include #include #include #include "cuda/std/__type_traits/conditional.h" #include "cuda/std/__type_traits/is_enum.h" #include "cuda/std/__type_traits/underlying_type.h" #include "test_macros.h" #if !TEST_COMPILER(NVRTC) && !defined(TEST_COMPILER_HIPRTC) # include #endif // !TEST_COMPILER(NVRTC) template __host__ __device__ constexpr void test() { constexpr auto maxv = cuda::std::numeric_limits::max(); using CommonType = cuda::std::common_type_t; // ensure that we return the right type static_assert(cuda::std::is_same::value, ""); static_assert(cuda::std::is_same::value, ""); assert(cuda::round_up(T(0), U(1)) == CommonType(0)); assert(cuda::round_up(T(1), U(1)) == CommonType(1)); assert(cuda::round_up(T(45), U(32)) == CommonType(64)); // ensure that we are resilient against overflow assert(cuda::round_up(maxv, U(1)) == maxv); assert(cuda::round_up(maxv, maxv) == maxv); assert(cuda::round_down(T(0), U(1)) == CommonType(0)); assert(cuda::round_down(T(1), U(1)) == CommonType(1)); assert(cuda::round_down(T(78), U(64)) == CommonType(64)); // ensure that we are resilient against overflow assert(cuda::round_down(maxv, U(1)) == maxv); assert(cuda::round_down(maxv, maxv) == maxv); } template struct relaxed_underlying_type { using type = T; }; template struct relaxed_underlying_type>> { using type = cuda::std::underlying_type_t; }; template __host__ __device__ constexpr void test_enum() { using T = typename relaxed_underlying_type::type; using U = typename relaxed_underlying_type::type; using CommonType = cuda::std::common_type_t; // ensure that we return the right type static_assert(cuda::std::is_same::value, ""); static_assert(cuda::std::is_same::value, ""); assert(cuda::round_up(T(0), U(1)) == CommonType(0)); assert(cuda::round_up(T(1), U(1)) == CommonType(1)); assert(cuda::round_up(T(46), U(32)) == CommonType(64)); assert(cuda::round_down(T(0), U(1)) == CommonType(0)); assert(cuda::round_down(T(1), U(1)) == CommonType(1)); assert(cuda::round_down(T(78), U(64)) == CommonType(64)); } enum Enum1 : short { E1, E2, E3 = 64, E4 = 78 }; enum class Enum2 : long { C1, C2, C3 = 64, C4 = 78 }; template __host__ __device__ constexpr void test() { // Builtin integer types: test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test_enum(); test_enum(); test_enum(); test_enum(); #if !TEST_COMPILER(NVRTC) && !defined(TEST_COMPILER_HIPRTC) // cstdint types: test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); #endif // !TEST_COMPILER(NVRTC) #if _CCCL_HAS_INT128() test(); test(); #endif // _CCCL_HAS_INT128() } __host__ __device__ constexpr bool test() { // Builtin integer types: test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); test(); #if !TEST_COMPILER(NVRTC) && !defined(TEST_COMPILER_HIPRTC) // 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 arg, char** argv) { test(); static_assert(test(), ""); return 0; }