|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of libcu++, the C++ Standard Library for your entire system, |
| 4 | +// under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#ifndef _CUDA___CMATH_SINCOS_H |
| 12 | +#define _CUDA___CMATH_SINCOS_H |
| 13 | + |
| 14 | +#include <cuda/std/detail/__config> |
| 15 | + |
| 16 | +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| 17 | +# pragma GCC system_header |
| 18 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| 19 | +# pragma clang system_header |
| 20 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| 21 | +# pragma system_header |
| 22 | +#endif // no system header |
| 23 | + |
| 24 | +#include <cuda/std/__cmath/trigonometric_functions.h> |
| 25 | +#include <cuda/std/__concepts/concept_macros.h> |
| 26 | +#include <cuda/std/__floating_point/traits.h> |
| 27 | +#include <cuda/std/__type_traits/is_integral.h> |
| 28 | +#include <cuda/std/__type_traits/is_same.h> |
| 29 | + |
| 30 | +#include <cuda/std/__cccl/prologue.h> |
| 31 | + |
| 32 | +#if _CCCL_HAS_BUILTIN(__builtin_sincosf) || _CCCL_COMPILER(GCC) |
| 33 | +# define _CCCL_BUILTIN_SINCOSF(...) __builtin_sincosf(__VA_ARGS__) |
| 34 | +#endif // _CCCL_HAS_BUILTIN(__builtin_sincosf) || _CCCL_COMPILER(GCC) |
| 35 | + |
| 36 | +#if _CCCL_HAS_BUILTIN(__builtin_sincos) || _CCCL_COMPILER(GCC) |
| 37 | +# define _CCCL_BUILTIN_SINCOS(...) __builtin_sincos(__VA_ARGS__) |
| 38 | +#endif // _CCCL_HAS_BUILTIN(__builtin_sincos) || _CCCL_COMPILER(GCC) |
| 39 | + |
| 40 | +#if _CCCL_HAS_BUILTIN(__builtin_sincosl) || _CCCL_COMPILER(GCC) |
| 41 | +# define _CCCL_BUILTIN_SINCOSL(...) __builtin_sincosl(__VA_ARGS__) |
| 42 | +#endif // _CCCL_HAS_BUILTIN(__builtin_sincosl) || _CCCL_COMPILER(GCC) |
| 43 | + |
| 44 | +// clang-cuda crashes when using these builtins in device code |
| 45 | +#if _CCCL_CUDA_COMPILER(CLANG) && _CCCL_DEVICE_COMPILATION() |
| 46 | +# undef _CCCL_BUILTIN_SINCOSF |
| 47 | +# undef _CCCL_BUILTIN_SINCOS |
| 48 | +# undef _CCCL_BUILTIN_SINCOSL |
| 49 | +#endif // _CCCL_CUDA_COMPILER(CLANG) && _CCCL_DEVICE_COMPILATION() |
| 50 | + |
| 51 | +_CCCL_BEGIN_NAMESPACE_CUDA |
| 52 | + |
| 53 | +template <class _Tp> |
| 54 | +struct _CCCL_TYPE_VISIBILITY_DEFAULT sincos_result |
| 55 | +{ |
| 56 | + _Tp sin; |
| 57 | + _Tp cos; |
| 58 | +}; |
| 59 | + |
| 60 | +_CCCL_TEMPLATE(class _Tp) |
| 61 | +_CCCL_REQUIRES(::cuda::std::__is_fp_v<_Tp>) |
| 62 | +[[nodiscard]] _CCCL_API sincos_result<_Tp> sincos(_Tp __v) noexcept |
| 63 | +{ |
| 64 | + sincos_result<_Tp> __ret{}; |
| 65 | +#if defined(_CCCL_BUILTIN_SINCOSF) |
| 66 | + if constexpr (::cuda::std::is_same_v<_Tp, float>) |
| 67 | + { |
| 68 | + _CCCL_BUILTIN_SINCOSF(__v, &__ret.sin, &__ret.cos); |
| 69 | + return __ret; |
| 70 | + } |
| 71 | +#endif // _CCCL_BUILTIN_SINCOSF |
| 72 | +#if defined(_CCCL_BUILTIN_SINCOS) |
| 73 | + if constexpr (::cuda::std::is_same_v<_Tp, double>) |
| 74 | + { |
| 75 | + _CCCL_BUILTIN_SINCOS(__v, &__ret.sin, &__ret.cos); |
| 76 | + return __ret; |
| 77 | + } |
| 78 | +#endif // _CCCL_BUILTIN_SINCOS |
| 79 | +#if _CCCL_HAS_LONG_DOUBLE() && defined(_CCCL_BUILTIN_SINCOSL) |
| 80 | + if constexpr (::cuda::std::is_same_v<_Tp, long double>) |
| 81 | + { |
| 82 | + _CCCL_BUILTIN_SINCOSL(__v, &__ret.sin, &__ret.cos); |
| 83 | + return __ret; |
| 84 | + } |
| 85 | +#endif // _CCCL_HAS_LONG_DOUBLE() && _CCCL_BUILTIN_SINCOSL |
| 86 | + _CCCL_IF_NOT_CONSTEVAL_DEFAULT |
| 87 | + { |
| 88 | + if constexpr (::cuda::std::is_same_v<_Tp, float>) |
| 89 | + { |
| 90 | + NV_IF_TARGET(NV_IS_DEVICE, (::sincosf(__v, &__ret.sin, &__ret.cos); return __ret;)) |
| 91 | + } |
| 92 | + if constexpr (::cuda::std::is_same_v<_Tp, double>) |
| 93 | + { |
| 94 | + NV_IF_TARGET(NV_IS_DEVICE, (::sincos(__v, &__ret.sin, &__ret.cos); return __ret;)) |
| 95 | + } |
| 96 | + } |
| 97 | + __ret.sin = ::cuda::std::sin(__v); |
| 98 | + __ret.cos = ::cuda::std::cos(__v); |
| 99 | + return __ret; |
| 100 | +} |
| 101 | + |
| 102 | +_CCCL_TEMPLATE(class _Tp) |
| 103 | +_CCCL_REQUIRES(::cuda::std::is_integral_v<_Tp>) |
| 104 | +[[nodiscard]] _CCCL_API sincos_result<double> sincos(_Tp __v) noexcept |
| 105 | +{ |
| 106 | + return ::cuda::sincos(static_cast<double>(__v)); |
| 107 | +} |
| 108 | + |
| 109 | +_CCCL_END_NAMESPACE_CUDA |
| 110 | + |
| 111 | +#include <cuda/std/__cccl/epilogue.h> |
| 112 | + |
| 113 | +#endif // _CUDA___CMATH_SINCOS_H |
0 commit comments