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