//===----------------------------------------------------------------------===// // // Part of the LLVM 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 "comparison.h" #include "test_macros.h" TEST_DIAG_SUPPRESS_MSVC(4244) // conversion from 'double' to 'float', possible loss of data TEST_DIAG_SUPPRESS_MSVC(4305) // 'argument': truncation from 'T' to 'float' TEST_DIAG_SUPPRESS_MSVC(4146) // unary minus operator applied to unsigned type, result still unsigned template __host__ __device__ void test_lgamma(T val) { using ret = cuda::std::conditional_t, double, T>; static_assert(cuda::std::is_same_v, ""); // If the argument is 1, +0 is returned. assert(eq(cuda::std::lgamma(T(1.0)), val)); // If the argument is 2, +0 is returned. assert(eq(cuda::std::lgamma(T(2.0)), val)); // If the argument is a negative integer, +∞ is returned. assert(eq(cuda::std::lgamma(-2), cuda::std::numeric_limits::infinity())); // If the argument is ±0, +∞ is returned. assert(eq(cuda::std::lgamma(val), cuda::std::numeric_limits::infinity())); if constexpr (!cuda::std::is_integral_v) { // If the argument is ±0, +∞ is returned. assert(eq(cuda::std::lgamma(-val), cuda::std::numeric_limits::infinity())); // If the argument is ±∞, +∞ is returned. assert(eq(cuda::std::lgamma(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::lgamma(-cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::lgamma(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgamma(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::lgamma(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgamma(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(0.5723649429247000819387380943226162); assert(is_about(cuda::std::lgamma(T(0.5)), expected)); } if constexpr (cuda::std::is_same_v) { // If the argument is 1, +0 is returned. assert(eq(cuda::std::lgammaf(T(1.0)), val)); // If the argument is 2, +0 is returned. assert(eq(cuda::std::lgammaf(T(2.0)), val)); // If the argument is ±0, +∞ is returned. assert(eq(cuda::std::lgammaf(val), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::lgammaf(-val), cuda::std::numeric_limits::infinity())); // If the argument is ±∞, +∞ is returned. assert(eq(cuda::std::lgammaf(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::lgammaf(-cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::lgammaf(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgammaf(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::lgammaf(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgammaf(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(0.5723649429247000819387380943226162); assert(is_about(cuda::std::lgammaf(T(0.5)), expected)); } #if _CCCL_HAS_LONG_DOUBLE() else if constexpr (cuda::std::is_same_v) { // If the argument is 1, +0 is returned. assert(eq(cuda::std::lgammal(T(1.0)), val)); // If the argument is 2, +0 is returned. assert(eq(cuda::std::lgammal(T(2.0)), val)); // If the argument is ±0, +∞ is returned. assert(eq(cuda::std::lgammal(val), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::lgammal(-val), cuda::std::numeric_limits::infinity())); // If the argument is ±∞, +∞ is returned. assert(eq(cuda::std::lgammal(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::lgammal(-cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::lgammal(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgammal(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::lgammal(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::lgammal(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(0.5723649429247000819387380943226162); assert(is_about(cuda::std::lgammal(T(0.5)), expected)); } #endif // _CCCL_HAS_LONG_DOUBLE() } template __host__ __device__ void test_tgamma(T val) { using ret = cuda::std::conditional_t, double, T>; static_assert(cuda::std::is_same_v, ""); // If the argument is a negative integer. assert(cuda::std::isnan(cuda::std::tgamma(-2))); // Some characteristic value // NOTE(HIP/AMD): tgamma(2.0) returns a slightly incorrect version for ROCm versions below 7.2 #if LIBHIPCXX_ROCM_VERSION_GE(7,2,0) assert(eq(cuda::std::tgamma(T(2.0)), T(1.0))); #endif // If the argument is ±0, ±∞ is returned. assert(eq(cuda::std::tgamma(val), cuda::std::numeric_limits::infinity())); if constexpr (!cuda::std::is_integral_v) { // If the argument is ±0, ±∞ is returned. assert(eq(cuda::std::tgamma(-val), -cuda::std::numeric_limits::infinity())); // If the argument is -∞, NaN is returned. assert(cuda::std::isnan(cuda::std::tgamma(-cuda::std::numeric_limits::infinity()))); // If the argument is +∞, +∞ is returned. assert(eq(cuda::std::tgamma(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::tgamma(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgamma(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgamma(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::tgamma(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(1.772453850905516103964032481599133); assert(is_about(cuda::std::tgamma(T(0.5)), expected)); } if constexpr (cuda::std::is_same_v) { // If the argument is ±0, ±∞ is returned. assert(eq(cuda::std::tgammaf(val), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::tgammaf(-val), -cuda::std::numeric_limits::infinity())); // If the argument is -∞, NaN is returned. assert(cuda::std::isnan(cuda::std::tgammaf(-cuda::std::numeric_limits::infinity()))); // If the argument is +∞, +∞ is returned. assert(eq(cuda::std::tgammaf(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::tgammaf(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgammaf(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgammaf(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::tgammaf(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(1.772453850905516103964032481599133); assert(is_about(cuda::std::tgammaf(T(0.5)), expected)); } #if _CCCL_HAS_LONG_DOUBLE() else if constexpr (cuda::std::is_same_v) { // If the argument is ±0, ±∞ is returned. assert(eq(cuda::std::tgammal(val), cuda::std::numeric_limits::infinity())); assert(eq(cuda::std::tgammal(-val), -cuda::std::numeric_limits::infinity())); // If the argument is -∞, NaN is returned. assert(cuda::std::isnan(cuda::std::tgammal(-cuda::std::numeric_limits::infinity()))); // If the argument is +∞, +∞ is returned. assert(eq(cuda::std::tgammal(cuda::std::numeric_limits::infinity()), cuda::std::numeric_limits::infinity())); // If the argument is NaN, NaN is returned. assert(cuda::std::isnan(cuda::std::tgammal(cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgammal(-cuda::std::numeric_limits::signaling_NaN()))); assert(cuda::std::isnan(cuda::std::tgammal(cuda::std::numeric_limits::quiet_NaN()))); assert(cuda::std::isnan(cuda::std::tgammal(-cuda::std::numeric_limits::quiet_NaN()))); // Some random value const T expected = T(1.772453850905516103964032481599133); assert(is_about(cuda::std::tgammal(T(0.5)), expected)); } #endif // _CCCL_HAS_LONG_DOUBLE() } template __host__ __device__ void test(const T val) { test_lgamma(val); test_tgamma(val); } __host__ __device__ void test(const float val) { test(val); test(val); #if _CCCL_HAS_LONG_DOUBLE() test(); #endif // _CCCL_HAS_LONG_DOUBLE() #if _LIBCUDACXX_HAS_NVFP16() test<__half>(val); #endif // _LIBCUDACXX_HAS_NVFP16() #if _LIBCUDACXX_HAS_NVBF16() test<__nv_bfloat16>(val); #endif // _LIBCUDACXX_HAS_NVBF16() test(static_cast(val)); test(static_cast(val)); test(static_cast(val)); test(static_cast(val)); test(static_cast(val)); test(static_cast(val)); test(static_cast(val)); } __global__ void test_global_kernel(float* val) { test(*val); } int main(int, char**) { volatile float val = 0.0f; test(val); return 0; }