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