-
Notifications
You must be signed in to change notification settings - Fork 346
Implement fp constants #4256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement fp constants #4256
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
libcudacxx/include/cuda/std/__floating_point/constants.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 _LIBCUDACXX___FLOATING_POINT_CONSTANTS_H | ||
| #define _LIBCUDACXX___FLOATING_POINT_CONSTANTS_H | ||
|
|
||
| #include <cuda/std/detail/__config> | ||
|
|
||
| #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 <cuda/std/__concepts/concept_macros.h> | ||
| #include <cuda/std/__floating_point/format.h> | ||
| #include <cuda/std/__floating_point/mask.h> | ||
| #include <cuda/std/__floating_point/properties.h> | ||
| #include <cuda/std/__floating_point/storage.h> | ||
|
|
||
| _LIBCUDACXX_BEGIN_NAMESPACE_STD | ||
|
|
||
| template <__fp_format _Fmt> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr __fp_storage_t<_Fmt> __fp_inf() noexcept | ||
| { | ||
| static_assert(__fp_has_inf_v<_Fmt>, "The format does not support infinity"); | ||
|
|
||
| return __fp_exp_mask_v<_Fmt>; | ||
| } | ||
|
|
||
| template <class _Tp> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp __fp_inf() noexcept | ||
| { | ||
| return _CUDA_VSTD::__fp_from_storage<_Tp>(_CUDA_VSTD::__fp_inf<__fp_format_of_v<_Tp>>()); | ||
| } | ||
|
|
||
| template <__fp_format _Fmt> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr __fp_storage_t<_Fmt> __fp_nan() noexcept | ||
| { | ||
| static_assert(__fp_has_nan_v<_Fmt>, "The format does not support nan"); | ||
|
|
||
| if constexpr (_Fmt == __fp_format::__fp8_nv_e4m3) | ||
| { | ||
| return __fp_storage_t<_Fmt>(0x7fu); | ||
| } | ||
| else if constexpr (_Fmt == __fp_format::__fp8_nv_e8m0) | ||
| { | ||
| return __fp_storage_t<_Fmt>(0xffu); | ||
| } | ||
| else if constexpr (__fp_has_implicit_bit_v<_Fmt>) | ||
| { | ||
| return static_cast<__fp_storage_t<_Fmt>>( | ||
| __fp_exp_mask_v<_Fmt> | (__fp_storage_t<_Fmt>(1) << (__fp_mant_nbits_v<_Fmt> - 1))); | ||
| } | ||
| else | ||
| { | ||
| return static_cast<__fp_storage_t<_Fmt>>( | ||
| __fp_exp_mask_v<_Fmt> | (__fp_storage_t<_Fmt>(3) << (__fp_mant_nbits_v<_Fmt> - 2))); | ||
| } | ||
| } | ||
|
|
||
| template <class _Tp> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp __fp_nan() noexcept | ||
| { | ||
| return _CUDA_VSTD::__fp_from_storage<_Tp>(_CUDA_VSTD::__fp_nan<__fp_format_of_v<_Tp>>()); | ||
| } | ||
|
|
||
| template <__fp_format _Fmt> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr __fp_storage_t<_Fmt> __fp_nans() noexcept | ||
| { | ||
| static_assert(__fp_has_nans_v<_Fmt>, "The format does not support nans"); | ||
|
|
||
| if constexpr (__fp_has_implicit_bit_v<_Fmt>) | ||
| { | ||
| return static_cast<__fp_storage_t<_Fmt>>( | ||
| __fp_exp_mask_v<_Fmt> | (__fp_storage_t<_Fmt>(1) << (__fp_mant_nbits_v<_Fmt> - 2))); | ||
| } | ||
| else | ||
| { | ||
| return static_cast<__fp_storage_t<_Fmt>>( | ||
| __fp_exp_mask_v<_Fmt> | (__fp_storage_t<_Fmt>(5) << (__fp_mant_nbits_v<_Fmt> - 3))); | ||
| } | ||
| } | ||
|
|
||
| template <class _Tp> | ||
| _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp __fp_nans() noexcept | ||
| { | ||
| return _CUDA_VSTD::__fp_from_storage<_Tp>(_CUDA_VSTD::__fp_nans<__fp_format_of_v<_Tp>>()); | ||
| } | ||
|
|
||
| _LIBCUDACXX_END_NAMESPACE_STD | ||
|
|
||
| #endif // _LIBCUDACXX___FLOATING_POINT_CONSTANTS_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
libcudacxx/test/libcudacxx/libcxx/numerics/floating.point/constants.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <cuda/std/__floating_point/fp.h> | ||
| #include <cuda/std/cassert> | ||
| #include <cuda/std/cmath> | ||
| #include <cuda/std/cstring> | ||
| #include <cuda/std/limits> | ||
| #include <cuda/std/type_traits> | ||
|
|
||
| #include "test_macros.h" | ||
|
|
||
| template <class T> | ||
| __host__ __device__ void test_fp_storage() | ||
| { | ||
| constexpr auto fmt = cuda::std::__fp_format_of_v<T>; | ||
|
|
||
| // __fp_has_inf_v must match numeric_limits::has_infinity | ||
| static_assert(cuda::std::__fp_has_inf_v<fmt> == cuda::std::numeric_limits<T>::has_infinity); | ||
|
|
||
| // test __fp_inf value to match numeric_limits::infinity() | ||
| if constexpr (cuda::std::__fp_has_inf_v<fmt>) | ||
| { | ||
| const auto val = cuda::std::__fp_inf<T>(); | ||
| const auto ref = cuda::std::numeric_limits<T>::infinity(); | ||
| assert(cuda::std::memcmp(&val, &ref, sizeof(T)) == 0); | ||
| } | ||
|
|
||
| // __fp_has_nan_v must match numeric_limits::has_quiet_NaN | ||
| static_assert(cuda::std::__fp_has_nan_v<fmt> == cuda::std::numeric_limits<T>::has_quiet_NaN); | ||
|
|
||
| // test __fp_nan value | ||
| if constexpr (cuda::std::__fp_has_nan_v<fmt>) | ||
| { | ||
| assert(cuda::std::isnan(cuda::std::__fp_nan<T>())); | ||
| } | ||
|
|
||
| // __fp_has_nans_v must match numeric_limits::has_signaling_NaN | ||
| static_assert(cuda::std::__fp_has_nans_v<fmt> == cuda::std::numeric_limits<T>::has_signaling_NaN); | ||
|
|
||
| // test __fp_nans value | ||
| if constexpr (cuda::std::__fp_has_nans_v<fmt>) | ||
| { | ||
| assert(cuda::std::isnan(cuda::std::__fp_nans<T>())); | ||
| } | ||
| } | ||
|
|
||
| int main(int, char**) | ||
| { | ||
| test_fp_storage<float>(); | ||
| test_fp_storage<double>(); | ||
| #if _CCCL_HAS_LONG_DOUBLE() | ||
| test_fp_storage<long double>(); | ||
| #endif // _CCCL_HAS_LONG_DOUBLE() | ||
| #if _CCCL_HAS_NVFP16() | ||
| test_fp_storage<__half>(); | ||
| #endif // _CCCL_HAS_NVFP16() | ||
| #if _CCCL_HAS_NVBF16() | ||
| test_fp_storage<__nv_bfloat16>(); | ||
| #endif // _CCCL_HAS_NVBF16() | ||
| #if _CCCL_HAS_NVFP8_E4M3() | ||
| test_fp_storage<__nv_fp8_e4m3>(); | ||
| #endif // _CCCL_HAS_NVFP8_E4M3() | ||
| #if _CCCL_HAS_NVFP8_E5M2() | ||
| test_fp_storage<__nv_fp8_e5m2>(); | ||
| #endif // _CCCL_HAS_NVFP8_E5M2() | ||
| #if _CCCL_HAS_NVFP8_E8M0() | ||
| test_fp_storage<__nv_fp8_e8m0>(); | ||
| #endif // _CCCL_HAS_NVFP8_E8M0() | ||
| #if _CCCL_HAS_NVFP6_E2M3() | ||
| test_fp_storage<__nv_fp6_e2m3>(); | ||
| #endif // _CCCL_HAS_NVFP6_E2M3() | ||
| #if _CCCL_HAS_NVFP6_E3M2() | ||
| test_fp_storage<__nv_fp6_e3m2>(); | ||
| #endif // _CCCL_HAS_NVFP6_E3M2() | ||
| #if _CCCL_HAS_NVFP4_E2M1() | ||
| test_fp_storage<__nv_fp4_e2m1>(); | ||
| #endif // _CCCL_HAS_NVFP4_E2M1() | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.