xref: /openbsd-src/gnu/llvm/clang/lib/Headers/cuda_wrappers/cmath (revision 12c855180aad702bbcca06e0398d774beeafb155)
1*12c85518Srobert/*===---- cmath - CUDA wrapper for <cmath> ---------------------------------===
2*12c85518Srobert *
3*12c85518Srobert * Permission is hereby granted, free of charge, to any person obtaining a copy
4*12c85518Srobert * of this software and associated documentation files (the "Software"), to deal
5*12c85518Srobert * in the Software without restriction, including without limitation the rights
6*12c85518Srobert * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*12c85518Srobert * copies of the Software, and to permit persons to whom the Software is
8*12c85518Srobert * furnished to do so, subject to the following conditions:
9*12c85518Srobert *
10*12c85518Srobert * The above copyright notice and this permission notice shall be included in
11*12c85518Srobert * all copies or substantial portions of the Software.
12*12c85518Srobert *
13*12c85518Srobert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*12c85518Srobert * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*12c85518Srobert * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*12c85518Srobert * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*12c85518Srobert * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18*12c85518Srobert * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19*12c85518Srobert * THE SOFTWARE.
20*12c85518Srobert *
21*12c85518Srobert *===-----------------------------------------------------------------------===
22*12c85518Srobert */
23*12c85518Srobert
24*12c85518Srobert#ifndef __CLANG_CUDA_WRAPPERS_CMATH
25*12c85518Srobert#define __CLANG_CUDA_WRAPPERS_CMATH
26*12c85518Srobert
27*12c85518Srobert#include_next <cmath>
28*12c85518Srobert
29*12c85518Srobert#if defined(_LIBCPP_STD_VER)
30*12c85518Srobert
31*12c85518Srobert// libc++ will need long double variants of these functions, but CUDA does not
32*12c85518Srobert// provide them. We'll provide their declarations, which should allow the
33*12c85518Srobert// headers to parse, but would not allow accidental use of them on a GPU.
34*12c85518Srobert
35*12c85518Srobert__attribute__((device)) long double logb(long double);
36*12c85518Srobert__attribute__((device)) long double scalbn(long double, int);
37*12c85518Srobert
38*12c85518Srobertnamespace std {
39*12c85518Srobert
40*12c85518Srobert// For __constexpr_fmin/fmax we only need device-side overloads before c++14
41*12c85518Srobert// where they are not constexpr.
42*12c85518Srobert#if _LIBCPP_STD_VER < 14
43*12c85518Srobert
44*12c85518Srobert__attribute__((device))
45*12c85518Srobertinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 float __constexpr_fmax(float __x, float __y) _NOEXCEPT {
46*12c85518Srobert  return __builtin_fmaxf(__x, __y);
47*12c85518Srobert}
48*12c85518Srobert
49*12c85518Srobert__attribute__((device))
50*12c85518Srobertinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 double __constexpr_fmax(double __x, double __y) _NOEXCEPT {
51*12c85518Srobert  return __builtin_fmax(__x, __y);
52*12c85518Srobert}
53*12c85518Srobert
54*12c85518Srobert__attribute__((device))
55*12c85518Srobertinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 long double
56*12c85518Srobert__constexpr_fmax(long double __x, long double __y) _NOEXCEPT {
57*12c85518Srobert  return __builtin_fmaxl(__x, __y);
58*12c85518Srobert}
59*12c85518Srobert
60*12c85518Sroberttemplate <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
61*12c85518Srobert__attribute__((device))
62*12c85518Srobert_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __promote<_Tp, _Up>::type
63*12c85518Srobert__constexpr_fmax(_Tp __x, _Up __y) _NOEXCEPT {
64*12c85518Srobert  using __result_type = typename __promote<_Tp, _Up>::type;
65*12c85518Srobert  return std::__constexpr_fmax(static_cast<__result_type>(__x), static_cast<__result_type>(__y));
66*12c85518Srobert}
67*12c85518Srobert#endif // _LIBCPP_STD_VER < 14
68*12c85518Srobert
69*12c85518Srobert// For logb/scalbn templates we must always provide device overloads because
70*12c85518Srobert// libc++ implementation uses __builtin_XXX which gets translated into a libcall
71*12c85518Srobert// which we can't handle on GPU. We need to forward those to CUDA-provided
72*12c85518Srobert// implementations.
73*12c85518Srobert
74*12c85518Sroberttemplate <class _Tp>
75*12c85518Srobert__attribute__((device))
76*12c85518Srobert_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __constexpr_logb(_Tp __x) {
77*12c85518Srobert  return ::logb(__x);
78*12c85518Srobert}
79*12c85518Srobert
80*12c85518Sroberttemplate <class _Tp>
81*12c85518Srobert__attribute__((device))
82*12c85518Srobert_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp __constexpr_scalbn(_Tp __x, int __exp) {
83*12c85518Srobert  return ::scalbn(__x, __exp);
84*12c85518Srobert}
85*12c85518Srobert
86*12c85518Srobert} // namespace std//
87*12c85518Srobert
88*12c85518Srobert#endif // _LIBCPP_STD_VER
89*12c85518Srobert
90*12c85518Srobert#endif // include guard
91