blob: e0fbd9eed6c67289fa0689890f97906a386a4c3e [file] [log] [blame]
// (C) Copyright John Maddock 2006.
// (C) Copyright Johan Rade 2006.
// (C) Copyright Paul A. Bristow 2011 (added changesign).
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef NDNBOOST_MATH_TOOLS_SIGN_HPP
#define NDNBOOST_MATH_TOOLS_SIGN_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include <ndnboost/math/tools/config.hpp>
#include <ndnboost/math/special_functions/math_fwd.hpp>
#include <ndnboost/math/special_functions/detail/fp_traits.hpp>
namespace ndnboost{ namespace math{
namespace detail {
// signbit
#ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY
template<class T>
inline int signbit_impl(T x, native_tag const&)
{
return (std::signbit)(x);
}
#endif
template<class T>
inline int signbit_impl(T x, generic_tag<true> const&)
{
return x < 0;
}
template<class T>
inline int signbit_impl(T x, generic_tag<false> const&)
{
return x < 0;
}
template<class T>
inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
{
typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
NDNBOOST_DEDUCED_TYPENAME traits::bits a;
traits::get_bits(x,a);
return a & traits::sign ? 1 : 0;
}
template<class T>
inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
{
typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
NDNBOOST_DEDUCED_TYPENAME traits::bits a;
traits::get_bits(x,a);
return a & traits::sign ? 1 : 0;
}
// Changesign
template<class T>
inline T (changesign_impl)(T x, generic_tag<true> const&)
{
return -x;
}
template<class T>
inline T (changesign_impl)(T x, generic_tag<false> const&)
{
return -x;
}
template<class T>
inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)
{
typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
NDNBOOST_DEDUCED_TYPENAME traits::bits a;
traits::get_bits(x,a);
a ^= traits::sign;
traits::set_bits(x,a);
return x;
}
template<class T>
inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&)
{
typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
NDNBOOST_DEDUCED_TYPENAME traits::bits a;
traits::get_bits(x,a);
a ^= traits::sign;
traits::set_bits(x,a);
return x;
}
} // namespace detail
template<class T> int (signbit)(T x)
{
typedef typename detail::fp_traits<T>::type traits;
typedef typename traits::method method;
// typedef typename ndnboost::is_floating_point<T>::type fp_tag;
typedef typename tools::promote_args<T>::type result_type;
return detail::signbit_impl(static_cast<result_type>(x), method());
}
template <class T>
inline int sign NDNBOOST_NO_MACRO_EXPAND(const T& z)
{
return (z == 0) ? 0 : (ndnboost::math::signbit)(z) ? -1 : 1;
}
template <class T> typename tools::promote_args<T>::type (changesign)(const T& x)
{ //!< \brief return unchanged binary pattern of x, except for change of sign bit.
typedef typename detail::fp_traits<T>::sign_change_type traits;
typedef typename traits::method method;
// typedef typename ndnboost::is_floating_point<T>::type fp_tag;
typedef typename tools::promote_args<T>::type result_type;
return detail::changesign_impl(static_cast<result_type>(x), method());
}
template <class T, class U>
inline typename tools::promote_args<T, U>::type
copysign NDNBOOST_NO_MACRO_EXPAND(const T& x, const U& y)
{
NDNBOOST_MATH_STD_USING
typedef typename tools::promote_args<T, U>::type result_type;
return (ndnboost::math::signbit)(static_cast<result_type>(x)) != (ndnboost::math::signbit)(static_cast<result_type>(y))
? (ndnboost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);
}
} // namespace math
} // namespace ndnboost
#endif // NDNBOOST_MATH_TOOLS_SIGN_HPP