sign.hpp 6.4 KB

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