82 lines · plain
1// (C) Copyright Matt Borland 2021.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_COPYSIGN_HPP7#define BOOST_MATH_CCMATH_COPYSIGN_HPP8 9#include <cmath>10#include <cstdint>11#include <limits>12#include <type_traits>13#include <boost/math/tools/is_constant_evaluated.hpp>14#include <boost/math/tools/promotion.hpp>15#include <boost/math/tools/config.hpp>16#include <boost/math/ccmath/abs.hpp>17#include <boost/math/ccmath/signbit.hpp>18 19namespace boost::math::ccmath {20 21namespace detail {22 23template <typename T>24constexpr T copysign_impl(const T mag, const T sgn) noexcept25{26 if (boost::math::ccmath::signbit(sgn))27 {28 return -boost::math::ccmath::abs(mag);29 }30 else31 {32 return boost::math::ccmath::abs(mag);33 }34}35 36} // Namespace detail37 38template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>39constexpr Real copysign(Real mag, Real sgn) noexcept40{41 if(BOOST_MATH_IS_CONSTANT_EVALUATED(mag))42 {43 return boost::math::ccmath::detail::copysign_impl(mag, sgn);44 }45 else46 {47 using std::copysign;48 return copysign(mag, sgn);49 }50}51 52template <typename T1, typename T2>53constexpr auto copysign(T1 mag, T2 sgn) noexcept54{55 if (BOOST_MATH_IS_CONSTANT_EVALUATED(mag))56 { 57 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;58 return boost::math::ccmath::copysign(static_cast<promoted_type>(mag), static_cast<promoted_type>(sgn));59 }60 else61 {62 using std::copysign;63 return copysign(mag, sgn);64 }65}66 67constexpr float copysignf(float mag, float sgn) noexcept68{69 return boost::math::ccmath::copysign(mag, sgn);70}71 72#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS73constexpr long double copysignl(long double mag, long double sgn) noexcept74{75 return boost::math::ccmath::copysign(mag, sgn);76}77#endif78 79} // Namespaces80 81#endif // BOOST_MATH_CCMATH_COPYSIGN_HPP82