Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 9 | #ifndef NDNBOOST_MATH_TOOLS_SIGN_HPP |
| 10 | #define NDNBOOST_MATH_TOOLS_SIGN_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 11 | |
| 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 | |
| 20 | namespace ndnboost{ namespace math{ |
| 21 | |
| 22 | namespace detail { |
| 23 | |
| 24 | // signbit |
| 25 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 26 | #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 27 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 49 | typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 51 | NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 52 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 59 | typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 60 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 61 | NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 62 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 85 | typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 87 | NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 88 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 97 | typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 99 | NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 100 | 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 | |
| 109 | template<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 | |
| 118 | template <class T> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 119 | inline int sign NDNBOOST_NO_MACRO_EXPAND(const T& z) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 120 | { |
| 121 | return (z == 0) ? 0 : (ndnboost::math::signbit)(z) ? -1 : 1; |
| 122 | } |
| 123 | |
| 124 | template <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 | |
| 134 | template <class T, class U> |
| 135 | inline typename tools::promote_args<T, U>::type |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 136 | copysign NDNBOOST_NO_MACRO_EXPAND(const T& x, const U& y) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 137 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 138 | NDNBOOST_MATH_STD_USING |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 139 | 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 148 | #endif // NDNBOOST_MATH_TOOLS_SIGN_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 149 | |
| 150 | |