From 7991d0bb3293fde1523c65fc6105f39a32a02128 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Wed, 24 Jul 2024 13:37:01 -0400 Subject: [PATCH] Make trunc GPU compatible --- .../special_functions/detail/round_fwd.hpp | 17 ++++--- .../boost/math/special_functions/math_fwd.hpp | 8 +-- .../boost/math/special_functions/trunc.hpp | 50 ++++++++++--------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/include/boost/math/special_functions/detail/round_fwd.hpp b/include/boost/math/special_functions/detail/round_fwd.hpp index c58459e36..679e01c4d 100644 --- a/include/boost/math/special_functions/detail/round_fwd.hpp +++ b/include/boost/math/special_functions/detail/round_fwd.hpp @@ -1,4 +1,5 @@ // Copyright John Maddock 2008. +// Copyright Matt Borland 2024 // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. @@ -21,21 +22,21 @@ namespace boost { template - typename tools::promote_args::type trunc(const T& v, const Policy& pol); + BOOST_MATH_GPU_ENABLED typename tools::promote_args::type trunc(const T& v, const Policy& pol); template - typename tools::promote_args::type trunc(const T& v); + BOOST_MATH_GPU_ENABLED typename tools::promote_args::type trunc(const T& v); template - int itrunc(const T& v, const Policy& pol); + BOOST_MATH_GPU_ENABLED int itrunc(const T& v, const Policy& pol); template - int itrunc(const T& v); + BOOST_MATH_GPU_ENABLED int itrunc(const T& v); template - long ltrunc(const T& v, const Policy& pol); + BOOST_MATH_GPU_ENABLED long ltrunc(const T& v, const Policy& pol); template - long ltrunc(const T& v); + BOOST_MATH_GPU_ENABLED long ltrunc(const T& v); template - long long lltrunc(const T& v, const Policy& pol); + BOOST_MATH_GPU_ENABLED long long lltrunc(const T& v, const Policy& pol); template - long long lltrunc(const T& v); + BOOST_MATH_GPU_ENABLED long long lltrunc(const T& v); template typename tools::promote_args::type round(const T& v, const Policy& pol); template diff --git a/include/boost/math/special_functions/math_fwd.hpp b/include/boost/math/special_functions/math_fwd.hpp index b6fa2abf9..57775e627 100644 --- a/include/boost/math/special_functions/math_fwd.hpp +++ b/include/boost/math/special_functions/math_fwd.hpp @@ -1198,7 +1198,7 @@ namespace boost inline T modf(const T& v, long long* ipart){ using boost::math::modf; return modf(v, ipart, Policy()); }\ \ template \ - inline long long lltrunc(const T& v){ using boost::math::lltrunc; return lltrunc(v, Policy()); }\ + BOOST_MATH_GPU_ENABLED inline long long lltrunc(const T& v){ using boost::math::lltrunc; return lltrunc(v, Policy()); }\ \ template \ inline long long llround(const T& v){ using boost::math::llround; return llround(v, Policy()); }\ @@ -1607,13 +1607,13 @@ template \ inline long lround(const T& v){ using boost::math::lround; return lround(v, Policy()); }\ \ template \ - inline T trunc(const T& v){ using boost::math::trunc; return trunc(v, Policy()); }\ + BOOST_MATH_GPU_ENABLED inline T trunc(const T& v){ using boost::math::trunc; return trunc(v, Policy()); }\ \ template \ - inline int itrunc(const T& v){ using boost::math::itrunc; return itrunc(v, Policy()); }\ + BOOST_MATH_GPU_ENABLED inline int itrunc(const T& v){ using boost::math::itrunc; return itrunc(v, Policy()); }\ \ template \ - inline long ltrunc(const T& v){ using boost::math::ltrunc; return ltrunc(v, Policy()); }\ + BOOST_MATH_GPU_ENABLED inline long ltrunc(const T& v){ using boost::math::ltrunc; return ltrunc(v, Policy()); }\ \ template \ inline T modf(const T& v, T* ipart){ using boost::math::modf; return modf(v, ipart, Policy()); }\ diff --git a/include/boost/math/special_functions/trunc.hpp b/include/boost/math/special_functions/trunc.hpp index a084de560..4288225f1 100644 --- a/include/boost/math/special_functions/trunc.hpp +++ b/include/boost/math/special_functions/trunc.hpp @@ -27,7 +27,7 @@ namespace boost{ namespace math{ namespace detail{ template -inline tools::promote_args_t trunc(const T& v, const Policy& pol, const std::false_type&) +BOOST_MATH_GPU_ENABLED inline tools::promote_args_t trunc(const T& v, const Policy& pol, const std::false_type&) { BOOST_MATH_STD_USING using result_type = tools::promote_args_t; @@ -39,20 +39,21 @@ inline tools::promote_args_t trunc(const T& v, const Policy& pol, const std:: } template -inline tools::promote_args_t trunc(const T& v, const Policy&, const std::true_type&) +BOOST_MATH_GPU_ENABLED inline tools::promote_args_t trunc(const T& v, const Policy&, const std::true_type&) { return v; } -} +} // Namespace detail template -inline tools::promote_args_t trunc(const T& v, const Policy& pol) +BOOST_MATH_GPU_ENABLED inline tools::promote_args_t trunc(const T& v, const Policy& pol) { return detail::trunc(v, pol, std::integral_constant::value>()); } + template -inline tools::promote_args_t trunc(const T& v) +BOOST_MATH_GPU_ENABLED inline tools::promote_args_t trunc(const T& v) { return trunc(v, policies::policy<>()); } @@ -70,13 +71,13 @@ inline tools::promote_args_t trunc(const T& v) // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax // template -inline int itrunc(const T& v, const Policy& pol) +BOOST_MATH_GPU_ENABLED inline int itrunc(const T& v, const Policy& pol) { BOOST_MATH_STD_USING using result_type = tools::promote_args_t; result_type r = boost::math::trunc(v, pol); - #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP + #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) if constexpr (std::is_arithmetic_v #ifdef BOOST_MATH_FLOAT128_TYPE && !std::is_same_v @@ -100,7 +101,7 @@ inline int itrunc(const T& v, const Policy& pol) } } #else - static const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); + BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); if (r >= max_val || r < -max_val) { @@ -110,20 +111,21 @@ inline int itrunc(const T& v, const Policy& pol) return static_cast(r); } + template -inline int itrunc(const T& v) +BOOST_MATH_GPU_ENABLED inline int itrunc(const T& v) { return itrunc(v, policies::policy<>()); } template -inline long ltrunc(const T& v, const Policy& pol) +BOOST_MATH_GPU_ENABLED inline long ltrunc(const T& v, const Policy& pol) { BOOST_MATH_STD_USING using result_type = tools::promote_args_t; result_type r = boost::math::trunc(v, pol); - #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP + #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) if constexpr (std::is_arithmetic_v #ifdef BOOST_MATH_FLOAT128_TYPE && !std::is_same_v @@ -147,7 +149,7 @@ inline long ltrunc(const T& v, const Policy& pol) } } #else - static const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); + BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); if (r >= max_val || r < -max_val) { @@ -157,20 +159,21 @@ inline long ltrunc(const T& v, const Policy& pol) return static_cast(r); } + template -inline long ltrunc(const T& v) +BOOST_MATH_GPU_ENABLED inline long ltrunc(const T& v) { return ltrunc(v, policies::policy<>()); } template -inline long long lltrunc(const T& v, const Policy& pol) +BOOST_MATH_GPU_ENABLED inline long long lltrunc(const T& v, const Policy& pol) { BOOST_MATH_STD_USING using result_type = tools::promote_args_t; result_type r = boost::math::trunc(v, pol); - #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP + #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) if constexpr (std::is_arithmetic_v #ifdef BOOST_MATH_FLOAT128_TYPE && !std::is_same_v @@ -194,7 +197,7 @@ inline long long lltrunc(const T& v, const Policy& pol) } } #else - static const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); + BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast(1), std::numeric_limits::digits); if (r >= max_val || r < -max_val) { @@ -204,21 +207,22 @@ inline long long lltrunc(const T& v, const Policy& pol) return static_cast(r); } + template -inline long long lltrunc(const T& v) +BOOST_MATH_GPU_ENABLED inline long long lltrunc(const T& v) { return lltrunc(v, policies::policy<>()); } template -inline typename std::enable_if::value, int>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, int>::type iconvert(const T& v, const Policy&) { return static_cast(v); } template -inline typename std::enable_if::value, int>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, int>::type iconvert(const T& v, const Policy& pol) { using boost::math::itrunc; @@ -226,14 +230,14 @@ inline typename std::enable_if::value, int>::type } template -inline typename std::enable_if::value, long>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, long>::type lconvert(const T& v, const Policy&) { return static_cast(v); } template -inline typename std::enable_if::value, long>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, long>::type lconvert(const T& v, const Policy& pol) { using boost::math::ltrunc; @@ -241,14 +245,14 @@ inline typename std::enable_if::value, long>::ty } template -inline typename std::enable_if::value, long long>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, long long>::type llconvertert(const T& v, const Policy&) { return static_cast(v); } template -inline typename std::enable_if::value, long long>::type +BOOST_MATH_GPU_ENABLED inline typename std::enable_if::value, long long>::type llconvertert(const T& v, const Policy& pol) { using boost::math::lltrunc;