blob: e0fbd9eed6c67289fa0689890f97906a386a4c3e [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright John Maddock 2006.
2// (C) Copyright Johan Rade 2006.
3// (C) Copyright Paul A. Bristow 2011 (added changesign).
4
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -07009#ifndef NDNBOOST_MATH_TOOLS_SIGN_HPP
10#define NDNBOOST_MATH_TOOLS_SIGN_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070011
12#ifdef _MSC_VER
13#pragma once
14#endif
15
16#include <ndnboost/math/tools/config.hpp>
17#include <ndnboost/math/special_functions/math_fwd.hpp>
18#include <ndnboost/math/special_functions/detail/fp_traits.hpp>
19
20namespace ndnboost{ namespace math{
21
22namespace detail {
23
24 // signbit
25
Jeff Thompson3d613fd2013-10-15 15:39:04 -070026#ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070027 template<class T>
28 inline int signbit_impl(T x, native_tag const&)
29 {
30 return (std::signbit)(x);
31 }
32#endif
33
34 template<class T>
35 inline int signbit_impl(T x, generic_tag<true> const&)
36 {
37 return x < 0;
38 }
39
40 template<class T>
41 inline int signbit_impl(T x, generic_tag<false> const&)
42 {
43 return x < 0;
44 }
45
46 template<class T>
47 inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
48 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070049 typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070050
Jeff Thompson3d613fd2013-10-15 15:39:04 -070051 NDNBOOST_DEDUCED_TYPENAME traits::bits a;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070052 traits::get_bits(x,a);
53 return a & traits::sign ? 1 : 0;
54 }
55
56 template<class T>
57 inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
58 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070059 typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070060
Jeff Thompson3d613fd2013-10-15 15:39:04 -070061 NDNBOOST_DEDUCED_TYPENAME traits::bits a;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070062 traits::get_bits(x,a);
63
64 return a & traits::sign ? 1 : 0;
65 }
66
67 // Changesign
68
69 template<class T>
70 inline T (changesign_impl)(T x, generic_tag<true> const&)
71 {
72 return -x;
73 }
74
75 template<class T>
76 inline T (changesign_impl)(T x, generic_tag<false> const&)
77 {
78 return -x;
79 }
80
81
82 template<class T>
83 inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)
84 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070085 typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070086
Jeff Thompson3d613fd2013-10-15 15:39:04 -070087 NDNBOOST_DEDUCED_TYPENAME traits::bits a;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070088 traits::get_bits(x,a);
89 a ^= traits::sign;
90 traits::set_bits(x,a);
91 return x;
92 }
93
94 template<class T>
95 inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&)
96 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070097 typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070098
Jeff Thompson3d613fd2013-10-15 15:39:04 -070099 NDNBOOST_DEDUCED_TYPENAME traits::bits a;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700100 traits::get_bits(x,a);
101 a ^= traits::sign;
102 traits::set_bits(x,a);
103 return x;
104 }
105
106
107} // namespace detail
108
109template<class T> int (signbit)(T x)
110{
111 typedef typename detail::fp_traits<T>::type traits;
112 typedef typename traits::method method;
113 // typedef typename ndnboost::is_floating_point<T>::type fp_tag;
114 typedef typename tools::promote_args<T>::type result_type;
115 return detail::signbit_impl(static_cast<result_type>(x), method());
116}
117
118template <class T>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700119inline int sign NDNBOOST_NO_MACRO_EXPAND(const T& z)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700120{
121 return (z == 0) ? 0 : (ndnboost::math::signbit)(z) ? -1 : 1;
122}
123
124template <class T> typename tools::promote_args<T>::type (changesign)(const T& x)
125{ //!< \brief return unchanged binary pattern of x, except for change of sign bit.
126 typedef typename detail::fp_traits<T>::sign_change_type traits;
127 typedef typename traits::method method;
128 // typedef typename ndnboost::is_floating_point<T>::type fp_tag;
129 typedef typename tools::promote_args<T>::type result_type;
130
131 return detail::changesign_impl(static_cast<result_type>(x), method());
132}
133
134template <class T, class U>
135inline typename tools::promote_args<T, U>::type
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136 copysign NDNBOOST_NO_MACRO_EXPAND(const T& x, const U& y)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700137{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138 NDNBOOST_MATH_STD_USING
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700139 typedef typename tools::promote_args<T, U>::type result_type;
140 return (ndnboost::math::signbit)(static_cast<result_type>(x)) != (ndnboost::math::signbit)(static_cast<result_type>(y))
141 ? (ndnboost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);
142}
143
144} // namespace math
145} // namespace ndnboost
146
147
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700148#endif // NDNBOOST_MATH_TOOLS_SIGN_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700149
150