brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.4 KiB · 4f76522 Raw
242 lines · plain
1//  (C) Copyright John Maddock 2006.2//  (C) Copyright Johan Rade 2006.3//  (C) Copyright Paul A. Bristow 2011 (added changesign).4//  (C) Copyright Matt Borland 20245 6//  Use, modification and distribution are subject to the7//  Boost Software License, Version 1.0. (See accompanying file8//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)9 10#ifndef BOOST_MATH_TOOLS_SIGN_HPP11#define BOOST_MATH_TOOLS_SIGN_HPP12 13#ifdef _MSC_VER14#pragma once15#endif16 17#ifndef __CUDACC_RTC__18 19#include <boost/math/tools/config.hpp>20#include <boost/math/special_functions/math_fwd.hpp>21#include <boost/math/special_functions/detail/fp_traits.hpp>22 23namespace boost{ namespace math{ 24 25namespace detail {26 27  // signbit28 29#ifdef BOOST_MATH_USE_STD_FPCLASSIFY30    template<class T> 31    BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, native_tag const&)32    {33        using std::signbit;34        return (signbit)(x) ? 1 : 0;35    }36#endif37 38    // Generic versions first, note that these do not handle39    // signed zero or NaN.40 41    template<class T>42    BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, generic_tag<true> const&)43    {44        return x < 0;45    }46 47    template<class T> 48    BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, generic_tag<false> const&)49    {50        return x < 0;51    }52 53#if defined(__GNUC__) && (LDBL_MANT_DIG == 106)54    //55    // Special handling for GCC's "double double" type, 56    // in this case the sign is the same as the sign we57    // get by casting to double, no overflow/underflow58    // can occur since the exponents are the same magnitude59    // for the two types:60    //61    inline int signbit_impl(long double x, generic_tag<true> const&)62    {63       return (boost::math::signbit)(static_cast<double>(x));64    }65    inline int signbit_impl(long double x, generic_tag<false> const&)66    {67       return (boost::math::signbit)(static_cast<double>(x));68    }69#endif70 71    template<class T>72    BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)73    {74        typedef typename fp_traits<T>::type traits;75 76        typename traits::bits a;77        traits::get_bits(x,a);78        return a & traits::sign ? 1 : 0;79    }80 81    template<class T> 82    BOOST_MATH_GPU_ENABLED inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)83    {84        typedef typename fp_traits<T>::type traits;85 86        typename traits::bits a;87        traits::get_bits(x,a);88 89        return a & traits::sign ? 1 : 0;90    }91 92    // Changesign93    94    // Generic versions first, note that these do not handle95    // signed zero or NaN.96 97    template<class T>98    BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, generic_tag<true> const&)99    {100        return -x;101    }102 103    template<class T>104    BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, generic_tag<false> const&)105    {106        return -x;107    }108#if defined(__GNUC__) && (LDBL_MANT_DIG == 106)109    //110    // Special handling for GCC's "double double" type, 111    // in this case we need to change the sign of both112    // components of the "double double":113    //114    inline long double (changesign_impl)(long double x, generic_tag<true> const&)115    {116       double* pd = reinterpret_cast<double*>(&x);117       pd[0] = boost::math::changesign(pd[0]);118       pd[1] = boost::math::changesign(pd[1]);119       return x;120    }121    inline long double (changesign_impl)(long double x, generic_tag<false> const&)122    {123       double* pd = reinterpret_cast<double*>(&x);124       pd[0] = boost::math::changesign(pd[0]);125       pd[1] = boost::math::changesign(pd[1]);126       return x;127    }128#endif129 130    template<class T>131    BOOST_MATH_GPU_ENABLED inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)132    {133        typedef typename fp_traits<T>::sign_change_type traits;134 135        typename traits::bits a;136        traits::get_bits(x,a);137        a ^= traits::sign;138        traits::set_bits(x,a);139        return x;140    }141 142    template<class T>143    BOOST_MATH_GPU_ENABLED inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&)144    {145        typedef typename fp_traits<T>::sign_change_type traits;146 147        typename traits::bits a;148        traits::get_bits(x,a);149        a ^= traits::sign;150        traits::set_bits(x,a);151        return x;152    }153 154 155}   // namespace detail156 157template<class T> 158BOOST_MATH_GPU_ENABLED int (signbit)(T x)159{ 160   typedef typename detail::fp_traits<T>::type traits;161   typedef typename traits::method method;162   // typedef typename boost::is_floating_point<T>::type fp_tag;163   typedef typename tools::promote_args_permissive<T>::type result_type;164   return detail::signbit_impl(static_cast<result_type>(x), method());165}166 167template <class T>168BOOST_MATH_GPU_ENABLED inline int sign BOOST_NO_MACRO_EXPAND(const T& z)169{170   return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;171}172 173template <class T> 174BOOST_MATH_GPU_ENABLED typename tools::promote_args_permissive<T>::type (changesign)(const T& x)175{ //!< \brief return unchanged binary pattern of x, except for change of sign bit. 176   typedef typename detail::fp_traits<T>::sign_change_type traits;177   typedef typename traits::method method;178   // typedef typename boost::is_floating_point<T>::type fp_tag;179   typedef typename tools::promote_args_permissive<T>::type result_type;180 181   return detail::changesign_impl(static_cast<result_type>(x), method());182}183 184template <class T, class U>185BOOST_MATH_GPU_ENABLED inline typename tools::promote_args_permissive<T, U>::type 186   copysign BOOST_NO_MACRO_EXPAND(const T& x, const U& y)187{188   BOOST_MATH_STD_USING189   typedef typename tools::promote_args_permissive<T, U>::type result_type;190   return (boost::math::signbit)(static_cast<result_type>(x)) != (boost::math::signbit)(static_cast<result_type>(y)) 191      ? (boost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);192}193 194} // namespace math195} // namespace boost196 197#else // NVRTC alias versions198 199#include <boost/math/tools/config.hpp>200 201namespace boost {202namespace math {203 204template <typename T>205BOOST_MATH_GPU_ENABLED int signbit(T x)206{207    return ::signbit(x);208}209 210template <typename T>211BOOST_MATH_GPU_ENABLED T changesign(T x)212{213    return -x;214}215 216template <typename T>217BOOST_MATH_GPU_ENABLED T copysign(T x, T y)218{219    return ::copysign(x, y);220}221 222template <>223BOOST_MATH_GPU_ENABLED float copysign(float x, float y)224{225    return ::copysignf(x, y);226}227 228template <typename T>229BOOST_MATH_GPU_ENABLED T sign(T z)230{231    return (z == 0) ? 0 : ::signbit(z) ? -1 : 1;232}233 234} // namespace math235} // namespace boost236 237#endif // __CUDACC_RTC__238 239#endif // BOOST_MATH_TOOLS_SIGN_HPP240 241 242