123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #ifndef BOOST_MATH_TOOLS_SIGN_HPP
- #define BOOST_MATH_TOOLS_SIGN_HPP
- #ifdef _MSC_VER
- #pragma once
- #endif
- #include <boost/math/tools/config.hpp>
- #include <boost/math/special_functions/math_fwd.hpp>
- #include <boost/math/special_functions/detail/fp_traits.hpp>
- namespace boost{ namespace math{
- namespace detail {
-
- #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
- template<class T>
- inline int signbit_impl(T x, native_tag const&)
- {
- return (std::signbit)(x) ? 1 : 0;
- }
- #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;
- }
- #if defined(__GNUC__) && (LDBL_MANT_DIG == 106)
-
-
-
-
-
-
-
- inline int signbit_impl(long double x, generic_tag<true> const&)
- {
- return (boost::math::signbit)(static_cast<double>(x));
- }
- inline int signbit_impl(long double x, generic_tag<false> const&)
- {
- return (boost::math::signbit)(static_cast<double>(x));
- }
- #endif
- template<class T>
- inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
- {
- typedef typename fp_traits<T>::type traits;
- 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 typename fp_traits<T>::type traits;
- typename traits::bits a;
- traits::get_bits(x,a);
- return a & traits::sign ? 1 : 0;
- }
-
-
-
-
- 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;
- }
- #if defined(__GNUC__) && (LDBL_MANT_DIG == 106)
-
-
-
-
-
- inline long double (changesign_impl)(long double x, generic_tag<true> const&)
- {
- double* pd = reinterpret_cast<double*>(&x);
- pd[0] = boost::math::changesign(pd[0]);
- pd[1] = boost::math::changesign(pd[1]);
- return x;
- }
- inline long double (changesign_impl)(long double x, generic_tag<false> const&)
- {
- double* pd = reinterpret_cast<double*>(&x);
- pd[0] = boost::math::changesign(pd[0]);
- pd[1] = boost::math::changesign(pd[1]);
- return x;
- }
- #endif
- template<class T>
- inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)
- {
- typedef typename fp_traits<T>::sign_change_type traits;
- 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 typename fp_traits<T>::sign_change_type traits;
- typename traits::bits a;
- traits::get_bits(x,a);
- a ^= traits::sign;
- traits::set_bits(x,a);
- return x;
- }
- }
- template<class T> int (signbit)(T x)
- {
- typedef typename detail::fp_traits<T>::type traits;
- typedef typename traits::method method;
-
- typedef typename tools::promote_args_permissive<T>::type result_type;
- return detail::signbit_impl(static_cast<result_type>(x), method());
- }
- template <class T>
- inline int sign BOOST_NO_MACRO_EXPAND(const T& z)
- {
- return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;
- }
- template <class T> typename tools::promote_args_permissive<T>::type (changesign)(const T& x)
- {
- typedef typename detail::fp_traits<T>::sign_change_type traits;
- typedef typename traits::method method;
-
- typedef typename tools::promote_args_permissive<T>::type result_type;
- return detail::changesign_impl(static_cast<result_type>(x), method());
- }
- template <class T, class U>
- inline typename tools::promote_args_permissive<T, U>::type
- copysign BOOST_NO_MACRO_EXPAND(const T& x, const U& y)
- {
- BOOST_MATH_STD_USING
- typedef typename tools::promote_args_permissive<T, U>::type result_type;
- return (boost::math::signbit)(static_cast<result_type>(x)) != (boost::math::signbit)(static_cast<result_type>(y))
- ? (boost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);
- }
- }
- }
- #endif
|