diff --git a/libcudacxx/include/cuda/__type_traits/is_floating_point.h b/libcudacxx/include/cuda/__type_traits/is_floating_point.h new file mode 100644 index 00000000000..e253315a672 --- /dev/null +++ b/libcudacxx/include/cuda/__type_traits/is_floating_point.h @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef __CUDA__TYPE_TRAITS_IS_FLOATING_POINT_H +#define __CUDA__TYPE_TRAITS_IS_FLOATING_POINT_H + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include +#include +#include +#include + +_LIBCUDACXX_BEGIN_NAMESPACE_CUDA + +template +struct _CCCL_TYPE_VISIBILITY_DEFAULT is_floating_point + : _CUDA_VSTD::bool_constant<_CUDA_VSTD::is_floating_point<_CUDA_VSTD::remove_cv_t<_Tp>>::value + || _CUDA_VSTD::__is_extended_floating_point<_CUDA_VSTD::remove_cv_t<_Tp>>::value> +{}; + +#if !defined(_CCCL_NO_VARIABLE_TEMPLATES) +template +_CCCL_INLINE_VAR constexpr bool is_floating_point_v = + _CUDA_VSTD::is_floating_point_v<_CUDA_VSTD::remove_cv_t<_Tp>> + || _CUDA_VSTD::__is_extended_floating_point_v<_CUDA_VSTD::remove_cv_t<_Tp>>; +#endif // !_CCCL_NO_VARIABLE_TEMPLATES + +_LIBCUDACXX_END_NAMESPACE_CUDA + +#endif // __CUDA__TYPE_TRAITS_IS_FLOATING_POINT_H diff --git a/libcudacxx/include/cuda/std/__cccl/extended_floating_point.h b/libcudacxx/include/cuda/std/__cccl/extended_floating_point.h index 5169ea4ad67..dee553633d8 100644 --- a/libcudacxx/include/cuda/std/__cccl/extended_floating_point.h +++ b/libcudacxx/include/cuda/std/__cccl/extended_floating_point.h @@ -39,4 +39,14 @@ # endif #endif // !_CCCL_HAS_NVBF16 +#if !defined(_CCCL_DISABLE_NVFP8_SUPPORT) +# if _CCCL_HAS_INCLUDE() && defined(_CCCL_HAS_NVFP16) && defined(_CCCL_HAS_NVBF16) +# define _CCCL_HAS_NVFP8() 1 +# else +# define _CCCL_HAS_NVFP8() 0 +# endif // _CCCL_HAS_INCLUDE() +#else +# define _CCCL_HAS_NVFP8() 0 +#endif // !defined(_CCCL_DISABLE_NVFP8_SUPPORT) + #endif // __CCCL_EXTENDED_FLOATING_POINT_H diff --git a/libcudacxx/include/cuda/std/__type_traits/is_extended_floating_point.h b/libcudacxx/include/cuda/std/__type_traits/is_extended_floating_point.h index bb1afa4225b..b9700a87066 100644 --- a/libcudacxx/include/cuda/std/__type_traits/is_extended_floating_point.h +++ b/libcudacxx/include/cuda/std/__type_traits/is_extended_floating_point.h @@ -33,6 +33,10 @@ _CCCL_DIAG_SUPPRESS_CLANG("-Wunused-function") _CCCL_DIAG_POP #endif // _LIBCUDACXX_HAS_NVBF16 +#if _CCCL_HAS_NVFP8() +# include +#endif // _CCCL_HAS_NVFP8() + _LIBCUDACXX_BEGIN_NAMESPACE_STD template @@ -71,6 +75,22 @@ _CCCL_INLINE_VAR constexpr bool __is_extended_floating_point_v<__nv_bfloat16> = # endif // !_CCCL_NO_INLINE_VARIABLES #endif // _LIBCUDACXX_HAS_NVBF16 +#if _CCCL_HAS_NVFP8() +template <> +struct __is_extended_floating_point<__nv_fp8_e4m3> : true_type +{}; +template <> +struct __is_extended_floating_point<__nv_fp8_e5m2> : true_type +{}; + +# ifndef _CCCL_NO_INLINE_VARIABLES +template <> +_CCCL_INLINE_VAR constexpr bool __is_extended_floating_point_v<__nv_fp8_e4m3> = true; +template <> +_CCCL_INLINE_VAR constexpr bool __is_extended_floating_point_v<__nv_fp8_e5m2> = true; +# endif // !_CCCL_NO_INLINE_VARIABLES +#endif // _CCCL_HAS_NVFP8() + _LIBCUDACXX_END_NAMESPACE_STD #endif // _LIBCUDACXX___TYPE_TRAITS_IS_EXTENDED_FLOATING_POINT_H diff --git a/libcudacxx/include/cuda/type_traits b/libcudacxx/include/cuda/type_traits new file mode 100644 index 00000000000..9b732130a65 --- /dev/null +++ b/libcudacxx/include/cuda/type_traits @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of libcu++, the C++ Standard Library for your entire system, +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef _CUDA_TYPE_TRAITS_ +#define _CUDA_TYPE_TRAITS_ + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include +#include + +#endif // _CUDA_TYPE_TRAITS_ diff --git a/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.fail.cpp b/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.fail.cpp new file mode 100644 index 00000000000..3b6b457e633 --- /dev/null +++ b/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.fail.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +#include + +#include "test_macros.h" + +#if !_CCCL_HAS_NVFP8() +# include +#endif +#if !defined(_CCCL_HAS_NVFP16) +# include +#endif +#if !defined(_CCCL_HAS_NVBF16) +# include +#endif + +int main(int, char**) +{ +#if !_CCCL_HAS_NVFP8() + auto x = __nv_fp8_e4m3(1.0f); + unused(x); +#else + static_assert(false); +#endif +#if !defined(_CCCL_HAS_NVFP16) + auto y = __half(1.0f); + unused(y); +#else + static_assert(false); +#endif +#if !defined(_CCCL_HAS_NVBF16) + auto z = __nv_bfloat16(1.0f); + unused(z); +#else + static_assert(false); +#endif + return 0; +} diff --git a/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.pass.cpp b/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.pass.cpp new file mode 100644 index 00000000000..fa1476611bc --- /dev/null +++ b/libcudacxx/test/libcudacxx/libcxx/macros/extended_floating_point.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +#include + +#include "test_macros.h" + +#if _CCCL_HAS_NVFP8() +# include +#endif +#if defined(_CCCL_HAS_NVFP16) +# include +#endif +#if defined(_CCCL_HAS_NVBF16) +# include +#endif + +int main(int, char**) +{ +#if _CCCL_HAS_NVFP8() + auto x = __nv_fp8_e4m3(1.0f); + unused(x); +#endif +#if defined(_CCCL_HAS_NVFP16) + auto y = __half(1.0f); + unused(y); +#endif +#if defined(_CCCL_HAS_NVBF16) + auto z = __nv_bfloat16(1.0f); + unused(z); +#endif + return 0; +} diff --git a/libcudacxx/test/libcudacxx/libcxx/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp b/libcudacxx/test/libcudacxx/libcxx/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp new file mode 100644 index 00000000000..b0b7a3f3b69 --- /dev/null +++ b/libcudacxx/test/libcudacxx/libcxx/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp @@ -0,0 +1,119 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// keep this test in sync with `is_floating_point.pass.cpp` for `cuda::std::is_floating_point` + +#include // for cuda::std::nullptr_t +#include + +#include "test_macros.h" + +TEST_NV_DIAG_SUPPRESS(cuda_demote_unsupported_floating_point) + +template +__host__ __device__ void test_is_floating_point() +{ + static_assert(cuda::is_floating_point::value, ""); + static_assert(cuda::is_floating_point::value, ""); + static_assert(cuda::is_floating_point::value, ""); + static_assert(cuda::is_floating_point::value, ""); +#if TEST_STD_VER > 2011 + static_assert(cuda::is_floating_point_v, ""); + static_assert(cuda::is_floating_point_v, ""); + static_assert(cuda::is_floating_point_v, ""); + static_assert(cuda::is_floating_point_v, ""); +#endif +} + +template +__host__ __device__ void test_is_not_floating_point() +{ + static_assert(!cuda::is_floating_point::value, ""); + static_assert(!cuda::is_floating_point::value, ""); + static_assert(!cuda::is_floating_point::value, ""); + static_assert(!cuda::is_floating_point::value, ""); +#if TEST_STD_VER > 2011 + static_assert(!cuda::is_floating_point_v, ""); + static_assert(!cuda::is_floating_point_v, ""); + static_assert(!cuda::is_floating_point_v, ""); + static_assert(!cuda::is_floating_point_v, ""); +#endif +} + +class Empty +{}; + +class NotEmpty +{ + __host__ __device__ virtual ~NotEmpty(); +}; + +union Union +{}; + +struct bit_zero +{ + int : 0; +}; + +class Abstract +{ + __host__ __device__ virtual ~Abstract() = 0; +}; + +enum Enum +{ + zero, + one +}; +struct incomplete_type; + +typedef void (*FunctionPtr)(); + +int main(int, char**) +{ + test_is_floating_point(); + test_is_floating_point(); + test_is_floating_point(); +#ifdef _LIBCUDACXX_HAS_NVFP16 + test_is_floating_point<__half>(); +#endif // _LIBCUDACXX_HAS_NVFP16 +#ifdef _LIBCUDACXX_HAS_NVBF16 + test_is_floating_point<__nv_bfloat16>(); +#endif // _LIBCUDACXX_HAS_NVBF16 +#if _CCCL_HAS_NVFP8() + test_is_floating_point<__nv_fp8_e4m3>(); + test_is_floating_point<__nv_fp8_e5m2>(); +#endif // ()) + + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + test_is_not_floating_point(); + + return 0; +} diff --git a/libcudacxx/test/libcudacxx/std/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp b/libcudacxx/test/libcudacxx/std/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp index 1baff1fe485..79b0a07d862 100644 --- a/libcudacxx/test/libcudacxx/std/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp +++ b/libcudacxx/test/libcudacxx/std/utilities/meta/meta.unary/meta.unary.cat/is_floating_point.pass.cpp @@ -6,6 +6,8 @@ // //===----------------------------------------------------------------------===// +// keep this test in sync with `is_floating_point.pass.cpp` for `cuda::is_floating_point` + // type_traits // is_floating_point