//===----------------------------------------------------------------------===// // // 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 "test_macros.h" template __host__ __device__ constexpr void test_overflow_cast(const From& from, const bool overflow) { const auto result = cuda::overflow_cast(from); assert(result.value == static_cast(from)); assert(result.overflow == overflow); } template __host__ __device__ constexpr void test_type() { static_assert(cuda::std::is_same_v(From{})), cuda::overflow_result>); static_assert(noexcept(cuda::overflow_cast(From{}))); // 1. Casting zero should never overflow test_overflow_cast(From{}, false); // 2. Casting positive one should never overflow test_overflow_cast(From{1}, false); // 3. Casting negative one should overflow if the destination type is unsigned if constexpr (cuda::std::is_signed_v) { test_overflow_cast(From{-1}, cuda::std::is_unsigned_v); } // 4. Casting the minimum value of From type constexpr bool min_overflows = cuda::std::cmp_less(cuda::std::numeric_limits::min(), cuda::std::numeric_limits::min()); test_overflow_cast(cuda::std::numeric_limits::min(), min_overflows); // 5. Casting the maximum value of From type constexpr bool max_overflows = cuda::std::cmp_greater(cuda::std::numeric_limits::max(), cuda::std::numeric_limits::max()); test_overflow_cast(cuda::std::numeric_limits::max(), max_overflows); } template __host__ __device__ constexpr void test_type() { test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); #if _CCCL_HAS_INT128() test_type(); test_type(); #endif // _CCCL_HAS_INT128() } __host__ __device__ constexpr bool test() { test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); test_type(); #if _CCCL_HAS_INT128() test_type<__int128_t>(); test_type<__uint128_t>(); #endif // _CCCL_HAS_INT128() return true; } int main(int arg, char** argv) { test(); static_assert(test()); return 0; }