1 // (C) Copyright John Maddock 2006.
2 // (C) Copyright Johan Rade 2006.
3 // (C) Copyright Paul A. Bristow 2011 (added changesign).
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)
9 #ifndef BOOST_MATH_TOOLS_SIGN_HPP
10 #define BOOST_MATH_TOOLS_SIGN_HPP
16 #include <boost/math/tools/config.hpp>
17 #include <boost/math/special_functions/math_fwd.hpp>
18 #include <boost/math/special_functions/detail/fp_traits.hpp>
20 namespace boost{ namespace math{
26 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
28 inline int signbit_impl(T x, native_tag const&)
30 return (std::signbit)(x) ? 1 : 0;
34 // Generic versions first, note that these do not handle
35 // signed zero or NaN.
38 inline int signbit_impl(T x, generic_tag<true> const&)
44 inline int signbit_impl(T x, generic_tag<false> const&)
49 #if defined(__GNUC__) && (LDBL_MANT_DIG == 106)
51 // Special handling for GCC's "double double" type,
52 // in this case the sign is the same as the sign we
53 // get by casting to double, no overflow/underflow
54 // can occur since the exponents are the same magnitude
57 inline int signbit_impl(long double x, generic_tag<true> const&)
59 return (boost::math::signbit)(static_cast<double>(x));
61 inline int signbit_impl(long double x, generic_tag<false> const&)
63 return (boost::math::signbit)(static_cast<double>(x));
68 inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
70 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
72 BOOST_DEDUCED_TYPENAME traits::bits a;
73 traits::get_bits(x,a);
74 return a & traits::sign ? 1 : 0;
78 inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
80 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
82 BOOST_DEDUCED_TYPENAME traits::bits a;
83 traits::get_bits(x,a);
85 return a & traits::sign ? 1 : 0;
90 // Generic versions first, note that these do not handle
91 // signed zero or NaN.
94 inline T (changesign_impl)(T x, generic_tag<true> const&)
100 inline T (changesign_impl)(T x, generic_tag<false> const&)
104 #if defined(__GNUC__) && (LDBL_MANT_DIG == 106)
106 // Special handling for GCC's "double double" type,
107 // in this case we need to change the sign of both
108 // components of the "double double":
110 inline long double (changesign_impl)(long double x, generic_tag<true> const&)
112 double* pd = reinterpret_cast<double*>(&x);
113 pd[0] = boost::math::changesign(pd[0]);
114 pd[1] = boost::math::changesign(pd[1]);
117 inline long double (changesign_impl)(long double x, generic_tag<false> const&)
119 double* pd = reinterpret_cast<double*>(&x);
120 pd[0] = boost::math::changesign(pd[0]);
121 pd[1] = boost::math::changesign(pd[1]);
127 inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)
129 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
131 BOOST_DEDUCED_TYPENAME traits::bits a;
132 traits::get_bits(x,a);
134 traits::set_bits(x,a);
139 inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&)
141 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
143 BOOST_DEDUCED_TYPENAME traits::bits a;
144 traits::get_bits(x,a);
146 traits::set_bits(x,a);
151 } // namespace detail
153 template<class T> int (signbit)(T x)
155 typedef typename detail::fp_traits<T>::type traits;
156 typedef typename traits::method method;
157 // typedef typename boost::is_floating_point<T>::type fp_tag;
158 typedef typename tools::promote_args_permissive<T>::type result_type;
159 return detail::signbit_impl(static_cast<result_type>(x), method());
163 inline int sign BOOST_NO_MACRO_EXPAND(const T& z)
165 return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;
168 template <class T> typename tools::promote_args_permissive<T>::type (changesign)(const T& x)
169 { //!< \brief return unchanged binary pattern of x, except for change of sign bit.
170 typedef typename detail::fp_traits<T>::sign_change_type traits;
171 typedef typename traits::method method;
172 // typedef typename boost::is_floating_point<T>::type fp_tag;
173 typedef typename tools::promote_args_permissive<T>::type result_type;
175 return detail::changesign_impl(static_cast<result_type>(x), method());
178 template <class T, class U>
179 inline typename tools::promote_args_permissive<T, U>::type
180 copysign BOOST_NO_MACRO_EXPAND(const T& x, const U& y)
183 typedef typename tools::promote_args_permissive<T, U>::type result_type;
184 return (boost::math::signbit)(static_cast<result_type>(x)) != (boost::math::signbit)(static_cast<result_type>(y))
185 ? (boost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);
192 #endif // BOOST_MATH_TOOLS_SIGN_HPP