| 1 |
// (C) Copyright John Maddock 2006. |
| 2 |
// (C) Copyright Johan Rade 2006. |
| 3 |
// (C) Copyright Paul A. Bristow 2011 (added changesign). |
| 4 |
// (C) Copyright Matt Borland 2024 |
| 5 |
|
| 6 |
// Use, modification and distribution are subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying file |
| 8 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 9 |
|
| 10 |
#ifndef BOOST_MATH_TOOLS_SIGN_HPP |
| 11 |
#define BOOST_MATH_TOOLS_SIGN_HPP |
| 12 |
|
| 13 |
#ifdef _MSC_VER |
| 14 |
#pragma once |
| 15 |
#endif |
| 16 |
|
| 17 |
#ifndef __CUDACC_RTC__ |
| 18 |
|
| 19 |
#include <boost/math/tools/config.hpp> |
| 20 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 21 |
#include <boost/math/special_functions/detail/fp_traits.hpp> |
| 22 |
|
| 23 |
namespace boost{ namespace math{ |
| 24 |
|
| 25 |
namespace detail { |
| 26 |
|
| 27 |
// signbit |
| 28 |
|
| 29 |
#ifdef BOOST_MATH_USE_STD_FPCLASSIFY |
| 30 |
template<class T> |
| 31 |
BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, native_tag const&) |
| 32 |
{ |
| 33 |
using std::signbit; |
| 34 |
return (signbit)(x) ? 1 : 0; |
| 35 |
} |
| 36 |
#endif |
| 37 |
|
| 38 |
// Generic versions first, note that these do not handle |
| 39 |
// signed zero or NaN. |
| 40 |
|
| 41 |
template<class T> |
| 42 |
BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, generic_tag<true> const&) |
| 43 |
{ |
| 44 |
return x < 0; |
| 45 |
} |
| 46 |
|
| 47 |
template<class T> |
| 48 |
BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, generic_tag<false> const&) |
| 49 |
{ |
| 50 |
return x < 0; |
| 51 |
} |
| 52 |
|
| 53 |
#if defined(__GNUC__) && (LDBL_MANT_DIG == 106) |
| 54 |
// |
| 55 |
// Special handling for GCC's "double double" type, |
| 56 |
// in this case the sign is the same as the sign we |
| 57 |
// get by casting to double, no overflow/underflow |
| 58 |
// can occur since the exponents are the same magnitude |
| 59 |
// for the two types: |
| 60 |
// |
| 61 |
inline int signbit_impl(long double x, generic_tag<true> const&) |
| 62 |
{ |
| 63 |
return (boost::math::signbit)(static_cast<double>(x)); |
| 64 |
} |
| 65 |
inline int signbit_impl(long double x, generic_tag<false> const&) |
| 66 |
{ |
| 67 |
return (boost::math::signbit)(static_cast<double>(x)); |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
template<class T> |
| 72 |
BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, ieee_copy_all_bits_tag const&) |
| 73 |
{ |
| 74 |
typedef typename fp_traits<T>::type traits; |
| 75 |
|
| 76 |
typename traits::bits a; |
| 77 |
traits::get_bits(x,a); |
| 78 |
return a & traits::sign ? 1 : 0; |
| 79 |
} |
| 80 |
|
| 81 |
template<class T> |
| 82 |
BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&) |
| 83 |
{ |
| 84 |
typedef typename fp_traits<T>::type traits; |
| 85 |
|
| 86 |
typename traits::bits a; |
| 87 |
traits::get_bits(x,a); |
| 88 |
|
| 89 |
return a & traits::sign ? 1 : 0; |
| 90 |
} |
| 91 |
|
| 92 |
// Changesign |
| 93 |
|
| 94 |
// Generic versions first, note that these do not handle |
| 95 |
// signed zero or NaN. |
| 96 |
|
| 97 |
template<class T> |
| 98 |
BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, generic_tag<true> const&) |
| 99 |
{ |
| 100 |
return -x; |
| 101 |
} |
| 102 |
|
| 103 |
template<class T> |
| 104 |
BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, generic_tag<false> const&) |
| 105 |
{ |
| 106 |
return -x; |
| 107 |
} |
| 108 |
#if defined(__GNUC__) && (LDBL_MANT_DIG == 106) |
| 109 |
// |
| 110 |
// Special handling for GCC's "double double" type, |
| 111 |
// in this case we need to change the sign of both |
| 112 |
// components of the "double double": |
| 113 |
// |
| 114 |
inline long double (changesign_impl)(long double x, generic_tag<true> const&) |
| 115 |
{ |
| 116 |
double* pd = reinterpret_cast<double*>(&x); |
| 117 |
pd[0] = boost::math::changesign(pd[0]); |
| 118 |
pd[1] = boost::math::changesign(pd[1]); |
| 119 |
return x; |
| 120 |
} |
| 121 |
inline long double (changesign_impl)(long double x, generic_tag<false> const&) |
| 122 |
{ |
| 123 |
double* pd = reinterpret_cast<double*>(&x); |
| 124 |
pd[0] = boost::math::changesign(pd[0]); |
| 125 |
pd[1] = boost::math::changesign(pd[1]); |
| 126 |
return x; |
| 127 |
} |
| 128 |
#endif |
| 129 |
|
| 130 |
template<class T> |
| 131 |
BOOST_MATH_GPU_ENABLED inline T changesign_impl(T x, ieee_copy_all_bits_tag const&) |
| 132 |
{ |
| 133 |
typedef typename fp_traits<T>::sign_change_type traits; |
| 134 |
|
| 135 |
typename traits::bits a; |
| 136 |
traits::get_bits(x,a); |
| 137 |
a ^= traits::sign; |
| 138 |
traits::set_bits(x,a); |
| 139 |
return x; |
| 140 |
} |
| 141 |
|
| 142 |
template<class T> |
| 143 |
BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&) |
| 144 |
{ |
| 145 |
typedef typename fp_traits<T>::sign_change_type traits; |
| 146 |
|
| 147 |
typename traits::bits a; |
| 148 |
traits::get_bits(x,a); |
| 149 |
a ^= traits::sign; |
| 150 |
traits::set_bits(x,a); |
| 151 |
return x; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
} // namespace detail |
| 156 |
|
| 157 |
template<class T> |
| 158 |
BOOST_MATH_GPU_ENABLED int (signbit)(T x) |
| 159 |
{ |
| 160 |
typedef typename detail::fp_traits<T>::type traits; |
| 161 |
typedef typename traits::method method; |
| 162 |
// typedef typename boost::is_floating_point<T>::type fp_tag; |
| 163 |
typedef typename tools::promote_args_permissive<T>::type result_type; |
| 164 |
return detail::signbit_impl(static_cast<result_type>(x), method()); |
| 165 |
} |
| 166 |
|
| 167 |
template <class T> |
| 168 |
BOOST_MATH_GPU_ENABLED inline int sign BOOST_NO_MACRO_EXPAND(const T& z) |
| 169 |
{ |
| 170 |
return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1; |
| 171 |
} |
| 172 |
|
| 173 |
template <class T> |
| 174 |
BOOST_MATH_GPU_ENABLED typename tools::promote_args_permissive<T>::type (changesign)(const T& x) |
| 175 |
{ //!< \brief return unchanged binary pattern of x, except for change of sign bit. |
| 176 |
typedef typename detail::fp_traits<T>::sign_change_type traits; |
| 177 |
typedef typename traits::method method; |
| 178 |
// typedef typename boost::is_floating_point<T>::type fp_tag; |
| 179 |
typedef typename tools::promote_args_permissive<T>::type result_type; |
| 180 |
|
| 181 |
return detail::changesign_impl(static_cast<result_type>(x), method()); |
| 182 |
} |
| 183 |
|
| 184 |
template <class T, class U> |
| 185 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args_permissive<T, U>::type |
| 186 |
copysign BOOST_NO_MACRO_EXPAND(const T& x, const U& y) |
| 187 |
{ |
| 188 |
BOOST_MATH_STD_USING |
| 189 |
typedef typename tools::promote_args_permissive<T, U>::type result_type; |
| 190 |
return (boost::math::signbit)(static_cast<result_type>(x)) != (boost::math::signbit)(static_cast<result_type>(y)) |
| 191 |
? (boost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x); |
| 192 |
} |
| 193 |
|
| 194 |
} // namespace math |
| 195 |
} // namespace boost |
| 196 |
|
| 197 |
#else // NVRTC alias versions |
| 198 |
|
| 199 |
#include <boost/math/tools/config.hpp> |
| 200 |
|
| 201 |
namespace boost { |
| 202 |
namespace math { |
| 203 |
|
| 204 |
template <typename T> |
| 205 |
BOOST_MATH_GPU_ENABLED int signbit(T x) |
| 206 |
{ |
| 207 |
return ::signbit(x); |
| 208 |
} |
| 209 |
|
| 210 |
template <typename T> |
| 211 |
BOOST_MATH_GPU_ENABLED T changesign(T x) |
| 212 |
{ |
| 213 |
return -x; |
| 214 |
} |
| 215 |
|
| 216 |
template <typename T> |
| 217 |
BOOST_MATH_GPU_ENABLED T copysign(T x, T y) |
| 218 |
{ |
| 219 |
return ::copysign(x, y); |
| 220 |
} |
| 221 |
|
| 222 |
template <> |
| 223 |
BOOST_MATH_GPU_ENABLED float copysign(float x, float y) |
| 224 |
{ |
| 225 |
return ::copysignf(x, y); |
| 226 |
} |
| 227 |
|
| 228 |
template <typename T> |
| 229 |
BOOST_MATH_GPU_ENABLED T sign(T z) |
| 230 |
{ |
| 231 |
return (z == 0) ? 0 : ::signbit(z) ? -1 : 1; |
| 232 |
} |
| 233 |
|
| 234 |
} // namespace math |
| 235 |
} // namespace boost |
| 236 |
|
| 237 |
#endif // __CUDACC_RTC__ |
| 238 |
|
| 239 |
#endif // BOOST_MATH_TOOLS_SIGN_HPP |
| 240 |
|
| 241 |
|